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