This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
87 lines (70 loc) · 1.59 KB
/
thread.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
// thread.c - thread management
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2014-2015 Joris van der Geer.
*/
#include <pthread.h>
#include "base.h"
#include "thread.h"
#include "os.h"
static ub4 msgfile;
#include "msg.h"
static pthread_t ptids[Nthread];
static int runtids[Nthread];
static int endtids[Nthread];
static ub4 hitid;
int newthread(ub4 tid,void *(*startfn)(void *),void *arg)
{
int rv;
error_ge(tid,Nthread);
hitid = max(hitid,tid);
rv = pthread_create(ptids + tid,NULL,startfn,arg);
if (rv) return oserror(0,"cannot create thread %u",tid);
else return 0;
}
void *jointhread(ub4 tid)
{
int rv;
void *trv;
error_ge(tid,Nthread);
rv = pthread_join(ptids[tid],&trv);
if (rv) { oserror(0,"cannot join thread %u",tid); return NULL; }
if (trv == NULL) error(0,"cannot join thread %u",tid);
globs.tids[tid] = runtids[tid] = 0;
return trv;
}
void *joinanythread(void)
{
ub4 tid;
int rv;
void *trv;
for (tid = 0; tid <= hitid; tid++) {
if (endtids[tid] == 0) continue;
endtids[tid] = 0;
rv = pthread_join(ptids[tid],&trv);
if (rv == 0) {
globs.tids[tid] = runtids[tid] = 0;
if (trv == NULL) error(0,"cannot join thread %u",tid);
return trv;
}
oserror(0,"cannot join thread %u",tid);
return NULL;
}
return NULL;
}
// to be called from started thread
void threadstart(ub4 tid)
{
error_ge(tid,Nthread);
globs.tids[tid] = runtids[tid] = 1;
endtids[tid] = 0;
}
void threadend(ub4 tid)
{
error_ge(tid,Nthread);
endtids[tid] = 1;
}
void inithread(void)
{
iniassert();
}