Browse code

- new locking tests

Andrei Pelinescu-Onciul authored on 13/02/2002 18:28:09
Showing 2 changed files
... ...
@@ -1,10 +1,12 @@
1
-
1
+#
2
+# $Id$
3
+#
2 4
 
3 5
 
4 6
 NAME=locking_test
5 7
 
6 8
 CC=gcc
7
-CFLAGS= -O2
9
+CFLAGS= -O2 -Wall
8 10
 DEFS=
9 11
 LIBS=
10 12
 
... ...
@@ -19,12 +21,19 @@ $(NAME)_sysv: $(NAME).c
19 21
 $(NAME)_flock: $(NAME).c
20 22
 	gcc $(CFLAGS) $(DEFS) -DFLOCK -o $@ $< $(LIBS)
21 23
 
22
-$(NAME)_lin: $(NAME).c
23
-	gcc $(CFLAGS) $(DEFS) -DLIN_SEM -o $@ $< $(LIBS) -lpthread
24
+$(NAME)_posix: $(NAME).c
25
+	gcc $(CFLAGS) $(DEFS) -DPOSIX_SEM -o $@ $< $(LIBS) -lpthread
26
+
27
+$(NAME)_pmutex: $(NAME).c
28
+	gcc $(CFLAGS) $(DEFS) -DPTHREAD_MUTEX -o $@ $< $(LIBS) -lpthread
24 29
 
30
+$(NAME)_fastlock: $(NAME).c
31
+	gcc $(CFLAGS) $(DEFS) -DFAST_LOCK -o $@ $< $(LIBS) 
25 32
 
26
-all: $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_lin
33
+all: $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_posix $(NAME)_pmutex \
34
+		$(NAME)_fastlock
27 35
 
28 36
 
29 37
 clean:
30
-	rm $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_lin
38
+	-@rm $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_posix \
39
+		$(NAME)_pmutex $(NAME)_fastlock
... ...
@@ -6,13 +6,9 @@
6 6
 #include <errno.h>
7 7
 #include <string.h>
8 8
 #include <ctype.h>
9
-#include <netdb.h>
10 9
 #include <unistd.h>
11
-#include <sys/types.h>
12
-#include <fcntl.h>
13
-#include <sys/socket.h>
14
-#include <netinet/in.h>
15
-#include <arpa/inet.h>
10
+//#include <sys/types.h>
11
+//#include <fcntl.h>
16 12
 
17 13
 #ifdef FLOCK
18 14
 #include <sys/file.h>
... ...
@@ -20,12 +16,22 @@
20 16
 static int lock_fd;
21 17
 #endif
22 18
 
23
-#ifdef LIN_SEM
19
+#ifdef POSIX_SEM
24 20
 #include <semaphore.h>
25 21
 
26 22
 static sem_t sem;
27 23
 #endif
28 24
 
25
+#ifdef PTHREAD_MUTEX
26
+#include <pthread.h>
27
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
28
+#endif
29
+
30
+#ifdef FAST_LOCK
31
+#include "../../fastlock.h"
32
+fl_lock_t lock;
33
+#endif
34
+
29 35
 #ifdef SYSV_SEM
30 36
 #include <sys/ipc.h>
31 37
 #include <sys/sem.h>
... ...
@@ -75,11 +81,21 @@ static int semid=-1;
75 81
 		flock(lock_fd, LOCK_EX)
76 82
 	#define  UNLOCK() \
77 83
 		flock(lock_fd, LOCK_UN)
78
-#elif defined LIN_SEM
84
+#elif defined POSIX_SEM
79 85
 	#define LOCK() \
80 86
 		sem_wait(&sem)
81 87
 	#define UNLOCK() \
82 88
 		sem_post(&sem);
89
+#elif defined PTHREAD_MUTEX
90
+	#define LOCK() \
91
+		pthread_mutex_lock(&mutex)
92
+	#define UNLOCK() \
93
+		pthread_mutex_unlock(&mutex)
94
+#elif defined FAST_LOCK
95
+	#define LOCK() \
96
+		get_lock(&lock)
97
+	#define UNLOCK() \
98
+		release_lock(&lock)
83 99
 #endif
84 100
 
85 101
 
... ...
@@ -93,10 +109,15 @@ static char *version="locking_test 0.1-"
93 109
  "sysv_sem"
94 110
 #elif defined FLOCK
95 111
  "flock"
96
-#elif defined LIN_SEM
97
- "lin_sem";
112
+#elif defined POSIX_SEM
113
+ "posix_sem"
114
+#elif defined PTHREAD_MUTEX
115
+ "pthread_mutext"
116
+#elif defined FAST_LOCK
117
+ "fast_lock"
98 118
 #endif
99 119
 ;
120
+
100 121
 static char* help_msg="\
101 122
 Usage: locking_test -n address [-c count] [-v]\n\
102 123
 Options:\n\
... ...
@@ -111,7 +132,6 @@ Options:\n\
111 132
 int main (int argc, char** argv)
112 133
 {
113 134
 	char c;
114
-	struct hostent* he;
115 135
 	int r;
116 136
 	char *tmp;
117 137
 	
... ...
@@ -190,7 +210,7 @@ int main (int argc, char** argv)
190 210
 	if (semctl(semid, 0, SETVAL, su)==-1){
191 211
 		fprintf(stderr, "ERROR: could not set initial sempahore value: %s\n",
192 212
 				strerror(errno));
193
-		shmctl(semid, IPC_RMID, (union semun)0);
213
+		semctl(semid, 0, IPC_RMID, (union semun)0);
194 214
 		goto error;
195 215
 	}
196 216
 #elif defined FLOCK
... ...
@@ -200,13 +220,19 @@ int main (int argc, char** argv)
200 220
 		fprintf(stderr, "ERROR: could not open file: %s\n", strerror(errno));
201 221
 		goto error;
202 222
 	}
203
-#elif defined LIN_SEM
223
+#elif defined POSIX_SEM
204 224
 	puts("Initializing sempahores\n");
205 225
 	if (sem_init(&sem, 0, 1)<0){
206 226
 		fprintf(stderr, "ERROR: could not initialize sempahore: %s\n",
207 227
 				strerror(errno));
208 228
 		goto error;
209 229
 	}
230
+#elif defined PTHREAD_MUTEX
231
+	puts("Initializing mutex -already initialized (statically)\n");
232
+	/*pthread_mutext_init(&mutex, 0 );*/
233
+#elif defined FAST_LOCK
234
+	puts("Initializing fast lock\n");
235
+	init_lock(lock);
210 236
 #endif
211 237
 
212 238
 
... ...
@@ -220,7 +246,7 @@ int main (int argc, char** argv)
220 246
 	printf("%d loops\n", count);
221 247
 
222 248
 #ifdef SYSV_SEM
223
-	shmctl(semid, IPC_RMID, (union semun)0);
249
+	semctl(semid, 0, IPC_RMID, (union semun)0);
224 250
 #elif defined LIN_SEM
225 251
 	sem_destroy(&sem);
226 252
 #endif