GPLization banner introduced to *.[hc] files
[sip-router] / test / locking / locking_test.c
1 /* $Id$ */
2 /*
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38
39 #ifdef FLOCK
40 #include <sys/file.h>
41
42 static int lock_fd;
43 #endif
44
45 #ifdef POSIX_SEM
46 #include <semaphore.h>
47
48 static sem_t sem;
49 #endif
50
51 #ifdef PTHREAD_MUTEX
52 #include <pthread.h>
53 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54 #endif
55
56 #ifdef FAST_LOCK
57 #include "../../fastlock.h"
58 fl_lock_t lock;
59 #endif
60
61 #ifdef SYSV_SEM
62 #include <sys/ipc.h>
63 #include <sys/sem.h>
64
65
66 #if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)) || \
67         defined(__FreeBSD__)
68         /* union semun is defined by including <sys/sem.h> */
69 #else
70         /* according to X/OPEN we have to define it ourselves */
71         union semun {
72                 int val;                    /* value for SETVAL */
73                 struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
74                 unsigned short int *array;  /* array for GETALL, SETALL */
75                 struct seminfo *__buf;      /* buffer for IPC_INFO */
76         };
77 #endif
78
79 static int semid=-1;
80
81 #endif
82
83
84 #ifdef NO_LOCK
85         #define LOCK()
86         #define UNLOCK()
87 #elif defined SYSV_SEM
88         #define LOCK() \
89         {\
90                 struct sembuf sop; \
91                 sop.sem_num=0; \
92                 sop.sem_op=-1; /*down*/ \
93                 sop.sem_flg=0 /*SEM_UNDO*/; \
94                 semop(semid, &sop, 1); \
95         }
96
97         #define UNLOCK()        \
98         {\
99                 struct sembuf sop;\
100                 sop.sem_num=0;\
101                 sop.sem_op=1; /*up*/\
102                 sop.sem_flg=0 /*SEM_UNDO*/;\
103                 semop(semid, &sop, 1);\
104         }
105 #elif defined FLOCK
106
107         #define LOCK() \
108                 flock(lock_fd, LOCK_EX)
109         #define  UNLOCK() \
110                 flock(lock_fd, LOCK_UN)
111 #elif defined POSIX_SEM
112         #define LOCK() \
113                 sem_wait(&sem)
114         #define UNLOCK() \
115                 sem_post(&sem);
116 #elif defined PTHREAD_MUTEX
117         #define LOCK() \
118                 pthread_mutex_lock(&mutex)
119         #define UNLOCK() \
120                 pthread_mutex_unlock(&mutex)
121 #elif defined FAST_LOCK
122         #define LOCK() \
123                 get_lock(&lock)
124         #define UNLOCK() \
125                 release_lock(&lock)
126 #endif
127
128
129
130
131 static char *id="$Id$";
132 static char *version="locking_test 0.1-"
133 #ifdef NO_LOCK
134  "nolock"
135 #elif defined SYSV_SEM
136  "sysv_sem"
137 #elif defined FLOCK
138  "flock"
139 #elif defined POSIX_SEM
140  "posix_sem"
141 #elif defined PTHREAD_MUTEX
142  "pthread_mutext"
143 #elif defined FAST_LOCK
144  "fast_lock"
145 #endif
146 ;
147
148 static char* help_msg="\
149 Usage: locking_test -n address [-c count] [-v]\n\
150 Options:\n\
151     -c count      how many times to try lock/unlock \n\
152     -v            increase verbosity level\n\
153     -V            version number\n\
154     -h            this help message\n\
155 ";
156
157
158
159 int main (int argc, char** argv)
160 {
161         int c;
162         int r;
163         char *tmp;
164         
165         int count;
166         int verbose;
167         char *address;
168 #ifdef SYSV_SEM
169         union semun su;
170 #endif
171         
172         /* init */
173         count=0;
174         verbose=0;
175         address=0;
176
177
178
179         opterr=0;
180         while ((c=getopt(argc,argv, "c:vhV"))!=-1){
181                 switch(c){
182                         case 'v':
183                                 verbose++;
184                                 break;
185                         case 'c':
186                                 count=strtol(optarg, &tmp, 10);
187                                 if ((tmp==0)||(*tmp)){
188                                         fprintf(stderr, "bad count: -c %s\n", optarg);
189                                         goto error;
190                                 }
191                                 break;
192                         case 'V':
193                                 printf("version: %s\n", version);
194                                 printf("%s\n",id);
195                                 exit(0);
196                                 break;
197                         case 'h':
198                                 printf("version: %s\n", version);
199                                 printf("%s", help_msg);
200                                 exit(0);
201                                 break;
202                         case '?':
203                                 if (isprint(optopt))
204                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
205                                 else
206                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
207                                 goto error;
208                         case ':':
209                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
210                                                 optopt);
211                                 goto error;
212                                 break;
213                         default:
214                                         abort();
215                 }
216         }
217
218         
219         /* check if all the required params are present */
220         if(count==0){
221                 fprintf(stderr, "Missing count (-c number)\n");
222                 exit(-1);
223         }else if(count<0){
224                 fprintf(stderr, "Invalid count (-c %d)\n", count);
225                 exit(-1);
226         }
227
228
229 #ifdef SYSV_SEM
230         /*init*/
231         puts("Initializing SYS V semaphores\n");
232         semid=semget(IPC_PRIVATE,1,0700);
233         if(semid==-1){
234                 fprintf(stderr, "ERROR: could not init sempahore: %s\n",
235                                 strerror(errno));
236                 goto error;
237         }
238         /*set init value to 1 (mutex)*/
239         su.val=1;
240         if (semctl(semid, 0, SETVAL, su)==-1){
241                 fprintf(stderr, "ERROR: could not set initial sempahore value: %s\n",
242                                 strerror(errno));
243                 semctl(semid, 0, IPC_RMID, (union semun)0);
244                 goto error;
245         }
246 #elif defined FLOCK
247         puts("Initializing flock\n");
248         lock_fd=open("/dev/zero", O_RDONLY);
249         if (lock_fd==-1){
250                 fprintf(stderr, "ERROR: could not open file: %s\n", strerror(errno));
251                 goto error;
252         }
253 #elif defined POSIX_SEM
254         puts("Initializing sempahores\n");
255         if (sem_init(&sem, 0, 1)<0){
256                 fprintf(stderr, "ERROR: could not initialize sempahore: %s\n",
257                                 strerror(errno));
258                 goto error;
259         }
260 #elif defined PTHREAD_MUTEX
261         puts("Initializing mutex -already initialized (statically)\n");
262         /*pthread_mutext_init(&mutex, 0 );*/
263 #elif defined FAST_LOCK
264         puts("Initializing fast lock\n");
265         init_lock(lock);
266 #endif
267
268
269         /*  loop */
270         for (r=0; r<count; r++){
271                 LOCK();
272                 if ((verbose>1)&&(r%1000))  putchar('.');
273                 UNLOCK();
274         }
275
276         printf("%d loops\n", count);
277
278 #ifdef SYSV_SEM
279         semctl(semid, 0, IPC_RMID, (union semun)0);
280 #elif defined LIN_SEM
281         sem_destroy(&sem);
282 #endif
283
284         exit(0);
285
286 error:
287         exit(-1);
288 }