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