Source

stmgc / c4 / demo_random.c

Full commit
  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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>

#include "stmgc.h"
#include "stmimpl.h"
#include "fprintcolor.h"

extern revision_t get_private_rev_num(void);


#define NUMTHREADS 4
#define STEPS_PER_THREAD 5000
#define THREAD_STARTS 100 // how many restarts of threads
#define NUMROOTS 10 // per thread
#define PREBUILT 3 // per thread
#define MAXROOTS 1000
#define SHARED_ROOTS 5 // shared by threads
#define DO_MAJOR_COLLECTS 1
#define MAX_PUBLIC_INTS 5



// SUPPORT
#define GCTID_STRUCT_NODE     123
#define GCTID_WEAKREF         122

struct node;
typedef struct node * nodeptr;
struct weak_node {
    struct stm_object_s hdr;
    nodeptr node;
};
typedef struct weak_node * weaknodeptr;
#define WEAKNODE_SIZE  sizeof(struct weak_node)

struct node {
    struct stm_object_s hdr;
    long value;
    revision_t id;
    revision_t hash;
    nodeptr next;
    weaknodeptr weakref;
};



size_t stmcb_size(gcptr ob)
{
    if (stm_get_tid(ob) == GCTID_STRUCT_NODE)
        return sizeof(struct node);
    else if (stm_get_tid(ob) == GCTID_WEAKREF)
        return WEAKNODE_SIZE;
    assert(0);
}

void stmcb_trace(gcptr ob, void visit(gcptr *))
{
    nodeptr n;
    if (stm_get_tid(ob) == GCTID_WEAKREF)
        return;
    assert(stm_get_tid(ob) == GCTID_STRUCT_NODE);
    n = (nodeptr)ob;
    visit((gcptr *)&n->next);
    visit((gcptr *)&n->weakref);
}


// global and per-thread-data
time_t default_seed;
gcptr shared_roots[SHARED_ROOTS];

struct thread_data {
    unsigned int thread_seed;
    gcptr roots[MAXROOTS];
    gcptr roots_outside_perform[MAXROOTS];
    int num_roots;
    int num_roots_outside_perform;
    int steps_left;
    int interruptible;
    int atomic;
    char to_clear_on_abort[20];
    intptr_t public_ints[MAX_PUBLIC_INTS];
    int num_public_ints;
};
__thread struct thread_data td;


// helper functions
int classify(gcptr p);
void check(gcptr p);
int in_nursery(gcptr obj);
static int is_private(gcptr P)
{
  return (P->h_revision == stm_private_rev_num) ||
    (P->h_tid & GCFLAG_PRIVATE_FROM_PROTECTED);
}

static void inc_atomic()
{
    assert(td.interruptible);
    assert(stm_atomic(0) == td.atomic);
    td.atomic++;
    stm_atomic(1);
    assert(stm_atomic(0) == td.atomic);
}

static void dec_atomic()
{
    assert(td.interruptible);
    assert(stm_atomic(0) == td.atomic);
    td.atomic--;
    stm_atomic(-1);
    assert(stm_atomic(0) == td.atomic);
}

int get_rand(int max)
{
    return (int)(rand_r(&td.thread_seed) % (unsigned int)max);
}

gcptr get_random_root()
{
    int num = get_rand(td.num_roots + 1);
    if (num == 0)
        return stm_thread_local_obj;
    else
        return td.roots[num - 1];
}

gcptr get_random_shared_root()
{
    int num = get_rand(SHARED_ROOTS);
    return shared_roots[num];
}

void copy_roots(gcptr *from, gcptr *to, int num)
{
    int i;
    for (i = 0; i < num; i++)
        *(to++) = *(from++);
}

gcptr allocate_old(size_t size, int tid)
{
    gcptr p = stmgcpage_malloc(size);
    memset(p, 0, size);
    p->h_tid = GCFLAG_OLD | GCFLAG_WRITE_BARRIER | tid;
    p->h_revision = -INT_MAX;
    return p;
}

