-
Notifications
You must be signed in to change notification settings - Fork 1
/
protothread_test.c
985 lines (826 loc) · 23.6 KB
/
protothread_test.c
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/**************************************************************/
/* PROTOTHREAD_TEST.C */
/* Copyright (c) 2008, Larry Ruane, LeftHand Networks Inc. */
/* See license.txt */
/**************************************************************/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "protothread.h"
#include "protothread_sem.h"
#include "protothread_lock.h"
/******************************************************************************/
static void
test_create_dynamic(void)
{
protothread_t const pt = protothread_create() ;
protothread_run(pt) ;
protothread_free(pt) ;
}
/******************************************************************************/
static void
test_create_static(void)
{
struct protothread_s static_pt ;
protothread_t const pt = &static_pt ;
protothread_init(pt) ;
protothread_run(pt) ;
protothread_deinit(pt) ;
}
/******************************************************************************/
typedef struct create_context_s {
int i ;
pt_thread_t pt_thread ;
pt_func_t pt_func ;
} create_context_t ;
static pt_t
create_thr(env_t const env)
{
create_context_t * const c = env ;
pt_resume(c) ;
return PT_DONE ;
}
static void
test_thread_create(void)
{
protothread_t const pt = protothread_create() ;
int i ;
create_context_t * const c = malloc(sizeof(*c)) ;
for (i = 0; i < 1000; i++) {
pt_create(pt, &c->pt_thread, create_thr, c) ;
protothread_run(pt) ;
}
free(c) ;
protothread_free(pt) ;
}
/******************************************************************************/
typedef struct yield_context_s {
pt_func_t pt_func ;
pt_thread_t pt_thread ;
int i ;
} yield_context_t ;
static pt_t
yield_thr(env_t const env)
{
yield_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 10; c->i++) {
pt_yield(c) ;
}
return PT_DONE ;
}
static void
test_yield(void)
{
protothread_t const pt = protothread_create() ;
yield_context_t * const c = malloc(sizeof(*c)) ;
int i ;
c->i = -1 ; /* invalid value */
pt_create(pt, &c->pt_thread, yield_thr, c) ;
/* it hasn't run yet at all, make it reach the yield */
protothread_run(pt) ;
for (i = 0; i < 10; i++) {
/* make sure the protothread advances its loop */
assert(i == c->i) ;
protothread_run(pt) ;
}
free(c) ;
protothread_free(pt) ;
}
/******************************************************************************/
typedef struct wait_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
int i ;
} wait_context_t ;
static pt_t
wait_thr(env_t const env)
{
wait_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 10; c->i++) {
pt_wait(c, NULL) ;
}
return PT_DONE ;
}
static void
test_wait(void)
{
protothread_t const pt = protothread_create() ;
wait_context_t * c[10] ;
int i ;
int j ;
for (j = 0; j < 10; j++) {
c[j] = malloc(sizeof(*(c[j]))) ;
c[j]->i = -1 ;
pt_create(pt, &c[j]->pt_thread, wait_thr, c[j]) ;
}
/* threads haven't run yet at all, make them reach the wait */
for (j = 0; j < 10; j++) {
protothread_run(pt) ;
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
assert(i == c[j]->i) ;
}
/* make threads runnable, but do not actually run the threads */
pt_broadcast(pt, NULL) ;
for (j = 0; j < 10; j++) {
assert(i == c[j]->i) ;
}
/* run each thread once */
for (j = 0; j < 10; j++) {
protothread_run(pt) ;
}
for (j = 0; j < 10; j++) {
assert(i+1 == c[j]->i) ;
}
/* extra steps and wrong signals shouldn't advance the thread */
protothread_run(pt) ;
pt_broadcast(pt, c[0]) ;
protothread_run(pt) ;
for (j = 0; j < 10; j++) {
assert(i+1 == c[j]->i) ;
}
}
for (j = 0; j < 10; j++) {
free(c[j]) ;
}
protothread_free(pt) ;
}
/******************************************************************************/
/* make sure that broadcast wakes up all the threads it should,
* none of the threads it shouldn't
*/
#define N 1000
typedef struct broadcast_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
struct broadcast_global_context_s * gc ;
void * chan ;
bool_t run ;
} broadcast_context_t ;
typedef struct broadcast_global_context_s {
struct broadcast_context_s c[N] ;
int count ;
bool_t done ;
} broadcast_global_context_t ;
static pt_t
broadcast_thr(env_t const env)
{
broadcast_context_t * const c = env ;
broadcast_global_context_t * const gc = c->gc ;
pt_resume(c) ;
while (TRUE) {
/* the /3 ensures multiple threads wait on the same chan */
c->chan = &gc->c[(random() % N)/3] ;
pt_wait(c, c->chan) ;
if (gc->done) {
break ;
}
assert(c->run) ;
c->run = FALSE ;
}
return PT_DONE ;
}
static void
test_broadcast(void)
{
protothread_t const pt = protothread_create() ;
broadcast_global_context_t gc ;
int i, j ;
srand(0) ;
memset(&gc, 0, sizeof(gc)) ;
for (j = 0; j < N; j++) {
gc.c[j].gc = &gc ;
pt_create(pt, &gc.c[j].pt_thread, broadcast_thr, &gc.c[j]) ;
}
/* threads haven't run yet at all, make them reach the wait */
for (j = 0; j < N; j++) {
protothread_run(pt) ;
}
for (i = 0; i < 10000; i++) {
broadcast_context_t * const chan = &gc.c[(random() % N)/3] ;
pt_broadcast(pt, chan) ;
for (j = 0; j < N; j++) {
if (gc.c[j].chan == chan) {
/* should run */
gc.c[j].run = TRUE ;
}
}
while (protothread_run(pt)) ;
/* make sure every tread that should have run did run */
for (j = 0; j < N; j++) {
assert(!gc.c[j].run) ;
}
}
gc.done = TRUE ;
for (j = 0; j < N; j++) {
pt_broadcast(pt, &gc.c[j]) ;
}
while (protothread_run(pt)) ;
protothread_free(pt) ;
}
#undef N
/******************************************************************************/
/* Producer-comsumer
*
* Every thread needs a pt_thread_t; every thread function (including
* the top-level function) needs a pt_func_t with the name pt_func.
* These can be anywhere in the structure.
*/
typedef struct pc_thread_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
int * mailbox ; /* pointer to (shared) mailbox */
int i ; /* next value to send or expect to receive */
} pc_thread_context_t ;
#define N 1000
/* The producer thread waits until the mailbox is empty, and then writes
* the next value to the mailbox and pokes the consumer.
*/
static pt_t
producer_thr(env_t const env)
{
pc_thread_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 1; c->i <= N; c->i++) {
while (*c->mailbox) {
/* mailbox is full */
pt_wait(c, c->mailbox) ;
}
*c->mailbox = c->i ;
pt_signal(pt_get_pt(c), c->mailbox) ;
}
return PT_DONE ;
}
/* The consumer thread waits until something (non-zero) appears in the
* mailbox, verifies that it's the expected value, writes a zero to
* signify that the mailbox is empty, and wakes up the producer.
*/
static pt_t
consumer_thr(env_t const env)
{
pc_thread_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 1; c->i <= N; c->i++) {
while (*c->mailbox == 0) {
/* mailbox is empty */
pt_wait(c, c->mailbox) ;
}
assert(*c->mailbox == c->i) ;
*c->mailbox = 0 ; /* remove the item */
pt_signal(pt_get_pt(c), c->mailbox) ;
}
return PT_DONE ;
}
static void
test_pc(void)
{
protothread_t const pt = protothread_create() ;
pc_thread_context_t * const cc = malloc(sizeof(*cc)) ;
pc_thread_context_t * const pc = malloc(sizeof(*pc)) ;
int mailbox = 0 ;
/* set up consumer context, start consumer thread */
cc->mailbox = &mailbox ;
cc->i = 0 ;
pt_create(pt, &cc->pt_thread, consumer_thr, cc) ;
/* set up producer context, start producer thread */
pc->mailbox = &mailbox ;
pc->i = 0 ;
pt_create(pt, &pc->pt_thread, producer_thr, pc) ;
/* while threads are available to run ... */
while (protothread_run(pt)) ;
/* threads have completed */
assert(cc->i == N+1) ;
assert(pc->i == N+1) ;
free(cc) ;
free(pc) ;
protothread_free(pt) ;
}
static void
test_pc_big(void)
{
protothread_t const pt = protothread_create() ;
int mailbox[400] ;
pc_thread_context_t * const pc = malloc(sizeof(*pc) * 400) ;
pc_thread_context_t * const cc = malloc(sizeof(*cc) * 400) ;
int i ;
/* Start 400 independent pairs of threads, each pair sharing a
* mailbox.
*/
for (i = 0; i < 400; i++) {
mailbox[i] = 0 ;
cc[i].mailbox = &mailbox[i] ;
cc[i].i = 0 ;
pt_create(pt, &cc[i].pt_thread, consumer_thr, &cc[i]) ;
pc[i].mailbox = &mailbox[i] ;
pc[i].i = 0 ;
pt_create(pt, &pc[i].pt_thread, producer_thr, &pc[i]) ;
}
/* as long as there is work to do */
while (protothread_run(pt)) ;
free(cc) ;
free(pc) ;
protothread_free(pt) ;
}
#undef N
/******************************************************************************/
/* count to 4096 the hard way */
#define DEPTH 12
#define NODES (1 << DEPTH)
#define CHANS (NODES/8)
typedef struct recursive_call_global_context_s {
bool_t seen[NODES] ;
int nseen ;
} recursive_call_global_context_t ;
typedef struct recursive_call_context_s {
int level ; /* zero is top-level function */
int value ;
pt_func_t pt_func ;
pt_thread_t pt_thread ;
recursive_call_global_context_t * gc ;
struct recursive_call_context_s * child_c ;
} recursive_call_context_t ;
static pt_t
recursive_thr(env_t const env)
{
recursive_call_context_t * const c = env ;
recursive_call_global_context_t * const gc = c->gc ;
pt_resume(c) ;
pt_wait(c, &gc[rand() % CHANS]) ;
if (c->level >= DEPTH) {
/* leaf */
assert(c->value < NODES) ;
assert(!gc->seen[c->value]) ;
gc->seen[c->value] = TRUE ;
pt_wait(c, &gc[rand() % CHANS]) ;
gc->nseen ++ ;
free(c) ;
return PT_DONE ;
}
/* create the "left" (0) child; it will free this */
c->child_c = malloc(sizeof(*c->child_c)) ;
*c->child_c = *c ;
c->child_c->level ++ ;
c->child_c->value <<= 1 ;
if ((rand() % 4)) {
/* usually make a synchronous function call */
pt_call(c, recursive_thr, c->child_c) ;
} else {
/* once in a while create a new thread (asynchronous) */
pt_create(pt_get_pt(c), &c->child_c->pt_thread, recursive_thr, c->child_c) ;
}
pt_wait(c, &gc[rand() % CHANS]) ;
/* create the "right" (1) child; it will free this */
c->child_c = malloc(sizeof(*c->child_c)) ;
*c->child_c = *c ;
c->child_c->level ++ ;
c->child_c->value <<= 1 ;
c->child_c->value ++ ;
if ((rand() % 4)) {
pt_call(c, recursive_thr, c->child_c) ;
} else {
pt_create(pt_get_pt(c), &c->child_c->pt_thread, recursive_thr, c->child_c) ;
}
free(c) ;
return PT_DONE ;
}
static void
test_recursive_once(void)
{
protothread_t const pt = protothread_create() ;
recursive_call_global_context_t * gc = malloc(sizeof(*gc)) ;
recursive_call_context_t * top_c = malloc(sizeof(*top_c)) ;
int i ;
memset(gc, 0, sizeof(*gc)) ;
memset(top_c, 0, sizeof(*top_c)) ;
top_c->gc = gc ;
pt_create(pt, &top_c->pt_thread, recursive_thr, top_c) ;
/* it hasn't run yet at all, make it reach the call */
i = 0 ;
while (gc->nseen < NODES) {
if (protothread_run(pt)) {
i++ ;
}
/* make sure it is not taking too long (the 10 is cushion) */
assert(i < NODES*4*10) ;
pt_broadcast(pt, &gc[rand() % CHANS]) ;
}
for (i = 0; i < NODES; i++) {
assert(gc->seen[i]) ;
}
free(gc) ;
protothread_free(pt) ;
}
static void
test_recursive(void)
{
int i ;
/* because we're using random numbers, can run this multiple times
*/
srand(0) ;
for (i = 0; i < 10; i++) {
test_recursive_once() ;
}
}
/******************************************************************************/
typedef struct sem_global_context_s {
int owner ; /* zero, or thread who is in the critical section */
unsigned int sem_value ;
} sem_global_context_t ;
typedef struct sem_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
sem_global_context_t *gc ;
int i ; /* loop index */
int id ; /* thread id (>= 1) */
pt_sem_env_t sem_env ;
} sem_context_t ;
/* implement mutual exclusion using a semaphore */
static pt_t
sem_thr(env_t const env)
{
sem_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 100; c->i++) {
/* enter critical section */
pt_sem_acquire(c, &c->sem_env, &c->gc->sem_value) ;
assert(c->gc->owner == 0) ;
c->gc->owner = c->id ;
pt_yield(c) ;
assert(c->gc->owner == c->id) ;
c->gc->owner = 0 ;
pt_sem_release(&c->sem_env, &c->gc->sem_value) ;
pt_yield(c) ;
}
free(c) ;
return PT_DONE ;
}
static void
test_sem(void)
{
protothread_t const pt = protothread_create() ;
sem_global_context_t * gc = malloc(sizeof(*gc)) ;
int i ;
gc->owner = 0 ; /* invalid ID (no one in critical section) */
/* for mutual exclusion, init the semaphore count to 1 */
gc->sem_value = 1 ;
for (i = 0; i < 100; i++) {
sem_context_t * const c = malloc(sizeof(*c)) ;
c->gc = gc ;
c->id = i+1 ;
pt_create(pt, &c->pt_thread, sem_thr, c) ;
}
/* as long as there is work to do */
while (protothread_run(pt)) ;
free(gc) ;
protothread_free(pt) ;
}
/******************************************************************************/
typedef struct lock_global_context_s {
pt_lock_t lock ;
unsigned int nthreads ;
} lock_global_context_t ;
/* static state is easier to debug */
static lock_global_context_t lock_gc ;
typedef struct lock_context_s {
lock_global_context_t *gc ;
pt_func_t pt_func ;
pt_thread_t pt_thread ;
int i ; /* loop index */
int yi ; /* yield loop index */
int id ; /* thread id (>= 1) */
pt_lock_env_t lock_env ;
} lock_context_t ;
#define LOCK_NTHREADS 10
static lock_context_t lock_r_tc[LOCK_NTHREADS] ;
static lock_context_t lock_w_tc[LOCK_NTHREADS] ;
static void
lock_trc(char const * str, int id) {
static int n ;
if (0) {
printf("%s%d ", str, id-1) ;
if (++n > 16) {
n = 0 ;
printf("\n") ;
}
}
}
static pt_t
read_thr(env_t const env)
{
lock_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 10000; c->i++) {
/* enter critical section */
pt_lock_acquire_read(c, &c->lock_env, &c->gc->lock) ;
lock_trc("rs", c->id) ;
for (c->yi = random() % 100; c->yi; c->yi--) {
assert(c->gc->lock.nwriters == 0) ;
assert(c->gc->lock.nreaders > 0) ;
pt_yield(c) ;
assert(c->gc->lock.nwriters == 0) ;
assert(c->gc->lock.nreaders > 0) ;
}
lock_trc("re", c->id) ;
pt_lock_release_read(&c->lock_env, &c->gc->lock) ;
for (c->yi = random() % 200; c->yi; c->yi--) {
pt_yield(c) ;
}
}
c->gc->nthreads -- ;
return PT_DONE ;
}
static pt_t
write_thr(env_t const env)
{
lock_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 1000; c->i++) {
/* enter critical section */
pt_lock_acquire_write(c, &c->lock_env, &c->gc->lock) ;
lock_trc("ws", c->id) ;
for (c->yi = rand() % 100; c->yi; c->yi--) {
assert(c->gc->lock.nreaders == 0) ;
assert(c->gc->lock.nwriters == 1) ;
pt_yield(c) ;
assert(c->gc->lock.nreaders == 0) ;
assert(c->gc->lock.nwriters == 1) ;
}
lock_trc("we", c->id) ;
pt_lock_release_write(&c->lock_env, &c->gc->lock) ;
for (c->yi = rand() % 100; c->yi; c->yi--) {
pt_yield(c) ;
}
}
c->gc->nthreads -- ;
return PT_DONE ;
}
/* This test does not verify fairness; that's hard to do */
static void
test_lock(void)
{
protothread_t const pt = protothread_create() ;
int i ;
srand(0) ;
pt_lock_init(&lock_gc.lock) ;
for (i = 0; i < LOCK_NTHREADS; i++) {
lock_context_t * c = &lock_r_tc[i] ;
c->gc = &lock_gc ;
c->id = i+1 ;
pt_create(pt, &c->pt_thread, read_thr, c) ;
lock_gc.nthreads ++ ;
}
for (i = 0; i < LOCK_NTHREADS; i++) {
lock_context_t * c = &lock_w_tc[i] ;
c->gc = &lock_gc ;
c->id = i+1 ;
pt_create(pt, &c->pt_thread, write_thr, c) ;
lock_gc.nthreads ++ ;
}
/* as long as there is work to do */
while (protothread_run(pt)) ;
assert(lock_gc.nthreads == 0) ;
protothread_free(pt) ;
}
#undef LOCK_NTHREADS
/******************************************************************************/
typedef struct func_pointer_context_s {
int i ;
pt_thread_t pt_thread ;
pt_func_t pt_func ;
struct level2_s {
pt_func_t pt_func ;
bool_t ran ;
} level2 ;
} func_pointer_context_t ;
static pt_t
func_pointer_level2(env_t const env)
{
struct level2_s * const c = env ;
pt_resume(c) ;
pt_yield(c) ;
c->ran = TRUE ;
return PT_DONE ;
}
static pt_t
func_pointer_thr(env_t const env)
{
func_pointer_context_t * const c = env ;
pt_f_t const func_ptr = func_pointer_level2 ;
/* pt_call() can take a function pointer */
pt_resume(c) ;
pt_call(c, func_ptr, &c->level2) ;
return PT_DONE ;
}
static void
test_func_pointer(void)
{
protothread_t const pt = protothread_create() ;
func_pointer_context_t c ;
pt_f_t const func_ptr = func_pointer_thr ;
/* pt_create() can take a function pointer */
c.level2.ran = FALSE ;
pt_create(pt, &c.pt_thread, func_ptr, &c) ;
protothread_run(pt) ;
assert(!c.level2.ran) ;
protothread_run(pt) ;
assert(c.level2.ran) ;
protothread_free(pt) ;
}
/******************************************************************************/
static bool_t ready ;
static void
set_ready(env_t env)
{
assert(env == &ready) ;
assert(!ready) ;
ready = TRUE ;
}
typedef struct ready_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
} ready_context_t ;
static pt_t
ready_thr(env_t const env)
{
ready_context_t * const c = env ;
pt_resume(c) ;
pt_wait(c, c) ;
pt_yield(c) ;
return PT_DONE ;
}
static void
test_ready(void)
{
protothread_t const pt = protothread_create() ;
protothread_set_ready_function(pt, set_ready, &ready) ;
bool_t more ; /* more work to do */
ready_context_t * const c = malloc(sizeof(*c)) ;
/* nothing to run */
more = protothread_run(pt) ;
assert(!more) ;
assert(!ready) ;
pt_create(pt, &c->pt_thread, ready_thr, c) ;
assert(ready) ;
ready = FALSE ;
/* advance thread to the wait */
more = protothread_run(pt) ;
assert(!more) ;
assert(!ready) ;
/* make the thread runnable */
pt_signal(pt, c) ;
assert(ready) ;
ready = FALSE ;
/* should advance the thread to the pt_yield() */
more = protothread_run(pt) ;
assert(more) ;
assert(!ready) ;
/* advance the thread to its exit, nothing ready to run */
more = protothread_run(pt) ;
assert(!more) ;
assert(!ready) ;
free(c) ;
protothread_free(pt) ;
}
/******************************************************************************/
typedef struct kill_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
} kill_context_t ;
static pt_t
kill_thr(env_t const env)
{
kill_context_t * const c = env ;
pt_resume(c) ;
pt_yield(c) ;
pt_wait(c, c) ;
while (1) {
pt_yield(c) ;
}
return PT_DONE ;
}
static void
test_kill(void)
{
protothread_t const pt = protothread_create() ;
kill_context_t * const c = calloc(2, sizeof(*c)) ;
bool_t more ;
/* Create the thread, kill it while it is in the run queue and make
* sure it didn't run.
*/
pt_create(pt, &c[0].pt_thread, kill_thr, &c[0]) ;
pt_kill(&c[0].pt_thread) ;
more = protothread_run(pt) ;
assert(!more) ;
/* Try to kill it one more time, just for giggles. This may not cause any
* apparent problems, but memory-checker tools like valgrind will flag
* any problems created here.
*/
assert(!pt_kill(&c[0].pt_thread)) ;
/* Create the thread, wait until it is in the wait queue, wake it,
* kill it and make sure it isn't scheduled any longer.
*
* This actually tests killing while in the run queue (thus the same
* test as above), but helps justify the following test.
*/
pt_create(pt, &c[0].pt_thread, kill_thr, &c[0]) ;
more = protothread_run(pt) ;
assert(more) ;
more = protothread_run(pt) ;
assert(!more) ;
pt_broadcast(pt, &c[0]) ;
more = protothread_run(pt) ;
assert(more) ;
assert(pt_kill(&c[0].pt_thread)) ;
more = protothread_run(pt) ;
assert(!more) ;
/* Create the thread, wait until it is in the wait queue, kill it,
* wake it and make sure it never scheduled again.
*/
pt_create(pt, &c[0].pt_thread, kill_thr, &c[0]) ;
more = protothread_run(pt) ;
assert(more) ;
more = protothread_run(pt) ;
assert(!more) ;
assert(pt_kill(&c[0].pt_thread)) ;
pt_broadcast(pt, &c[0]) ;
more = protothread_run(pt) ;
assert(!more) ;
/* Create two threads, delete them one way and then
* delete them the other way.
*/
pt_create(pt, &c[0].pt_thread, kill_thr, &c[0]) ;
pt_create(pt, &c[1].pt_thread, kill_thr, &c[1]) ;
assert(pt_kill(&c[0].pt_thread)) ;
assert(pt_kill(&c[1].pt_thread)) ;
more = protothread_run(pt) ;
assert(!more) ;
pt_create(pt, &c[0].pt_thread, kill_thr, &c[0]) ;
pt_create(pt, &c[1].pt_thread, kill_thr, &c[1]) ;
assert(pt_kill(&c[1].pt_thread)) ;
assert(pt_kill(&c[0].pt_thread)) ;
more = protothread_run(pt) ;
assert(!more) ;
free(c) ;
protothread_free(pt) ;
}
/******************************************************************************/
typedef struct reset_context_s {
pt_thread_t pt_thread ;
pt_func_t pt_func ;
unsigned i ;
} reset_context_t ;
static pt_t
reset_thr(env_t const env)
{
reset_context_t * const c = env ;
pt_resume(c) ;
for (c->i = 0; c->i < 5; c->i++) {
pt_yield(c) ;
}
return PT_DONE ;
}
static void
test_reset(void)
{
protothread_t const pt = protothread_create() ;
reset_context_t * const c = calloc(1, sizeof(*c)) ;
/* Start running the thread and then make sure we
* can really reset the thread location
*/
pt_create(pt, &c->pt_thread, reset_thr, c) ;
protothread_run(pt) ;
assert(c->i == 0) ;
protothread_run(pt) ;
assert(c->i == 1) ;
pt_reset(c) ;
protothread_run(pt) ;
assert(c->i == 0) ;
while (protothread_run(pt)) ;
free(c) ;
protothread_free(pt) ;
}
/******************************************************************************/
int
main(int argc, char **argv)
{
test_create_dynamic() ;
test_create_static() ;
test_thread_create() ;
test_yield() ;
test_wait() ;
test_broadcast() ;
test_pc() ;
test_pc_big() ;
test_recursive() ;
test_sem() ;
test_lock() ;
test_func_pointer() ;
test_ready() ;
test_kill() ;
test_reset() ;
return 0 ;
}