void killerthr(void *vptr) { shared_data_t *data = (shared_data_t*)vptr; time_t finish = time(NULL) + 5; while (time(NULL) < finish) ta_yield(); data->stop = TRUE; }
void thread1(void *arg) { int *i = (int *)arg; *i += 7; fprintf(stderr, "begin t1: %d\n", *i); ta_yield(); *i += 7; fprintf(stderr, "end t1: %d\n", *i); }
void thread2(void *arg) { int *i = (int *)arg; *i -= 7; fprintf(stderr, "begin t2: %d\n", *i); ta_yield(); *i -= 7; fprintf(stderr, "end t2: %d\n", *i); }
void thread1(void *v) { fprintf(stderr, "thread1 started up\n"); ta_lock(&mutex); ta_yield(); while (value == 0) { fprintf(stderr, "thread1 going into cond_wait()\n"); ta_wait(&condv, &mutex); } fprintf(stderr, "thread1 emerged from cond_wait()\n"); value = 42; ta_yield(); ta_unlock(&mutex); fprintf(stderr, "thread1 exiting\n"); }
void killerthr(void *arg) { time_t now = time(NULL); time_t finish = now + DURATION; while (now < finish) { ta_yield(); now = time(NULL); } stop = 1; }
void killerthr(void *v) { shared_data_t *d = (shared_data_t*)v; time_t finish = time(NULL) + 5; while (time(NULL) < finish) { ta_yield(); } d->stop = TRUE; }
void producer(void *vptr) { shared_data_t *data = (shared_data_t*)vptr; int i = 0; while (!data->stop) { ta_yield(); ta_sem_wait(&(data->emptyBuffer)); // produce assert(data->buffer[i] == 0xFEEDFEED); ta_yield(); printf("producer filled slot %d\n", i); data->buffer[i] = 0xDEADBEEF; i = (i+1)%BUFFERSIZE; ta_sem_signal(&(data->fullBuffer)); } }
void thread2(void *v) { fprintf(stderr, "thread2 started up\n"); ta_lock(&mutex); ta_yield(); fprintf(stderr, "thread2 not updating value, signalling thread1\n"); ta_signal(&condv); ta_unlock(&mutex); ta_yield(); ta_lock(&mutex); ta_yield(); fprintf(stderr, "thread2 IS updating value, signalling thread1\n"); value = -1; ta_signal(&condv); ta_unlock(&mutex); fprintf(stderr, "thread2 exiting\n"); }
void producer(void *vptr) { shared_data_t *data = (shared_data_t*)vptr; while (!data->stop) { ta_yield(); ta_sem_wait(&(data->emptyBuffer)); // produce ta_sem_wait(&(data->pmutex)); ta_yield(); printf("producer filling slot %d\n", data->pindex); data->buffer[data->pindex] = pcount++; data->pindex = (data->pindex + 1) % BUFFERSIZE; ta_sem_signal(&(data->pmutex)); ta_sem_signal(&(data->fullBuffer)); } }
void consumer(void *vptr) { shared_data_t *data = (shared_data_t*)vptr; int j = 0; while (!data->stop) { ta_yield(); ta_sem_wait(&(data->fullBuffer)); // consume assert(data->buffer[j] == 0xDEADBEEF); data->buffer[j] = 0xFEEDFEED; ta_yield(); printf("consumer read slot %d\n", j); j = (j+1)%BUFFERSIZE; ta_sem_signal(&(data->emptyBuffer)); } }
void consumer(void *vptr) { shared_data_t *data = (shared_data_t*)vptr; while (!data->stop) { ta_yield(); ta_sem_wait(&(data->fullBuffer)); // consume ta_sem_wait(&(data->cmutex)); printf("consumer eating slot %d\n", data->cindex); ta_yield(); assert(data->buffer[data->cindex] == ccount); ccount++; data->cindex = (data->cindex + 1) % BUFFERSIZE; ta_sem_signal(&(data->cmutex)); ta_sem_signal(&(data->emptyBuffer)); } }
void reader(void *arg) { int tid = (int)arg; int val = 0; while (!stop) { ta_sem_wait(&writersem); ta_lock(&wmutex); int loc = readerloc; readerloc = (readerloc+1) % datalen; ta_unlock(&wmutex); val = data[loc]; ta_sem_post(&readersem); fprintf(stderr, "reader %d read location %d\n", tid, loc); if (random() % 2 == 0) ta_yield(); } }
void writer(void *arg) { fprintf(stderr,"in writer\n"); int tid = (int)arg; int val = 1000000; int writerloc = 0; while (!stop) { ta_sem_wait(&readersem); ta_lock(&rmutex); int loc = writerloc; writerloc = (writerloc+1) % datalen; ta_unlock(&rmutex); data[loc] = val++; ta_sem_post(&writersem); fprintf(stderr, "writer %d wrote location %d\n", tid, loc); if (random() % 2 == 0) ta_yield(); } }
void eat(int i) { fprintf(stderr, "Philosopher %d is eating\n", i); ta_yield(); }
void think(int i) { fprintf(stderr, "Philosopher %d is thinking\n", i); ta_yield(); }