gcptr allocate_pseudoprebuilt(size_t size, int tid)
{
    gcptr x = calloc(1, size);
    x->h_tid = PREBUILT_FLAGS | tid;
    x->h_revision = PREBUILT_REVISION;
    return x;
}

gcptr allocate_pseudoprebuilt_with_hash(size_t size, int tid,
                                        revision_t hash)
{
    gcptr x = allocate_pseudoprebuilt(size, tid);
    x->h_original = hash;
    return x;
}

void push_roots()
{
    int i;
    for (i = 0; i < td.num_roots; i++) {
        check(td.roots[i]);
        if (td.roots[i])
            stm_push_root(td.roots[i]);
    }
}

void pop_roots()
{
    int i;
    for (i = td.num_roots - 1; i >= 0; i--) {
        if (td.roots[i])
            td.roots[i] = stm_pop_root();
        check(td.roots[i]);
    }
}

void del_root(int idx)
{
    int i;
    for (i = idx; i < td.num_roots - 1; i++)
        td.roots[i] = td.roots[i + 1];
}

nodeptr allocate_node()
{
    nodeptr r;
    push_roots();
    r = (nodeptr)stm_allocate(sizeof(struct node), GCTID_STRUCT_NODE);
    pop_roots();
    return r;
}


weaknodeptr allocate_weaknodeptr(nodeptr to)
{
    weaknodeptr w;
    push_roots();
    w = (weaknodeptr)stm_weakref_allocate(WEAKNODE_SIZE, GCTID_WEAKREF,
                                          (gcptr)to);
    pop_roots();
    return w;
}

void set_weakref(nodeptr n, nodeptr to)
{
    stm_push_root((gcptr)n);
    weaknodeptr w = allocate_weaknodeptr(to);
    n = (nodeptr)stm_pop_root();
    n = (nodeptr)stm_write_barrier((gcptr)n);
    n->weakref = w;
    dprintf(("set_weakref %p -> %p -> %p\n", n, w, to));
}

int is_shared_prebuilt(gcptr p)
{
    int i;
    for (i = 0; i < SHARED_ROOTS; i++)
        if (shared_roots[i] == p)
            return 1;
    return 0;
}

#ifdef _GC_DEBUG
int is_free_old(gcptr p)
{
    fprintf(stdout, "\n=== check ===\n");
    return (!_stm_can_access_memory((char*)p))
        || (p->h_tid == DEBUG_WORD(0xDD));
}
#endif

void check_not_free(gcptr p)
{
    assert(p != NULL);
    assert((p->h_tid & 0xFFFF) == GCTID_STRUCT_NODE);
    if (is_shared_prebuilt(p))
        assert(p->h_tid & GCFLAG_PREBUILT_ORIGINAL);
}

void check(gcptr p)
{
    if (p != NULL) {
        check_not_free(p);
        classify(p); // additional asserts
        if (p->h_original && !(p->h_tid & GCFLAG_PREBUILT_ORIGINAL)) {
            // must point to valid old object
            gcptr id = (gcptr)p->h_original;
            assert(!in_nursery(id));
#ifdef _GC_DEBUG
            if (!is_shared_prebuilt(id) && !(id->h_tid & GCFLAG_PREBUILT))
                assert(!is_free_old(id));
#endif
        }
    }
}

void check_public_ints()
{
    int i;
    for (i = 0; i < td.num_public_ints; i++) {
        intptr_t ip = td.public_ints[i];
        gcptr obj = (gcptr)ip;
        assert(obj->h_tid & GCFLAG_PUBLIC);
        assert((obj->h_tid & GCFLAG_SMALLSTUB)
               || (obj->h_original == 0 
                   || obj->h_tid & GCFLAG_PREBUILT_ORIGINAL));
        check(obj);
        if (obj->h_revision & 2)
            check((gcptr)(obj->h_revision - 2));
        else if ((obj->h_revision & 3) == 0)
            check((gcptr)(obj->h_revision));
    }
}

