aboutsummaryrefslogtreecommitdiff
path: root/src/core/aio.c
blob: f4512a34ea98f81f1745ea609b95934d3a2c6ba4 (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
//
// 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.
//

#include <string.h>
#include "core/nng_impl.h"

#define NNI_AIO_WAKE	(1<<0)
#define NNI_AIO_DONE	(1<<1)
#define NNI_AIO_FINI	(1<<2)
#define NNI_AIO_STOP	(1<<3)

int
nni_aio_init(nni_aio *aio, nni_cb cb, void *arg)
{
	int rv;

	if (cb == NULL) {
		cb = (nni_cb) nni_aio_wake;
		arg = aio;
	}
	memset(aio, 0, sizeof (*aio));
	if ((rv = nni_mtx_init(&aio->a_lk)) != 0) {
		return (rv);
	}
	if ((rv = nni_cv_init(&aio->a_cv, &aio->a_lk)) != 0) {
		nni_mtx_fini(&aio->a_lk);
		return (rv);
	}
	aio->a_cb = cb;
	aio->a_cbarg = arg;
	aio->a_expire = NNI_TIME_NEVER;
	aio->a_flags = 0;
	nni_taskq_ent_init(&aio->a_tqe, cb, arg);

	return (0);
}


void
nni_aio_fini(nni_aio *aio)
{
	void (*cancelfn)(nni_aio *);

	nni_mtx_lock(&aio->a_lk);
	aio->a_flags |= NNI_AIO_FINI; // this prevents us from being scheduled
	cancelfn = aio->a_prov_cancel;
	nni_cv_wake(&aio->a_cv);
	nni_mtx_unlock(&aio->a_lk);

	// Cancel the AIO if it was scheduled.
	if (cancelfn != NULL) {
		cancelfn(aio);
	}

	// if the task is already dispatched, cancel it (or wait for it to
	// complete).  No further dispatches will happen because of the
	// above logic to set NNI_AIO_FINI.
	nni_taskq_cancel(NULL, &aio->a_tqe);

	// At this point the AIO is done.
	nni_cv_fini(&aio->a_cv);
	nni_mtx_fini(&aio->a_lk);
}


int
nni_aio_result(nni_aio *aio)
{
	int rv;

	nni_mtx_lock(&aio->a_lk);
	rv = aio->a_result;
	if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) {
		rv = NNG_ECANCELED;
	}
	nni_mtx_unlock(&aio->a_lk);
	return (rv);
}


size_t
nni_aio_count(nni_aio *aio)
{
	return (aio->a_count);
}


void
nni_aio_wake(nni_aio *aio)
{
	nni_mtx_lock(&aio->a_lk);
	aio->a_flags |= NNI_AIO_WAKE;
	nni_cv_wake(&aio->a_cv);
	nni_mtx_unlock(&aio->a_lk);
}


void
nni_aio_wait(nni_aio *aio)
{
	nni_mtx_lock(&aio->a_lk);
	while ((aio->a_flags & (NNI_AIO_WAKE|NNI_AIO_FINI)) == 0) {
		nni_cv_wait(&aio->a_cv);
	}
	nni_mtx_unlock(&aio->a_lk);
}


int
nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data)
{
	NNI_ASSERT(aio->a_prov_data == NULL);
	NNI_ASSERT(aio->a_prov_cancel == NULL);

	nni_mtx_lock(&aio->a_lk);
	aio->a_flags &= ~(NNI_AIO_DONE|NNI_AIO_WAKE);
	if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) {
		// We should not reschedule anything at this point.
		nni_mtx_unlock(&aio->a_lk);
		return (NNG_ECANCELED);
	}
	aio->a_result = 0;
	aio->a_count = 0;
	aio->a_prov_cancel = cancel;
	aio->a_prov_data = data;
	nni_mtx_unlock(&aio->a_lk);
	return (0);
}


void
nni_aio_stop(nni_aio *aio)
{
	void (*cancelfn)(nni_aio *);

	nni_mtx_lock(&aio->a_lk);
	aio->a_flags |= NNI_AIO_DONE|NNI_AIO_STOP;
	cancelfn = aio->a_prov_cancel;
	nni_mtx_unlock(&aio->a_lk);

	// This unregisters the AIO from the provider.
	if (cancelfn != NULL) {
		cancelfn(aio);
	}

	nni_mtx_lock(&aio->a_lk);
	aio->a_prov_data = NULL;
	aio->a_prov_cancel = NULL;
	nni_cv_wake(&aio->a_cv);
	nni_mtx_unlock(&aio->a_lk);

	// This either aborts the task, or waits for it to complete if already
	// dispatched.
	nni_taskq_cancel(NULL, &aio->a_tqe);
}


void
nni_aio_cancel(nni_aio *aio)
{
	void (*cancelfn)(nni_aio *);

	nni_mtx_lock(&aio->a_lk);
	if (aio->a_flags & NNI_AIO_DONE) {
		// The operation already completed - so there's nothing
		// left for us to do.
		nni_mtx_unlock(&aio->a_lk);
		return;
	}
	aio->a_flags |= NNI_AIO_DONE;
	aio->a_result = NNG_ECANCELED;
	cancelfn = aio->a_prov_cancel;
	nni_mtx_unlock(&aio->a_lk);

	// This unregisters the AIO from the provider.
	if (cancelfn != NULL) {
		cancelfn(aio);
	}

	nni_mtx_lock(&aio->a_lk);
	// These should have already been cleared by the cancel function.
	aio->a_prov_data = NULL;
	aio->a_prov_cancel = NULL;

	if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) {
		nni_taskq_dispatch(NULL, &aio->a_tqe);
	}
	nni_mtx_unlock(&aio->a_lk);
}


// I/O provider related functions.

void
nni_aio_finish(nni_aio *aio, int result, size_t count)
{
	nni_mtx_lock(&aio->a_lk);
	if (aio->a_flags & NNI_AIO_DONE) {
		// Operation already done (canceled or timed out?)
		nni_mtx_unlock(&aio->a_lk);
		return;
	}
	aio->a_flags |= NNI_AIO_DONE;
	aio->a_result = result;
	aio->a_count = count;
	aio->a_prov_cancel = NULL;
	aio->a_prov_data = NULL;

	if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) {
		nni_taskq_dispatch(NULL, &aio->a_tqe);
	}
	nni_mtx_unlock(&aio->a_lk);
}