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