#include #include struct thr_state t0,t1,t2,t3,t4; int s0[512], s1[512], s2[512], s3[512], s4[512]; struct lock lck0, lck1; void worker(void *arg) { int numeric_id = (int) arg; int i; /* loop must be an even number of times so acquire/releases balance */ for (i = 0; i < 512; i++) { printf("worker %d, top of loop %d\n",numeric_id,i); if ((numeric_id & 1) == 0) { if ((i & 1) == 0) { lock_acquire(&lck0); printf("worker %d, acquired\n",numeric_id); thr_yield(); } else { thr_yield(); lock_release(&lck0); printf("worker %d, released\n",numeric_id); thr_yield(); } } else { if ((i & 1) == 0) { lock_acquire(&lck1); printf("worker %d, acquired\n",numeric_id); thr_yield(); } else { lock_release(&lck1); printf("worker %d, released\n",numeric_id); } } thr_yield(); } } void spawner(void *arg) { char *name = (char *) arg; int i; printf("I am %s, spawning other threads\n",name); thr_init(&t1,worker,1,s1,2048); thr_init(&t2,worker,2,s2,2048); thr_init(&t3,worker,3,s3,2048); thr_init(&t4,worker,4,s4,2048); for (i = 0; i < 512; i++) { lock_acquire(&lck0); thr_yield(); lock_acquire(&lck1); thr_yield(); } } void main(void) { lock_init(&lck0); lock_init(&lck1); thr_init(&t0,spawner,"spawner thread",s0,2048); thr_go(); printf("All done!\n"); }