| 1 |
/* Thread_emulation.h */ |
| 2 |
/* Author: Johnson M. Hart */ |
| 3 |
/* Emulate the Pthreads model for the Win32 platform */ |
| 4 |
/* The emulation is not complete, but it does provide a subset */ |
| 5 |
/* required for a first project */ |
| 6 |
/* Source: http://world.std.com/~jmhart/opensource.htm */ |
| 7 |
/* The emulation is not complete, but it does provide a subset */ |
| 8 |
/* that will work with many well-behaved programs */ |
| 9 |
/* IF YOU ARE REALLY SERIOUS ABOUT THIS, USE THE OPEN SOURCE */ |
| 10 |
/* PTHREAD LIBRARY. YOU'LL FIND IT ON THE RED HAT SITE */ |
| 11 |
|
| 12 |
#ifndef _THREAD_EMULATION |
| 13 |
# define _THREAD_EMULATION |
| 14 |
|
| 15 |
/* Thread management macros */ |
| 16 |
# ifdef _WIN32 |
| 17 |
# define _WIN32_WINNT 0x500 /* WINBASE.H - Enable SignalObjectAndWait */ |
| 18 |
# include <process.h> |
| 19 |
# include <windows.h> |
| 20 |
# define THREAD_FUNCTION_PROTO THREAD_FUNCTION_RETURN (__stdcall *) (void *) |
| 21 |
# define THREAD_FUNCTION_RETURN unsigned int |
| 22 |
# define THREAD_SPECIFIC_INDEX DWORD |
| 23 |
# define pthread_t HANDLE |
| 24 |
# define pthread_attr_t DWORD |
| 25 |
# define pthread_create(thhandle, attr, thfunc, tharg) ((int) ((*thhandle = (HANDLE) _beginthreadex(NULL, 0, (THREAD_FUNCTION_PROTO) thfunc, tharg, 0, NULL)) == NULL)) |
| 26 |
# define pthread_join(thread, result) ((WaitForSingleObject((thread), INFINITE) != WAIT_OBJECT_0) || !CloseHandle(thread)) |
| 27 |
# define pthread_detach(thread) { if (((void *) thread) != NULL) { CloseHandle((void *) thread); }} |
| 28 |
# define thread_sleep(nms) Sleep(nms) |
| 29 |
# define pthread_cancel(thread) TerminateThread(thread, 0) |
| 30 |
# define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();} |
| 31 |
# define pthread_getspecific(ts_key) TlsGetValue(ts_key) |
| 32 |
# define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value) |
| 33 |
# define pthread_self() GetCurrentThreadId() |
| 34 |
# else |
| 35 |
# include <pthread.h> |
| 36 |
# define THREAD_FUNCTION_RETURN void * |
| 37 |
# endif |
| 38 |
|
| 39 |
/* Syncrhronization macros: Win32->pthread */ |
| 40 |
# ifdef _WIN32 |
| 41 |
# define pthread_mutex_t HANDLE |
| 42 |
# define pthread_cond_t HANDLE |
| 43 |
# define pthread_mutex_lock(pobject) WaitForSingleObject(*pobject, INFINITE) |
| 44 |
# define pthread_mutex_unlock(pobject) (!ReleaseMutex(*pobject)) |
| 45 |
# define pthread_mutex_init(pobject,pattr) ((*pobject=CreateMutex(NULL, FALSE, NULL)) == NULL) |
| 46 |
# define pthread_cond_init(pobject,pattr) (*pobject=CreateEvent(NULL, FALSE, FALSE, NULL)) |
| 47 |
# define pthread_mutex_destroy(pobject) CloseHandle(*pobject) |
| 48 |
# define pthread_cond_destroy(pobject) CloseHandle(*pobject) |
| 49 |
# define pthread_cond_wait(pcv,pmutex) { SignalObjectAndWait(*pmutex, *pcv, INFINITE, FALSE); WaitForSingleObject(*pmutex, INFINITE); } |
| 50 |
# define pthread_cond_signal(pcv) SetEvent(*pcv) |
| 51 |
# endif |
| 52 |
|
| 53 |
#endif |