anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

pthread.cpp (2097B)

      1 /* POSIX emulation layer for Windows.
      2  *
      3  * (C) 2008-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 #include "pthread.h"
     10 
     11 struct ThreadInfo
     12 {
     13 	void *(*entry)(void *);
     14 	void *param;
     15 };
     16 
     17 static DWORD WINAPI entry_point(void *parameter)
     18 {
     19 	ThreadInfo *ti = static_cast<ThreadInfo *>(parameter);
     20 	ti->entry(ti->param);
     21 	delete ti;
     22 	return 0;
     23 }
     24 
     25 int pthread_attr_init(pthread_attr_t *)
     26 {
     27 	/* No need for this */
     28 	return 0;
     29 }
     30 
     31 int pthread_attr_setdetachstate(pthread_attr_t *, int)
     32 {
     33 	/* No need for this */
     34 	return 0;
     35 }
     36 
     37 int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param)
     38 {
     39 	ThreadInfo *ti = new ThreadInfo;
     40 	ti->entry = entry;
     41 	ti->param = param;
     42 
     43 	*thread = CreateThread(NULL, 0, entry_point, ti, 0, NULL);
     44 	if (!*thread)
     45 	{
     46 		delete ti;
     47 		return -1;
     48 	}
     49 
     50 	return 0;
     51 }
     52 
     53 int pthread_join(pthread_t thread, void **)
     54 {
     55 	if (WaitForSingleObject(thread, INFINITE) == WAIT_FAILED)
     56 		return -1;
     57 	CloseHandle(thread);
     58 	return 0;
     59 }
     60 
     61 void pthread_exit(int i)
     62 {
     63 	ExitThread(i);
     64 }
     65 
     66 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *)
     67 {
     68 	InitializeCriticalSection(mutex);
     69 	return 0;
     70 }
     71 
     72 int pthread_mutex_destroy(pthread_mutex_t *mutex)
     73 {
     74 	DeleteCriticalSection(mutex);
     75 	return 0;
     76 }
     77 
     78 int pthread_mutex_lock(pthread_mutex_t *mutex)
     79 {
     80 	EnterCriticalSection(mutex);
     81 	return 0;
     82 }
     83 
     84 int pthread_mutex_trylock(pthread_mutex_t *mutex)
     85 {
     86 	return !TryEnterCriticalSection(mutex);
     87 }
     88 
     89 int pthread_mutex_unlock(pthread_mutex_t *mutex)
     90 {
     91 	LeaveCriticalSection(mutex);
     92 	return 0;
     93 }
     94 
     95 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *)
     96 {
     97 	*cond = CreateEvent(NULL, false, false, NULL);
     98 	if (*cond == NULL)
     99 		return -1;
    100 	return 0;
    101 }
    102 
    103 int pthread_cond_destroy(pthread_cond_t *cond)
    104 {
    105 	return !CloseHandle(*cond);
    106 }
    107 
    108 int pthread_cond_signal(pthread_cond_t *cond)
    109 {
    110 	return !PulseEvent(*cond);
    111 }
    112 
    113 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    114 {
    115 	LeaveCriticalSection(mutex);
    116 	WaitForSingleObject(*cond, INFINITE);
    117 	EnterCriticalSection(mutex);
    118 	return 0;
    119 }