/******************************** * file: synchtest.c * sync primitives unit test ******************************/ #include "netsync.h" #include "netapi_util.h" #if 0 /* timing */ static inline unsigned long timing_start(void) { volatile int vval; //read clock asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(vval)); return vval; } static inline unsigned long timing_stop(void) { volatile int vval2; //read clock asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(vval2)); return vval2; } #endif #define timing_start netapi_timing_start #define timing_stop netapi_timing_stop int spot=0; NETAPI_RWLOCK_T temp; void test1() { int v1, v2; netapi_rwlock_init(&temp); v1 = timing_start(); netapi_rwlock_write_lock(&temp); v2 = timing_stop(); printf("rwlock write locked, cycles= %d \n", v2-v1); } void test2() { int v1, v2; v1 = timing_start(); netapi_rwlock_write_unlock(&temp); v2 = timing_stop(); printf("rwlock write unlocked, cycles= %d \n",v2-v1); } void test3() { int v1,v2; v1 = timing_start(); netapi_rwlock_read_lock(&temp); v2 = timing_stop(); printf("rwlock read locked, cycles= %d \n", v2-v1); } void test4() { int v1,v2; v1 = timing_start(); netapi_rwlock_read_unlock(&temp); v2 = timing_stop(); printf("rwlock read_unlocked, cycles= %d\n", v2-v1); } main() { NETAPI_SPINLOCK_T val; int i; unsigned long v1,v2; int val2; for(i=0;i<10;i++) { val=__sync_fetch_and_add(&spot, 1); printf(" val = %d %d\n",val,spot); //now we try the synch_lock_and_test netapi_spinlock_init(&val); v1 = timing_start(); netapi_spinlock_lock(&val); v2 = timing_stop(); printf("locked, val= %d cycles=%d\n",val,v2-v1); //try the lock v1 = timing_start(); val2=netapi_spinlock_try_lock(&val); v2 = timing_stop(); printf("try lock has returns %d, cycles=%d\n", val2, v2-v1); //poll the lock v1 = timing_start(); val2=netapi_spinlock_is_locked(&val); v2 = timing_stop(); printf("is_locked has returns %d, cycles=%d\n", val2, v2-v1); //unlock v1 = timing_start(); netapi_spinlock_unlock(&val); v2 = timing_stop(); printf("unlocked, val= %d cycles=%d\n", val,v2-v1); /*-------now try rwlock--------*/ test1(); test2(); test3(); test3(); test4(); test4(); } }