1 /********************************
2 * file: synchtest.c
3 * sync primitives unit test
4 ************************************************
5 * FILE: synchtest.c
6 *
7 * DESCRIPTION: netapi user space transport
8 * library test application -> synchronization primitives
9 *
10 * REVISION HISTORY: rev 0.0.1
11 *
12 * Copyright (c) Texas Instruments Incorporated 2010-2011
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 * its contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ******************************/
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include "netsync.h"
47 #include "netapi_util.h"
49 #if 0
50 /* timing */
51 static inline unsigned long timing_start(void)
52 {
53 volatile int vval;
54 //read clock
55 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(vval));
56 return vval;
57 }
58 static inline unsigned long timing_stop(void)
59 {
60 volatile int vval2;
61 //read clock
62 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(vval2));
63 return vval2;
64 }
65 #endif
66 #define timing_start netapi_timing_start
67 #define timing_stop netapi_timing_stop
69 int spot=0;
71 NETAPI_RWLOCK_T temp;
73 void test1()
74 {
75 int v1, v2;
76 netapi_rwlock_init(&temp);
77 v1 = timing_start();
78 netapi_rwlock_write_lock(&temp);
79 v2 = timing_stop();
80 printf("rwlock write locked, cycles= %d \n", v2-v1);
81 }
83 void test2()
84 {
85 int v1, v2;
87 v1 = timing_start();
88 netapi_rwlock_write_unlock(&temp);
89 v2 = timing_stop();
90 printf("rwlock write unlocked, cycles= %d \n",v2-v1);
91 }
92 void test3()
93 {
94 int v1,v2;
95 v1 = timing_start();
96 netapi_rwlock_read_lock(&temp);
97 v2 = timing_stop();
98 printf("rwlock read locked, cycles= %d \n", v2-v1);
99 }
100 void test4()
101 {
102 int v1,v2;
103 v1 = timing_start();
104 netapi_rwlock_read_unlock(&temp);
105 v2 = timing_stop();
106 printf("rwlock read_unlocked, cycles= %d\n", v2-v1);
107 }
109 main()
110 {
111 NETAPI_SPINLOCK_T val;
112 int i;
113 unsigned long v1,v2;
114 int val2;
115 for(i=0;i<10;i++) {
116 val=__sync_fetch_and_add(&spot, 1);
117 printf(" val = %d %d\n",val,spot);
119 //now we try the synch_lock_and_test
120 netapi_spinlock_init(&val);
121 v1 = timing_start();
122 netapi_spinlock_lock(&val);
123 v2 = timing_stop();
124 printf("locked, val= %d cycles=%d\n",val,v2-v1);
126 //try the lock
127 v1 = timing_start();
128 val2=netapi_spinlock_try_lock(&val);
129 v2 = timing_stop();
130 printf("try lock has returns %d, cycles=%d\n", val2, v2-v1);
132 //poll the lock
133 v1 = timing_start();
134 val2=netapi_spinlock_is_locked(&val);
135 v2 = timing_stop();
136 printf("is_locked has returns %d, cycles=%d\n", val2, v2-v1);
139 //unlock
140 v1 = timing_start();
141 netapi_spinlock_unlock(&val);
142 v2 = timing_stop();
143 printf("unlocked, val= %d cycles=%d\n", val,v2-v1);
145 /*-------now try rwlock--------*/
146 test1();
147 test2();
148 test3();
149 test3();
150 test4();
151 test4();
153 }
154 }