diff src/win32/7zip/7z/C/Threads.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/C/Threads.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* Threads.c -- multithreading library
     1.5 +2008-08-05
     1.6 +Igor Pavlov
     1.7 +Public domain */
     1.8 +
     1.9 +#include "Threads.h"
    1.10 +#include <process.h>
    1.11 +
    1.12 +static WRes GetError()
    1.13 +{
    1.14 +  DWORD res = GetLastError();
    1.15 +  return (res) ? (WRes)(res) : 1;
    1.16 +}
    1.17 +
    1.18 +WRes HandleToWRes(HANDLE h) { return (h != 0) ? 0 : GetError(); }
    1.19 +WRes BOOLToWRes(BOOL v) { return v ? 0 : GetError(); }
    1.20 +
    1.21 +static WRes MyCloseHandle(HANDLE *h)
    1.22 +{
    1.23 +  if (*h != NULL)
    1.24 +    if (!CloseHandle(*h))
    1.25 +      return GetError();
    1.26 +  *h = NULL;
    1.27 +  return 0;
    1.28 +}
    1.29 +
    1.30 +WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter)
    1.31 +{
    1.32 +  unsigned threadId; /* Windows Me/98/95: threadId parameter may not be NULL in _beginthreadex/CreateThread functions */
    1.33 +  thread->handle =
    1.34 +    /* CreateThread(0, 0, startAddress, parameter, 0, &threadId); */
    1.35 +    (HANDLE)_beginthreadex(NULL, 0, startAddress, parameter, 0, &threadId);
    1.36 +    /* maybe we must use errno here, but probably GetLastError() is also OK. */
    1.37 +  return HandleToWRes(thread->handle);
    1.38 +}
    1.39 +
    1.40 +WRes WaitObject(HANDLE h)
    1.41 +{
    1.42 +  return (WRes)WaitForSingleObject(h, INFINITE);
    1.43 +}
    1.44 +
    1.45 +WRes Thread_Wait(CThread *thread)
    1.46 +{
    1.47 +  if (thread->handle == NULL)
    1.48 +    return 1;
    1.49 +  return WaitObject(thread->handle);
    1.50 +}
    1.51 +
    1.52 +WRes Thread_Close(CThread *thread)
    1.53 +{
    1.54 +  return MyCloseHandle(&thread->handle);
    1.55 +}
    1.56 +
    1.57 +WRes Event_Create(CEvent *p, BOOL manualReset, int initialSignaled)
    1.58 +{
    1.59 +  p->handle = CreateEvent(NULL, manualReset, (initialSignaled ? TRUE : FALSE), NULL);
    1.60 +  return HandleToWRes(p->handle);
    1.61 +}
    1.62 +
    1.63 +WRes ManualResetEvent_Create(CManualResetEvent *p, int initialSignaled)
    1.64 +  { return Event_Create(p, TRUE, initialSignaled); }
    1.65 +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p)
    1.66 +  { return ManualResetEvent_Create(p, 0); }
    1.67 +
    1.68 +WRes AutoResetEvent_Create(CAutoResetEvent *p, int initialSignaled)
    1.69 +  { return Event_Create(p, FALSE, initialSignaled); }
    1.70 +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p)
    1.71 +  { return AutoResetEvent_Create(p, 0); }
    1.72 +
    1.73 +WRes Event_Set(CEvent *p) { return BOOLToWRes(SetEvent(p->handle)); }
    1.74 +WRes Event_Reset(CEvent *p) { return BOOLToWRes(ResetEvent(p->handle)); }
    1.75 +WRes Event_Wait(CEvent *p) { return WaitObject(p->handle); }
    1.76 +WRes Event_Close(CEvent *p) { return MyCloseHandle(&p->handle); }
    1.77 +
    1.78 +
    1.79 +WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount)
    1.80 +{
    1.81 +  p->handle = CreateSemaphore(NULL, (LONG)initiallyCount, (LONG)maxCount, NULL);
    1.82 +  return HandleToWRes(p->handle);
    1.83 +}
    1.84 +
    1.85 +WRes Semaphore_Release(CSemaphore *p, LONG releaseCount, LONG *previousCount)
    1.86 +{
    1.87 +  return BOOLToWRes(ReleaseSemaphore(p->handle, releaseCount, previousCount));
    1.88 +}
    1.89 +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
    1.90 +{
    1.91 +  return Semaphore_Release(p, (LONG)releaseCount, NULL);
    1.92 +}
    1.93 +WRes Semaphore_Release1(CSemaphore *p)
    1.94 +{
    1.95 +  return Semaphore_ReleaseN(p, 1);
    1.96 +}
    1.97 +
    1.98 +WRes Semaphore_Wait(CSemaphore *p) { return WaitObject(p->handle); }
    1.99 +WRes Semaphore_Close(CSemaphore *p) { return MyCloseHandle(&p->handle); }
   1.100 +
   1.101 +WRes CriticalSection_Init(CCriticalSection *p)
   1.102 +{
   1.103 +  /* InitializeCriticalSection can raise only STATUS_NO_MEMORY exception */
   1.104 +  __try
   1.105 +  {
   1.106 +    InitializeCriticalSection(p);
   1.107 +    /* InitializeCriticalSectionAndSpinCount(p, 0); */
   1.108 +  }
   1.109 +  __except (EXCEPTION_EXECUTE_HANDLER) { return 1; }
   1.110 +  return 0;
   1.111 +}
   1.112 +