-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpass.c
More file actions
76 lines (65 loc) · 1.31 KB
/
threadpass.c
File metadata and controls
76 lines (65 loc) · 1.31 KB
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
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
struct thread_pass {
pthread_mutex_t server;
pthread_mutex_t clients;
pthread_cond_t done;
int work;
void *data;
};
struct thread_pass *
thread_pass_new(void *data)
{
struct thread_pass *p = malloc(sizeof(*p));
pthread_mutex_init(&p->server, NULL);
pthread_mutex_init(&p->clients, NULL);
pthread_cond_init(&p->done, NULL);
p->data = data;
p->work = 0;
return p;
}
void
thread_pass_free(struct thread_pass *p)
{
pthread_mutex_destroy(&p->server);
pthread_mutex_destroy(&p->clients);
pthread_cond_destroy(&p->done);
free(p);
}
void *
thread_pass_get(struct thread_pass *p)
{
pthread_mutex_lock(&p->clients);
return p->data;
}
void
thread_pass_return(struct thread_pass *p)
{
pthread_mutex_lock(&p->server);
p->work = 1;
/* Unlocks &p->server letting server know
* to call pthread_cond_signal so we can
* progress.
*/
pthread_cond_wait(&p->done, &p->server);
pthread_mutex_unlock(&p->server);
pthread_mutex_unlock(&p->clients);
}
int
thread_pass_work(struct thread_pass *p)
{
pthread_mutex_lock(&p->server);
if (p->work)
return 1;
pthread_mutex_unlock(&p->server);
return 0;
}
void
thread_pass_continue(struct thread_pass *p)
{
p->work = 0;
pthread_mutex_unlock(&p->server);
pthread_cond_signal(&p->done);
}