Changeset 3526 for trunk/kProfile/kProfileR3.cpp
- Timestamp:
- Aug 20, 2007, 12:46:14 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kProfile/kProfileR3.cpp
r3524 r3526 4 4 * Header Files * 5 5 *******************************************************************************/ 6 #if defined( __WIN32__) || defined(__WIN64__)6 #if defined(KPRF_OS_WINDOWS) 7 7 # include <windows.h> 8 8 # include <psapi.h> 9 9 # include <malloc.h> 10 # define IN_RING3 11 # include <iprt/stdint.h> /* Temporary IPRT convenience */ 10 typedef unsigned char uint8_t; 11 typedef signed char int8_t; 12 typedef unsigned short uint16_t; 13 typedef signed short int16_t; 14 typedef unsigned int uint32_t; 15 typedef signed int int32_t; 16 typedef unsigned _int64 uint64_t; 17 typedef signed _int64 int64_t; 18 typedef size_t uintptr_t; 12 19 # if _MSC_VER >= 1400 13 20 # include <intrin.h> … … 15 22 # endif 16 23 17 #elif defined( __LINUX__) || defined(__FREEBSD__)24 #elif defined(KPRF_OS_LINUX) || defined(KPRF_OS_FREEBSD) 18 25 # define KPRF_USE_PTHREAD 19 26 # include <pthread.h> … … 28 35 # endif 29 36 30 #elif defined( __OS2__)37 #elif defined(KPRF_OS_OS2) 31 38 # define INCL_BASE 32 # include <os2 s.h>39 # include <os2.h> 33 40 # include <stdint.h> 34 41 # include <sys/fmutex.h> … … 44 51 #define KPRF_NAME(Suffix) KPrf##Suffix 45 52 #define KPRF_TYPE(Prefix,Suffix) Prefix##KPRF##Suffix 46 #if defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)53 #if defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 47 54 # define KPRF_DECL_FUNC(type, name) extern "C" __declspec(dllexport) type __cdecl KPRF_NAME(name) 48 55 #else … … 53 60 # define KPRF_ASSERT(expr) do { if (!(expr)) { __asm__ __volatile__("int3\n\tnop\n\t");} } while (0) 54 61 # else 55 # define KPRF_ASSERT(expr) do { if (!(expr)) { __asm int 3 \ 56 } } while (0) 62 # define KPRF_ASSERT(expr) do { if (!(expr)) { __debugbreak(); } } while (0) 57 63 # endif 58 64 #else … … 70 76 #if defined(KPRF_USE_PTHREAD) 71 77 typedef pthread_mutex_t KPRF_TYPE(,MUTEX); 72 #elif defined( __WIN32__) || defined(__WIN64__)78 #elif defined(KPRF_OS_WINDOWS) 73 79 typedef CRITICAL_SECTION KPRF_TYPE(,MUTEX); 74 #elif defined( __OS2__)80 #elif defined(KPRF_OS_OS2) 75 81 typedef struct _fmutex KPRF_TYPE(,MUTEX); 76 82 #endif … … 82 88 /** Read/Write lock type. */ 83 89 typedef pthread_rwlock_t KPRF_TYPE(,RWLOCK); 84 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)90 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 85 91 /** Read/Write lock state. */ 86 92 typedef enum KPRF_TYPE(,RWLOCKSTATE) … … 108 114 /** The current number of waiting writers. */ 109 115 uint32_t cWritersWaiting; 110 # if defined( __WIN32__) || defined(__WIN64__)116 # if defined(KPRF_OS_WINDOWS) 111 117 /** The handle of the event object on which the waiting readers block. (manual reset). */ 112 118 HANDLE hevReaders; 113 119 /** The handle of the event object on which the waiting writers block. (manual reset). */ 114 120 HANDLE hevWriters; 115 # elif defined( __OS2__)121 # elif defined(KPRF_OS_OS2) 116 122 /** The handle of the event semaphore on which the waiting readers block. */ 117 123 HEV hevReaders; … … 132 138 *******************************************************************************/ 133 139 /** The TLS index / key. */ 134 #if defined( __WIN32__) || defined(__WIN64__)140 #if defined(KPRF_OS_WINDOWS) 135 141 static DWORD g_dwThreadTLS = TLS_OUT_OF_INDEXES; 136 142 … … 138 144 static pthread_key_t g_ThreadKey = (pthread_key_t)-1; 139 145 140 #elif defined( __OS2__)146 #elif defined(KPRF_OS_OS2) 141 147 static KPRF_TYPE(P,THREAD) *g_ppThread = NULL; 142 148 … … 187 193 188 194 /* Win32/64 */ 189 #if defined( __WIN32__) || defined(__WIN64__)195 #if defined(KPRF_OS_WINDOWS) 190 196 pThread = (KPRF_TYPE(P,THREAD))TlsGetValue(g_dwThreadTLS); 191 197 … … 194 200 pThread = (KPRF_TYPE(P,THREAD))pthread_getspecific(g_ThreadKey); 195 201 196 #elif defined( __OS2__)202 #elif defined(KPRF_OS_OS2) 197 203 pThread = *g_ppThread; 198 204 … … 215 221 { 216 222 /* Win32/64 */ 217 #if defined( __WIN32__) || defined(__WIN64__)223 #if defined(KPRF_OS_WINDOWS) 218 224 uintptr_t ThreadId = (uintptr_t)GetCurrentThreadId(); 219 225 … … 222 228 uintptr_t ThreadId = (uintptr_t)pthread_self(); 223 229 224 #elif defined( __OS2__)230 #elif defined(KPRF_OS_OS2) 225 231 PTIB pTib; 226 232 PPIB pPib; … … 245 251 { 246 252 /* Win32/64 */ 247 #if defined( __WIN32__) || defined(__WIN64__)253 #if defined(KPRF_OS_WINDOWS) 248 254 uintptr_t ThreadId = (uintptr_t)GetProcessId(GetCurrentProcess()); 249 255 250 #elif defined( __OS2__)256 #elif defined(KPRF_OS_OS2) 251 257 PTIB pTib; 252 258 PPIB pPib; … … 275 281 { 276 282 /* Win32/64 */ 277 #if defined( __WIN32__) || defined(__WIN64__)283 #if defined(KPRF_OS_WINDOWS) 278 284 BOOL fRc = TlsSetValue(g_dwThreadTLS, pThread); 279 285 … … 282 288 int rc = pthread_setspecific(g_ThreadKey, pThread); 283 289 284 #elif defined( __OS2__)290 #elif defined(KPRF_OS_OS2) 285 291 *g_ppThread = pThread; 286 292 … … 333 339 { 334 340 #if defined(HAVE_INTRIN) 335 _InterlockedExchange(( int32_t *)pu32, (const int32_t)u32);341 _InterlockedExchange((long volatile *)pu32, (const long)u32); 336 342 337 343 #elif defined(__GNUC__) … … 404 410 { 405 411 #if defined(HAVE_INTRIN) 406 _InterlockedExchangeAdd 32((volatile int32_t *)pu32, (const int32_t)u32);412 _InterlockedExchangeAdd((volatile long *)pu32, (const long)u32); 407 413 408 414 #elif defined(__GNUC__) … … 482 488 return -1; 483 489 484 #elif defined( __WIN32__) || defined(__WIN64__)490 #elif defined(KPRF_OS_WINDOWS) 485 491 InitializeCriticalSection(pMutex); 486 492 return 0; 487 493 488 #elif defined( __OS2__)494 #elif defined(KPRF_OS_OS2) 489 495 if (!_fmutex_create(pMutex, 0)) 490 496 return 0; … … 503 509 pthread_mutex_destroy(pMutex); 504 510 505 #elif defined( __WIN32__) || defined(__WIN64__)511 #elif defined(KPRF_OS_WINDOWS) 506 512 DeleteCriticalSection(pMutex); 507 513 508 #elif defined( __OS2__)514 #elif defined(KPRF_OS_OS2) 509 515 _fmutex_close(pMutex); 510 516 #endif … … 517 523 static inline void kPrfMutexAcquire(KPRF_TYPE(P,MUTEX) pMutex) 518 524 { 519 #if defined( __WIN32__) || defined(__WIN64__)525 #if defined(KPRF_OS_WINDOWS) 520 526 EnterCriticalSection(pMutex); 521 527 … … 523 529 pthread_mutex_lock(pMutex); 524 530 525 #elif defined( __OS2__)531 #elif defined(KPRF_OS_OS2) 526 532 fmutex_request(pMutex); 527 533 #endif … … 535 541 static inline void kPrfMutexRelease(KPRF_TYPE(P,MUTEX) pMutex) 536 542 { 537 #if defined( __WIN32__) || defined(__WIN64__)543 #if defined(KPRF_OS_WINDOWS) 538 544 LeaveCriticalSection(pMutex); 539 545 … … 541 547 pthread_mutex_lock(pMutex); 542 548 543 #elif defined( __OS2__)549 #elif defined(KPRF_OS_OS2) 544 550 fmutex_request(pMutex); 545 551 #endif … … 568 574 return -1; 569 575 570 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)576 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 571 577 if (kPrfMutexInit(&pRWLock->Mutex)) 572 578 return -1; … … 575 581 pRWLock->cWritersWaiting = 0; 576 582 pRWLock->enmState = RWLOCK_STATE_SHARED; 577 # if defined( __WIN32__) || defined(__WIN64__)583 # if defined(KPRF_OS_WINDOWS) 578 584 pRWLock->hevReaders = CreateEvent(NULL, TRUE, TRUE, NULL); 579 585 pRWLock->hevWriters = CreateEvent(NULL, FALSE, FALSE, NULL); … … 584 590 CloseHandle(pRWLock->hevWriters); 585 591 586 # elif defined( __OS2__)592 # elif defined(KPRF_OS_OS2) 587 593 APIRET rc = DosCreateEventSem(NULL, &pRWLock->hevReaders, 0, TRUE); 588 594 if (!rc) … … 614 620 pthread_rwlock_destroy(pRWLock); 615 621 616 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)622 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 617 623 if (pRWLock->enmState == RWLOCK_STATE_UNINITIALIZED) 618 624 return; … … 623 629 pRWLock->cReadersWaiting = 0; 624 630 pRWLock->cWritersWaiting = 0; 625 # if defined( __WIN32__) || defined(__WIN64__)631 # if defined(KPRF_OS_WINDOWS) 626 632 CloseHandle(pRWLock->hevReaders); 627 633 pRWLock->hevReaders = INVALID_HANDLE_VALUE; … … 629 635 pRWLock->hevWriters = INVALID_HANDLE_VALUE; 630 636 631 # elif defined( __OS2__)637 # elif defined(KPRF_OS_OS2) 632 638 DosCloseEventSem(pRWLock->hevReaders); 633 639 pRWLock->hevReaders = NULLHANDLE; … … 648 654 pthread_rwlock_rdlock(pRWLock); 649 655 650 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)656 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 651 657 if (pRWLock->enmState == RWLOCK_STATE_UNINITIALIZED) 652 658 return; … … 664 670 /* have to wait */ 665 671 KPRF_ATOMIC_INC32(&pRWLock->cReadersWaiting); 666 # if defined( __WIN32__) || defined(__WIN64__)672 # if defined(KPRF_OS_WINDOWS) 667 673 HANDLE hev = pRWLock->hevReaders; 668 674 ResetEvent(hev); 669 675 670 # elif defined( __OS2__)676 # elif defined(KPRF_OS_OS2) 671 677 HEV hev = pRWLock->hevReaders; 672 678 ULONG cIgnored; … … 676 682 kPrfMutexRelease(&pRWLock->Mutex); 677 683 678 # if defined( __WIN32__) || defined(__WIN64__)684 # if defined(KPRF_OS_WINDOWS) 679 685 switch (WaitForSingleObject(hev, INFINITE)) 680 686 { … … 688 694 } 689 695 690 # elif defined( __OS2__)696 # elif defined(KPRF_OS_OS2) 691 697 switch (DosWaitEventSem(hev, SEM_INDEFINITE_WAIT)) 692 698 { … … 723 729 pthread_rwlock_unlock(pRWLock); 724 730 725 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)731 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 726 732 if (pRWLock->enmState == RWLOCK_STATE_UNINITIALIZED) 727 733 return; … … 747 753 * Wake up one (or more on OS/2) waiting writers. 748 754 */ 749 # if defined( __WIN32__) || defined(__WIN64__)755 # if defined(KPRF_OS_WINDOWS) 750 756 SetEvent(pRWLock->hevWriters); 751 # elif defined( __OS2__)757 # elif defined(KPRF_OS_OS2) 752 758 DosPostEvent(pRWLock->hevwriters); 753 759 # endif … … 767 773 pthread_rwlock_wrlock(pRWLock); 768 774 769 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)775 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 770 776 if (pRWLock->enmState == RWLOCK_STATE_UNINITIALIZED) 771 777 return; … … 790 796 for (;;) 791 797 { 792 # if defined( __WIN32__) || defined(__WIN64__)798 # if defined(KPRF_OS_WINDOWS) 793 799 HANDLE hev = pRWLock->hevWriters; 794 # elif defined( __OS2__)800 # elif defined(KPRF_OS_OS2) 795 801 HEV hev = pRWLock->hevWriters; 796 802 # endif 797 803 kPrfMutexRelease(&pRWLock->Mutex); 798 # if defined( __WIN32__) || defined(__WIN64__)804 # if defined(KPRF_OS_WINDOWS) 799 805 switch (WaitForSingleObject(hev, INFINITE)) 800 806 { … … 809 815 } 810 816 811 # elif defined( __OS2__)817 # elif defined(KPRF_OS_OS2) 812 818 switch (DosWaitEventSem(hev, SEM_INDEFINITE_WAIT)) 813 819 { … … 853 859 pthread_rwlock_unlock(pRWLock); 854 860 855 #elif defined( __WIN32__) || defined(__WIN64__) || defined(__OS2__)861 #elif defined(KPRF_OS_WINDOWS) || defined(KPRF_OS_OS2) 856 862 if (pRWLock->enmState == RWLOCK_STATE_UNINITIALIZED) 857 863 return; … … 878 884 * Someone is waiting, wake them up as we change the state. 879 885 */ 880 # if defined( __WIN32__) || defined(__WIN64__)886 # if defined(KPRF_OS_WINDOWS) 881 887 HANDLE hev = INVALID_HANDLE_VALUE; 882 # elif defined( __OS2__)888 # elif defined(KPRF_OS_OS2) 883 889 HEV hev = NULLHANDLE; 884 890 # endif … … 894 900 hev = pRWLock->hevReaders; 895 901 } 896 # if defined( __WIN32__) || defined(__WIN64__)902 # if defined(KPRF_OS_WINDOWS) 897 903 SetEvent(hev); 898 # elif defined( __OS2__)904 # elif defined(KPRF_OS_OS2) 899 905 DosPostEvent(pRWLock->hevwriters); 900 906 # endif … … 919 925 KPRF_TYPE(P,UPTR) puBasePtr, KPRF_TYPE(P,UPTR) pcbSegmentMinusOne) 920 926 { 921 #if defined( __WIN32__) || defined(__WIN64__)927 #if defined(KPRF_OS_WINDOWS) 922 928 /* 923 929 * Enumerate the module handles. … … 1037 1043 } 1038 1044 1039 #elif defined( __OS2__)1045 #elif defined(KPRF_OS_OS2) 1040 1046 /* 1041 1047 * Just ask the loader. … … 1118 1124 * Some limit stuff in posix / ansi also comes to mind... */ 1119 1125 1120 #elif defined( __OS2__)1126 #elif defined(KPRF_OS_OS2) 1121 1127 PTIB pTib; 1122 1128 PPIB pPib; … … 1148 1154 static char *kPrfGetEnvString(const char *pszVar, char *pszValue, uint32_t cchValue, const char *pszDefault) 1149 1155 { 1150 #if defined( __WIN32__) || defined(__WIN64__)1156 #if defined(KPRF_OS_WINDOWS) 1151 1157 if (GetEnvironmentVariable(pszVar, pszValue, cchValue)) 1152 1158 return pszValue; 1153 1159 1154 #elif defined( __OS2__)1160 #elif defined(KPRF_OS_OS2) 1155 1161 PSZ pszValue; 1156 1162 if ( !DosScanEnv((PCSZ)pszVar, &pszValue) … … 1187 1193 static uint32_t kPrfGetEnvValue(const char *pszVar, uint32_t uDefault) 1188 1194 { 1189 #if defined( __WIN32__) || defined(__WIN64__)1195 #if defined(KPRF_OS_WINDOWS) 1190 1196 char szBuf[128]; 1191 1197 const char *pszValue = szBuf; … … 1193 1199 pszValue = NULL; 1194 1200 1195 #elif defined( __OS2__)1201 #elif defined(KPRF_OS_OS2) 1196 1202 PSZ pszValue; 1197 1203 if (DosScanEnv((PCSZ)pszVar, &pszValue)) … … 1275 1281 static void *kPrfAllocMem(uint32_t cb) 1276 1282 { 1277 #if defined( __WIN32__) || defined(__WIN64__)1283 #if defined(KPRF_OS_WINDOWS) 1278 1284 void *pv = VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_EXECUTE_READWRITE); 1279 1285 … … 1281 1287 void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 1282 1288 1283 #elif defined( __OS2__)1289 #elif defined(KPRF_OS_OS2) 1284 1290 void *pv; 1285 1291 # ifdef INCL_DOSEXAPIS … … 1304 1310 static void kPrfFreeMem(void *pv) 1305 1311 { 1306 #if defined( __WIN32__) || defined(__WIN64__)1312 #if defined(KPRF_OS_WINDOWS) 1307 1313 VirtualFree(pv, 0, MEM_RELEASE); 1308 1314 … … 1310 1316 munmap(pv, 0); /** @todo check if 0 is allowed here.. */ 1311 1317 1312 #elif defined( __OS2__)1318 #elif defined(KPRF_OS_OS2) 1313 1319 # ifdef INCL_DOSEXAPIS 1314 1320 DosFreeMemEx(&pv); … … 1338 1344 static int kPrfWriteFile(const char *pszName, const void *pvData, uint32_t cbData) 1339 1345 { 1340 #if defined( __WIN32__) || defined(__WIN64__)1346 #if defined(KPRF_OS_WINDOWS) 1341 1347 int rc = -1; 1342 1348 HANDLE hFile = CreateFile(pszName,GENERIC_WRITE, FILE_SHARE_READ, NULL, … … 1352 1358 return rc; 1353 1359 1354 #elif defined( __OS2__)1360 #elif defined(KPRF_OS_OS2) 1355 1361 HFILE hFile; 1356 1362 ULONG ulAction = 0; … … 1436 1442 * Allocate the TLS entry. 1437 1443 */ 1438 #if defined( __WIN32__) || defined(__WIN64__)1444 #if defined(KPRF_OS_WINDOWS) 1439 1445 g_dwThreadTLS = TlsAlloc(); 1440 1446 if (g_dwThreadTLS != TLS_OUT_OF_INDEXES) … … 1444 1450 if (!rc) 1445 1451 1446 #elif defined( __OS2__)1452 #elif defined(KPRF_OS_OS2) 1447 1453 int rc = DosAllocThreadLocalMemory(sizeof(void *), (PULONG*)&g_ppThread); /** @todo check if this is a count or a size. */ 1448 1454 if (!rc) … … 1489 1495 return -1; 1490 1496 1491 #if defined( __WIN32__) || defined(__WIN64__)1497 #if defined(KPRF_OS_WINDOWS) 1492 1498 Sleep(10); 1493 #elif defined( __OS2__)1499 #elif defined(KPRF_OS_OS2) 1494 1500 DosSleep(10); 1495 1501 #else … … 1505 1511 * Use the stack space to fill in process details. 1506 1512 */ 1507 #if defined( __WIN32__) || defined(__WIN64__)1513 #if defined(KPRF_OS_WINDOWS) 1508 1514 /* all is one single string */ 1509 1515 const char *pszCommandLine = GetCommandLine(); … … 1511 1517 KPRF_NAME(SetCommandLine)(pHdr, 1, &pszCommandLine); 1512 1518 1513 #elif defined( __OS2__) || defined(__OS2__)1519 #elif defined(KPRF_OS_OS2) || defined(KPRF_OS_OS2) 1514 1520 PTIB pTib; 1515 1521 PPIB pPib; … … 1575 1581 */ 1576 1582 kPrfFreeMem(pHdr); 1577 #if defined( __WIN32__) || defined(__WIN64__)1583 #if defined(KPRF_OS_WINDOWS) 1578 1584 TlsFree(g_dwThreadTLS); 1579 1585 g_dwThreadTLS = TLS_OUT_OF_INDEXES; … … 1583 1589 g_ThreadKey = (pthread_key_t)-1; 1584 1590 1585 #elif defined( __OS2__)1591 #elif defined(KPRF_OS_OS2) 1586 1592 DosFreeThreadLocalMemory((PULONG)g_ppThread); 1587 1593 g_ppThread = NULL;
Note:
See TracChangeset
for help on using the changeset viewer.