[Kernelloader-cvs] linux/testprograms/condtest Makefile, NONE, 1.1 condtest.c, NONE, 1.1
Run Linux on the Playstation 2
Brought to you by:
kloader
|
From: Mega M. <kl...@us...> - 2014-08-21 19:27:50
|
Update of /cvsroot/kernelloader/linux/testprograms/condtest In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv9021 Added Files: Makefile condtest.c Log Message: Add test program for tetsing bug in pthread_cond_wait(). --- NEW FILE: Makefile --- PROGRAM = condtest include ../ps2cross.mk CPPFLAGS = -W -Wall -Werror-implicit-function-declaration MODS = condtest OBJS = $(addsuffix .o,$(MODS)) LDLIBS = -lpthread all: $(PROGRAM) $(PROGRAM): $(OBJS) clean: rm -f $(PROGRAM) $(OBJS) --- NEW FILE: condtest.c --- #include <stdio.h> #include <pthread.h> #include <assert.h> #include <unistd.h> pthread_cond_t cond; pthread_mutex_t mutex; pthread_t cthread; pthread_t pthread; volatile int pstate = 0; volatile int cstate = 0; volatile unsigned int counter; volatile unsigned int consumed; void *producer_thread(void *arg) { int rv; (void) arg; pstate = __LINE__; while(1) { pstate = __LINE__; rv = pthread_mutex_lock(&mutex); pstate = __LINE__; assert(rv == 0); pstate = __LINE__; if (consumed == counter) { counter++; } pstate = __LINE__; rv = pthread_cond_signal(&cond); pstate = __LINE__; assert(rv == 0); pstate = __LINE__; rv = pthread_mutex_unlock(&mutex); pstate = __LINE__; assert(rv == 0); } pstate = __LINE__; return NULL; } void *consumer_thread(void *arg) { int rv; (void) arg; cstate = __LINE__; rv = pthread_mutex_lock(&mutex); cstate = __LINE__; assert(rv == 0); while(1) { cstate = __LINE__; rv = pthread_cond_wait(&cond, &mutex); cstate = __LINE__; assert(rv == 0); assert((consumed + 1) == counter); cstate = __LINE__; printf("counter %u\n", counter); cstate = __LINE__; consumed = counter; cstate = __LINE__; } cstate = __LINE__; rv = pthread_mutex_unlock(&mutex); assert(rv == 0); cstate = __LINE__; return NULL; } /* Test a bug when pthread_cond_wait() is not woken. */ void cond_test(void) { int rv; unsigned int prev_counter; int c; counter = 0; rv = pthread_mutex_init(&mutex, NULL); assert(rv == 0); rv = pthread_cond_init(&cond, NULL); assert(rv == 0); rv = pthread_create(&cthread, NULL, consumer_thread, NULL); assert(rv == 0); rv = pthread_create(&pthread, NULL, producer_thread, NULL); assert(rv == 0); prev_counter = counter; c = 0; while (1) { if (counter == prev_counter) { c++; if ((c & 255) == 255) { printf("Error detected: pstate line %u cstate line %u\n", pstate, cstate); } } else { c = 0; } prev_counter = counter; usleep(10000); } rv = pthread_join(pthread, NULL); assert(rv == 0); rv = pthread_join(cthread, NULL); assert(rv == 0); rv = pthread_mutex_destroy(&mutex); assert(rv == 0); rv = pthread_cond_destroy(&cond); assert(rv == 0); } int main(void) { printf("pthread_cond_wait() test program.\n"); cond_test(); return 0; } |