aboutsummaryrefslogtreecommitdiff
path: root/src/core/msgqueue.c
blob: 3af4e08152632867ee4086c3ea4aab2a8e219cc7 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//
// 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.
//

#include "nng_impl.h"

// Message queue.  These operate in some respects like Go channels,
// but as we have access to the internals, we have made some fundamental
// differences and improvements.  For example, these can grow, and either
// side can close, and they may be closed more than once.

struct nni_msgqueue {
	nni_mutex	mq_lock;
	nni_cond	mq_readable;
	nni_cond	mq_writeable;
	nni_cond	mq_drained;
	int		mq_cap;
	int		mq_alloc;       // alloc is cap + 2...
	int		mq_len;
	int		mq_get;
	int		mq_put;
	int		mq_closed;
	int		mq_rwait;       // readers waiting (unbuffered)
	int		mq_wwait;
	nni_msg **	mq_msgs;
};

int
nni_msgqueue_create(nni_msgqueue **mqp, int cap)
{
	struct nni_msgqueue *mq;
	int rv;
	int alloc;

	if (cap < 0) {
		return (NNG_EINVAL);
	}

	// We allocate 2 extra cells in the fifo.  One to accommodate a
	// waiting writer when cap == 0. (We can "briefly" move the message
	// through.)  This lets us behave the same as unbuffered Go channels.
	// The second cell is to permit pushback later, e.g. for REQ to stash
	// a message back at the end to do a retry.
	alloc = cap + 2;

	if ((mq = nni_alloc(sizeof (*mq))) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_mutex_init(&mq->mq_lock)) != 0) {
		nni_free(mq, sizeof (*mq));
		return (rv);
	}
	if ((rv = nni_cond_init(&mq->mq_readable, &mq->mq_lock)) != 0) {
		nni_mutex_fini(&mq->mq_lock);
		nni_free(mq, sizeof (*mq));
		return (NNG_ENOMEM);
	}
	if ((rv = nni_cond_init(&mq->mq_writeable, &mq->mq_lock)) != 0) {
		nni_cond_fini(&mq->mq_readable);
		nni_mutex_fini(&mq->mq_lock);
		return (NNG_ENOMEM);
	}
	if ((rv = nni_cond_init(&mq->mq_drained, &mq->mq_lock)) != 0) {
		nni_cond_fini(&mq->mq_writeable);
		nni_cond_fini(&mq->mq_readable);
		nni_mutex_fini(&mq->mq_lock);
		return (NNG_ENOMEM);
	}
	if ((mq->mq_msgs = nni_alloc(sizeof (nng_msg *) * alloc)) == NULL) {
		nni_cond_fini(&mq->mq_drained);
		nni_cond_fini(&mq->mq_writeable);
		nni_cond_fini(&mq->mq_readable);
		nni_mutex_fini(&mq->mq_lock);
		return (NNG_ENOMEM);
	}

	mq->mq_cap = cap;
	mq->mq_alloc = alloc;
	mq->mq_len = 0;
	mq->mq_get = 0;
	mq->mq_put = 0;
	mq->mq_closed = 0;
	*mqp = mq;

	return (0);
}


void
nni_msgqueue_destroy(nni_msgqueue *mq)
{
	nni_msg *msg;

	nni_cond_fini(&mq->mq_drained);
	nni_cond_fini(&mq->mq_writeable);
	nni_cond_fini(&mq->mq_readable);
	nni_mutex_fini(&mq->mq_lock);

	/* Free any orphaned messages. */
	while (mq->mq_len > 0) {
		msg = mq->mq_msgs[mq->mq_get];
		mq->mq_get++;
		if (mq->mq_get > mq->mq_alloc) {
			mq->mq_get = 0;
		}
		mq->mq_len--;
		nni_msg_free(msg);
	}

	nni_free(mq->mq_msgs, mq->mq_alloc * sizeof (nng_msg *));
	nni_free(mq, sizeof (*mq));
}


// nni_msgqueue_signal raises a signal on the signal object. This allows a
// waiter to be signaled, so that it can be woken e.g. due to a pipe closing.
// Note that the signal object must be *zero* if no signal is raised.
void
nni_msgqueue_signal(nni_msgqueue *mq, int *signal)
{
	nni_mutex_enter(&mq->mq_lock);
	*signal = 1;

	// We have to wake everyone.
	nni_cond_broadcast(&mq->mq_readable);
	nni_cond_broadcast(&mq->mq_writeable);
	nni_mutex_exit(&mq->mq_lock);
}


