Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <tchar.h>
- #include <errno.h>
- #include "pthread.h"
- typedef BOOL (WINAPI *LPINIT_CSEX)(LPCRITICAL_SECTION pCS, DWORD dwSpinCount, DWORD Flags);
- typedef void (WINAPI *LPINIT_CS)(LPCRITICAL_SECTION pCS);
- static LPINIT_CSEX pInitializeCriticalSectionEx;
- #define RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO 0x01000000
- #define CRITICAL_SECTION_NO_DEBUG_INFO RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO
- typedef struct
- {
- CRITICAL_SECTION BroadcastLock;
- CRITICAL_SECTION WaiterCountLock;
- int iWaiterCount;
- HANDLE hSemaphore;
- HANDLE hWaitersDone;
- BOOL bNowBroadcast;
- } cond_handle;
- static void WINAPI InitCriticalSectionXP(LPCRITICAL_SECTION pCS)
- {
- InitializeCriticalSection(pCS);
- }
- static void WINAPI InitCriticalSectionVista(LPCRITICAL_SECTION pCS)
- {
- pInitializeCriticalSectionEx(pCS, 0, CRITICAL_SECTION_NO_DEBUG_INFO);
- }
- static LPINIT_CS InitCriticalSection = InitCriticalSectionXP;
- #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
- static pthread_t defMutex;
- void pthread_initialize(void)
- {
- BOOL VistaSupport = FALSE;
- OSVERSIONINFOEX infoex = {};
- infoex.dwOSVersionInfoSize = sizeof(infoex);
- if(!GetVersionEx((LPOSVERSIONINFO) &infoex))
- {
- OSVERSIONINFO info = {};
- info.dwOSVersionInfoSize = sizeof(info);
- if(GetVersionEx(&info))
- {
- if(info.dwMajorVersion >= 6)
- VistaSupport = TRUE;
- }
- }
- else
- {
- if(infoex.dwMajorVersion >= 6)
- {
- VistaSupport = TRUE;
- }
- }
- if(VistaSupport)
- {
- InitCriticalSection = InitCriticalSectionVista;
- pInitializeCriticalSectionEx = (LPINIT_CSEX) GetProcAddress( GetModuleHandle(_T("Kernel32.dll")), "InitializeCriticalSectionEx");
- }
- pthread_mutex_init(&defMutex, NULL);
- }
- void pthread_finalize(void)
- {
- pthread_mutex_destroy(&defMutex);
- }
- struct func_convert_param
- {
- HANDLE hEvent;
- void *(*start_routine)(void *);
- void* arg;
- };
- unsigned int __stdcall func_convert(void* ptr)
- {
- struct func_convert_param Param = *((struct func_convert_param*) ptr);
- SetEvent(Param.hEvent);
- Param.start_routine(Param.arg);
- return 0;
- }
- int pthread_create(
- pthread_t *thread,
- const pthread_attr_t *attr,
- void *(*start_routine)(void *),
- void *arg
- )
- {
- struct func_convert_param param;
- param.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- param.arg = arg;
- param.start_routine = start_routine;
- unsigned int x = _beginthreadex(NULL, 0, func_convert, ¶m, 0, NULL);
- if(x == 0)
- {
- return errno;
- }
- WaitForSingleObject(param.hEvent, INFINITE);
- CloseHandle(param.hEvent);
- *thread = (pthread_t) x;
- return 0;
- }
- int pthread_join(
- pthread_t thread,
- void** value_ptr
- )
- {
- HANDLE hv = (HANDLE) thread;
- DWORD dwRet = WaitForSingleObject(thread, INFINITE);
- if(WAIT_OBJECT_0)
- {
- CloseHandle(hv);
- return 0;
- }
- return EINVAL;
- }
- int pthread_detach(
- pthread_t thread
- )
- {
- HANDLE hv = (HANDLE) thread;
- CloseHandle(hv);
- return 0;
- }
- pthread_t pthread_self(void)
- {
- return (pthread_t) GetCurrentThread();
- }
- int pthread_setschedparam(
- pthread_t target_thread,
- int policy,
- const struct sched_param *param
- )
- {
- return 0;
- }
- int pthread_getschedparam(
- pthread_t target_thread,
- int *policy,
- struct sched_param *param
- )
- {
- param->sched_priority = 0;
- return 0;
- }
- int pthread_attr_init(
- pthread_attr_t *attr
- )
- {
- return 0;
- }
- int pthread_attr_destroy(
- pthread_attr_t *attr
- )
- {
- return 0;
- }
- // mutex
- int pthread_mutex_init(
- pthread_mutex_t *mutex,
- const pthread_mutexattr_t *attr
- )
- {
- HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
- *mutex = (pthread_mutex_t*) hMutex;
- return 0;
- }
- int pthread_mutex_destroy(
- pthread_mutex_t *mutex
- )
- {
- HANDLE hMutex = (HANDLE) *mutex;
- CloseHandle(hMutex);
- return 0;
- }
- int pthread_mutex_lock(
- pthread_mutex_t *mutex
- )
- {
- if(*mutex == PTHREAD_MUTEX_INITIALIZER)
- {
- *mutex = defMutex;
- }
- HANDLE hMutex = (HANDLE) *mutex;
- WaitForSingleObject(hMutex, INFINITE);
- return 0;
- }
- int pthread_mutex_trylock(
- pthread_mutex_t *mutex
- )
- {
- if(*mutex == PTHREAD_MUTEX_INITIALIZER)
- {
- *mutex = defMutex;
- }
- HANDLE hMutex = (HANDLE) *mutex;
- if(WaitForSingleObject(hMutex, 0) == WAIT_OBJECT_0) return 0;
- return 1;
- }
- int pthread_mutex_unlock(
- pthread_mutex_t *mutex
- )
- {
- if(*mutex == PTHREAD_MUTEX_INITIALIZER)
- {
- *mutex = defMutex;
- }
- HANDLE hMutex = (HANDLE) *mutex;
- ReleaseMutex(hMutex);
- return 0;
- }
- int pthread_cond_init(
- pthread_cond_t *cond,
- const pthread_condattr_t *attr
- )
- {
- cond_handle* x;
- x = malloc(sizeof(cond_handle));
- x->iWaiterCount = 0;
- x->bNowBroadcast = FALSE;
- x->hSemaphore = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
- if(x->hSemaphore == NULL)
- {
- free(x);
- return ENOMEM;
- }
- InitCriticalSection(&(x->WaiterCountLock));
- InitCriticalSection(&(x->BroadcastLock));
- x->hWaitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
- if(x->hWaitersDone == NULL)
- {
- CloseHandle(x->hWaitersDone);
- free(x);
- return ENOMEM;
- }
- *cond = (pthread_cond_t) x;
- return 0;
- }
- int pthread_cond_destroy(
- pthread_cond_t *cond
- )
- {
- cond_handle* x = (cond_handle*) *cond;
- CloseHandle(x->hSemaphore);
- CloseHandle(x->hWaitersDone);
- DeleteCriticalSection(&(x->WaiterCountLock));
- free(x);
- return 0;
- }
- int pthread_cond_signal(
- pthread_cond_t *cond
- )
- {
- cond_handle* x = (cond_handle*) *cond;
- EnterCriticalSection(&(x->WaiterCountLock));
- BOOL bHaveWaiters = x->iWaiterCount > 0;
- LeaveCriticalSection(&(x->WaiterCountLock));
- if (bHaveWaiters)
- {
- ReleaseSemaphore(x->hSemaphore, 1, 0);
- }
- return 0;
- }
- int pthread_cond_broadcast(
- pthread_cond_t *cond
- )
- {
- cond_handle* x = (cond_handle*) *cond;
- EnterCriticalSection(&(x->BroadcastLock));
- EnterCriticalSection(&(x->WaiterCountLock));
- BOOL bHaveWaiter = FALSE;
- if (x->iWaiterCount > 0) {
- x->bNowBroadcast = TRUE;
- bHaveWaiter = TRUE;
- }
- if (bHaveWaiter) {
- ReleaseSemaphore(x->hSemaphore, x->iWaiterCount, 0);
- LeaveCriticalSection(&(x->WaiterCountLock));
- WaitForSingleObject(x->hWaitersDone, INFINITE);
- x->bNowBroadcast = FALSE;
- }
- else
- {
- LeaveCriticalSection(&(x->WaiterCountLock));
- }
- LeaveCriticalSection(&(x->BroadcastLock));
- return 0;
- }
- int pthread_cond_wait(
- pthread_cond_t *cond,
- pthread_mutex_t *mutex
- )
- {
- cond_handle* x = (cond_handle*) *cond;
- HANDLE y = (HANDLE) *mutex;
- EnterCriticalSection(&(x->BroadcastLock));
- LeaveCriticalSection(&(x->BroadcastLock));
- EnterCriticalSection(&(x->WaiterCountLock));
- x->iWaiterCount++;
- LeaveCriticalSection(&(x->WaiterCountLock));
- SignalObjectAndWait(y, x->hSemaphore, INFINITE, FALSE);
- EnterCriticalSection(&(x->WaiterCountLock));
- x->iWaiterCount--;
- BOOL bLastWaiter = x->bNowBroadcast && x->iWaiterCount == 0;
- LeaveCriticalSection(&(x->WaiterCountLock));
- if(bLastWaiter)
- {
- SignalObjectAndWait(x->hWaitersDone, y, INFINITE, FALSE);
- }
- else
- {
- WaitForSingleObject(y, INFINITE);
- }
- return 0;
- }
- int pthread_num_processors_np(void)
- {
- DWORD_PTR mask ;
- DWORD_PTR unused ;
- DWORD_PTR bit ;
- int num = 0;
- GetProcessAffinityMask( GetCurrentProcess(), &mask, &unused ) ;
- for ( bit = 1; bit != 0 ; bit <<= 1)
- {
- if ( mask & bit )
- {
- ++num;
- }
- }
- return num ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement