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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
//
// Copyright 2016 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
// POSIX synchronization (mutexes and condition variables). This uses
// pthreads.
#include "core/nng_impl.h"
#ifdef PLATFORM_POSIX_SYNCH
#include <pthread.h>
#include <time.h>
#include <string.h>
extern pthread_condattr_t nni_cvattr;
extern pthread_mutexattr_t nni_mxattr;
int
nni_mutex_init(nni_mutex *mp)
{
if (pthread_mutex_init(&mp->mx, &nni_mxattr) != 0) {
return (NNG_ENOMEM);
}
return (0);
}
void
nni_mutex_fini(nni_mutex *mp)
{
int rv;
if ((rv = pthread_mutex_destroy(&mp->mx)) != 0) {
nni_panic("pthread_mutex_destroy failed: %s", strerror(rv));
}
}
void
nni_mutex_enter(nni_mutex *m)
{
int rv;
if ((rv = pthread_mutex_lock(&m->mx)) != 0) {
nni_panic("pthread_mutex_lock failed: %s", strerror(rv));
}
}
void
nni_mutex_exit(nni_mutex *m)
{
if (pthread_mutex_unlock(&m->mx) != 0) {
nni_panic("pthread_mutex_unlock failed");
}
}
int
nni_mutex_tryenter(nni_mutex *m)
{
if (pthread_mutex_trylock(&m->mx) != 0) {
return (NNG_EBUSY);
}
return (0);
}
int
nni_cond_init(nni_cond *c, nni_mutex *m)
{
if (pthread_cond_init(&c->cv, &nni_cvattr) != 0) {
// In theory could be EAGAIN, but handle like ENOMEM
return (NNG_ENOMEM);
}
c->mx = &m->mx;
return (0);
}
void
nni_cond_fini(nni_cond *c)
{
if (pthread_cond_destroy(&c->cv) != 0) {
nni_panic("pthread_cond_destroy failed");
}
}
void
nni_cond_signal(nni_cond *c)
{
if (pthread_cond_signal(&c->cv) != 0) {
nni_panic("pthread_cond_signal failed");
}
}
void
nni_cond_broadcast(nni_cond *c)
{
if (pthread_cond_broadcast(&c->cv) != 0) {
nni_panic("pthread_cond_broadcast failed");
}
}
void
nni_cond_wait(nni_cond *c)
{
if (pthread_cond_wait(&c->cv, c->mx) != 0) {
nni_panic("pthread_cond_wait failed");
}
}
int
nni_cond_waituntil(nni_cond *c, nni_time usec)
{
struct timespec ts;
int rv;
nni_duration delta = usec - nni_clock();
if (usec != NNI_TIME_NEVER) {
ts.tv_sec = usec / 1000000;
ts.tv_nsec = (usec % 1000000) * 1000;
rv = pthread_cond_timedwait(&c->cv, c->mx, &ts);
} else {
rv = pthread_cond_wait(&c->cv, c->mx);
}
if (rv == ETIMEDOUT) {
if (nni_clock() < usec) {
// This only happens if the implementation
// is buggy.
nni_panic("Premature wakupe!");
}
return (NNG_ETIMEDOUT);
} else if (rv != 0) {
nni_panic("pthread_cond_timedwait returned %d", rv);
}
return (0);
}
#endif
|