-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwqueue.c
161 lines (135 loc) · 3.26 KB
/
wqueue.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __linux__
#include <pthread.h>
#endif
#include "wqueue.h"
#ifdef _WIN32
static DWORD wqueue_thread(LPVOID p)
#endif
#ifdef __linux__
static void *wqueue_thread(void *p)
#endif
{
struct wqueue *q = (struct wqueue *) p;
#ifdef _WIN32
while (1) {
int close = 0;
WaitForSingleObject(q->lock, INFINITE);
close = q->closed;
ReleaseMutex(q->lock);
if (close)
break;
WaitForSingleObject(q->wpending, INFINITE);
WaitForSingleObject(q->lock, INFINITE);
void *work = q->work[0];
for (size_t i = 1; i < q->wcount; i++)
q->work[i - 1] = q->work[i];
q->wcount--;
ReleaseMutex(q->lock);
q->worker((void *) q, work);
}
CloseHandle(q->wpending);
CloseHandle(q->thread);
CloseHandle(q->lock);
#endif
#ifdef __linux__
while (1) {
pthread_mutex_lock(&q->lock);
if (q->closed) {
pthread_mutex_unlock(&q->lock);
break;
}
pthread_cond_wait(&q->wpending, &q->lock);
void *work = q->work[0];
for (size_t i = 1; i < q->wcount; i++)
q->work[i - 1] = q->work[i];
q->wcount--;
pthread_mutex_unlock(&q->lock);
q->worker(q, work);
}
pthread_cond_destroy(&q->wpending);
pthread_mutex_destroy(&q->lock);
/*
* seems like this call is not required, we
* can simply return from the thread and
* it should be the same
*/
/* pthread_exit(0); */
#endif
return 0;
}
void wstart(struct wqueue *q, worker *worker)
{
q->closed = 0;
q->worker = worker;
#ifdef _WIN32
q->lock = CreateMutex(0, FALSE, 0);
q->wpending = CreateEvent(0, FALSE, FALSE, 0);
q->thread = CreateThread(0, 0, wqueue_thread, q, 0, 0);
#endif
#ifdef __linux__
pthread_mutex_init(&q->lock, 0);
pthread_cond_init(&q->wpending, 0);
pthread_create(&q->thread, 0, wqueue_thread, q);
#endif
}
void wpush(struct wqueue *q, void *work)
{
if (q->closed)
return;
#ifdef _WIN32
WaitForSingleObject(q->lock, INFINITE);
q->work[q->wcount++] = work;
ReleaseMutex(q->lock);
SetEvent(q->wpending);
#endif
#ifdef __linux__
pthread_mutex_lock(&q->lock);
q->work[q->wcount++] = work;
pthread_cond_signal(&q->wpending);
pthread_mutex_unlock(&q->lock);
#endif
}
void wclose(struct wqueue *q)
{
if (q->closed)
return;
#ifdef _WIN32
WaitForSingleObject(q->lock, INFINITE);
q->closed = 1;
ReleaseMutex(q->lock);
#endif
#ifdef __linux__
pthread_mutex_lock(&q->lock);
q->closed = 1;
pthread_mutex_unlock(&q->lock);
#endif
}
#ifdef run_wqueue
// example on how to use these functions
#include <stdio.h>
void do_work(struct wqueue *q, void *w)
{
static int tmp = 42;
int value = *(int *) w;
if (value == 2) {
fprintf(stderr, "adding new value because we found 2\n");
wpush(q, &tmp);
}
fprintf(stderr, "new work! value is %d\n", value);
}
int main()
{
struct wqueue q = {0};
wstart(&q, do_work);
int values[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
wpush(&q, values + i);
// wclose(&q);
Sleep(1000);
}
fprintf(stderr, "all done!\n");
}
#endif