00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef WIN32
00024
00025
00026
00027
00028
00029 JThread::JThread(bool shouldStart, int pri) {
00030 hThread = NULL;
00031 publicSemaphore = NULL;
00032 testSemaphore = NULL;
00033 priority = pri;
00034
00035 LARGE_INTEGER PerfFreq;
00036 QueryPerformanceFrequency(&PerfFreq);
00037 usecFreq = (double) PerfFreq.LowPart / 1000000;
00038
00039 procLastStat = procStartStat = getCPUUsage(NULL);
00040
00041 if (shouldStart) start();
00042 }
00043
00044 JThread::~JThread() {
00045 terminate();
00046 CloseHandle(hThread);
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 HANDLE JThread::getCallingThreadID() {
00063
00064 HANDLE h = GetCurrentThread();
00065 return h;
00066 }
00067
00068
00069
00070
00071
00072 bool JThread::start() {
00073 if (hThread != NULL) {
00074
00075 return true;
00076 }
00077
00078 DWORD dwThreadId;
00079
00080 hThread = CreateThread(
00081 NULL,
00082 0,
00083 (LPTHREAD_START_ROUTINE) &staticRunThread,
00084 (void*) this,
00085 0,
00086 &dwThreadId);
00087
00088 if (hThread == NULL) {
00089 printf("******* Thread could not be created!!! *******\n");
00090 return false;
00091 }
00092
00093 this->waitThread(0);
00094
00095 return true;
00096 }
00097
00098
00099 bool JThread::terminate() {
00100 return (TerminateThread(hThread, 0) != 0);
00101 }
00102
00103 bool JThread::suspend() {
00104 return (SuspendThread(hThread) >= 0);
00105 }
00106
00107 bool JThread::resume() {
00108 DWORD count = 2;
00109
00110 while (count > 1) {
00111 count = ResumeThread(hThread);
00112 }
00113
00114 if (count == 1) return true;
00115 else return false;
00116 }
00117
00118 void JThread::yield() {
00119 Sleep(0);
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 bool JThread::isRunning() {
00129 DWORD code;
00130
00131 if (!GetExitCodeThread(hThread, &code))
00132 return false;
00133
00134 return (code == STILL_ACTIVE);
00135 }
00136
00137 ThreadStat JThread::getCPUUsage(HANDLE aThread) {
00138 ThreadStat newStat;
00139 JTime t;
00140 LARGE_INTEGER tStart;
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 FILETIME creationTime;
00152 FILETIME exitTime;
00153 FILETIME kernelTime;
00154 FILETIME userTime;
00155
00156 if (aThread == NULL) {
00157
00158
00159
00160 return newStat;
00161 }
00162 else {
00163 if (!GetThreadTimes(aThread, &creationTime, &exitTime, &kernelTime, &userTime)) {
00164 printf("\n*** ERROR getCPUUsage!!! ***\n");
00165
00166 }
00167 }
00168
00169 newStat.userLast = (userTime.dwLowDateTime) / 10;
00170 newStat.kernelLast = (kernelTime.dwLowDateTime) / 10;
00171
00172 QueryPerformanceCounter(&tStart);
00173 newStat.timeLast.tv_sec = t.seconds();
00174 newStat.timeLast.tv_usec = (long) (tStart.LowPart / usecFreq);
00175
00176 return newStat;
00177 }
00178
00179 bool JThread::isThread() {
00180 return (hThread == GetCurrentThread);
00181 }
00182
00183
00184
00185
00186
00187 int JThread::getPriority() {
00188 return(priority = convPriorityToInternal(GetThreadPriority(hThread)));
00189 }
00190
00191 bool JThread::setPriority(int pri) {
00192 priority = pri;
00193 return (SetThreadPriority(hThread, convPriorityToOS(priority)) != 0);
00194 }
00195
00196 int JThread::convPriorityToOS(int pri) {
00197 int val;
00198
00199 if (pri == 0)
00200 val = THREAD_PRIORITY_NORMAL;
00201 else if (pri >= 80)
00202 val = THREAD_PRIORITY_TIME_CRITICAL;
00203 else if (pri >= 60)
00204 val = THREAD_PRIORITY_HIGHEST;
00205 else if (pri >= 55)
00206 val = THREAD_PRIORITY_ABOVE_NORMAL;
00207 else if (pri >= 50)
00208 val = THREAD_PRIORITY_NORMAL;
00209 else if (pri >= 40)
00210 val = THREAD_PRIORITY_BELOW_NORMAL;
00211 else if (pri >= 30)
00212 val = THREAD_PRIORITY_LOWEST;
00213 else
00214 val = THREAD_PRIORITY_IDLE;
00215 return val;
00216 }
00217
00218 int JThread::convPriorityToInternal(int pri) {
00219 switch (pri) {
00220 case THREAD_PRIORITY_TIME_CRITICAL:
00221 return 80;
00222 case THREAD_PRIORITY_HIGHEST:
00223 return 60;
00224 case THREAD_PRIORITY_ABOVE_NORMAL:
00225 return 55;
00226 case THREAD_PRIORITY_NORMAL:
00227 return 50;
00228 case THREAD_PRIORITY_BELOW_NORMAL:
00229 return 40;
00230 case THREAD_PRIORITY_LOWEST:
00231 return 30;
00232 default:
00233 return 20;
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242 bool JThread::signalThread(int signo) {
00243 return true;
00244 }
00245
00246 bool JThread::waitSignal(int signo) {
00247 return true;
00248 }
00249
00250
00251 bool JThread::waitThread(long ms) {
00252 Sleep(ms);
00253 return true;
00254 }
00255
00256
00257
00258
00259
00260 JString JThread::osGetNameVersion() {
00261 JString ver = osGetVersion();
00262 if (ver.length() == 0)
00263 return osGetName();
00264 else
00265 return JString::format("%s, %s", (char*) osGetName(), (char*) ver);
00266 }
00267
00268 JString JThread::osGetName() {
00269
00270
00271
00272 OSVERSIONINFO osvi;
00273 BOOL bOsVersionInfoEx;
00274
00275
00276
00277
00278 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00279 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00280
00281 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00282 {
00283 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00284 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00285 return "";
00286 }
00287
00288 JString osName;
00289
00290 switch (osvi.dwPlatformId)
00291 {
00292
00293 case VER_PLATFORM_WIN32_CE:
00294 return "Windows CE";
00295 default:
00296 return "Microsoft Win32";
00297 }
00298
00299 return "";
00300 }
00301
00302 JString JThread::osGetVersion() {
00303 OSVERSIONINFO osvi;
00304 BOOL bOsVersionInfoEx;
00305
00306
00307
00308
00309 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00310 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00311
00312 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00313 {
00314 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00315 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00316 return "";
00317 }
00318
00319 JString osVersion = JString::format("%d.%d %s", osvi.dwMajorVersion, osvi.dwMinorVersion, (char*)osvi.szCSDVersion);
00320
00321 return osVersion;
00322 }
00323
00324 JString JThread::osGetMachineArch() {
00325 return "";
00326 }
00327
00328 JString JThread::osGetMachineName() {
00329 return "";
00330 }
00331
00332 int JThread::osGetTotalMemory() {
00333 MEMORYSTATUS memstat;
00334 GlobalMemoryStatus(&memstat);
00335 return memstat.dwTotalPhys / (1024*1024);
00336 }
00337
00338 int JThread::osGetCPUSpeed() {
00339 return 800;
00340 }
00341
00342 int JThread::osGetCPUCount() {
00343 return 2;
00344 }
00345
00346 int JThread::procMemUsage() {
00347
00348 int ret = -1;
00349
00350 #ifdef _WIN32_WCE
00351 #else
00352
00353 HANDLE hProcess = GetCurrentProcess();
00354 PROCESS_MEMORY_COUNTERS pmc;
00355
00356 if (hProcess == NULL)
00357 return -1;
00358
00359
00360
00361
00362
00363
00364
00365 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
00366 {
00367 ret = pmc.WorkingSetSize;
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 }
00384
00385
00386 #endif
00387
00388 return ret;
00389 }
00390
00391 JString JThread::getComputerName() {
00392 return "PocketPC";
00393 }
00394
00395 JString JThread::runOSTextCommand(JString cmd) {
00396 return "";
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 static DWORD WINAPI staticRunThread(LPVOID lpParam) {
00455 JThread* thr = (JThread*) lpParam;
00456 thr->run();
00457 return 0;
00458 }
00459
00460
00461 #endif