Threads.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Threads.c -- multithreading library
  2. 2008-08-05
  3. Igor Pavlov
  4. Public domain */
  5. #include "Threads.h"
  6. #include <process.h>
  7. static WRes GetError()
  8. {
  9. DWORD res = GetLastError();
  10. return (res) ? (WRes)(res) : 1;
  11. }
  12. WRes HandleToWRes(HANDLE h) { return (h != 0) ? 0 : GetError(); }
  13. WRes BOOLToWRes(BOOL v) { return v ? 0 : GetError(); }
  14. static WRes MyCloseHandle(HANDLE *h)
  15. {
  16. if (*h != NULL)
  17. if (!CloseHandle(*h))
  18. return GetError();
  19. *h = NULL;
  20. return 0;
  21. }
  22. WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter)
  23. {
  24. unsigned threadId; /* Windows Me/98/95: threadId parameter may not be NULL in _beginthreadex/CreateThread functions */
  25. thread->handle =
  26. /* CreateThread(0, 0, startAddress, parameter, 0, &threadId); */
  27. (HANDLE)_beginthreadex(NULL, 0, startAddress, parameter, 0, &threadId);
  28. /* maybe we must use errno here, but probably GetLastError() is also OK. */
  29. return HandleToWRes(thread->handle);
  30. }
  31. WRes WaitObject(HANDLE h)
  32. {
  33. return (WRes)WaitForSingleObject(h, INFINITE);
  34. }
  35. WRes Thread_Wait(CThread *thread)
  36. {
  37. if (thread->handle == NULL)
  38. return 1;
  39. return WaitObject(thread->handle);
  40. }
  41. WRes Thread_Close(CThread *thread)
  42. {
  43. return MyCloseHandle(&thread->handle);
  44. }
  45. WRes Event_Create(CEvent *p, BOOL manualReset, int initialSignaled)
  46. {
  47. p->handle = CreateEvent(NULL, manualReset, (initialSignaled ? TRUE : FALSE), NULL);
  48. return HandleToWRes(p->handle);
  49. }
  50. WRes ManualResetEvent_Create(CManualResetEvent *p, int initialSignaled)
  51. { return Event_Create(p, TRUE, initialSignaled); }
  52. WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p)
  53. { return ManualResetEvent_Create(p, 0); }
  54. WRes AutoResetEvent_Create(CAutoResetEvent *p, int initialSignaled)
  55. { return Event_Create(p, FALSE, initialSignaled); }
  56. WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p)
  57. { return AutoResetEvent_Create(p, 0); }
  58. WRes Event_Set(CEvent *p) { return BOOLToWRes(SetEvent(p->handle)); }
  59. WRes Event_Reset(CEvent *p) { return BOOLToWRes(ResetEvent(p->handle)); }
  60. WRes Event_Wait(CEvent *p) { return WaitObject(p->handle); }
  61. WRes Event_Close(CEvent *p) { return MyCloseHandle(&p->handle); }
  62. WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount)
  63. {
  64. p->handle = CreateSemaphore(NULL, (LONG)initiallyCount, (LONG)maxCount, NULL);
  65. return HandleToWRes(p->handle);
  66. }
  67. WRes Semaphore_Release(CSemaphore *p, LONG releaseCount, LONG *previousCount)
  68. {
  69. return BOOLToWRes(ReleaseSemaphore(p->handle, releaseCount, previousCount));
  70. }
  71. WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
  72. {
  73. return Semaphore_Release(p, (LONG)releaseCount, NULL);
  74. }
  75. WRes Semaphore_Release1(CSemaphore *p)
  76. {
  77. return Semaphore_ReleaseN(p, 1);
  78. }
  79. WRes Semaphore_Wait(CSemaphore *p) { return WaitObject(p->handle); }
  80. WRes Semaphore_Close(CSemaphore *p) { return MyCloseHandle(&p->handle); }
  81. WRes CriticalSection_Init(CCriticalSection *p)
  82. {
  83. /* InitializeCriticalSection can raise only STATUS_NO_MEMORY exception */
  84. __try
  85. {
  86. InitializeCriticalSection(p);
  87. /* InitializeCriticalSectionAndSpinCount(p, 0); */
  88. }
  89. __except (EXCEPTION_EXECUTE_HANDLER) { return 1; }
  90. return 0;
  91. }