-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsimplethread.h
146 lines (118 loc) · 2.92 KB
/
simplethread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef SIMPLE_THREAD_H
#define SIMPLE_THREAD_H
struct thread {
void (*func)(void *);
void *ud;
};
struct thread_event;
static void thread_join(struct thread * threads, int n);
static void thread_event_create(struct thread_event *ev);
static void thread_event_release(struct thread_event *ev);
static void thread_event_trigger(struct thread_event *ev);
static void thread_event_wait(struct thread_event *ev);
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
#include <windows.h>
#ifdef _MSC_VER
#define INLINE __inline
#else
#define INLINE inline
#endif
static DWORD INLINE WINAPI
thread_function(LPVOID lpParam) {
struct thread * t = (struct thread *)lpParam;
t->func(t->ud);
return 0;
}
static INLINE void
thread_join(struct thread * threads, int n) {
int i;
HANDLE *thread_handle = (HANDLE *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,n*sizeof(HANDLE));
for (i=0;i<n;i++) {
thread_handle[i] = CreateThread(NULL, 0, thread_function, (LPVOID)&threads[i], 0, NULL);
if (thread_handle[i] == NULL) {
HeapFree(GetProcessHeap(), 0, thread_handle);
return;
}
}
WaitForMultipleObjects(n, thread_handle, TRUE, INFINITE);
for (i=0;i<n;i++) {
CloseHandle(thread_handle[i]);
}
HeapFree(GetProcessHeap(), 0, thread_handle);
}
struct thread_event {
HANDLE event;
};
static INLINE void
thread_event_create(struct thread_event *ev) {
ev->event = CreateEvent(NULL, FALSE, FALSE, NULL);
}
static INLINE void
thread_event_release(struct thread_event *ev) {
if (ev->event) {
CloseHandle(ev->event);
ev->event = NULL;
}
}
static INLINE void
thread_event_trigger(struct thread_event *ev) {
SetEvent(ev->event);
}
static INLINE void
thread_event_wait(struct thread_event *ev) {
WaitForSingleObject(ev->event, INFINITE);
}
#else
#include <pthread.h>
static inline void *
thread_function(void * args) {
struct thread * t = (struct thread *)args;
t->func(t->ud);
return NULL;
}
static inline void
thread_join(struct thread *threads, int n) {
pthread_t pid[n];
int i;
for (i=0;i<n;i++) {
if (pthread_create(&pid[i], NULL, thread_function, &threads[i])) {
return;
}
}
for (i=0;i<n;i++) {
pthread_join(pid[i], NULL);
}
}
struct thread_event {
pthread_cond_t cond;
pthread_mutex_t mutex;
int flag;
};
static inline void
thread_event_create(struct thread_event *ev) {
pthread_mutex_init(&ev->mutex, NULL);
pthread_cond_init(&ev->cond, NULL);
ev->flag = 0;
}
static inline void
thread_event_release(struct thread_event *ev) {
pthread_mutex_destroy(&ev->mutex);
pthread_cond_destroy(&ev->cond);
}
static inline void
thread_event_trigger(struct thread_event *ev) {
pthread_mutex_lock(&ev->mutex);
ev->flag = 1;
pthread_mutex_unlock(&ev->mutex);
pthread_cond_signal(&ev->cond);
}
static inline void
thread_event_wait(struct thread_event *ev) {
pthread_mutex_lock(&ev->mutex);
while (!ev->flag)
pthread_cond_wait(&ev->cond, &ev->mutex);
ev->flag = 0;
pthread_mutex_unlock(&ev->mutex);
}
#endif
#endif