Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  1. /* Threads.c -- multithreading library
  2. 2013-11-12 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #ifndef _WIN32_WCE
  5. #include <process.h>
  6. #endif
  7. #include "Threads.h"
  8. static WRes GetError()
  9. {
  10. DWORD res = GetLastError();
  11. return (res) ? (WRes)(res) : 1;
  12. }
  13. WRes HandleToWRes(HANDLE h) { return (h != 0) ? 0 : GetError(); }
  14. WRes BOOLToWRes(BOOL v) { return v ? 0 : GetError(); }
  15. WRes HandlePtr_Close(HANDLE *p)
  16. {
  17. if (*p != NULL)
  18. if (!CloseHandle(*p))
  19. return GetError();
  20. *p = NULL;
  21. return 0;
  22. }
  23. WRes Handle_WaitObject(HANDLE h) { return (WRes)WaitForSingleObject(h, INFINITE); }
  24. WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param)
  25. {
  26. /* Windows Me/98/95: threadId parameter may not be NULL in _beginthreadex/CreateThread functions */
  27. #ifdef UNDER_CE
  28. DWORD threadId;
  29. *p = CreateThread(0, 0, func, param, 0, &threadId);
  30. #else
  31. unsigned threadId;
  32. *p = (HANDLE)_beginthreadex(NULL, 0, func, param, 0, &threadId);
  33. #endif
  34. /* maybe we must use errno here, but probably GetLastError() is also OK. */
  35. return HandleToWRes(*p);
  36. }
  37. WRes Event_Create(CEvent *p, BOOL manualReset, int signaled)
  38. {
  39. *p = CreateEvent(NULL, manualReset, (signaled ? TRUE : FALSE), NULL);
  40. return HandleToWRes(*p);
  41. }
  42. WRes Event_Set(CEvent *p) { return BOOLToWRes(SetEvent(*p)); }
  43. WRes Event_Reset(CEvent *p) { return BOOLToWRes(ResetEvent(*p)); }
  44. WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled) { return Event_Create(p, TRUE, signaled); }
  45. WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled) { return Event_Create(p, FALSE, signaled); }
  46. WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p) { return ManualResetEvent_Create(p, 0); }
  47. WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p) { return AutoResetEvent_Create(p, 0); }
  48. WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount)
  49. {
  50. *p = CreateSemaphore(NULL, (LONG)initCount, (LONG)maxCount, NULL);
  51. return HandleToWRes(*p);
  52. }
  53. static WRes Semaphore_Release(CSemaphore *p, LONG releaseCount, LONG *previousCount)
  54. { return BOOLToWRes(ReleaseSemaphore(*p, releaseCount, previousCount)); }
  55. WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num)
  56. { return Semaphore_Release(p, (LONG)num, NULL); }
  57. WRes Semaphore_Release1(CSemaphore *p) { return Semaphore_ReleaseN(p, 1); }
  58. WRes CriticalSection_Init(CCriticalSection *p)
  59. {
  60. /* InitializeCriticalSection can raise only STATUS_NO_MEMORY exception */
  61. #ifdef _MSC_VER
  62. __try
  63. #endif
  64. {
  65. InitializeCriticalSection(p);
  66. /* InitializeCriticalSectionAndSpinCount(p, 0); */
  67. }
  68. #ifdef _MSC_VER
  69. __except (EXCEPTION_EXECUTE_HANDLER) { return 1; }
  70. #endif
  71. return 0;
  72. }