void add_as_public_int(gcptr p)
{
    if (!p || td.num_public_ints >= MAX_PUBLIC_INTS)
        return;

    push_roots();
    intptr_t ip = stm_allocate_public_integer_address(p);
    pop_roots();
    td.public_ints[td.num_public_ints++] = ip;
}

void pop_public_int()
{
    if (td.num_public_ints == 0)
        return;

    stm_unregister_integer_address(td.public_ints[--td.num_public_ints]);
}

gcptr read_barrier(gcptr p)
{
    gcptr r = p;
    if (p != NULL) {
        check(p);
        r = stm_read_barrier(p);
        check(r);
    }
    return r;
}

gcptr write_barrier(gcptr p)
{
    gcptr w = p;
    if (p != NULL) {
        check(p);
        w = stm_write_barrier(p);
        check(w);
        assert(is_private(w));
    }
    return w;
}

int in_nursery(gcptr obj)
{
    struct tx_descriptor *d = thread_descriptor;
    int result1 = (d->nursery_base <= (char*)obj &&
                   ((char*)obj) < d->nursery_end);
    if (obj->h_tid & GCFLAG_OLD) {
        assert(result1 == 0);
    }
    else {
        /* this assert() also fails if "obj" is in another nursery than
           the one of the current thread.  This is ok, because we
           should not see such pointers. */
        assert(result1 == 1);
    }
    return result1;
}

static const int C_PRIVATE_FROM_PROTECTED = 1;
static const int C_PRIVATE                = 2;
static const int C_STUB                   = 3;
static const int C_PUBLIC                 = 4;
static const int C_BACKUP                 = 5;
static const int C_PROTECTED              = 6;
int classify(gcptr p)
{
    int priv_from_prot = (p->h_tid & GCFLAG_PRIVATE_FROM_PROTECTED) != 0;
    int private_other = p->h_revision == get_private_rev_num();
    int public = (p->h_tid & GCFLAG_PUBLIC) != 0;
    int backup = (p->h_tid & GCFLAG_BACKUP_COPY) != 0;
    int stub = (p->h_tid & GCFLAG_STUB) != 0;
    assert(priv_from_prot + private_other + public + backup <= 1);
    assert(public || !stub);
    
    if (priv_from_prot)
        return C_PRIVATE_FROM_PROTECTED;
    if (private_other)
        return C_PRIVATE;
    if (public) {
        if (stub) {
            assert(!in_nursery(p));
        }
        else {
            if (in_nursery(p)) {
                assert(p->h_tid & GCFLAG_MOVED);
                assert(!(p->h_revision & 1));
            }
            return C_PUBLIC;
        }
    }
    if (backup)
        return C_BACKUP;
    return C_PROTECTED;
}




// THREAD TESTER
int interruptible_callback(gcptr arg1, int retry_counter);
int run_me();
void transaction_break();

void setup_thread()
{
    int i;
    memset(&td, 0, sizeof(struct thread_data));

    td.thread_seed = default_seed++;
    td.steps_left = STEPS_PER_THREAD;
    td.interruptible = 0;
    td.atomic = 0;

    stm_clear_on_abort(&td.to_clear_on_abort, sizeof(td.to_clear_on_abort));
    td.to_clear_on_abort[0] = 1;
    td.to_clear_on_abort[19] = 1;

    td.num_roots = PREBUILT + NUMROOTS;
    for (i = 0; i < PREBUILT; i++) {
        if (i % 3 == 0) {
            td.roots[i] = allocate_pseudoprebuilt_with_hash(
                                          sizeof(struct node), 
                                          GCTID_STRUCT_NODE,
                                          i);
            ((nodeptr)td.roots[i])->hash = i;
        }
        else {
            td.roots[i] = allocate_pseudoprebuilt(sizeof(struct node), 
                                                  GCTID_STRUCT_NODE);
        }
    }
    for (i = PREBUILT; i < PREBUILT + NUMROOTS; i++) {
        td.roots[i] = (gcptr)allocate_node();
    }

    if (td.thread_seed % 3 == 0) {
        stm_thread_local_obj = (gcptr)allocate_node();
    }
    else if (td.thread_seed % 3 == 1) {
        stm_thread_local_obj = allocate_pseudoprebuilt_with_hash
            (
             sizeof(struct node), GCTID_STRUCT_NODE, PREBUILT);
        ((nodeptr)stm_thread_local_obj)->hash = PREBUILT;
    } 
    else {
        stm_thread_local_obj = allocate_pseudoprebuilt
            (sizeof(struct node), GCTID_STRUCT_NODE);
    }

}

