benchlib.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. *
  3. *
  4. * --------------------------------------------------------------------------
  5. *
  6. * Pthreads-win32 - POSIX Threads Library for Win32
  7. * Copyright(C) 1998 John E. Bossom
  8. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. * The current list of contributors is contained
  13. * in the file CONTRIBUTORS included with the source
  14. * code distribution. The list can also be seen at the
  15. * following World Wide Web location:
  16. * http://sources.redhat.com/pthreads-win32/contributors.html
  17. *
  18. * This library is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2 of the License, or (at your option) any later version.
  22. *
  23. * This library is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with this library in the file COPYING.LIB;
  30. * if not, write to the Free Software Foundation, Inc.,
  31. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  32. *
  33. */
  34. #include "../config.h"
  35. #include "pthread.h"
  36. #include "sched.h"
  37. #include "semaphore.h"
  38. #include <windows.h>
  39. #include <stdio.h>
  40. #ifdef __GNUC__
  41. #include <stdlib.h>
  42. #endif
  43. #include "benchtest.h"
  44. int old_mutex_use = OLD_WIN32CS;
  45. BOOL (WINAPI *ptw32_try_enter_critical_section)(LPCRITICAL_SECTION) = NULL;
  46. HINSTANCE ptw32_h_kernel32;
  47. void
  48. dummy_call(int * a)
  49. {
  50. }
  51. void
  52. interlocked_inc_with_conditionals(int * a)
  53. {
  54. if (a != NULL)
  55. if (InterlockedIncrement((long *) a) == -1)
  56. {
  57. *a = 0;
  58. }
  59. }
  60. void
  61. interlocked_dec_with_conditionals(int * a)
  62. {
  63. if (a != NULL)
  64. if (InterlockedDecrement((long *) a) == -1)
  65. {
  66. *a = 0;
  67. }
  68. }
  69. int
  70. old_mutex_init(old_mutex_t *mutex, const old_mutexattr_t *attr)
  71. {
  72. int result = 0;
  73. old_mutex_t mx;
  74. if (mutex == NULL)
  75. {
  76. return EINVAL;
  77. }
  78. mx = (old_mutex_t) calloc(1, sizeof(*mx));
  79. if (mx == NULL)
  80. {
  81. result = ENOMEM;
  82. goto FAIL0;
  83. }
  84. mx->mutex = 0;
  85. if (attr != NULL
  86. && *attr != NULL
  87. && (*attr)->pshared == PTHREAD_PROCESS_SHARED
  88. )
  89. {
  90. result = ENOSYS;
  91. }
  92. else
  93. {
  94. CRITICAL_SECTION cs;
  95. /*
  96. * Load KERNEL32 and try to get address of TryEnterCriticalSection
  97. */
  98. ptw32_h_kernel32 = LoadLibrary(TEXT("KERNEL32.DLL"));
  99. ptw32_try_enter_critical_section = (BOOL (WINAPI *)(LPCRITICAL_SECTION))
  100. #if defined(NEED_UNICODE_CONSTS)
  101. GetProcAddress(ptw32_h_kernel32,
  102. (const TCHAR *)TEXT("TryEnterCriticalSection"));
  103. #else
  104. GetProcAddress(ptw32_h_kernel32,
  105. (LPCSTR) "TryEnterCriticalSection");
  106. #endif
  107. if (ptw32_try_enter_critical_section != NULL)
  108. {
  109. InitializeCriticalSection(&cs);
  110. if ((*ptw32_try_enter_critical_section)(&cs))
  111. {
  112. LeaveCriticalSection(&cs);
  113. }
  114. else
  115. {
  116. /*
  117. * Not really supported (Win98?).
  118. */
  119. ptw32_try_enter_critical_section = NULL;
  120. }
  121. DeleteCriticalSection(&cs);
  122. }
  123. if (ptw32_try_enter_critical_section == NULL)
  124. {
  125. (void) FreeLibrary(ptw32_h_kernel32);
  126. ptw32_h_kernel32 = 0;
  127. }
  128. if (old_mutex_use == OLD_WIN32CS)
  129. {
  130. InitializeCriticalSection(&mx->cs);
  131. }
  132. else if (old_mutex_use == OLD_WIN32MUTEX)
  133. {
  134. mx->mutex = CreateMutex (NULL,
  135. FALSE,
  136. NULL);
  137. if (mx->mutex == 0)
  138. {
  139. result = EAGAIN;
  140. }
  141. }
  142. else
  143. {
  144. result = EINVAL;
  145. }
  146. }
  147. if (result != 0 && mx != NULL)
  148. {
  149. free(mx);
  150. mx = NULL;
  151. }
  152. FAIL0:
  153. *mutex = mx;
  154. return(result);
  155. }
  156. int
  157. old_mutex_lock(old_mutex_t *mutex)
  158. {
  159. int result = 0;
  160. old_mutex_t mx;
  161. if (mutex == NULL || *mutex == NULL)
  162. {
  163. return EINVAL;
  164. }
  165. if (*mutex == (old_mutex_t) PTW32_OBJECT_AUTO_INIT)
  166. {
  167. /*
  168. * Don't use initialisers when benchtesting.
  169. */
  170. result = EINVAL;
  171. }
  172. mx = *mutex;
  173. if (result == 0)
  174. {
  175. if (mx->mutex == 0)
  176. {
  177. EnterCriticalSection(&mx->cs);
  178. }
  179. else
  180. {
  181. result = (WaitForSingleObject(mx->mutex, INFINITE)
  182. == WAIT_OBJECT_0)
  183. ? 0
  184. : EINVAL;
  185. }
  186. }
  187. return(result);
  188. }
  189. int
  190. old_mutex_unlock(old_mutex_t *mutex)
  191. {
  192. int result = 0;
  193. old_mutex_t mx;
  194. if (mutex == NULL || *mutex == NULL)
  195. {
  196. return EINVAL;
  197. }
  198. mx = *mutex;
  199. if (mx != (old_mutex_t) PTW32_OBJECT_AUTO_INIT)
  200. {
  201. if (mx->mutex == 0)
  202. {
  203. LeaveCriticalSection(&mx->cs);
  204. }
  205. else
  206. {
  207. result = (ReleaseMutex (mx->mutex) ? 0 : EINVAL);
  208. }
  209. }
  210. else
  211. {
  212. result = EINVAL;
  213. }
  214. return(result);
  215. }
  216. int
  217. old_mutex_trylock(old_mutex_t *mutex)
  218. {
  219. int result = 0;
  220. old_mutex_t mx;
  221. if (mutex == NULL || *mutex == NULL)
  222. {
  223. return EINVAL;
  224. }
  225. if (*mutex == (old_mutex_t) PTW32_OBJECT_AUTO_INIT)
  226. {
  227. /*
  228. * Don't use initialisers when benchtesting.
  229. */
  230. result = EINVAL;
  231. }
  232. mx = *mutex;
  233. if (result == 0)
  234. {
  235. if (mx->mutex == 0)
  236. {
  237. if (ptw32_try_enter_critical_section == NULL)
  238. {
  239. result = 0;
  240. }
  241. else if ((*ptw32_try_enter_critical_section)(&mx->cs) != TRUE)
  242. {
  243. result = EBUSY;
  244. }
  245. }
  246. else
  247. {
  248. DWORD status;
  249. status = WaitForSingleObject (mx->mutex, 0);
  250. if (status != WAIT_OBJECT_0)
  251. {
  252. result = ((status == WAIT_TIMEOUT)
  253. ? EBUSY
  254. : EINVAL);
  255. }
  256. }
  257. }
  258. return(result);
  259. }
  260. int
  261. old_mutex_destroy(old_mutex_t *mutex)
  262. {
  263. int result = 0;
  264. old_mutex_t mx;
  265. if (mutex == NULL
  266. || *mutex == NULL)
  267. {
  268. return EINVAL;
  269. }
  270. if (*mutex != (old_mutex_t) PTW32_OBJECT_AUTO_INIT)
  271. {
  272. mx = *mutex;
  273. if ((result = old_mutex_trylock(&mx)) == 0)
  274. {
  275. *mutex = NULL;
  276. (void) old_mutex_unlock(&mx);
  277. if (mx->mutex == 0)
  278. {
  279. DeleteCriticalSection(&mx->cs);
  280. }
  281. else
  282. {
  283. result = (CloseHandle (mx->mutex) ? 0 : EINVAL);
  284. }
  285. if (result == 0)
  286. {
  287. mx->mutex = 0;
  288. free(mx);
  289. }
  290. else
  291. {
  292. *mutex = mx;
  293. }
  294. }
  295. }
  296. else
  297. {
  298. result = EINVAL;
  299. }
  300. if (ptw32_try_enter_critical_section != NULL)
  301. {
  302. (void) FreeLibrary(ptw32_h_kernel32);
  303. ptw32_h_kernel32 = 0;
  304. }
  305. return(result);
  306. }
  307. /****************************************************************************************/