00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 namespace cmlabs {
00023
00024 #ifdef WIN32
00025
00026
00027
00028
00029
00030 JThread::JThread(bool shouldStart, int pri) {
00031 threadStartTime.setInvalid();
00032 threadEndTime.setInvalid();
00033 hThread = NULL;
00034 publicSemaphore = NULL;
00035 testSemaphore = NULL;
00036 priority = pri;
00037
00038 LARGE_INTEGER PerfFreq;
00039 QueryPerformanceFrequency(&PerfFreq);
00040
00041 usecFreq = (double) PerfFreq.QuadPart / 1000000.0;
00042
00043 procLastStat = procStartStat = getCPUUsage(NULL);
00044
00045 if (shouldStart) start();
00046 }
00047
00048 JThread::~JThread() {
00049 terminate();
00050 CloseHandle(hThread);
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 HANDLE JThread::getCallingThreadID() {
00067
00068 HANDLE h = GetCurrentThread();
00069 return h;
00070 }
00071
00072
00073
00074
00075
00076 bool JThread::start() {
00077 if (hThread != NULL) {
00078
00079 return true;
00080 }
00081
00082 DWORD dwThreadId;
00083
00084 hThread = CreateThread(
00085 NULL,
00086 0,
00087 (LPTHREAD_START_ROUTINE) &staticRunThread,
00088 (void*) this,
00089 0,
00090 &dwThreadId);
00091
00092 if (hThread == NULL) {
00093 printf("******* Thread could not be created!!! *******\n");
00094 return false;
00095 }
00096
00097 setPriority(priority);
00098 addToAllThreadsStats(hThread);
00099 this->waitThread(0);
00100
00101 return true;
00102 }
00103
00104 bool JThread::join()
00105 {
00106
00107
00108 int res = 0;
00109
00110 res = WaitForSingleObject(hThread, INFINITE);
00111
00112
00113
00114 if(res != WAIT_OBJECT_0)
00115 return false;
00116 res = CloseHandle(hThread);
00117
00118 if (res==0)
00119 return false;
00120 hThread=NULL;
00121 return true;
00122 }
00123
00124
00125 bool JThread::terminate() {
00126 bool res = (TerminateThread(hThread, 0) != 0);
00127 removeFromAllThreadsStats(hThread);
00128 return res;
00129 }
00130
00131 bool JThread::suspend() {
00132 return (SuspendThread(hThread) >= 0);
00133 }
00134
00135 bool JThread::resume() {
00136 DWORD count = 2;
00137
00138 while (count > 1) {
00139 count = ResumeThread(hThread);
00140 }
00141
00142 if (count == 1) return true;
00143 else return false;
00144 }
00145
00146 void JThread::yield() {
00147 Sleep(0);
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 bool JThread::isRunning() {
00157 DWORD code;
00158
00159 if (!GetExitCodeThread(hThread, &code))
00160 return false;
00161
00162 return (code == STILL_ACTIVE);
00163 }
00164
00165 ThreadStat JThread::getCPUUsage(HANDLE aThread) {
00166 ThreadStat newStat;
00167 JTime t;
00168 LARGE_INTEGER tStart;
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 FILETIME creationTime;
00182 FILETIME exitTime;
00183 FILETIME kernelTime;
00184 FILETIME userTime;
00185
00186 if (aThread == NULL) {
00187 newStat = getAllThreadsStats();
00188
00189
00190
00191
00192
00193
00194
00195 }
00196 else {
00197 if (!GetThreadTimes(aThread, &creationTime, &exitTime, &kernelTime, &userTime)) {
00198 printf("\n*** ERROR getCPUUsage!!! ***\n");
00199
00200 }
00201 else {
00202 newStat.userLast = (userTime.dwLowDateTime) / 10;
00203 newStat.kernelLast = (kernelTime.dwLowDateTime) / 10;
00204
00205 }
00206 }
00207
00208 QueryPerformanceCounter(&tStart);
00209 newStat.timeLast.tv_sec = t.seconds();
00210
00211 newStat.timeLast.tv_usec = (long) (tStart.QuadPart / usecFreq);
00212
00213
00214 return newStat;
00215 }
00216
00217 bool JThread::isThread() {
00218 return (hThread == GetCurrentThread);
00219 }
00220
00221
00222
00223
00224
00225 int JThread::getPriority() {
00226 return(priority = convPriorityToInternal(GetThreadPriority(hThread)));
00227 }
00228
00229 bool JThread::setPriority(int pri) {
00230 priority = pri;
00231 return (SetThreadPriority(hThread, convPriorityToOS(priority)) != 0);
00232 }
00233
00234 int JThread::convPriorityToOS(int pri) {
00235 int val;
00236
00237 if (pri == 0)
00238 val = THREAD_PRIORITY_NORMAL;
00239 else if (pri >= 90)
00240 val = THREAD_PRIORITY_TIME_CRITICAL;
00241 else if (pri >= 80)
00242 val = THREAD_PRIORITY_HIGHEST;
00243 else if (pri >= 70)
00244 val = THREAD_PRIORITY_ABOVE_NORMAL;
00245 else if (pri >= 60)
00246 val = THREAD_PRIORITY_NORMAL;
00247 else if (pri >= 50)
00248 val = THREAD_PRIORITY_BELOW_NORMAL;
00249 else if (pri >= 40)
00250 val = THREAD_PRIORITY_LOWEST;
00251 else
00252 val = THREAD_PRIORITY_IDLE;
00253 return val;
00254 }
00255
00256 int JThread::convPriorityToInternal(int pri) {
00257 switch (pri) {
00258 case THREAD_PRIORITY_TIME_CRITICAL:
00259 return 80;
00260 case THREAD_PRIORITY_HIGHEST:
00261 return 60;
00262 case THREAD_PRIORITY_ABOVE_NORMAL:
00263 return 55;
00264 case THREAD_PRIORITY_NORMAL:
00265 return 50;
00266 case THREAD_PRIORITY_BELOW_NORMAL:
00267 return 40;
00268 case THREAD_PRIORITY_LOWEST:
00269 return 30;
00270 default:
00271 return 20;
00272 }
00273 }
00274
00275
00276
00277
00278
00279
00280 bool JThread::signalThread(int signo) {
00281 return true;
00282 }
00283
00284 bool JThread::waitSignal(int signo) {
00285 return true;
00286 }
00287
00288
00289 bool JThread::waitThread(long ms) {
00290 Sleep(ms);
00291 return true;
00292 }
00293
00294
00295
00296
00297
00298 JString JThread::osGetNameVersion() {
00299 JString ver = osGetVersion();
00300 if (ver.length() == 0)
00301 return osGetName();
00302 else
00303 return JString::format("%s, %s", (char*) osGetName(), (char*) ver);
00304 }
00305
00306 JString JThread::osGetName() {
00307
00308
00309 #ifdef _WIN32_WCE
00310
00311 OSVERSIONINFO osvi;
00312 BOOL bOsVersionInfoEx;
00313
00314
00315
00316
00317 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00318 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00319
00320 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00321 {
00322 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00323 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00324 return "";
00325 }
00326
00327 JString osName;
00328
00329 switch (osvi.dwPlatformId)
00330 {
00331
00332 case VER_PLATFORM_WIN32_CE:
00333 return "Windows CE";
00334 default:
00335 return "Microsoft Win32";
00336 }
00337
00338 return "";
00339 #else
00340 OSVERSIONINFOEX osvi;
00341 BOOL bOsVersionInfoEx;
00342
00343
00344
00345
00346 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
00347 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
00348
00349 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00350 {
00351 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00352 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00353 return "";
00354 }
00355
00356 JString osName;
00357
00358 switch (osvi.dwPlatformId)
00359 {
00360
00361 case VER_PLATFORM_WIN32_NT:
00362
00363
00364 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
00365 osName += JString("Microsoft Windows Server 2003");
00366
00367 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
00368 osName += JString("Microsoft Windows XP");
00369
00370 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
00371 osName += JString("Microsoft Windows 2000");
00372
00373 if ( osvi.dwMajorVersion <= 4 )
00374 osName += JString("Microsoft Windows NT");
00375
00376 break;
00377
00378
00379 case VER_PLATFORM_WIN32_WINDOWS:
00380
00381 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
00382 {
00383 osName += JString("Microsoft Windows 95");
00384 if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
00385 osName += JString(" OSR2" );
00386 }
00387
00388 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
00389 {
00390 osName += JString("Microsoft Windows 98");
00391 if ( osvi.szCSDVersion[1] == 'A' )
00392 osName += JString(" SE" );
00393 }
00394
00395 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
00396 {
00397 osName += JString("Microsoft Windows Millennium Edition");
00398 }
00399 break;
00400
00401 case VER_PLATFORM_WIN32s:
00402
00403 osName += JString("Microsoft Win32s");
00404 break;
00405 }
00406
00407 return osName;
00408 #endif
00409 }
00410
00411 JString JThread::osGetVersion() {
00412
00413 int major = 0;
00414 int minor = 0;
00415 int sp = 0;
00416 int build = 0;
00417 JString text;
00418
00419 #ifdef _WIN32_WCE
00420
00421 OSVERSIONINFO osvi;
00422 BOOL bOsVersionInfoEx;
00423
00424
00425
00426
00427 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00428 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00429
00430 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00431 {
00432 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00433 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00434 return "";
00435 }
00436
00437 major = osvi.dwMajorVersion;
00438 minor = osvi.dwMinorVersion;
00439 text = (char*)osvi.szCSDVersion;
00440
00441
00442 #else
00443
00444 OSVERSIONINFOEX osvi;
00445 BOOL bOsVersionInfoEx;
00446
00447
00448
00449
00450 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
00451 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
00452
00453 if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
00454 {
00455 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00456 if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
00457 return "";
00458 }
00459
00460 switch (osvi.dwPlatformId)
00461 {
00462
00463 case VER_PLATFORM_WIN32_NT:
00464
00465
00466
00467 if( osvi.dwMajorVersion == 4 &&
00468 lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 )
00469 {
00470 HKEY hKey;
00471 LONG lRet;
00472
00473
00474 lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
00475 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
00476 0, KEY_QUERY_VALUE, &hKey );
00477 if( lRet == ERROR_SUCCESS ) {
00478 text = "Service Pack 6a";
00479 build = osvi.dwBuildNumber & 0xFFFF;
00480 }
00481 else
00482 {
00483 major = osvi.dwMajorVersion;
00484 minor = osvi.dwMinorVersion;
00485 build = osvi.dwBuildNumber & 0xFFFF;
00486 text = (char*)osvi.szCSDVersion;
00487
00488
00489
00490 }
00491
00492 RegCloseKey( hKey );
00493 }
00494 else
00495 {
00496 major = osvi.dwMajorVersion;
00497 minor = osvi.dwMinorVersion;
00498 build = osvi.dwBuildNumber & 0xFFFF;
00499 text = (char*)osvi.szCSDVersion;
00500
00501
00502
00503 }
00504 break;
00505 }
00506
00507 if (text.length() > 0)
00508 sp = text.splitOnWhiteSpaces().getLast().toInt();
00509
00510 #endif
00511
00512 return JString::format("%d.%d.%d.%d %s", major, minor, sp, build, (char*) text);
00513 }
00514
00515 JString JThread::osGetMachineArch() {
00516
00517 SYSTEM_INFO siSysInfo;
00518 GetSystemInfo(&siSysInfo);
00519
00520 switch(siSysInfo.wProcessorArchitecture) {
00521 case PROCESSOR_ARCHITECTURE_UNKNOWN:
00522 return "i386";
00523 case PROCESSOR_ARCHITECTURE_INTEL:
00524 switch(siSysInfo.wProcessorLevel) {
00525 case 3:
00526 return "i386";
00527 case 4:
00528 return "i486";
00529 case 5:
00530 return "i586";
00531 case 6:
00532 return "i686";
00533 case 7:
00534 return "i786";
00535 case 8:
00536 return "i886";
00537 case 9:
00538 return "i986";
00539 default:
00540 return "i686";
00541 }
00542 case PROCESSOR_ARCHITECTURE_MIPS:
00543 return "mips";
00544 case PROCESSOR_ARCHITECTURE_ALPHA:
00545 return "alpha";
00546 case PROCESSOR_ARCHITECTURE_PPC:
00547 return "ppc";
00548 case PROCESSOR_ARCHITECTURE_IA64:
00549 return "ia64";
00550 case PROCESSOR_ARCHITECTURE_AMD64:
00551 return "amd64";
00552 default:
00553 return "";
00554 }
00555
00556 return "";
00557 }
00558
00559 double JThread::osGetTotalMemory() {
00560 MEMORYSTATUS memstat;
00561 GlobalMemoryStatus(&memstat);
00562 return ((double)memstat.dwTotalPhys) / (1024*1024);
00563 }
00564
00565 double JThread::osGetCPUSpeed() {
00566
00567 HKEY hKey;
00568 LONG lRet;
00569
00570
00571 JString key = "HARDWARE\\DESCRIPTION\\SYSTEM\\CentralProcessor\\0";
00572 lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hKey );
00573 if( lRet == ERROR_SUCCESS ) {
00574 DWORD len = sizeof(int);
00575 BYTE data[sizeof(int)];
00576 key = "~MHz";
00577 lRet = RegQueryValueEx(hKey, key, NULL, NULL, data, &len);
00578 if (lRet == ERROR_SUCCESS) {
00579 RegCloseKey(hKey);
00580 int speed = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + (data[0] << 0);
00581 return speed;
00582 }
00583 else {
00584 RegCloseKey(hKey);
00585 }
00586 }
00587
00588 #ifdef WIN32_VSNET_xxx
00589
00590 LARGE_INTEGER ulFreq, ulTicks, ulValue, ulStartCounter, ulEAX_EDX;
00591
00592
00593 if (QueryPerformanceFrequency(&ulFreq)) {
00594
00595 QueryPerformanceCounter(&ulTicks);
00596
00597 ulValue.QuadPart = ulTicks.QuadPart + (ulFreq.QuadPart/100);
00598
00599 __asm RDTSC
00600
00601 __asm mov ulEAX_EDX.LowPart, EAX
00602 __asm mov ulEAX_EDX.HighPart, EDX
00603
00604 ulStartCounter.QuadPart = ulEAX_EDX.QuadPart;
00605
00606 do {
00607 QueryPerformanceCounter(&ulTicks);
00608 } while (ulTicks.QuadPart <= ulValue.QuadPart);
00609
00610 __asm RDTSC
00611
00612 __asm mov ulEAX_EDX.LowPart, EAX
00613 __asm mov ulEAX_EDX.HighPart, EDX
00614
00615 return (DWORD) ((ulEAX_EDX.QuadPart - ulStartCounter.QuadPart) / 10000);
00616 } else {
00617
00618 LARGE_INTEGER pf;
00619 QueryPerformanceFrequency(&pf);
00620 double usecFre = (double) pf.QuadPart / 1000000.0;
00621 if (usecFre <= 0)
00622 return 0;
00623 else
00624 return usecFre;
00625 }
00626 #else
00627
00628 LARGE_INTEGER pf;
00629 QueryPerformanceFrequency(&pf);
00630 double usecFre = (double) pf.QuadPart / 1000000.0;
00631 if (usecFre <= 0)
00632 return 0;
00633 else
00634 return usecFre;
00635 #endif
00636 }
00637
00638 int JThread::osGetCPUCount() {
00639 SYSTEM_INFO siSysInfo;
00640 GetSystemInfo(&siSysInfo);
00641 return siSysInfo.dwNumberOfProcessors;
00642 }
00643
00644 int JThread::procMemUsage() {
00645
00646 int ret = -1;
00647
00648 #ifdef _WIN32_WCE
00649 #else
00650
00651 HANDLE hProcess = GetCurrentProcess();
00652 PROCESS_MEMORY_COUNTERS pmc;
00653
00654 if (hProcess == NULL)
00655 return -1;
00656
00657
00658
00659
00660
00661
00662
00663 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
00664 {
00665 ret = (int)pmc.WorkingSetSize;
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 }
00682
00683
00684 #endif
00685
00686 return ret;
00687 }
00688
00689 JString JThread::getComputerName() {
00690 #ifdef _WIN32_WCE
00691 return "PocketPC";
00692 #else
00693 DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
00694 char buffer[MAX_COMPUTERNAME_LENGTH + 1];
00695 if (GetComputerName(buffer, &len) == 0)
00696 return "";
00697 else
00698 return buffer;
00699 #endif
00700 }
00701
00702 JString JThread::runOSTextCommand(JString cmd) {
00703 return "";
00704 }
00705
00706
00707
00708 Collection JThread::getProgramTrace() {
00709 Collection col;
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 return col;
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780 static DWORD WINAPI staticRunThread(LPVOID lpParam) {
00781 JThread* thr = (JThread*) lpParam;
00782 thr->threadStartTime = JTime();
00783 thr->threadEndTime.setInvalid();
00784 thr->run();
00785 thr->threadEndTime = JTime();
00786 return 0;
00787 }
00788
00789
00790 #endif
00791
00792 }