locking_test.c 6.1 KB

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