Threads.c 3.1 KB

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