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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
|
//
// 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 "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_msgq {
nni_mtx mq_lock;
nni_cv mq_readable;
nni_cv mq_writeable;
nni_cv 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_puterr;
int mq_geterr;
int mq_rwait; // readers waiting (unbuffered)
int mq_wwait;
nni_msg ** mq_msgs;
int mq_notify_sig;
nni_cv mq_notify_cv;
nni_thr mq_notify_thr;
nni_msgq_notify_fn mq_notify_fn;
void * mq_notify_arg;
nni_list mq_aio_putq;
nni_list mq_aio_getq;
nni_timer_node mq_timer;
nni_time mq_expire;
};
// nni_msgq_notifier thread runs if events callbacks are registered on the
// message queue, and calls the notification callbacks outside of the
// lock. It looks at the actual msgq state to trigger the right events.
static void
nni_msgq_notifier(void *arg)
{
nni_msgq *mq = arg;
int sig;
nni_msgq_notify_fn fn;
void *fnarg;
for (;;) {
nni_mtx_lock(&mq->mq_lock);
while ((mq->mq_notify_sig == 0) && (!mq->mq_closed)) {
nni_cv_wait(&mq->mq_notify_cv);
}
if (mq->mq_closed) {
nni_mtx_unlock(&mq->mq_lock);
break;
}
sig = mq->mq_notify_sig;
mq->mq_notify_sig = 0;
fn = mq->mq_notify_fn;
fnarg = mq->mq_notify_arg;
nni_mtx_unlock(&mq->mq_lock);
if (fn != NULL) {
fn(mq, sig, fnarg);
}
}
}
// nni_msgq_kick kicks the msgq notification thread. It should be called
// with the lock held.
static void
nni_msgq_kick(nni_msgq *mq, int sig)
{
if (mq->mq_notify_fn != NULL) {
mq->mq_notify_sig |= sig;
nni_cv_wake(&mq->mq_notify_cv);
}
}
static void nni_msgq_run_timeout(void *);
int
nni_msgq_init(nni_msgq **mqp, int cap)
{
struct nni_msgq *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_STRUCT(mq)) == NULL) {
return (NNG_ENOMEM);
}
NNI_LIST_INIT(&mq->mq_aio_putq, nni_aio, a_prov_node);
NNI_LIST_INIT(&mq->mq_aio_getq, nni_aio, a_prov_node);
if ((rv = nni_mtx_init(&mq->mq_lock)) != 0) {
goto fail;
}
if (((rv = nni_cv_init(&mq->mq_readable, &mq->mq_lock)) != 0) ||
((rv = nni_cv_init(&mq->mq_writeable, &mq->mq_lock)) != 0) ||
((rv = nni_cv_init(&mq->mq_drained, &mq->mq_lock)) != 0) ||
((rv = nni_cv_init(&mq->mq_notify_cv, &mq->mq_lock)) != 0)) {
goto fail;
}
if ((mq->mq_msgs = nni_alloc(sizeof (nng_msg *) * alloc)) == NULL) {
rv = NNG_ENOMEM;
goto fail;
}
nni_timer_init(&mq->mq_timer, nni_msgq_run_timeout, mq);
mq->mq_cap = cap;
mq->mq_alloc = alloc;
mq->mq_len = 0;
mq->mq_get = 0;
mq->mq_put = 0;
mq->mq_closed = 0;
mq->mq_puterr = 0;
mq->mq_geterr = 0;
mq->mq_wwait = 0;
mq->mq_rwait = 0;
mq->mq_notify_fn = NULL;
mq->mq_notify_arg = NULL;
mq->mq_expire = NNI_TIME_NEVER;
*mqp = mq;
return (0);
fail:
nni_cv_fini(&mq->mq_drained);
nni_cv_fini(&mq->mq_writeable);
nni_cv_fini(&mq->mq_readable);
nni_mtx_fini(&mq->mq_lock);
if (mq->mq_msgs != NULL) {
nni_free(mq->mq_msgs, sizeof (nng_msg *) * alloc);
}
NNI_FREE_STRUCT(mq);
return (rv);
}
void
nni_msgq_fini(nni_msgq *mq)
{
nni_msg *msg;
if (mq == NULL) {
return;
}
nni_thr_fini(&mq->mq_notify_thr);
nni_cv_fini(&mq->mq_drained);
nni_cv_fini(&mq->mq_writeable);
nni_cv_fini(&mq->mq_readable);
nni_mtx_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_STRUCT(mq);
}
int
nni_msgq_notify(nni_msgq *mq, nni_msgq_notify_fn fn, void *arg)
{
int rv;
nni_thr_fini(&mq->mq_notify_thr);
nni_mtx_lock(&mq->mq_lock);
rv = nni_thr_init(&mq->mq_notify_thr, nni_msgq_notifier, mq);
if (rv != 0) {
nni_mtx_unlock(&mq->mq_lock);
return (rv);
}
mq->mq_notify_fn = fn;
mq->mq_notify_arg = arg;
nni_thr_run(&mq->mq_notify_thr);
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
void
nni_msgq_set_put_error(nni_msgq *mq, int error)
{
nni_mtx_lock(&mq->mq_lock);
mq->mq_puterr = error;
if (error) {
mq->mq_wwait = 0;
nni_cv_wake(&mq->mq_writeable);
}
nni_mtx_unlock(&mq->mq_lock);
}
void
nni_msgq_set_get_error(nni_msgq *mq, int error)
{
nni_mtx_lock(&mq->mq_lock);
mq->mq_geterr = error;
if (error) {
mq->mq_rwait = 0;
nni_cv_wake(&mq->mq_readable);
}
nni_mtx_unlock(&mq->mq_lock);
}
void
nni_msgq_set_error(nni_msgq *mq, int error)
{
nni_mtx_lock(&mq->mq_lock);
mq->mq_geterr = error;
mq->mq_puterr = error;
if (error) {
mq->mq_rwait = 0;
mq->mq_wwait = 0;
nni_cv_wake(&mq->mq_readable);
nni_cv_wake(&mq->mq_writeable);
}
nni_mtx_unlock(&mq->mq_lock);
}
// nni_msgq_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_msgq_signal(nni_msgq *mq, int *signal)
{
nni_mtx_lock(&mq->mq_lock);
*signal = 1;
// We have to wake everyone.
mq->mq_rwait = 0;
mq->mq_wwait = 0;
nni_cv_wake(&mq->mq_readable);
nni_cv_wake(&mq->mq_writeable);
nni_cv_wake(&mq->mq_notify_cv);
nni_mtx_unlock(&mq->mq_lock);
}
static void
nni_msgq_run_putq(nni_msgq *mq)
{
nni_aio *waio;
nni_aio *raio;
nni_msg *msg;
size_t len;
while ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) {
msg = waio->a_msg;
len = nni_msg_len(msg);
// The presence of any blocked reader indicates that
// the queue is empty, otherwise it would have just taken
// data from the queue.
if ((raio = nni_list_first(&mq->mq_aio_getq)) != NULL) {
nni_list_remove(&mq->mq_aio_getq, raio);
nni_list_remove(&mq->mq_aio_putq, waio);
raio->a_msg = msg;
waio->a_msg = NULL;
nni_aio_finish(raio, 0, len);
nni_aio_finish(waio, 0, len);
continue;
}
// Otherwise if we have room in the buffer, just queue it.
if ((mq->mq_len < mq->mq_cap) ||
((mq->mq_len == mq->mq_cap) && mq->mq_rwait)) {
nni_list_remove(&mq->mq_aio_putq, waio);
mq->mq_msgs[mq->mq_put++] = msg;
if (mq->mq_put == mq->mq_alloc) {
mq->mq_put = 0;
}
mq->mq_len++;
waio->a_msg = NULL;
nni_aio_finish(waio, 0, len);
continue;
}
// Unable to make progress, leave the aio where it is.
break;
}
// XXX: REMOVE ME WHEN WE GO COMPLETELY ASYNC.
if (mq->mq_len != 0) {
nni_cv_wake(&mq->mq_readable);
}
if (mq->mq_len < mq->mq_cap) {
nni_cv_wake(&mq->mq_writeable);
}
}
static void
nni_msgq_run_getq(nni_msgq *mq)
{
nni_aio *raio;
nni_aio *waio;
nni_msg *msg;
size_t len;
while ((raio = nni_list_first(&mq->mq_aio_getq)) != NULL) {
// If anything is waiting in the queue, get it first.
if (mq->mq_len != 0) {
nni_list_remove(&mq->mq_aio_getq, raio);
msg = mq->mq_msgs[mq->mq_get++];
if (mq->mq_get == mq->mq_alloc) {
mq->mq_get = 0;
}
mq->mq_len--;
len = nni_msg_len(msg);
raio->a_msg = msg;
nni_aio_finish(raio, 0, len);
continue;
}
// Nothing queued (unbuffered?), maybe a writer is waiting.
if ((waio = nni_list_first(&mq->mq_aio_putq)) != NULL) {
nni_list_remove(&mq->mq_aio_putq, waio);
nni_list_remove(&mq->mq_aio_getq, raio);
msg = waio->a_msg;
len = nni_msg_len(msg);
waio->a_msg = NULL;
raio->a_msg = msg;
nni_aio_finish(raio, 0, len);
nni_aio_finish(waio, 0, len);
continue;
}
// No data to get, and no unbuffered writers waiting. Just
// wait until something arrives.
break;
}
}
static void
nni_msgq_run_notify(nni_msgq *mq)
{
}
void
nni_msgq_aio_put(nni_msgq *mq, nni_aio *aio)
{
nni_time expire = aio->a_expire;
nni_mtx_lock(&mq->mq_lock);
nni_list_append(&mq->mq_aio_putq, aio);
nni_msgq_run_putq(mq);
nni_msgq_run_notify(mq);
if (expire < mq->mq_expire) {
mq->mq_expire = expire;
nni_timer_schedule(&mq->mq_timer, mq->mq_expire);
}
nni_mtx_unlock(&mq->mq_lock);
}
void
nni_msgq_aio_get(nni_msgq *mq, nni_aio *aio)
{
nni_time expire = aio->a_expire;
nni_mtx_lock(&mq->mq_lock);
nni_list_append(&mq->mq_aio_getq, aio);
nni_msgq_run_getq(mq);
nni_msgq_run_notify(mq);
if (expire < mq->mq_expire) {
mq->mq_expire = expire;
nni_timer_schedule(&mq->mq_timer, mq->mq_expire);
}
nni_mtx_unlock(&mq->mq_lock);
}
void
nni_msgq_aio_cancel(nni_msgq *mq, nni_aio *aio)
{
nni_mtx_lock(&mq->mq_lock);
// NB: nni_list_active and nni_list_remove only use the list structure
// to determine list node offsets. Otherwise, they only look at the
// node's linkage structure. Therefore the following check will remove
// the node from either the getq or the putq list.
if (nni_list_active(&mq->mq_aio_getq, aio)) {
nni_list_remove(&mq->mq_aio_getq, aio);
}
nni_mtx_unlock(&mq->mq_lock);
}
int
nni_msgq_canput(nni_msgq *mq)
{
nni_mtx_lock(&mq->mq_lock);
if ((mq->mq_len < mq->mq_cap) ||
(mq->mq_rwait != 0) || // XXX: REMOVE ME
(nni_list_first(&mq->mq_aio_getq) != NULL)) {
nni_mtx_unlock(&mq->mq_lock);
return (1);
}
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
int
nni_msgq_canget(nni_msgq *mq)
{
nni_mtx_lock(&mq->mq_lock);
if ((mq->mq_len != 0) ||
(mq->mq_wwait != 0) ||
(nni_list_first(&mq->mq_aio_putq) != NULL)) {
nni_mtx_unlock(&mq->mq_lock);
return (1);
}
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
int
nni_msgq_tryput(nni_msgq *mq, nni_msg *msg)
{
nni_aio *raio;
size_t len = nni_msg_len(msg);
nni_mtx_lock(&mq->mq_lock);
// The presence of any blocked reader indicates that
// the queue is empty, otherwise it would have just taken
// data from the queue.
if ((raio = nni_list_first(&mq->mq_aio_getq)) != NULL) {
nni_list_remove(&mq->mq_aio_getq, raio);
raio->a_msg = msg;
nni_aio_finish(raio, 0, len);
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
// Otherwise if we have room in the buffer, just queue it.
if ((mq->mq_len < mq->mq_cap) ||
((mq->mq_len == mq->mq_cap) && mq->mq_rwait)) {
mq->mq_msgs[mq->mq_put++] = msg;
if (mq->mq_put == mq->mq_alloc) {
mq->mq_put = 0;
}
mq->mq_len++;
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
nni_mtx_unlock(&mq->mq_lock);
return (NNG_EAGAIN);
}
void
nni_msgq_run_timeout(void *arg)
{
nni_msgq *mq = arg;
nni_time now;
nni_time exp;
nni_aio *aio;
nni_aio *naio;
int rv;
now = nni_clock();
exp = NNI_TIME_NEVER;
nni_mtx_lock(&mq->mq_lock);
naio = nni_list_first(&mq->mq_aio_getq);
while ((aio = naio) != NULL) {
naio = nni_list_next(&mq->mq_aio_getq, aio);
if (now >= aio->a_expire) {
nni_list_remove(&mq->mq_aio_getq, aio);
nni_aio_finish(aio, NNG_ETIMEDOUT, 0);
}
if (exp > aio->a_expire) {
exp = aio->a_expire;
}
}
naio = nni_list_first(&mq->mq_aio_putq);
while ((aio = naio) != NULL) {
naio = nni_list_next(&mq->mq_aio_putq, aio);
if (now >= aio->a_expire) {
nni_list_remove(&mq->mq_aio_putq, aio);
nni_aio_finish(aio, NNG_ETIMEDOUT, 0);
}
if (exp > aio->a_expire) {
exp = aio->a_expire;
}
}
mq->mq_expire = exp;
if (mq->mq_expire != NNI_TIME_NEVER) {
nni_timer_schedule(&mq->mq_timer, mq->mq_expire);
}
nni_mtx_unlock(&mq->mq_lock);
}
int
nni_msgq_put_(nni_msgq *mq, nni_msg *msg, nni_time expire, nni_signal *sig)
{
int rv;
nni_mtx_lock(&mq->mq_lock);
for (;;) {
// if closed, we don't put more... this check is first!
if (mq->mq_closed) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_ECLOSED);
}
if ((rv = mq->mq_puterr) != 0) {
nni_mtx_unlock(&mq->mq_lock);
return (rv);
}
// 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 (*sig) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_EINTR);
}
// single poll?
if (expire == NNI_TIME_ZERO) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_EAGAIN);
}
// waiting....
mq->mq_wwait = 1;
// if we are unbuffered, kick the notifier, because we're
// writable.
if (mq->mq_cap == 0) {
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANGET);
}
// not writeable, so wait until something changes
rv = nni_cv_until(&mq->mq_writeable, expire);
if (rv == NNG_ETIMEDOUT) {
nni_mtx_unlock(&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) {
mq->mq_rwait = 0;
nni_cv_wake(&mq->mq_readable);
}
if (mq->mq_len < mq->mq_cap) {
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANPUT);
}
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANGET);
nni_msgq_run_getq(mq);
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
static int
nni_msgq_get_(nni_msgq *mq, nni_msg **msgp, nni_time expire, nni_signal *sig)
{
int rv;
nni_mtx_lock(&mq->mq_lock);
for (;;) {
// always prefer to deliver data if its there
if (mq->mq_len != 0) {
break;
}
if (mq->mq_closed) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_ECLOSED);
}
if ((rv = mq->mq_geterr) != 0) {
nni_mtx_unlock(&mq->mq_lock);
return (rv);
}
if (expire == NNI_TIME_ZERO) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_EAGAIN);
}
if (*sig) {
nni_mtx_unlock(&mq->mq_lock);
return (NNG_EINTR);
}
if ((mq->mq_cap == 0) && (mq->mq_wwait)) {
// let a write waiter know we are ready
mq->mq_wwait = 0;
nni_cv_wake(&mq->mq_writeable);
}
mq->mq_rwait = 1;
if (mq->mq_cap == 0) {
// If unbuffered, kick it since a writer would not
// block.
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANPUT);
}
rv = nni_cv_until(&mq->mq_readable, expire);
if (rv == NNG_ETIMEDOUT) {
nni_mtx_unlock(&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) {
mq->mq_wwait = 0;
nni_cv_wake(&mq->mq_writeable);
}
if (mq->mq_len) {
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANGET);
}
nni_msgq_kick(mq, NNI_MSGQ_NOTIFY_CANPUT);
nni_msgq_run_putq(mq);
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
int
nni_msgq_get_until(nni_msgq *mq, nni_msg **msgp, nni_time expire)
{
nni_aio aio;
int rv;
if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
return (rv);
}
aio.a_expire = expire;
nni_msgq_aio_get(mq, &aio);
nni_aio_wait(&aio);
if ((rv = nni_aio_result(&aio)) == 0) {
*msgp = aio.a_msg;
aio.a_msg = NULL;
}
nni_aio_fini(&aio);
return (rv);
}
int
nni_msgq_get(nni_msgq *mq, nni_msg **msgp)
{
return (nni_msgq_get_until(mq, msgp, NNI_TIME_NEVER));
}
// XXX Remove this later.
int
nni_msgq_get_sig(nni_msgq *mq, nni_msg **msgp, nni_signal *signal)
{
return (nni_msgq_get_(mq, msgp, NNI_TIME_NEVER, signal));
}
int
nni_msgq_put_until(nni_msgq *mq, nni_msg *msg, nni_time expire)
{
nni_aio aio;
int rv;
if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
return (rv);
}
aio.a_expire = expire;
aio.a_msg = msg;
nni_msgq_aio_put(mq, &aio);
nni_aio_wait(&aio);
nni_aio_fini(&aio);
return (rv);
}
int
nni_msgq_put(nni_msgq *mq, nni_msg *msg)
{
return (nni_msgq_put_until(mq, msg, NNI_TIME_NEVER));
}
int
nni_msgq_put_sig(nni_msgq *mq, nni_msg *msg, nni_signal *signal)
{
return (nni_msgq_put_(mq, msg, NNI_TIME_NEVER, signal));
}
void
nni_msgq_drain(nni_msgq *mq, nni_time expire)
{
nni_mtx_lock(&mq->mq_lock);
mq->mq_closed = 1;
mq->mq_wwait = 0;
mq->mq_rwait = 0;
nni_cv_wake(&mq->mq_writeable);
nni_cv_wake(&mq->mq_readable);
nni_cv_wake(&mq->mq_notify_cv);
while (mq->mq_len > 0) {
if (nni_cv_until(&mq->mq_drained, expire) != 0) {
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_mtx_unlock(&mq->mq_lock);
}
void
nni_msgq_close(nni_msgq *mq)
{
nni_mtx_lock(&mq->mq_lock);
mq->mq_closed = 1;
mq->mq_wwait = 0;
mq->mq_rwait = 0;
nni_cv_wake(&mq->mq_writeable);
nni_cv_wake(&mq->mq_readable);
nni_cv_wake(&mq->mq_notify_cv);
// 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_mtx_unlock(&mq->mq_lock);
}
int
nni_msgq_len(nni_msgq *mq)
{
int rv;
nni_mtx_lock(&mq->mq_lock);
rv = mq->mq_len;
nni_mtx_unlock(&mq->mq_lock);
return (rv);
}
int
nni_msgq_cap(nni_msgq *mq)
{
int rv;
nni_mtx_lock(&mq->mq_lock);
rv = mq->mq_cap;
nni_mtx_unlock(&mq->mq_lock);
return (rv);
}
int
nni_msgq_resize(nni_msgq *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_mtx_lock(&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_cv_wake(&mq->mq_readable);
nni_cv_wake(&mq->mq_writeable);
nni_cv_wake(&mq->mq_drained);
nni_mtx_unlock(&mq->mq_lock);
return (0);
}
|