locking_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * [email protected]
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #ifdef FLOCK
  36. #include <sys/file.h>
  37. static int lock_fd;
  38. #endif
  39. #ifdef POSIX_SEM
  40. #include <semaphore.h>
  41. static sem_t sem;
  42. #endif
  43. #ifdef PTHREAD_MUTEX
  44. #include <pthread.h>
  45. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  46. #endif
  47. #ifdef FAST_LOCK
  48. #include "../../fastlock.h"
  49. fl_lock_t lock;
  50. #endif
  51. #ifdef SYSV_SEM
  52. #include <sys/ipc.h>
  53. #include <sys/sem.h>
  54. #if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)) || \
  55. defined(__FreeBSD__)
  56. /* union semun is defined by including <sys/sem.h> */
  57. #else
  58. /* according to X/OPEN we have to define it ourselves */
  59. union semun {
  60. int val; /* value for SETVAL */
  61. struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
  62. unsigned short int *array; /* array for GETALL, SETALL */
  63. struct seminfo *__buf; /* buffer for IPC_INFO */
  64. };
  65. #endif
  66. static int semid=-1;
  67. #endif
  68. #ifdef NO_LOCK
  69. #define LOCK()
  70. #define UNLOCK()
  71. #elif defined SYSV_SEM
  72. #define LOCK() \
  73. {\
  74. struct sembuf sop; \
  75. sop.sem_num=0; \
  76. sop.sem_op=-1; /*down*/ \
  77. sop.sem_flg=0 /*SEM_UNDO*/; \
  78. semop(semid, &sop, 1); \
  79. }
  80. #define UNLOCK() \
  81. {\
  82. struct sembuf sop;\
  83. sop.sem_num=0;\
  84. sop.sem_op=1; /*up*/\
  85. sop.sem_flg=0 /*SEM_UNDO*/;\
  86. semop(semid, &sop, 1);\
  87. }
  88. #elif defined FLOCK
  89. #define LOCK() \
  90. flock(lock_fd, LOCK_EX)
  91. #define UNLOCK() \
  92. flock(lock_fd, LOCK_UN)
  93. #elif defined POSIX_SEM
  94. #define LOCK() \
  95. sem_wait(&sem)
  96. #define UNLOCK() \
  97. sem_post(&sem);
  98. #elif defined PTHREAD_MUTEX
  99. #define LOCK() \
  100. pthread_mutex_lock(&mutex)
  101. #define UNLOCK() \
  102. pthread_mutex_unlock(&mutex)
  103. #elif defined FAST_LOCK
  104. #define LOCK() \
  105. get_lock(&lock)
  106. #define UNLOCK() \
  107. release_lock(&lock)
  108. #endif
  109. static char *id="$Id$";
  110. static char *version="locking_test 0.1-"
  111. #ifdef NO_LOCK
  112. "nolock"
  113. #elif defined SYSV_SEM
  114. "sysv_sem"
  115. #elif defined FLOCK
  116. "flock"
  117. #elif defined POSIX_SEM
  118. "posix_sem"
  119. #elif defined PTHREAD_MUTEX
  120. "pthread_mutext"
  121. #elif defined FAST_LOCK
  122. "fast_lock"
  123. #endif
  124. ;
  125. static char* help_msg="\
  126. Usage: locking_test -n address [-c count] [-v]\n\
  127. Options:\n\
  128. -c count how many times to try lock/unlock \n\
  129. -v increase verbosity level\n\
  130. -V version number\n\
  131. -h this help message\n\
  132. ";
  133. int main (int argc, char** argv)
  134. {
  135. int c;
  136. int r;
  137. char *tmp;
  138. int count;
  139. int verbose;
  140. char *address;
  141. #ifdef SYSV_SEM
  142. union semun su;
  143. #endif
  144. /* init */
  145. count=0;
  146. verbose=0;
  147. address=0;
  148. opterr=0;
  149. while ((c=getopt(argc,argv, "c:vhV"))!=-1){
  150. switch(c){
  151. case 'v':
  152. verbose++;
  153. break;
  154. case 'c':
  155. count=strtol(optarg, &tmp, 10);
  156. if ((tmp==0)||(*tmp)){
  157. fprintf(stderr, "bad count: -c %s\n", optarg);
  158. goto error;
  159. }
  160. break;
  161. case 'V':
  162. printf("version: %s\n", version);
  163. printf("%s\n",id);
  164. exit(0);
  165. break;
  166. case 'h':
  167. printf("version: %s\n", version);
  168. printf("%s", help_msg);
  169. exit(0);
  170. break;
  171. case '?':
  172. if (isprint(optopt))
  173. fprintf(stderr, "Unknown option `-%c´\n", optopt);
  174. else
  175. fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
  176. goto error;
  177. case ':':
  178. fprintf(stderr, "Option `-%c´ requires an argument.\n",
  179. optopt);
  180. goto error;
  181. break;
  182. default:
  183. abort();
  184. }
  185. }
  186. /* check if all the required params are present */
  187. if(count==0){
  188. fprintf(stderr, "Missing count (-c number)\n");
  189. exit(-1);
  190. }else if(count<0){
  191. fprintf(stderr, "Invalid count (-c %d)\n", count);
  192. exit(-1);
  193. }
  194. #ifdef SYSV_SEM
  195. /*init*/
  196. puts("Initializing SYS V semaphores\n");
  197. semid=semget(IPC_PRIVATE,1,0700);
  198. if(semid==-1){
  199. fprintf(stderr, "ERROR: could not init sempahore: %s\n",
  200. strerror(errno));
  201. goto error;
  202. }
  203. /*set init value to 1 (mutex)*/
  204. su.val=1;
  205. if (semctl(semid, 0, SETVAL, su)==-1){
  206. fprintf(stderr, "ERROR: could not set initial sempahore value: %s\n",
  207. strerror(errno));
  208. semctl(semid, 0, IPC_RMID, (union semun)0);
  209. goto error;
  210. }
  211. #elif defined FLOCK
  212. puts("Initializing flock\n");
  213. lock_fd=open("/dev/zero", O_RDONLY);
  214. if (lock_fd==-1){
  215. fprintf(stderr, "ERROR: could not open file: %s\n", strerror(errno));
  216. goto error;
  217. }
  218. #elif defined POSIX_SEM
  219. puts("Initializing sempahores\n");
  220. if (sem_init(&sem, 0, 1)<0){
  221. fprintf(stderr, "ERROR: could not initialize sempahore: %s\n",
  222. strerror(errno));
  223. goto error;
  224. }
  225. #elif defined PTHREAD_MUTEX
  226. puts("Initializing mutex -already initialized (statically)\n");
  227. /*pthread_mutext_init(&mutex, 0 );*/
  228. #elif defined FAST_LOCK
  229. puts("Initializing fast lock\n");
  230. init_lock(lock);
  231. #endif
  232. /* loop */
  233. for (r=0; r<count; r++){
  234. LOCK();
  235. if ((verbose>1)&&(r%1000)) putchar('.');
  236. UNLOCK();
  237. }
  238. printf("%d loops\n", count);
  239. #ifdef SYSV_SEM
  240. semctl(semid, 0, IPC_RMID, (union semun)0);
  241. #elif defined LIN_SEM
  242. sem_destroy(&sem);
  243. #endif
  244. exit(0);
  245. error:
  246. exit(-1);
  247. }