Changeset 6740 in vbox
- Timestamp:
- Feb 1, 2008 10:05:47 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 27822
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstSemMutex.cpp
r6739 r6740 35 35 #include <iprt/asm.h> 36 36 37 #include <stdio.h>38 #include <stdlib.h>39 40 37 #define SECONDS 10 41 38 … … 43 40 static uint64_t g_au64[10]; 44 41 static bool g_fTerminate; 42 static bool g_fYield = true; 45 43 static uint32_t g_cbConcurrent; 44 45 static uint32_t volatile g_cErrors; 46 47 48 int PrintError(const char *pszFormat, ...) 49 { 50 ASMAtomicIncU32(&g_cErrors); 51 52 RTPrintf("tstSemMutex: FAILURE - "); 53 va_list va; 54 va_start(va, pszFormat); 55 RTPrintfV(pszFormat, va); 56 va_end(va); 57 58 return 1; 59 } 46 60 47 61 48 62 int ThreadTest(RTTHREAD ThreadSelf, void *pvUser) 49 63 { 50 uint64_t *pu64 = (uint64_t *)pvUser;64 uint64_t *pu64 = (uint64_t *)pvUser; 51 65 for (;;) 52 66 { … … 54 68 if (RT_FAILURE(rc)) 55 69 { 56 RTPrintf("%x: RTSemMutexRequestNoResume failed with %Vrc\n", rc);70 PrintError("%x: RTSemMutexRequestNoResume failed with %Rrc\n", rc); 57 71 break; 58 72 } 59 73 if (ASMAtomicIncU32(&g_cbConcurrent) != 1) 60 74 { 61 RTPrintf("g_cbConcurrent=%d after request!\n", g_cbConcurrent);75 PrintError("g_cbConcurrent=%d after request!\n", g_cbConcurrent); 62 76 break; 63 77 } … … 69 83 70 84 /* 71 * Check for correctness: Give other threads a chance. If the implementation is 85 * Check for correctness: Give other threads a chance. If the implementation is 72 86 * correct, no other thread will be able to enter this lock now. 73 87 */ 74 RTThreadYield(); 88 if (g_fYield) 89 RTThreadYield(); 75 90 if (ASMAtomicDecU32(&g_cbConcurrent) != 0) 76 91 { 77 RTPrintf("g_cbConcurrent=%d before release!\n", g_cbConcurrent);92 PrintError("g_cbConcurrent=%d before release!\n", g_cbConcurrent); 78 93 break; 79 94 } … … 81 96 if (RT_FAILURE(rc)) 82 97 { 83 RTPrintf("%x: RTSemMutexRelease failed with %Vrc\n", rc);98 PrintError("%x: RTSemMutexRelease failed with %Rrc\n", rc); 84 99 break; 85 100 } … … 87 102 break; 88 103 } 89 RTPrintf(" Thread %08x exited with %lld\n", ThreadSelf, *pu64);104 RTPrintf("tstSemMutex: Thread %08x exited with %lld\n", ThreadSelf, *pu64); 90 105 return VINF_SUCCESS; 91 106 } … … 95 110 int rc; 96 111 unsigned u; 97 RTTHREAD Thread[ELEMENTS(g_au64)];112 RTTHREAD aThreads[RT_ELEMENTS(g_au64)]; 98 113 99 114 rc = RTR3Init(false, 0); 100 115 if (RT_FAILURE(rc)) 101 116 { 102 RTPrintf(" RTR3Init failed (rc=%Vrc)\n", rc);103 exit(1);117 RTPrintf("tstSemMutex: RTR3Init failed (rc=%Rrc)\n", rc); 118 return 1; 104 119 } 120 121 105 122 rc = RTSemMutexCreate(&g_mutex); 106 123 if (RT_FAILURE(rc)) 107 124 { 108 RTPrintf(" RTSemMutexCreate failed (rc=%Vrc)\n", rc);109 exit(1);125 RTPrintf("tstSemMutex: RTSemMutexCreate failed (rc=%Rrc)\n", rc); 126 return 1; 110 127 } 111 for (u =0; u<ELEMENTS(g_au64); u++)128 for (u = 0; u < RT_ELEMENTS(g_au64); u++) 112 129 { 113 rc = RTThreadCreate(& Thread[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test");130 rc = RTThreadCreate(&aThreads[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test"); 114 131 if (RT_FAILURE(rc)) 115 132 { 116 RTPrintf(" RTThreadCreate failed for thread %u (rc=%Vrc)\n", u, rc);117 exit(1);133 RTPrintf("tstSemMutex: RTThreadCreate failed for thread %u (rc=%Rrc)\n", u, rc); 134 return 1; 118 135 } 119 136 } 120 137 121 RTPrintf("%u Threads created. Waiting for %u seconds ...\n", ELEMENTS(g_au64), SECONDS); 138 RTPrintf("tstSemMutex: %zu Threads created. Racing them for %u seconds (%s) ...\n", 139 RT_ELEMENTS(g_au64), SECONDS, g_fYield ? "yielding" : "no yielding"); 122 140 RTThreadSleep(SECONDS * 1000); 123 141 g_fTerminate = true; … … 126 144 RTThreadSleep(100); 127 145 128 for (u =1; u<ELEMENTS(g_au64); u++)146 for (u = 1; u < RT_ELEMENTS(g_au64); u++) 129 147 g_au64[0] += g_au64[u]; 148 RTPrintf("tstSemMutex: Done. In total: %lld\n", g_au64[0]); 130 149 131 RTPrintf("Done. In total: %lld\n", g_au64[0]); 150 151 if (!g_cErrors) 152 RTPrintf("tstSemMutex: SUCCESS\n"); 153 else 154 RTPrintf("tstSemMutex: FAILURE - %u errors\n", g_cErrors); 155 return g_cErrors != 0; 132 156 } 157
Note:
See TracChangeset
for help on using the changeset viewer.