int
nni_msgqueue_put_impl(nni_msgqueue *mq, nni_msg *msg,
    nni_time expire, nni_signal *signal)
{
	int rv;

	nni_mutex_enter(&mq->mq_lock);

	for (;;) {
		// if closed, we don't put more... this check is first!
		if (mq->mq_closed) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_ECLOSED);
		}

		// room in the queue?
		if (mq->mq_len < mq->mq_cap) {
			break;
		}

		// unbuffered, room for one, and a reader waiting?
		if (mq->mq_rwait &&
		    (mq->mq_cap == 0) &&
		    (mq->mq_len == mq->mq_cap)) {
			break;
		}

		// interrupted?
		if (*signal) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_EINTR);
		}

		// single poll?
		if (expire == NNI_TIME_ZERO) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_EAGAIN);
		}

		// not writeable, so wait until something changes
		mq->mq_wwait++;
		rv = nni_cond_waituntil(&mq->mq_writeable, expire);
		mq->mq_wwait--;
		if (rv == NNG_ETIMEDOUT) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_ETIMEDOUT);
		}
	}

	// Writeable!  Yay!!

	mq->mq_msgs[mq->mq_put] = msg;
	mq->mq_put++;
	if (mq->mq_put == mq->mq_alloc) {
		mq->mq_put = 0;
	}
	mq->mq_len++;
	if (mq->mq_rwait) {
		nni_cond_broadcast(&mq->mq_readable);
	}
	nni_mutex_exit(&mq->mq_lock);
	return (0);
}


// nni_msgqueue_putback will attempt to put a single message back
// to the head of the queue.  It never blocks.  Message queues always
// have room for at least one putback.
int
nni_msgqueue_putback(nni_msgqueue *mq, nni_msg *msg)
{
	nni_mutex_enter(&mq->mq_lock);

	// if closed, we don't put more... this check is first!
	if (mq->mq_closed) {
		nni_mutex_exit(&mq->mq_lock);
		return (NNG_ECLOSED);
	}

	// room in the queue?
	if (mq->mq_len >= mq->mq_cap) {
		nni_mutex_exit(&mq->mq_lock);
		return (NNG_EAGAIN);
	}

	// Subtract one from the get index, possibly wrapping.
	mq->mq_get--;
	if (mq->mq_get == 0) {
		mq->mq_get = mq->mq_cap;
	}
	mq->mq_msgs[mq->mq_get] = msg;
	mq->mq_len++;
	if (mq->mq_rwait) {
		nni_cond_broadcast(&mq->mq_readable);
	}
	nni_mutex_exit(&mq->mq_lock);
	return (0);
}


static int
nni_msgqueue_get_impl(nni_msgqueue *mq, nni_msg **msgp,
    nni_time expire, nni_signal *signal)
{
	int rv;

	nni_mutex_enter(&mq->mq_lock);

	for (;;) {
		// always prefer to deliver data if its there
		if (mq->mq_len != 0) {
			break;
		}
		if (mq->mq_closed) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_ECLOSED);
		}
		if (expire == NNI_TIME_ZERO) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_EAGAIN);
		}
		if (*signal) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_EINTR);
		}
		if ((mq->mq_cap == 0) & (mq->mq_wwait)) {
			// let a write waiter know we are ready
			nni_cond_broadcast(&mq->mq_writeable);
		}
		mq->mq_rwait++;
		rv = nni_cond_waituntil(&mq->mq_readable, expire);
		mq->mq_rwait--;
		if (rv == NNG_ETIMEDOUT) {
			nni_mutex_exit(&mq->mq_lock);
			return (NNG_ETIMEDOUT);
		}
	}

	// Readable!  Yay!!

	*msgp = mq->mq_msgs[mq->mq_get];
	mq->mq_len--;
	mq->mq_get++;
	if (mq->mq_get == mq->mq_alloc) {
		mq->mq_get = 0;
	}
	if (mq->mq_wwait) {
		nni_cond_broadcast(&mq->mq_writeable);
	}
	nni_mutex_exit(&mq->mq_lock);
	return (0);
}


int
nni_msgqueue_get(nni_msgqueue *mq, nni_msg **msgp)
{
	nni_signal nosig = 0;

	return (nni_msgqueue_get_impl(mq, msgp, NNI_TIME_NEVER, &nosig));
}


int
nni_msgqueue_get_sig(nni_msgqueue *mq, nni_msg **msgp, nni_signal *signal)
{
	return (nni_msgqueue_get_impl(mq, msgp, NNI_TIME_NEVER, signal));
}


int
nni_msgqueue_get_until(nni_msgqueue *mq, nni_msg **msgp, nni_time expire)
{
	nni_signal nosig = 0;

	return (nni_msgqueue_get_impl(mq, msgp, expire, &nosig));
}


int
nni_msgqueue_put(nni_msgqueue *mq, nni_msg *msg)
{
	nni_signal nosig = 0;

	return (nni_msgqueue_put_impl(mq, msg, NNI_TIME_NEVER, &nosig));
}


