Skip to content

Latest commit

 

History

History
398 lines (348 loc) · 13.2 KB

yarn.c

File metadata and controls

398 lines (348 loc) · 13.2 KB
 
Jan 25, 2010
Jan 25, 2010
1
/* yarn.c -- generic thread operations implemented using pthread functions
Apr 13, 2020
Apr 13, 2020
2
3
* Copyright (C) 2008, 2011, 2012, 2015, 2018, 2019, 2020 Mark Adler
* Version 1.7 12 Apr 2020 Mark Adler
Jan 25, 2010
Jan 25, 2010
4
5
6
7
8
9
10
11
* For conditions of distribution and use, see copyright notice in yarn.h
*/
/* Basic thread operations implemented using the POSIX pthread library. All
pthread references are isolated within this module to allow alternate
implementations with other thread libraries. See yarn.h for the description
of these operations. */
Jan 25, 2010
Jan 25, 2010
12
13
14
15
/* Version history:
1.0 19 Oct 2008 First version
1.1 26 Oct 2008 No need to set the stack size -- remove
Add yarn_abort() function for clean-up on error exit
Jan 13, 2012
Jan 13, 2012
16
17
1.2 19 Dec 2011 (changes reversed in 1.3)
1.3 13 Jan 2012 Add large file #define for consistency with pigz.c
Jan 8, 2012
Jan 8, 2012
18
Update thread portability #defines per IEEE 1003.1-2008
Jan 13, 2012
Jan 13, 2012
19
Fix documentation in yarn.h for yarn_prefix
Jan 20, 2015
Jan 20, 2015
20
21
1.4 19 Jan 2015 Allow yarn_abort() to avoid error message to stderr
Accept and do nothing for NULL argument to free_lock()
May 9, 2018
May 9, 2018
22
1.5 8 May 2018 Remove destruct() to avoid use of pthread_cancel()
May 9, 2018
May 9, 2018
23
Normalize the code style
Apr 13, 2020
Apr 13, 2020
24
25
1.6 3 Apr 2019 Add debugging information to fail() error messages
1.7 12 Apr 2020 Fix use after free bug in ignition()
Jan 25, 2010
Jan 25, 2010
26
27
*/
May 9, 2018
May 9, 2018
28
// For thread portability.
Jan 8, 2012
Jan 8, 2012
29
30
31
32
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#define _THREAD_SAFE
May 9, 2018
May 9, 2018
33
// Use large file functions if available.
Jan 8, 2012
Jan 8, 2012
34
#define _FILE_OFFSET_BITS 64
Jan 25, 2010
Jan 25, 2010
35
May 9, 2018
May 9, 2018
36
37
38
39
40
41
42
43
44
45
46
// External libraries and entities referenced.
#include <stdio.h> // fprintf(), stderr
#include <stdlib.h> // exit(), malloc(), free(), NULL
#include <pthread.h> // pthread_t, pthread_create(), pthread_join(),
// pthread_attr_t, pthread_attr_init(), pthread_attr_destroy(),
// PTHREAD_CREATE_JOINABLE, pthread_attr_setdetachstate(),
// pthread_self(), pthread_equal(),
// pthread_mutex_t, PTHREAD_MUTEX_INITIALIZER, pthread_mutex_init(),
// pthread_mutex_lock(), pthread_mutex_unlock(), pthread_mutex_destroy(),
// pthread_cond_t, PTHREAD_COND_INITIALIZER, pthread_cond_init(),
// pthread_cond_broadcast(), pthread_cond_wait(), pthread_cond_destroy()
Apr 4, 2019
Apr 4, 2019
47
#include <errno.h> // EPERM, ESRCH, EDEADLK, ENOMEM, EBUSY, EINVAL, EAGAIN
May 9, 2018
May 9, 2018
48
49
// Interface definition.
Jan 25, 2010
Jan 25, 2010
50
51
#include "yarn.h"
May 9, 2018
May 9, 2018
52
53
// Constants.
#define local static // for non-exported functions and globals
Jan 25, 2010
Jan 25, 2010
54
May 9, 2018
May 9, 2018
55
// Error handling external globals, resettable by application.
Jan 25, 2010
Jan 25, 2010
56
57
58
char *yarn_prefix = "yarn";
void (*yarn_abort)(int) = NULL;
Jan 25, 2010
Jan 25, 2010
59
May 9, 2018
May 9, 2018
60
// Immediately exit -- use for errors that shouldn't ever happen.
Apr 4, 2019
Apr 4, 2019
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
local void fail(int err, char const *file, long line, char const *func) {
fprintf(stderr, "%s: ", yarn_prefix);
switch (err) {
case EPERM:
fputs("already unlocked", stderr);
break;
case ESRCH:
fputs("no such thread", stderr);
break;
case EDEADLK:
fputs("resource deadlock", stderr);
break;
case ENOMEM:
fputs("out of memory", stderr);
break;
case EBUSY:
fputs("can't destroy locked resource", stderr);
break;
case EINVAL:
fputs("invalid request", stderr);
break;
case EAGAIN:
fputs("resource unavailable", stderr);
break;
default:
fprintf(stderr, "internal error %d", err);
}
Apr 10, 2019
Apr 10, 2019
88
fprintf(stderr, " (%s:%ld:%s)\n", file, line, func);
Jan 25, 2010
Jan 25, 2010
89
90
if (yarn_abort != NULL)
yarn_abort(err);
Apr 4, 2019
Apr 4, 2019
91
exit(err);
Jan 25, 2010
Jan 25, 2010
92
93
}
May 9, 2018
May 9, 2018
94
95
// Memory handling routines provided by user. If none are provided, malloc()
// and free() are used, which are therefore assumed to be thread-safe.
Jan 25, 2010
Jan 25, 2010
96
97
98
99
100
typedef void *(*malloc_t)(size_t);
typedef void (*free_t)(void *);
local malloc_t my_malloc_f = malloc;
local free_t my_free = free;
May 9, 2018
May 9, 2018
101
102
// Use user-supplied allocation routines instead of malloc() and free().
void yarn_mem(malloc_t lease, free_t vacate) {
Jan 25, 2010
Jan 25, 2010
103
104
105
106
my_malloc_f = lease;
my_free = vacate;
}
May 9, 2018
May 9, 2018
107
// Memory allocation that cannot fail (from the point of view of the caller).
Apr 4, 2019
Apr 4, 2019
108
local void *my_malloc(size_t size, char const *file, long line) {
Jan 25, 2010
Jan 25, 2010
109
110
111
void *block;
if ((block = my_malloc_f(size)) == NULL)
Apr 4, 2019
Apr 4, 2019
112
fail(ENOMEM, file, line, "malloc");
Jan 25, 2010
Jan 25, 2010
113
114
115
return block;
}
May 9, 2018
May 9, 2018
116
// -- Lock functions --
Jan 25, 2010
Jan 25, 2010
117
118
119
120
121
122
123
struct lock_s {
pthread_mutex_t mutex;
pthread_cond_t cond;
long value;
};
Apr 4, 2019
Apr 4, 2019
124
125
126
127
128
129
130
131
lock *new_lock_(long initial, char const *file, long line) {
lock *bolt = my_malloc(sizeof(struct lock_s), file, line);
int ret = pthread_mutex_init(&(bolt->mutex), NULL);
if (ret)
fail(ret, file, line, "mutex_init");
ret = pthread_cond_init(&(bolt->cond), NULL);
if (ret)
fail(ret, file, line, "cond_init");
Jan 25, 2010
Jan 25, 2010
132
133
134
135
bolt->value = initial;
return bolt;
}
Apr 4, 2019
Apr 4, 2019
136
137
138
139
void possess_(lock *bolt, char const *file, long line) {
int ret = pthread_mutex_lock(&(bolt->mutex));
if (ret)
fail(ret, file, line, "mutex_lock");
Jan 25, 2010
Jan 25, 2010
140
141
}
Apr 4, 2019
Apr 4, 2019
142
143
144
145
void release_(lock *bolt, char const *file, long line) {
int ret = pthread_mutex_unlock(&(bolt->mutex));
if (ret)
fail(ret, file, line, "mutex_unlock");
Jan 25, 2010
Jan 25, 2010
146
147
}
Apr 4, 2019
Apr 4, 2019
148
149
void twist_(lock *bolt, enum twist_op op, long val,
char const *file, long line) {
Jan 25, 2010
Jan 25, 2010
150
151
152
153
if (op == TO)
bolt->value = val;
else if (op == BY)
bolt->value += val;
Apr 4, 2019
Apr 4, 2019
154
155
156
157
158
159
int ret = pthread_cond_broadcast(&(bolt->cond));
if (ret)
fail(ret, file, line, "cond_broadcast");
ret = pthread_mutex_unlock(&(bolt->mutex));
if (ret)
fail(ret, file, line, "mutex_unlock");
Jan 25, 2010
Jan 25, 2010
160
161
162
163
}
#define until(a) while(!(a))
Apr 4, 2019
Apr 4, 2019
164
165
void wait_for_(lock *bolt, enum wait_op op, long val,
char const *file, long line) {
Jan 25, 2010
Jan 25, 2010
166
switch (op) {
Apr 4, 2019
Apr 4, 2019
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
case TO_BE:
until (bolt->value == val) {
int ret = pthread_cond_wait(&(bolt->cond), &(bolt->mutex));
if (ret)
fail(ret, file, line, "cond_wait");
}
break;
case NOT_TO_BE:
until (bolt->value != val) {
int ret = pthread_cond_wait(&(bolt->cond), &(bolt->mutex));
if (ret)
fail(ret, file, line, "cond_wait");
}
break;
case TO_BE_MORE_THAN:
until (bolt->value > val) {
int ret = pthread_cond_wait(&(bolt->cond), &(bolt->mutex));
if (ret)
fail(ret, file, line, "cond_wait");
}
break;
case TO_BE_LESS_THAN:
until (bolt->value < val) {
int ret = pthread_cond_wait(&(bolt->cond), &(bolt->mutex));
if (ret)
fail(ret, file, line, "cond_wait");
}
Jan 25, 2010
Jan 25, 2010
194
195
196
}
}
May 9, 2018
May 9, 2018
197
long peek_lock(lock *bolt) {
Jan 25, 2010
Jan 25, 2010
198
199
200
return bolt->value;
}
Apr 4, 2019
Apr 4, 2019
201
void free_lock_(lock *bolt, char const *file, long line) {
Jan 20, 2015
Jan 20, 2015
202
203
if (bolt == NULL)
return;
Apr 4, 2019
Apr 4, 2019
204
205
206
207
208
209
int ret = pthread_cond_destroy(&(bolt->cond));
if (ret)
fail(ret, file, line, "cond_destroy");
ret = pthread_mutex_destroy(&(bolt->mutex));
if (ret)
fail(ret, file, line, "mutex_destroy");
Jan 25, 2010
Jan 25, 2010
210
211
212
my_free(bolt);
}
May 9, 2018
May 9, 2018
213
// -- Thread functions (uses the lock functions above) --
Jan 25, 2010
Jan 25, 2010
214
215
216
struct thread_s {
pthread_t id;
May 9, 2018
May 9, 2018
217
218
int done; // true if this thread has exited
thread *next; // for list of all launched threads
Jan 25, 2010
Jan 25, 2010
219
220
};
May 9, 2018
May 9, 2018
221
222
// List of threads launched but not joined, count of threads exited but not
// joined (incremented by ignition() just before exiting).
Jan 25, 2010
Jan 25, 2010
223
224
225
local lock threads_lock = {
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
May 9, 2018
May 9, 2018
226
0 // number of threads exited but not joined
Jan 25, 2010
Jan 25, 2010
227
};
May 9, 2018
May 9, 2018
228
local thread *threads = NULL; // list of extant threads
Jan 25, 2010
Jan 25, 2010
229
May 9, 2018
May 9, 2018
230
// Structure in which to pass the probe and its payload to ignition().
Jan 25, 2010
Jan 25, 2010
231
232
233
struct capsule {
void (*probe)(void *);
void *payload;
Apr 4, 2019
Apr 4, 2019
234
235
char const *file;
long line;
Jan 25, 2010
Jan 25, 2010
236
237
};
May 9, 2018
May 9, 2018
238
// Mark the calling thread as done and alert join_all().
Apr 4, 2019
Apr 4, 2019
239
240
local void reenter(void *arg) {
struct capsule *capsule = arg;
Jan 25, 2010
Jan 25, 2010
241
May 9, 2018
May 9, 2018
242
// find this thread in the threads list by matching the thread id
Apr 4, 2019
Apr 4, 2019
243
244
245
246
pthread_t me = pthread_self();
possess_(&(threads_lock), capsule->file, capsule->line);
thread **prior = &(threads);
thread *match;
Jan 25, 2010
Jan 25, 2010
247
248
249
250
251
252
while ((match = *prior) != NULL) {
if (pthread_equal(match->id, me))
break;
prior = &(match->next);
}
if (match == NULL)
Apr 4, 2019
Apr 4, 2019
253
fail(ESRCH, capsule->file, capsule->line, "reenter lost");
Jan 25, 2010
Jan 25, 2010
254
May 9, 2018
May 9, 2018
255
// mark this thread as done and move it to the head of the list
Jan 25, 2010
Jan 25, 2010
256
257
258
match->done = 1;
if (threads != match) {
*prior = match->next;
Jan 7, 2012
Jan 7, 2012
259
match->next = threads;
Jan 25, 2010
Jan 25, 2010
260
261
262
threads = match;
}
May 9, 2018
May 9, 2018
263
// update the count of threads to be joined and alert join_all()
Apr 4, 2019
Apr 4, 2019
264
twist_(&(threads_lock), BY, +1, capsule->file, capsule->line);
Apr 13, 2020
Apr 13, 2020
265
266
267
268
// free the capsule resource, even if the thread is cancelled (though yarn
// doesn't use pthread_cancel() -- you never know)
my_free(capsule);
Jan 25, 2010
Jan 25, 2010
269
270
}
May 9, 2018
May 9, 2018
271
272
273
274
275
// All threads go through this routine. Just before a thread exits, it marks
// itself as done in the threads list and alerts join_all() so that the thread
// resources can be released. Use a cleanup stack so that the marking occurs
// even if the thread is cancelled.
local void *ignition(void *arg) {
Jan 25, 2010
Jan 25, 2010
276
277
struct capsule *capsule = arg;
May 9, 2018
May 9, 2018
278
// run reenter() before leaving
Apr 4, 2019
Apr 4, 2019
279
pthread_cleanup_push(reenter, arg);
Jan 25, 2010
Jan 25, 2010
280
May 9, 2018
May 9, 2018
281
// execute the requested function with argument
Jan 25, 2010
Jan 25, 2010
282
283
capsule->probe(capsule->payload);
Apr 13, 2020
Apr 13, 2020
284
// mark this thread as done, letting join_all() know, and free capsule
Jan 25, 2010
Jan 25, 2010
285
286
pthread_cleanup_pop(1);
May 9, 2018
May 9, 2018
287
// exit thread
Jan 25, 2010
Jan 25, 2010
288
289
290
return NULL;
}
May 9, 2018
May 9, 2018
291
292
// Not all POSIX implementations create threads as joinable by default, so that
// is made explicit here.
Apr 4, 2019
Apr 4, 2019
293
294
thread *launch_(void (*probe)(void *), void *payload,
char const *file, long line) {
May 9, 2018
May 9, 2018
295
296
297
298
// construct the requested call and argument for the ignition() routine
// (allocated instead of automatic so that we're sure this will still be
// there when ignition() actually starts up -- ignition() will free this
// allocation)
Apr 4, 2019
Apr 4, 2019
299
struct capsule *capsule = my_malloc(sizeof(struct capsule), file, line);
Jan 25, 2010
Jan 25, 2010
300
301
capsule->probe = probe;
capsule->payload = payload;
Apr 4, 2019
Apr 4, 2019
302
303
capsule->file = file;
capsule->line = line;
Jan 25, 2010
Jan 25, 2010
304
May 9, 2018
May 9, 2018
305
306
// assure this thread is in the list before join_all() or ignition() looks
// for it
Apr 4, 2019
Apr 4, 2019
307
possess_(&(threads_lock), file, line);
Jan 25, 2010
Jan 25, 2010
308
May 9, 2018
May 9, 2018
309
// create the thread and call ignition() from that thread
Apr 4, 2019
Apr 4, 2019
310
311
312
313
314
315
316
317
318
319
320
321
322
323
thread *th = my_malloc(sizeof(struct thread_s), file, line);
pthread_attr_t attr;
int ret = pthread_attr_init(&attr);
if (ret)
fail(ret, file, line, "attr_init");
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (ret)
fail(ret, file, line, "attr_setdetachstate");
ret = pthread_create(&(th->id), &attr, ignition, capsule);
if (ret)
fail(ret, file, line, "create");
ret = pthread_attr_destroy(&attr);
if (ret)
fail(ret, file, line, "attr_destroy");
Jan 25, 2010
Jan 25, 2010
324
May 9, 2018
May 9, 2018
325
// put the thread in the threads list for join_all()
Jan 25, 2010
Jan 25, 2010
326
th->done = 0;
Jan 7, 2012
Jan 7, 2012
327
th->next = threads;
Jan 25, 2010
Jan 25, 2010
328
threads = th;
Apr 4, 2019
Apr 4, 2019
329
release_(&(threads_lock), file, line);
Jan 25, 2010
Jan 25, 2010
330
331
332
return th;
}
Apr 4, 2019
Apr 4, 2019
333
void join_(thread *ally, char const *file, long line) {
May 9, 2018
May 9, 2018
334
// wait for thread to exit and return its resources
Apr 4, 2019
Apr 4, 2019
335
336
337
int ret = pthread_join(ally->id, NULL);
if (ret)
fail(ret, file, line, "join");
Jan 25, 2010
Jan 25, 2010
338
May 9, 2018
May 9, 2018
339
// find the thread in the threads list
Apr 4, 2019
Apr 4, 2019
340
341
342
possess_(&(threads_lock), file, line);
thread **prior = &(threads);
thread *match;
Jan 25, 2010
Jan 25, 2010
343
344
345
346
347
348
while ((match = *prior) != NULL) {
if (match == ally)
break;
prior = &(match->next);
}
if (match == NULL)
Apr 4, 2019
Apr 4, 2019
349
fail(ESRCH, file, line, "join lost");
Jan 25, 2010
Jan 25, 2010
350
May 9, 2018
May 9, 2018
351
// remove thread from list and update exited count, free thread
Jan 25, 2010
Jan 25, 2010
352
353
354
if (match->done)
threads_lock.value--;
*prior = match->next;
Apr 4, 2019
Apr 4, 2019
355
release_(&(threads_lock), file, line);
Jan 25, 2010
Jan 25, 2010
356
357
358
my_free(ally);
}
May 9, 2018
May 9, 2018
359
360
361
362
// This implementation of join_all() only attempts to join threads that have
// announced that they have exited (see ignition()). When there are many
// threads, this is faster than waiting for some random thread to exit while a
// bunch of other threads have already exited.
Apr 4, 2019
Apr 4, 2019
363
int join_all_(char const *file, long line) {
May 9, 2018
May 9, 2018
364
// grab the threads list and initialize the joined count
Apr 4, 2019
Apr 4, 2019
365
366
int count = 0;
possess_(&(threads_lock), file, line);
Jan 25, 2010
Jan 25, 2010
367
May 9, 2018
May 9, 2018
368
// do until threads list is empty
Jan 25, 2010
Jan 25, 2010
369
while (threads != NULL) {
May 9, 2018
May 9, 2018
370
// wait until at least one thread has reentered
Apr 4, 2019
Apr 4, 2019
371
wait_for_(&(threads_lock), NOT_TO_BE, 0, file, line);
Jan 25, 2010
Jan 25, 2010
372
May 9, 2018
May 9, 2018
373
// find the first thread marked done (should be at or near the top)
Apr 4, 2019
Apr 4, 2019
374
375
thread **prior = &(threads);
thread *match;
Jan 25, 2010
Jan 25, 2010
376
377
378
379
380
381
while ((match = *prior) != NULL) {
if (match->done)
break;
prior = &(match->next);
}
if (match == NULL)
Apr 4, 2019
Apr 4, 2019
382
fail(ESRCH, file, line, "join_all lost");
Jan 25, 2010
Jan 25, 2010
383
May 9, 2018
May 9, 2018
384
385
// join the thread (will be almost immediate), remove from the threads
// list, update the reenter count, and free the thread
Apr 4, 2019
Apr 4, 2019
386
387
388
int ret = pthread_join(match->id, NULL);
if (ret)
fail(ret, file, line, "join");
Jan 25, 2010
Jan 25, 2010
389
390
391
392
393
394
threads_lock.value--;
*prior = match->next;
my_free(match);
count++;
}
May 9, 2018
May 9, 2018
395
// let go of the threads list and return the number of threads joined
Apr 4, 2019
Apr 4, 2019
396
release_(&(threads_lock), file, line);
Jan 25, 2010
Jan 25, 2010
397
398
return count;
}