-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_pthread.c
79 lines (69 loc) · 1.82 KB
/
event_pthread.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
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct event_s {
pthread_cond_t cond;
pthread_mutex_t mutex;
bool signalled;
} event_s;
bool event_set(void *ctx) {
event_s *event = (event_s *)ctx;
if (!event)
return false;
pthread_mutex_lock(&event->mutex);
int32_t err = pthread_cond_signal(&event->cond);
if (err == 0)
event->signalled = true;
pthread_mutex_unlock(&event->mutex);
return err == 0;
}
bool event_wait(void *ctx, int32_t timeout_ms) {
event_s *event = (event_s *)ctx;
int32_t err = 0;
if (!event)
return false;
pthread_mutex_lock(&event->mutex);
if (!event->signalled) {
if (timeout_ms < 0) {
err = pthread_cond_wait(&event->cond, &event->mutex);
} else {
struct timespec ts;
ts.tv_sec = timeout_ms / 1000;
ts.tv_nsec = (timeout_ms % 1000) * 1000000;
err = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
}
if (err == 0)
event->signalled = true;
}
pthread_mutex_unlock(&event->mutex);
return err == 0;
}
void *event_create(void) {
event_s *event = (event_s *)calloc(1, sizeof(event_s));
if (!event)
return NULL;
if (pthread_cond_init(&event->cond, NULL)) {
free(event);
return NULL;
}
if (pthread_mutex_init(&event->mutex, NULL)) {
pthread_cond_destroy(&event->cond);
free(event);
return NULL;
}
return event;
}
bool event_delete(void **ctx) {
if (!ctx)
return false;
event_s *event = (event_s *)*ctx;
if (!event)
return false;
pthread_cond_destroy(&event->cond);
pthread_mutex_destroy(&event->mutex);
free(event);
*ctx = NULL;
return true;
}