aboutsummaryrefslogtreecommitdiff
path: root/src/platform/windows/win_iocp.c
blob: 9c3343b78a8ee9a039d6956c0796bdac73185248 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// 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.
//

#include "core/nng_impl.h"

#ifdef PLATFORM_WINDOWS

#define NNI_WIN_IOCP_NTHREADS 4
#include <stdio.h>

// Windows IO Completion Port support.  We basically create a single
// IO completion port, then start threads on it.  Handles are added
// to the port on an as needed basis.  We use a single IO completion
// port for pretty much everything.

static HANDLE  nni_win_global_iocp = NULL;
static nni_thr nni_win_iocp_thrs[NNI_WIN_IOCP_NTHREADS];
static nni_mtx nni_win_iocp_mtx;

static void
nni_win_event_finish(nni_win_event *evt, nni_aio *aio)
{
	evt->run = 0;
	if (aio != NULL) {
		evt->ops.wev_finish(evt, aio);
	}
	if (evt->fini) {
		nni_cv_wake(&evt->cv);
	}
}

static void
nni_win_iocp_handler(void *arg)
{
	HANDLE         iocp;
	DWORD          cnt;
	ULONG_PTR      key;
	OVERLAPPED *   olpd;
	nni_win_event *evt;
	int            rv;
	BOOL           ok;
	nni_aio *      aio;

	NNI_ARG_UNUSED(arg);

	iocp = nni_win_global_iocp;

	for (;;) {
		key  = 0;
		olpd = NULL;

		ok = GetQueuedCompletionStatus(
		    iocp, &cnt, &key, &olpd, INFINITE);

		if (olpd == NULL) {
			// Completion port closed...
			NNI_ASSERT(ok == FALSE);
			break;
		}

		evt = CONTAINING_RECORD(olpd, nni_win_event, olpd);

		nni_mtx_lock(&evt->mtx);

		if (ok) {
			rv = ERROR_SUCCESS;
		} else if (evt->status == 0) {
			evt->status = nni_win_error(GetLastError());
		}

		aio        = evt->aio;
		evt->aio   = NULL;
		evt->count = cnt;

		nni_win_event_finish(evt, aio);
		nni_mtx_unlock(&evt->mtx);
	}
}

static void
nni_win_event_cancel(nni_aio *aio, int rv)
{
	nni_win_event *evt = aio->a_prov_data;

	nni_mtx_lock(&evt->mtx);
	if (evt->aio == aio) {
		evt->status = rv;

		// Use provider specific cancellation.
		evt->ops.wev_cancel(evt);
	}
	nni_mtx_unlock(&evt->mtx);
}

void
nni_win_event_resubmit(nni_win_event *evt, nni_aio *aio)
{
	// This is just continuation of a pre-existing AIO operation.
	// For example, continuing I/O of a multi-buffer s/g operation.
	// The lock is held.

	// Abort operation -- no further activity.
	if (evt->fini) {
		evt->run = 0;
		nni_cv_wake(&evt->cv);
		return;
	}

	evt->status = 0;
	evt->count  = 0;
	if (!ResetEvent(evt->olpd.hEvent)) {
		evt->status = nni_win_error(GetLastError());
		evt->count  = 0;
		nni_win_event_finish(evt, aio);
		return;
	}

	evt->aio = aio;
	evt->run = 1;
	if (evt->ops.wev_start(evt, aio) != 0) {
		// Start completed synchronously.  It will have stored
		// the count and status in the evt.
		evt->aio = NULL;
		nni_win_event_finish(evt, aio);
	}
}

void
nni_win_event_submit(nni_win_event *evt, nni_aio *aio)
{
	nni_mtx_lock(&evt->mtx);
	if (nni_aio_start(aio, nni_win_event_cancel, evt) != 0) {
		// the aio was aborted
		nni_mtx_unlock(&evt->mtx);
		return;
	}
	nni_win_event_resubmit(evt, aio);
	nni_mtx_unlock(&evt->mtx);
}

void
nni_win_event_complete(nni_win_event *evt, int cnt)
{
	PostQueuedCompletionStatus(nni_win_global_iocp, cnt, 0, &evt->olpd);
}

void
nni_win_event_close(nni_win_event *evt)
{
	if (evt->ptr != NULL) {
		nni_mtx_lock(&evt->mtx);
		evt->status = NNG_ECLOSED;
		evt->ops.wev_cancel(evt);
		nni_mtx_unlock(&evt->mtx);
	}
}

int
nni_win_iocp_register(HANDLE h)
{
	if (CreateIoCompletionPort(h, nni_win_global_iocp, 0, 0) == NULL) {
		return (nni_win_error(GetLastError()));
	}
	return (0);
}

int
nni_win_event_init(nni_win_event *evt, nni_win_event_ops *ops, void *ptr)
{
	ZeroMemory(&evt->olpd, sizeof(evt->olpd));
	evt->olpd.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
	if (evt->olpd.hEvent == NULL) {
		return (nni_win_error(GetLastError()));
	}
	nni_mtx_init(&evt->mtx);
	nni_cv_init(&evt->cv, &evt->mtx);

	evt->ops  = *ops;
	evt->aio  = NULL;
	evt->ptr  = ptr;
	evt->fini = 0;
	evt->run  = 0;
	return (0);
}

void
nni_win_event_fini(nni_win_event *evt)
{
	if (evt->ptr != NULL) {
		nni_mtx_lock(&evt->mtx);

		evt->fini = 1;

		// Use provider specific cancellation.
		evt->ops.wev_cancel(evt);

		// Wait for everything to stop referencing this.
		while (evt->run) {
			nni_cv_wait(&evt->cv);
		}

		nni_mtx_unlock(&evt->mtx);
	}

	if (evt->olpd.hEvent != NULL) {
		(void) CloseHandle(evt->olpd.hEvent);
		evt->olpd.hEvent = NULL;
	}
	nni_cv_fini(&evt->cv);
	nni_mtx_fini(&evt->mtx);
}

int
nni_win_iocp_sysinit(void)
{
	HANDLE h;
	int    i;
	int    rv;

	h = CreateIoCompletionPort(
	    INVALID_HANDLE_VALUE, NULL, 0, NNI_WIN_IOCP_NTHREADS);
	if (h == NULL) {
		return (nni_win_error(GetLastError()));
	}
	nni_win_global_iocp = h;
	for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) {
		rv = nni_thr_init(
		    &nni_win_iocp_thrs[i], nni_win_iocp_handler, NULL);
		if (rv != 0) {
			goto fail;
		}
	}
	nni_mtx_init(&nni_win_iocp_mtx);
	for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) {
		nni_thr_run(&nni_win_iocp_thrs[i]);
	}
	return (0);

fail:
	if ((h = nni_win_global_iocp) != NULL) {
		CloseHandle(h);
		nni_win_global_iocp = NULL;
	}
	for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) {
		nni_thr_fini(&nni_win_iocp_thrs[i]);
	}
	return (rv);
}

void
nni_win_iocp_sysfini(void)
{
	int    i;
	HANDLE h;

	if ((h = nni_win_global_iocp) != NULL) {
		CloseHandle(h);
		nni_win_global_iocp = NULL;
	}
	for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) {
		nni_thr_fini(&nni_win_iocp_thrs[i]);
	}
	nni_mtx_fini(&nni_win_iocp_mtx);
}

#else

// Suppress empty symbols warnings in ranlib.
int nni_win_iocp_not_used = 0;

#endif // PLATFORM_WINDOWS