/* How expensive is * - Obtaining a read-only lock for the first obtainer. * - Obtaining it for the second one? * - The third one? */ #include #include #include #include float tdiff (struct timeval *start, struct timeval *end) { return 1e6*(end->tv_sec-start->tv_sec) +(end->tv_usec - start->tv_usec); } /* My own rwlock implementation. */ struct brwl { int mutex; int state; // 0 for unlocked, -1 for a writer, otherwise many readers }; static inline int xchg(volatile int *ptr, int x) { __asm__("xchgl %0,%1" :"=r" (x) :"m" (*(ptr)), "0" (x) :"memory"); return x; } static inline void sfence (void) { asm volatile ("sfence":::"memory"); } static inline void brwl_rlock (struct brwl *l) { while (xchg(&l->mutex, 1)) ; l->state++; #if 1 sfence(); l->mutex=0; #else xchg(&l->mutex, 0); #endif } enum {K=1000}; pthread_rwlock_t rwlocks[K]; struct brwl blocks[K]; int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { int j; int i; int r; struct timeval start, end; for (j=0; j<3; j++) { for (i=0; i