gcptr rare_events(gcptr p, gcptr _r, gcptr _sr)
{
    check_public_ints();
    int k = get_rand(100);
    if (k < 10) {
        push_roots();
        stm_push_root(p);
        stm_become_inevitable("fun");
        p = stm_pop_root();
        pop_roots();
    }
    else if (k < 40) {
        push_roots();
        stmgc_minor_collect();
        pop_roots();
        p = NULL;
    }
    else if (k < 50) {
        add_as_public_int(p);
        p = NULL;
    }
    else if (k < 60) {
        pop_public_int();
        p = NULL;
    }
    else if (k < 61 && DO_MAJOR_COLLECTS) {
        fprintf(stdout, "major collect\n");
        push_roots();
        stmgcpage_possibly_major_collect(1);
        pop_roots();
        p = NULL;
    }
    return p;
}

gcptr simple_events(gcptr p, gcptr _r, gcptr _sr)
{
    nodeptr w_r;
    int k = get_rand(11);
    int num = get_rand(td.num_roots);
    switch (k) {
    case 0: // remove a root
        if (num > 0)
            del_root(num);
        break;
    case 1: // add 'p' to roots
        if (p && td.num_roots < MAXROOTS)
            td.roots[td.num_roots++] = p;
        break;
    case 2: // set 'p' to point to a root
        if (_r)
            p = _r;
        break;
    case 3: // allocate fresh 'p'
        p = (gcptr)allocate_node();
        break;
    case 4:  // read and validate 'p'
        p = read_barrier(p);
        break;
    case 5: // only do a stm_write_barrier
        p = write_barrier(p);
        break;
    case 6: // follow p->next
        if (p)
            p = (gcptr)(((nodeptr)read_barrier(p))->next);
        break;
    case 7: // set 'p' as *next in one of the roots
        check(_r);
        w_r = (nodeptr)write_barrier(_r);
        check((gcptr)w_r);
        check(p);
        w_r->next = (struct node*)p;
        break;
    case 8:
        if (td.interruptible) {
            inc_atomic();
        }
        break;
    case 9:
    case 10:
        /* more likely to be less atomic */
        if (td.atomic) {
            dec_atomic();
        }
        break;
    }
    return p;
}

gcptr weakref_events(gcptr p, gcptr _r, gcptr _sr)
{
    nodeptr t;
    weaknodeptr w, ww;
    gcptr ptrs[] = {_r, _sr};
    
    int i = get_rand(2);
    int k = get_rand(3);
    switch (k) {
    case 0: // check weakref
        t = (nodeptr)read_barrier(ptrs[i]);
        w = t->weakref;
        if(w) {
            ww = (weaknodeptr)stm_read_barrier((gcptr)w);
            assert(stm_get_tid((gcptr)ww) == GCTID_WEAKREF);
            if (ww->node) {
                check((gcptr)ww->node);
            }
            else {
                t->weakref = NULL;
            }
        }
        p = NULL;
        break;
    case 1: // set weakref to something
        if (p)
            set_weakref((nodeptr)_r, (nodeptr)p);
        else
            set_weakref((nodeptr)_r, (nodeptr)get_random_root());
        p = NULL;
        break;
    case 2: // set weakref on shared roots
        set_weakref((nodeptr)_sr, (nodeptr)get_random_shared_root());
        p = NULL;
        break;
    }
    return p;
}


