Browse code

- enabled locking in shm_malloc/free/... - added locking test program

Andrei Pelinescu-Onciul authored on 03/12/2001 21:45:59
Showing 3 changed files
... ...
@@ -38,7 +38,7 @@ inline static void shm_lock()
38 38
 	sop.sem_op=-1; /*down*/
39 39
 	sop.sem_flg=0 /*SEM_UNDO*/;
40 40
 again:
41
-//	semop(shm_semid, &sop, 1);
41
+	semop(shm_semid, &sop, 1);
42 42
 #if 0
43 43
 	switch(ret){
44 44
 		case 0: /*ok*/
... ...
@@ -63,7 +63,7 @@ inline static void shm_unlock()
63 63
 	sop.sem_op=1; /*up*/
64 64
 	sop.sem_flg=0 /*SEM_UNDO*/;
65 65
 again:
66
-//	semop(shm_semid, &sop, 1);
66
+	semop(shm_semid, &sop, 1);
67 67
 #if 0
68 68
 	/*should ret immediately*/
69 69
 	switch(ret){
70 70
new file mode 100644
... ...
@@ -0,0 +1,30 @@
1
+
2
+
3
+
4
+NAME=locking_test
5
+
6
+CC=gcc
7
+CFLAGS= -O2
8
+DEFS=
9
+LIBS=
10
+
11
+$(NAME): $(NAME).c
12
+	gcc $(CFLAGS) $(DEFS) -DNO_LOCK -o $@ $< $(LIBS)
13
+
14
+
15
+$(NAME)_sysv: $(NAME).c
16
+	gcc $(CFLAGS) $(DEFS) -DSYSV_SEM -o $@ $< $(LIBS)
17
+
18
+
19
+$(NAME)_flock: $(NAME).c
20
+	gcc $(CFLAGS) $(DEFS) -DFLOCK -o $@ $< $(LIBS)
21
+
22
+$(NAME)_lin: $(NAME).c
23
+	gcc $(CFLAGS) $(DEFS) -DLIN_SEM -o $@ $< $(LIBS) -lpthread
24
+
25
+
26
+all: $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_lin
27
+
28
+
29
+clean:
30
+	rm $(NAME) $(NAME)_sysv $(NAME)_flock $(NAME)_lin
0 31
new file mode 100644
... ...
@@ -0,0 +1,232 @@
1
+/* $Id$ */
2
+
3
+
4
+#include <stdio.h>
5
+#include <stdlib.h>
6
+#include <errno.h>
7
+#include <string.h>
8
+#include <ctype.h>
9
+#include <netdb.h>
10
+#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>
16
+
17
+#ifdef FLOCK
18
+#include <sys/file.h>
19
+
20
+static int lock_fd;
21
+#endif
22
+
23
+#ifdef LIN_SEM
24
+#include <semaphore.h>
25
+
26
+static sem_t sem;
27
+#endif
28
+
29
+#ifdef SYSV_SEM
30
+#include <sys/ipc.h>
31
+#include <sys/sem.h>
32
+
33
+
34
+#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
35
+	/* union semun is defined by including <sys/sem.h> */
36
+#else
37
+	/* according to X/OPEN we have to define it ourselves */
38
+	union semun {
39
+		int val;                    /* value for SETVAL */
40
+		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
41
+		unsigned short int *array;  /* array for GETALL, SETALL */
42
+		struct seminfo *__buf;      /* buffer for IPC_INFO */
43
+	};
44
+#endif
45
+
46
+static int semid=-1;
47
+
48
+#endif
49
+
50
+
51
+#ifdef NO_LOCK
52
+	#define LOCK()
53
+	#define UNLOCK()
54
+#elif defined SYSV_SEM
55
+	#define LOCK() \
56
+	{\
57
+		struct sembuf sop; \
58
+		sop.sem_num=0; \
59
+		sop.sem_op=-1; /*down*/ \
60
+		sop.sem_flg=0 /*SEM_UNDO*/; \
61
+		semop(semid, &sop, 1); \
62
+	}
63
+
64
+	#define UNLOCK()	\
65
+	{\
66
+		struct sembuf sop;\
67
+		sop.sem_num=0;\
68
+		sop.sem_op=1; /*up*/\
69
+		sop.sem_flg=0 /*SEM_UNDO*/;\
70
+		semop(semid, &sop, 1);\
71
+	}
72
+#elif defined FLOCK
73
+
74
+	#define LOCK() \
75
+		flock(lock_fd, LOCK_EX)
76
+	#define  UNLOCK() \
77
+		flock(lock_fd, LOCK_UN)
78
+#elif defined LIN_SEM
79
+	#define LOCK() \
80
+		sem_wait(&sem)
81
+	#define UNLOCK() \
82
+		sem_post(&sem);
83
+#endif
84
+
85
+
86
+
87
+
88
+static char *id="$Id$";
89
+static char *version="locking_test 0.1-"
90
+#ifdef NO_LOCK
91
+ "nolock"
92
+#elif defined SYSV_SEM
93
+ "sysv_sem"
94
+#elif defined FLOCK
95
+ "flock"
96
+#elif defined LIN_SEM
97
+ "lin_sem";
98
+#endif
99
+;
100
+static char* help_msg="\
101
+Usage: locking_test -n address [-c count] [-v]\n\
102
+Options:\n\
103
+    -c count      how many times to try lock/unlock \n\
104
+    -v            increase verbosity level\n\
105
+    -V            version number\n\
106
+    -h            this help message\n\
107
+";
108
+
109
+
110
+
111
+int main (int argc, char** argv)
112
+{
113
+	char c;
114
+	struct hostent* he;
115
+	int r;
116
+	char *tmp;
117
+	
118
+	int count;
119
+	int verbose;
120
+	char *address;
121
+#ifdef SYSV_SEM
122
+	union semun su;
123
+#endif
124
+	
125
+	/* init */
126
+	count=0;
127
+	verbose=0;
128
+	address=0;
129
+
130
+
131
+	opterr=0;
132
+	while ((c=getopt(argc,argv, "c:vhV"))!=-1){
133
+		switch(c){
134
+			case 'v':
135
+				verbose++;
136
+				break;
137
+			case 'c':
138
+				count=strtol(optarg, &tmp, 10);
139
+				if ((tmp==0)||(*tmp)){
140
+					fprintf(stderr, "bad count: -c %s\n", optarg);
141
+					goto error;
142
+				}
143
+				break;
144
+			case 'V':
145
+				printf("version: %s\n", version);
146
+				printf("%s\n",id);
147
+				exit(0);
148
+				break;
149
+			case 'h':
150
+				printf("version: %s\n", version);
151
+				printf("%s", help_msg);
152
+				exit(0);
153
+				break;
154
+			case '?':
155
+				if (isprint(optopt))
156
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
157
+				else
158
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
159
+				goto error;
160
+			case ':':
161
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
162
+						optopt);
163
+				goto error;
164
+				break;
165
+			default:
166
+					abort();
167
+		}
168
+	}
169
+	
170
+	/* check if all the required params are present */
171
+	if(count==0){
172
+		fprintf(stderr, "Missing count (-c number)\n");
173
+		exit(-1);
174
+	}else if(count<0){
175
+		fprintf(stderr, "Invalid count (-c %d)\n", count);
176
+		exit(-1);
177
+	}
178
+
179
+#ifdef SYSV_SEM
180
+	/*init*/
181
+	puts("Initializing SYS V semaphores\n");
182
+	semid=semget(IPC_PRIVATE,1,0700);
183
+	if(semid==-1){
184
+		fprintf(stderr, "ERROR: could not init sempahore: %s\n",
185
+				strerror(errno));
186
+		goto error;
187
+	}
188
+	/*set init value to 1 (mutex)*/
189
+	su.val=1;
190
+	if (semctl(semid, 0, SETVAL, su)==-1){
191
+		fprintf(stderr, "ERROR: could not set initial sempahore value: %s\n",
192
+				strerror(errno));
193
+		shmctl(semid, IPC_RMID, (union semun)0);
194
+		goto error;
195
+	}
196
+#elif defined FLOCK
197
+	puts("Initializing flock\n");
198
+	lock_fd=open("/dev/zero", O_RDONLY);
199
+	if (lock_fd==-1){
200
+		fprintf(stderr, "ERROR: could not open file: %s\n", strerror(errno));
201
+		goto error;
202
+	}
203
+#elif defined LIN_SEM
204
+	puts("Initializing sempahores\n");
205
+	if (sem_init(&sem, 0, 1)<0){
206
+		fprintf(stderr, "ERROR: could not initialize sempahore: %s\n",
207
+				strerror(errno));
208
+		goto error;
209
+	}
210
+#endif
211
+
212
+
213
+	/*  loop */
214
+	for (r=0; r<count; r++){
215
+		LOCK();
216
+		if ((verbose>1)&&(r%1000))  putchar('.');
217
+		UNLOCK();
218
+	}
219
+
220
+	printf("%d loops\n", count);
221
+
222
+#ifdef SYSV_SEM
223
+	shmctl(semid, IPC_RMID, (union semun)0);
224
+#elif defined LIN_SEM
225
+	sem_destroy(&sem);
226
+#endif
227
+
228
+	exit(0);
229
+
230
+error:
231
+	exit(-1);
232
+}