void CRWMutex::unlockRead() { #ifndef ANDROID pthread_rwlock_unlock(&mMutex); #else reader_unlock(&mMutex); #endif //ANDROID }
/* READER THREAD reads from shared memory and does some busy work */ void *Reader( void * num) { time_t now; int i; pid_t tid; tid = syscall(SYS_gettid); time(&now); /* grabs system time and stuff it into struct time_t now*/ if (DEBUG) fprintf(stderr,"create reader thread pid: %d tid: %d %s\n",getpid(), tid, ctime(&now)); fib(5); /* do some busy work */ /* reader thread simply reads three times */ for (i=1; i<= 3; i++){ fib(24); /* do some busy work */ int n_writers_waiting = semctl(semid, READER, GETZCNT); if(!n_writers_waiting) { reader_lock(semid); //if(!n_writers_waiting) //{ /* construct a string for the reader to spit out */ fib(30); char str[5]; char buf[80] = "reader "; strcat(buf,(char*)num); strcat(buf," reads: "); /* grab shared mem and display it */ sprintf(str,"%d",*shared); strcat(buf, str); strcat(buf, "\n"); write(1,buf,strlen(buf)); fsync(1); reader_unlock(semid); } } pthread_exit(0); }
/* Reader function reads from shared memory */ void *Reader( void * num) { int i; pid_t tid; tid = syscall(SYS_gettid); printf("create reader thread pid: %d tid: %d \n",getpid(), tid); /* should make loop random when you have multiple threads-fixed at 3 now */ for (i=1; i<= 3; i++){ int zsems = semctl(semid, WRITER, GETZCNT); if(zsems > 0){ reader_lock( semid ); fib(20); printf("reader #%s %s", (char *)num, mem_ptr); reader_unlock( semid ); } } pthread_exit(0); }