gcptr shared_roots_events(gcptr p, gcptr _r, gcptr _sr)
{
    nodeptr w_sr;

    int k = get_rand(3);
    switch (k) {
    case 0: // read_barrier on shared root
        read_barrier(_sr);
        break;
    case 1: // write_barrier on shared root
        write_barrier(_sr);
        break;
    case 2:
        w_sr = (nodeptr)write_barrier(_sr);
        w_sr->next = (nodeptr)get_random_shared_root();
        break;
    }
    return p;
}

gcptr id_hash_events(gcptr p, gcptr _r, gcptr _sr)
{
    nodeptr w_t;
    int k = get_rand(4);
    gcptr _t = NULL;

    switch (k) {
    case 0: /* test stm_id on (non-)shared roots */
        _t = _r;
    case 1:
        if (!_t)
            _t = _sr;
        w_t = (nodeptr)read_barrier(_t);
        if (w_t->id) {
            assert(w_t->id == stm_id((gcptr)w_t));
            assert(w_t->id == stm_id((gcptr)_t));
        }
        else {
            w_t = (nodeptr)write_barrier(_t);
            w_t->id = stm_id((gcptr)w_t);
            assert(w_t->id == stm_id((gcptr)_t));
        }
        break;
    case 2: /* test stm_hash on (non-)shared roots */
        _t = _r;
    case 3:
        if (!_t)
            _t = _sr;
        w_t = (nodeptr)read_barrier(_t);
        if (w_t->hash) {
            assert(w_t->hash == stm_hash((gcptr)w_t));
            assert(w_t->hash == stm_hash((gcptr)_t));
        }
        else {
            w_t = (nodeptr)write_barrier(_t);
            w_t->hash = stm_hash((gcptr)w_t);
            assert(w_t->hash == stm_hash((gcptr)_t));
        }
        if (w_t->hash >= 0 && (w_t->hash <= PREBUILT ||
                               w_t->hash < SHARED_ROOTS)) {
            // should be with predefined hash
            assert (stm_id((gcptr)w_t) != stm_hash((gcptr)w_t));
        }
        break;
    }
    return p;
}



gcptr do_step(gcptr p)
{
    gcptr _r, _sr;
    int k;

    _r = get_random_root();
    _sr = get_random_shared_root();

    k = get_rand(11);
    check(p);
    assert(*thread_descriptor->active_ref);

    if (k < 3)
        p = simple_events(p, _r, _sr);
    else if (k < 5)
        p = shared_roots_events(p, _r, _sr);
    else if (k < 7)
        p = id_hash_events(p, _r, _sr);
    else if (k < 8)
        p = rare_events(p, _r, _sr);
    else if (k < 10)
        p = weakref_events(p, _r, _sr);
    else if (get_rand(20) == 1) {
        // transaction break
        fprintf(stdout, "|");
        if (td.interruptible)
            return (gcptr)-1; // break current
        transaction_break();
        p = NULL;
    }
    return p;
}


void transaction_break()
{
    push_roots();
    td.interruptible = 1;
    
    copy_roots(td.roots, td.roots_outside_perform, td.num_roots);
    td.num_roots_outside_perform = td.num_roots;
    
    stm_perform_transaction(NULL, interruptible_callback);
    
    td.num_roots = td.num_roots_outside_perform;
    copy_roots(td.roots_outside_perform, td.roots, td.num_roots);
    
    td.interruptible = 0;
    pop_roots();
}