int
nni_msgqueue_put_sig(nni_msgqueue *mq, nni_msg *msg, nni_signal *signal)
{
	return (nni_msgqueue_put_impl(mq, msg, NNI_TIME_NEVER, signal));
}


int
nni_msgqueue_put_until(nni_msgqueue *mq, nni_msg *msg, nni_time expire)
{
	nni_signal nosig = 0;

	return (nni_msgqueue_put_impl(mq, msg, expire, &nosig));
}


void
nni_msgqueue_drain(nni_msgqueue *mq, nni_time expire)
{
	nni_mutex_enter(&mq->mq_lock);
	mq->mq_closed = 1;
	nni_cond_broadcast(&mq->mq_writeable);
	nni_cond_broadcast(&mq->mq_readable);
	while (mq->mq_len > 0) {
		if (nni_cond_waituntil(&mq->mq_drained, expire) ==
		    NNG_ETIMEDOUT) {
			break;
		}
	}
	// If we timedout, free any remaining messages in the queue.
	while (mq->mq_len > 0) {
		nni_msg *msg = mq->mq_msgs[mq->mq_get++];
		if (mq->mq_get > mq->mq_alloc) {
			mq->mq_get = 0;
		}
		mq->mq_len--;
		nni_msg_free(msg);
	}
	nni_mutex_exit(&mq->mq_lock);
}


void
nni_msgqueue_close(nni_msgqueue *mq)
{
	nni_mutex_enter(&mq->mq_lock);
	mq->mq_closed = 1;
	nni_cond_broadcast(&mq->mq_writeable);
	nni_cond_broadcast(&mq->mq_readable);

	// Free the messages orphaned in the queue.
	while (mq->mq_len > 0) {
		nni_msg *msg = mq->mq_msgs[mq->mq_get++];
		if (mq->mq_get > mq->mq_alloc) {
			mq->mq_get = 0;
		}
		mq->mq_len--;
		nni_msg_free(msg);
	}
	nni_mutex_exit(&mq->mq_lock);
}


int
nni_msgqueue_len(nni_msgqueue *mq)
{
	int rv;

	nni_mutex_enter(&mq->mq_lock);
	rv = mq->mq_len;
	nni_mutex_exit(&mq->mq_lock);
	return (rv);
}


int
nni_msgqueue_cap(nni_msgqueue *mq)
{
	int rv;

	nni_mutex_enter(&mq->mq_lock);
	rv = mq->mq_cap;
	nni_mutex_exit(&mq->mq_lock);
	return (rv);
}


int
nni_msgqueue_resize(nni_msgqueue *mq, int cap)
{
	int alloc;
	nni_msg *msg;
	nni_msg **newq, **oldq;
	int oldget;
	int oldput;
	int oldcap;
	int oldlen;
	int oldalloc;

	alloc = cap + 2;

	if (alloc > mq->mq_alloc) {
		newq = nni_alloc(sizeof (nni_msg *) * alloc);
		if (newq == NULL) {
			return (NNG_ENOMEM);
		}
	} else {
		newq = NULL;
	}

	nni_mutex_enter(&mq->mq_lock);
	while (mq->mq_len > (cap + 1)) {
		// too many messages -- we allow that one for
		// the case of pushback or cap == 0.
		// we delete the oldest messages first
		msg = mq->mq_msgs[mq->mq_get++];
		if (mq->mq_get > mq->mq_alloc) {
			mq->mq_get = 0;
		}
		mq->mq_len--;
		nni_msg_free(msg);
	}
	if (newq == NULL) {
		// Just shrinking the queue, no changes
		mq->mq_cap = cap;
		goto out;
	}

	oldq = mq->mq_msgs;
	oldget = mq->mq_get;
	oldput = mq->mq_put;
	oldcap = mq->mq_cap;
	oldalloc = mq->mq_alloc;
	oldlen = mq->mq_len;

	mq->mq_msgs = newq;
	mq->mq_len = mq->mq_get = mq->mq_put = 0;
	mq->mq_cap = cap;
	mq->mq_alloc = alloc;

	while (oldlen) {
		mq->mq_msgs[mq->mq_put++] = oldq[oldget++];
		if (oldget == oldalloc) {
			oldget = 0;
		}
		if (mq->mq_put == mq->mq_alloc) {
			mq->mq_put = 0;
		}
		mq->mq_len++;
		oldlen--;
	}
	nni_free(oldq, sizeof (nni_msg *) * oldalloc);

out:
	// Wake everyone up -- we changed everything.
	nni_cond_broadcast(&mq->mq_readable);
	nni_cond_broadcast(&mq->mq_writeable);
	nni_cond_broadcast(&mq->mq_drained);
	nni_mutex_exit(&mq->mq_lock);
	return (0);
}