/******************************** * file: synchtest.c * sync primitives unit test ************************************************ * FILE: synchtest.c * * DESCRIPTION: netapi user space transport * library test application -> synchronization primitives * * REVISION HISTORY: rev 0.0.1 * * Copyright (c) Texas Instruments Incorporated 2010-2011 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************/ #include #include #include #include #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(); } }