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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
//
// Copyright 2017 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 threads.
#include "core/nng_impl.h"
#ifdef PLATFORM_WINDOWS
void *
nni_alloc(size_t sz)
{
void *v;
v = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz);
return (v);
}
void
nni_free(void *b, size_t z)
{
NNI_ARG_UNUSED(z);
HeapFree(GetProcessHeap(), 0, b);
}
int
nni_plat_mtx_init(nni_plat_mtx *mtx)
{
InitializeSRWLock(&mtx->srl);
mtx->init = 1;
return (0);
}
void
nni_plat_mtx_fini(nni_plat_mtx *mtx)
{
mtx->init = 0;
}
void
nni_plat_mtx_lock(nni_plat_mtx *mtx)
{
AcquireSRWLockExclusive(&mtx->srl);
}
void
nni_plat_mtx_unlock(nni_plat_mtx *mtx)
{
ReleaseSRWLockExclusive(&mtx->srl);
}
int
nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx)
{
InitializeConditionVariable(&cv->cv);
cv->srl = &mtx->srl;
return (0);
}
void
nni_plat_cv_wake(nni_plat_cv *cv)
{
WakeAllConditionVariable(&cv->cv);
}
void
nni_plat_cv_wait(nni_plat_cv *cv)
{
(void) SleepConditionVariableSRW(&cv->cv, cv->srl, INFINITE, 0);
}
int
nni_plat_cv_until(nni_plat_cv *cv, nni_time until)
{
nni_time now;
DWORD msec;
BOOL ok;
now = nni_plat_clock();
if (now > until) {
msec = 0;
} else {
// times are in usec, but win32 wants millis
msec = (DWORD) (((until - now) + 999)/1000);
}
ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0);
return (ok ? 0 : NNG_ETIMEDOUT);
}
void
nni_plat_cv_fini(nni_plat_cv *cv)
{
}
static unsigned int __stdcall
nni_plat_thr_main(void *arg)
{
nni_plat_thr *thr = arg;
thr->func(thr->arg);
return (0);
}
int
nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg)
{
thr->func = fn;
thr->arg = arg;
// We could probably even go down to 8k... but crypto for some
// protocols might get bigger than this. 1MB is waaay too big.
thr->handle = (HANDLE) _beginthreadex(NULL, 16384,
nni_plat_thr_main, thr, STACK_SIZE_PARAM_IS_A_RESERVATION,
NULL);
if (thr->handle == NULL) {
return (NNG_ENOMEM); // Best guess...
}
return (0);
}
void
nni_plat_thr_fini(nni_plat_thr *thr)
{
if (WaitForSingleObject(thr->handle, INFINITE) == WAIT_FAILED) {
nni_panic("waiting for thread failed!");
}
if (CloseHandle(thr->handle) == 0) {
nni_panic("close handle for thread failed!");
}
}
int
nni_plat_init(int (*helper)(void))
{
static LONG inited = 0;
int rv;
static SRWLOCK lock = SRWLOCK_INIT;
if (inited) {
return (0); // fast path
}
AcquireSRWLockExclusive(&lock);
if (!inited) {
WSADATA data;
WORD ver;
ver = MAKEWORD(2, 2);
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
if ((LOBYTE(data.wVersion) != 2) ||
(HIBYTE(data.wVersion) != 2)) {
nni_panic("got back wrong winsock ver");
}
rv = NNG_EINVAL;
goto out;
}
if ((rv = nni_win_iocp_sysinit()) != 0) {
goto out;
}
if ((rv = nni_win_resolv_sysinit()) != 0) {
goto out;
}
if ((rv = nni_win_ipc_sysinit()) != 0) {
goto out;
}
helper();
inited = 1;
}
out:
ReleaseSRWLockExclusive(&lock);
return (rv);
}
void
nni_plat_fini(void)
{
nni_win_ipc_sysfini();
nni_win_resolv_sysfini();
nni_win_iocp_sysfini();
WSACleanup();
}
#else
// Suppress empty symbols warnings in ranlib.
int nni_win_thread_not_used = 0;
#endif
|