This is the initial commit.
[keystone-rtos/netapi.git] / ti / runtime / netapi / test / synchtest.c
1 /********************************
2  * file: synchtest.c
3  * sync primitives unit test
4  ******************************/
5 #include "netsync.h"
6 #include "netapi_util.h"
8 #if 0
9 /* timing */
10 static inline unsigned long timing_start(void)
11 {
12         volatile int vval;
13         //read clock
14         asm volatile("mrc p15, 0, %0, c9, c13, 0" :  "=r"(vval));
15         return vval;
16 }
17 static inline unsigned long timing_stop(void)
18 {
19         volatile int vval2;
20         //read clock
21         asm volatile("mrc p15, 0, %0, c9, c13, 0" :  "=r"(vval2));
22         return vval2;
23 }
24 #endif
25 #define timing_start netapi_timing_start
26 #define timing_stop netapi_timing_stop
28 int spot=0;
30 NETAPI_RWLOCK_T  temp;
32 void test1()
33 {
34 int v1, v2;
35 netapi_rwlock_init(&temp);
36 v1 = timing_start();
37 netapi_rwlock_write_lock(&temp);
38 v2 = timing_stop();
39 printf("rwlock write locked, cycles= %d \n", v2-v1);
40 }
42 void test2()
43 {
44 int v1, v2;
46 v1 = timing_start();
47 netapi_rwlock_write_unlock(&temp);
48 v2 = timing_stop();
49 printf("rwlock write unlocked, cycles= %d \n",v2-v1);
50 }
51 void test3()
52 {
53 int v1,v2;
54 v1 = timing_start();
55 netapi_rwlock_read_lock(&temp);
56 v2 = timing_stop();
57 printf("rwlock read locked, cycles= %d \n", v2-v1);
58 }
59 void test4()
60 {
61 int v1,v2;
62 v1 = timing_start();
63 netapi_rwlock_read_unlock(&temp);
64 v2 = timing_stop();
65 printf("rwlock read_unlocked, cycles= %d\n", v2-v1);
66 }
68 main()
69 {
70 NETAPI_SPINLOCK_T val;
71 int i;
72 unsigned long v1,v2;
73 int val2;
74 for(i=0;i<10;i++) {
75 val=__sync_fetch_and_add(&spot, 1);
76 printf(" val = %d %d\n",val,spot);
78 //now we try the synch_lock_and_test
79 netapi_spinlock_init(&val);
80 v1 = timing_start();
81 netapi_spinlock_lock(&val);
82 v2 = timing_stop();
83 printf("locked, val= %d cycles=%d\n",val,v2-v1);
85 //try the lock
86 v1 = timing_start();
87 val2=netapi_spinlock_try_lock(&val);
88 v2 = timing_stop();
89 printf("try lock has returns %d, cycles=%d\n", val2, v2-v1);
91 //poll the lock 
92 v1 = timing_start();
93 val2=netapi_spinlock_is_locked(&val);
94 v2 = timing_stop();
95 printf("is_locked has returns %d, cycles=%d\n", val2, v2-v1);
98 //unlock
99 v1 = timing_start();
100 netapi_spinlock_unlock(&val);
101 v2 = timing_stop();
102 printf("unlocked, val= %d cycles=%d\n", val,v2-v1);
104 /*-------now try rwlock--------*/
105 test1();
106 test2();
107 test3();
108 test3();
109 test4();
110 test4();