| 1 |
/*******************************************************/
|
| 2 |
/* */
|
| 3 |
/* LeafOK Innd */
|
| 4 |
/* Copyright (C) LeafOK.com, 2003-2004 */
|
| 5 |
/* */
|
| 6 |
/* Programmed by Leaf */
|
| 7 |
/* E-mail:leaf@leafok.com QQ:6049044 */
|
| 8 |
/* */
|
| 9 |
/* http://bbs.leafok.com */
|
| 10 |
/* http://bbs.leafok.net */
|
| 11 |
/* http://bbs.fenglin.info */
|
| 12 |
/* */
|
| 13 |
/*******************************************************/
|
| 14 |
|
| 15 |
#include "StdAfx.h"
|
| 16 |
#include ".\thread_pool.h"
|
| 17 |
|
| 18 |
thread_pool::thread_pool(UINT uThreadMax, double thread_timeout)
|
| 19 |
{
|
| 20 |
UINT i;
|
| 21 |
|
| 22 |
this->uLastErrorCode = E_NOERROR;
|
| 23 |
this->uThreadCount = 0;
|
| 24 |
this->uThreadMax = uThreadMax;
|
| 25 |
this->thread_timeout = thread_timeout;
|
| 26 |
|
| 27 |
for (i=0; i < this->uThreadMax; i++)
|
| 28 |
{
|
| 29 |
this->hThreadList[i] = NULL;
|
| 30 |
this->uThreadStatusList[i] = S_UNUSED;
|
| 31 |
this->time_status_keep[i] = 0;
|
| 32 |
}
|
| 33 |
|
| 34 |
this->SetLock(false);
|
| 35 |
}
|
| 36 |
|
| 37 |
thread_pool::~thread_pool(void)
|
| 38 |
{
|
| 39 |
|
| 40 |
}
|
| 41 |
|
| 42 |
int thread_pool::AddThread(HANDLE hThread)
|
| 43 |
{
|
| 44 |
UINT i;
|
| 45 |
|
| 46 |
if (this->SetLock(true) == 0)
|
| 47 |
{
|
| 48 |
return this->uLastErrorCode;
|
| 49 |
}
|
| 50 |
|
| 51 |
if (this->uThreadCount >= this->uThreadMax)
|
| 52 |
{
|
| 53 |
this->SetLock(false);
|
| 54 |
this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;
|
| 55 |
return E_MAX_THREAD_EXCEEDED;
|
| 56 |
}
|
| 57 |
|
| 58 |
for (i=0; i < this->uThreadMax; i++)
|
| 59 |
{
|
| 60 |
if (this->uThreadStatusList[i] == S_UNUSED)
|
| 61 |
{
|
| 62 |
this->hThreadList[i] = hThread;
|
| 63 |
this->uThreadCount++;
|
| 64 |
this->SetLock(false);
|
| 65 |
this->uLastErrorCode = E_NOERROR;
|
| 66 |
return E_NOERROR;
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
this->SetLock(false);
|
| 71 |
this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;
|
| 72 |
return E_MAX_THREAD_EXCEEDED;
|
| 73 |
}
|
| 74 |
|
| 75 |
int thread_pool::SetLock(bool bLock, time_t tTimeout)
|
| 76 |
{
|
| 77 |
time_t wait = 0;
|
| 78 |
|
| 79 |
if (!bLock) // Free lock
|
| 80 |
{
|
| 81 |
this->bProcessLock = false;
|
| 82 |
this->uLastErrorCode = E_NOERROR;
|
| 83 |
return E_NOERROR;
|
| 84 |
}
|
| 85 |
|
| 86 |
while (this->bProcessLock) // Locked by others
|
| 87 |
{
|
| 88 |
Sleep(10);
|
| 89 |
wait += 10;
|
| 90 |
if (wait > tTimeout) // Timeout
|
| 91 |
{
|
| 92 |
this->uLastErrorCode = E_LOCK_TIMEOUT;
|
| 93 |
return E_LOCK_TIMEOUT;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
this->bProcessLock = true;
|
| 97 |
this->uLastErrorCode = E_NOERROR;
|
| 98 |
|
| 99 |
return E_NOERROR;
|
| 100 |
}
|
| 101 |
|
| 102 |
UINT thread_pool::GetLastError(void)
|
| 103 |
{
|
| 104 |
return this->uLastErrorCode;
|
| 105 |
}
|
| 106 |
|
| 107 |
int thread_pool::RemoveThread(HANDLE hThread)
|
| 108 |
{
|
| 109 |
UINT i;
|
| 110 |
|
| 111 |
if (this->SetLock(true) == 0)
|
| 112 |
{
|
| 113 |
return this->uLastErrorCode;
|
| 114 |
}
|
| 115 |
|
| 116 |
if (this->uThreadCount <= 0)
|
| 117 |
{
|
| 118 |
this->SetLock(false);
|
| 119 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 120 |
return E_THREAD_NOT_FOUND;
|
| 121 |
}
|
| 122 |
|
| 123 |
for (i=0; i < this->uThreadMax; i++)
|
| 124 |
{
|
| 125 |
if (this->hThreadList[i] == hThread)
|
| 126 |
{
|
| 127 |
this->hThreadList[i] = NULL;
|
| 128 |
this->uThreadCount--;
|
| 129 |
this->SetLock(false);
|
| 130 |
this->uLastErrorCode = E_NOERROR;
|
| 131 |
return this->uLastErrorCode;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
this->SetLock(false);
|
| 136 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 137 |
return E_THREAD_NOT_FOUND;
|
| 138 |
}
|
| 139 |
|
| 140 |
|
| 141 |
int thread_pool::SetThreadStatus(HANDLE hThread, UINT uStatus)
|
| 142 |
{
|
| 143 |
UINTT i;
|
| 144 |
|
| 145 |
if (this->SetLock(true) == 0)
|
| 146 |
{
|
| 147 |
return this->uLastErrorCode;
|
| 148 |
}
|
| 149 |
|
| 150 |
if (this->uThreadCount <= 0)
|
| 151 |
{
|
| 152 |
this->SetLock(false);
|
| 153 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 154 |
return E_THREAD_NOT_FOUND;
|
| 155 |
}
|
| 156 |
|
| 157 |
for (i=0; i < this->uThreadMax; i++)
|
| 158 |
{
|
| 159 |
if (this->hThreadList[i] == hThread)
|
| 160 |
{
|
| 161 |
this->uThreadStatusList[i] = (thread_status)uStatus;
|
| 162 |
this->SetLock(false);
|
| 163 |
this->uLastErrorCode = E_NOERROR;
|
| 164 |
return this->uLastErrorCode;
|
| 165 |
}
|
| 166 |
}
|
| 167 |
|
| 168 |
this->SetLock(false);
|
| 169 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 170 |
return E_THREAD_NOT_FOUND;
|
| 171 |
}
|
| 172 |
|
| 173 |
UINT thread_pool::GetThreadStatus(HANDLE hThread)
|
| 174 |
{
|
| 175 |
UINT i;
|
| 176 |
|
| 177 |
if (this->uThreadCount <= 0)
|
| 178 |
{
|
| 179 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 180 |
return S_UNKNOWN;
|
| 181 |
}
|
| 182 |
|
| 183 |
for (i=0; i < this->uThreadMax; i++)
|
| 184 |
{
|
| 185 |
if (this->hThreadList[i] == hThread)
|
| 186 |
{
|
| 187 |
this->uLastErrorCode = E_NOERROR;
|
| 188 |
return this->uThreadStatusList[i];
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
| 192 |
this->uLastErrorCode = E_THREAD_NOT_FOUND;
|
| 193 |
return S_UNKNOWN;
|
| 194 |
}
|