int interruptible_callback(gcptr arg1, int retry_counter)
{
    if (retry_counter) {
        assert(td.to_clear_on_abort[0] == 0);
        assert(td.to_clear_on_abort[19] == 0);
        td.to_clear_on_abort[0] = 1;
        td.to_clear_on_abort[19] = 1;
    }
    else {
        assert(td.to_clear_on_abort[0] == 1);
        assert(td.to_clear_on_abort[19] == 1);
    }

    td.num_roots = td.num_roots_outside_perform;
    // done & overwritten by the following pop_roots():
    // copy_roots(td.roots_outside_perform, td.roots, td.num_roots);
    td.atomic = 0; // may be set differently on abort
    // refresh td.roots:
    gcptr end_marker = stm_pop_root();
    assert(end_marker == END_MARKER_ON || end_marker == END_MARKER_OFF);
    arg1 = stm_pop_root();
    assert(arg1 == NULL);
    pop_roots();
    push_roots();
    stm_push_root(arg1);
    stm_push_root(end_marker);

    int p = run_me();

    if (p == -1) // maybe restart transaction
        return get_rand(3) != 1;

    return 0;
}

int run_me()
{
    gcptr p = NULL;

    while (td.steps_left-->0 || td.atomic) {
        if (td.steps_left % 8 == 0)
            fprintf(stdout, "#");

        p = do_step(p);

        if (p == (gcptr)-1) {
            if (td.atomic) {
                // can't break, well, we could return to perform_transaction
                // while being atomic. (TODO)
                // may be true when major gc requested:
                // assert(stm_should_break_transaction() == 0);
                assert(stm_atomic(0) == td.atomic);
                p = NULL;
            }
            else {
                assert(stm_atomic(0) == 0);
                return -1;
            }
        }
    }
    return 0;
}


static sem_t done;
static int thr_mynum = 0;

void *demo(void *arg)
{  
    int status;
    
    stm_initialize();
    setup_thread();
    
    thr_mynum++;   /* protected by being inevitable here */
    dprintf(("THREAD STARTING\n"));

    run_me();
    
    stm_finalize();
    
    status = sem_post(&done);
    assert(status == 0);
    return NULL;
}


void newthread(void*(*func)(void*), void *arg)
{
    pthread_t th;
    int status = pthread_create(&th, NULL, func, arg);
    if (status != 0)
        stm_fatalerror("newthread: pthread_create failure\n");
    pthread_detach(th);
    printf("started new thread\n");
}

#ifdef _GC_MEMPROTECT
extern int stm_use_mprotect;
#endif


int main(void)
{
    int i, status;
#ifdef _GC_MEMPROTECT
    stm_use_mprotect = 0;   /* uses too much memory */
#endif
    
    /* pick a random seed from the time in seconds.
       A bit pointless for now... because the interleaving of the
       threads is really random. */
    default_seed = time(NULL);
    printf("running with seed=%lld\n", (long long)default_seed);

    for (i = 0; i < SHARED_ROOTS; i++) {
        if (i % 3 == 0) {
            shared_roots[i] = allocate_pseudoprebuilt_with_hash(
                                          sizeof(struct node), 
                                          GCTID_STRUCT_NODE,
                                          i);
            ((nodeptr)shared_roots[i])->hash = i;
        }
        else {
            shared_roots[i] = allocate_pseudoprebuilt(sizeof(struct node), 
                                                      GCTID_STRUCT_NODE);
        }
    }    
    
    status = sem_init(&done, 0, 0);
    assert(status == 0);
    
    int thread_starts = NUMTHREADS * THREAD_STARTS;
    for (i = 0; i < NUMTHREADS; i++) {
        newthread(demo, NULL);
        thread_starts--;
    }
    
    for (i=0; i < NUMTHREADS * THREAD_STARTS; i++) {
        status = sem_wait(&done);
        assert(status == 0);
        printf("thread finished\n");
        if (thread_starts) {
            thread_starts--;
            newthread(demo, NULL);
        }
    }

    printf("Test OK!\n");
    return 0;
}