-
Notifications
You must be signed in to change notification settings - Fork 12
/
equeue_posix.c
95 lines (74 loc) · 1.91 KB
/
equeue_posix.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
/*
* Implementation for Posix compliant platforms
*
* Copyright (c) 2016 Christopher Haster
* Distributed under the MIT license
*/
#include "equeue_platform.h"
#if defined(EQUEUE_PLATFORM_POSIX)
#include <time.h>
#include <sys/time.h>
#include <errno.h>
// Tick operations
unsigned equeue_tick(void) {
struct timeval tv;
gettimeofday(&tv, 0);
return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000);
}
// Mutex operations
int equeue_mutex_create(equeue_mutex_t *m) {
return pthread_mutex_init(m, 0);
}
void equeue_mutex_destroy(equeue_mutex_t *m) {
pthread_mutex_destroy(m);
}
void equeue_mutex_lock(equeue_mutex_t *m) {
pthread_mutex_lock(m);
}
void equeue_mutex_unlock(equeue_mutex_t *m) {
pthread_mutex_unlock(m);
}
// Semaphore operations
int equeue_sema_create(equeue_sema_t *s) {
int err = pthread_mutex_init(&s->mutex, 0);
if (err) {
return err;
}
err = pthread_cond_init(&s->cond, 0);
if (err) {
return err;
}
s->signal = false;
return 0;
}
void equeue_sema_destroy(equeue_sema_t *s) {
pthread_cond_destroy(&s->cond);
pthread_mutex_destroy(&s->mutex);
}
void equeue_sema_signal(equeue_sema_t *s) {
pthread_mutex_lock(&s->mutex);
s->signal = true;
pthread_cond_signal(&s->cond);
pthread_mutex_unlock(&s->mutex);
}
bool equeue_sema_wait(equeue_sema_t *s, int ms) {
pthread_mutex_lock(&s->mutex);
if (!s->signal) {
if (ms < 0) {
pthread_cond_wait(&s->cond, &s->mutex);
} else {
struct timeval tv;
gettimeofday(&tv, 0);
struct timespec ts = {
.tv_sec = ms/1000 + tv.tv_sec,
.tv_nsec = ms*1000000 + tv.tv_usec*1000,
};
pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
}
}
bool signal = s->signal;
s->signal = false;
pthread_mutex_unlock(&s->mutex);
return signal;
}
#endif