fast-stats 1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,278 @@
1
+ /* semaphore.c */
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <stdarg.h>
6
+ #include <string.h>
7
+ #include <fcntl.h>
8
+ #include <unistd.h>
9
+ #include <sys/stat.h>
10
+ #include <errno.h>
11
+
12
+ #include "stats/error.h"
13
+ #include "stats/semaphore.h"
14
+ #include "stats/debug.h"
15
+
16
+ int semaphore_create(const char *name, unsigned short size, struct semaphore **sem_out )
17
+ {
18
+ struct semaphore * sem;
19
+ int err;
20
+
21
+ if (sem_out == NULL)
22
+ return ERROR_INVALID_PARAMETERS;
23
+
24
+ if (strlen(name) >= SEMAPHORE_MAX_NAME_LEN)
25
+ return ERROR_SEMAPHORE_NAME_TOO_LONG;
26
+
27
+ sem = malloc(sizeof(struct semaphore));
28
+ if (!sem)
29
+ return ERROR_MEMORY;
30
+
31
+ err = semaphore_init(sem,name,size);
32
+ if (err == S_OK)
33
+ {
34
+ *sem_out = sem;
35
+ }
36
+ else
37
+ {
38
+ free(sem);
39
+ *sem_out = NULL;
40
+ }
41
+
42
+ return err;
43
+ }
44
+
45
+
46
+ int semaphore_init(struct semaphore *sem, const char *name, unsigned short size)
47
+ {
48
+ if (sem == NULL)
49
+ return ERROR_INVALID_PARAMETERS;
50
+
51
+ if (strlen(name) >= SEMAPHORE_MAX_NAME_LEN)
52
+ return ERROR_SEMAPHORE_NAME_TOO_LONG;
53
+
54
+ memset(sem, 0, sizeof(struct semaphore));
55
+
56
+ sem->magic = SEMAPHORE_MAGIC;
57
+ sem->semkey = -1;
58
+ sem->semid = -1;
59
+ sem->size = size;
60
+ strcpy(sem->name, name);
61
+
62
+ return S_OK;
63
+ }
64
+
65
+
66
+ int semaphore_open(struct semaphore * sem, int flags)
67
+ {
68
+ struct stat s;
69
+ char path[MAX_PATH];
70
+ int mode;
71
+ int omode;
72
+
73
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
74
+ return ERROR_INVALID_PARAMETERS;
75
+
76
+ /* extract open mode from flags */
77
+ omode = flags & OMODE_MASK;
78
+
79
+ /* check for existance of the directory */
80
+ if (stat(SEMAPHORE_DIRECTORY,&s) != 0)
81
+ {
82
+ /* try to create the directory */
83
+ if (mkdir(SEMAPHORE_DIRECTORY, 0755) != 0)
84
+ return ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY;
85
+ }
86
+ else
87
+ {
88
+ if ((s.st_mode & S_IFDIR) != S_IFDIR)
89
+ return ERROR_SEMAPHORE_PATH_NOT_DIRECTORY;
90
+ }
91
+
92
+ sprintf(path, "%s/%s", SEMAPHORE_DIRECTORY, sem->name);
93
+
94
+ if (access(path, W_OK) == -1)
95
+ {
96
+ int fd = creat(path, 0644 );
97
+ if (fd == -1)
98
+ return ERROR_SEMAPHORE_CANNOT_CREATE_PATH;
99
+ close(fd);
100
+ }
101
+
102
+ sem->semkey = ftok(path, 1);
103
+ if (sem->semkey == -1)
104
+ {
105
+ return ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN;
106
+ }
107
+
108
+ switch (omode)
109
+ {
110
+ case OMODE_CREATE:
111
+ mode = IPC_CREAT | IPC_EXCL | 0644;
112
+ break;
113
+ case OMODE_OPEN_OR_CREATE:
114
+ mode = IPC_CREAT | 0644;
115
+ break;
116
+ case OMODE_OPEN_EXISTING:
117
+ default:
118
+ mode = 0644;
119
+ break;
120
+ }
121
+
122
+ DPRINTF("Opening semaphore key %08x for %s (absolute path: %s), mode 0%o, size %d\n", sem->semkey, sem->name, path, mode, sem->size);
123
+
124
+ sem->semid = semget(sem->semkey, sem->size, mode) ;
125
+ if (sem->semid == -1)
126
+ {
127
+ if (errno == EEXIST && omode == OMODE_CREATE)
128
+ return ERROR_SEMAPHORE_ALREADY_EXISTS;
129
+ if (errno == ENOENT && omode == OMODE_OPEN_EXISTING)
130
+ return ERROR_SEMAPHORE_DOES_NOT_EXIST;
131
+ if (errno == EINVAL)
132
+ return ERROR_SEMAPHORE_INVALID_SIZE;
133
+ return ERROR_SEMAPHORE_CANNOT_OPEN;
134
+ }
135
+
136
+ DPRINTF("Successfylly openend semaphore %d (key 0x%08x) for %s.\n", sem->semid, sem->semkey, sem->name);
137
+
138
+ return S_OK;
139
+ }
140
+
141
+ int semaphore_set_value(struct semaphore *sem, unsigned short nsem, unsigned short value)
142
+ {
143
+ union semun s;
144
+
145
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
146
+ return ERROR_INVALID_PARAMETERS;
147
+
148
+ if (nsem >= sem->size)
149
+ return ERROR_INVALID_PARAMETERS;
150
+
151
+ s.val = value;
152
+
153
+ semctl(sem->semid, nsem, SETVAL, s);
154
+
155
+ return S_OK;
156
+ }
157
+
158
+ int semaphore_set_values(struct semaphore *sem, unsigned short *values)
159
+ {
160
+ union semun s;
161
+
162
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
163
+ return ERROR_INVALID_PARAMETERS;
164
+
165
+ s.array = values;
166
+
167
+ semctl(sem->semid, 0, SETALL, s);
168
+
169
+ return S_OK;
170
+ }
171
+
172
+ int semaphore_open_and_set(struct semaphore *sem, ... )
173
+ {
174
+ va_list ap;
175
+ int i;
176
+ unsigned short *values;
177
+ int res;
178
+
179
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
180
+ return ERROR_INVALID_PARAMETERS;
181
+
182
+ values = (unsigned short *) alloca(sizeof(unsigned short) * sem->size);
183
+
184
+ va_start(ap, sem);
185
+ for (i = 0; i < sem->size; i++)
186
+ {
187
+ values[i] = (unsigned short) va_arg(ap, int);
188
+ }
189
+ va_end(ap);
190
+
191
+
192
+ res = semaphore_open(sem, OMODE_CREATE);
193
+ if (res == S_OK)
194
+ {
195
+ DPRINTF("Created semaphore. setting initial value\n");
196
+ res = semaphore_set_values(sem,values);
197
+ }
198
+ else if (res == ERROR_SEMAPHORE_ALREADY_EXISTS)
199
+ {
200
+ res = semaphore_open(sem, OMODE_OPEN_OR_CREATE);
201
+ /*
202
+ Race condition possible here!!! this function could return when after the creating process
203
+ has created the semaphore, but not yet set the initial values, so it is possible that the
204
+ semaphore values are uninitialized. See the stevens solution with polling on ipc_stat
205
+ */
206
+ }
207
+
208
+ return res;
209
+ }
210
+
211
+ int semaphore_P(struct semaphore *sem, unsigned short nsem)
212
+ {
213
+ struct sembuf s;
214
+
215
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
216
+ return ERROR_INVALID_PARAMETERS;
217
+
218
+ if (nsem >= sem->size)
219
+ return ERROR_INVALID_PARAMETERS;
220
+
221
+ s.sem_num = nsem;
222
+ s.sem_op = -1;
223
+ s.sem_flg = SEM_UNDO;
224
+
225
+ if (semop(sem->semid,&s,1) == -1)
226
+ {
227
+
228
+ }
229
+
230
+ return S_OK;
231
+ }
232
+
233
+ int semaphore_V(struct semaphore *sem, unsigned short nsem)
234
+ {
235
+ struct sembuf s;
236
+
237
+ if (sem == NULL || sem->magic != SEMAPHORE_MAGIC)
238
+ return ERROR_INVALID_PARAMETERS;
239
+
240
+ if (nsem >= sem->size)
241
+ return ERROR_INVALID_PARAMETERS;
242
+
243
+ s.sem_num = nsem;
244
+ s.sem_op = 1;
245
+ s.sem_flg = SEM_UNDO;
246
+
247
+ if (semop(sem->semid,&s,1) == -1)
248
+ {
249
+
250
+ }
251
+
252
+ return S_OK;
253
+ }
254
+
255
+
256
+ int semaphore_close(struct semaphore *sem, int remove)
257
+ {
258
+ char path[MAX_PATH];
259
+
260
+ if (remove)
261
+ {
262
+ DPRINTF("Destroying semaphore %s 0x%08x (%d).\n", sem->name, sem->semkey, sem->semid);
263
+ semctl(sem->semid, 0, IPC_RMID);
264
+ sprintf(path, "%s/%s", SEMAPHORE_DIRECTORY, sem->name);
265
+ unlink(path);
266
+ }
267
+
268
+ sem->semid = -1;
269
+ sem->semkey = -1;
270
+
271
+ return S_OK;
272
+ }
273
+
274
+ void semaphore_free(struct semaphore *sem)
275
+ {
276
+ free(sem);
277
+ }
278
+
@@ -0,0 +1,238 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <string.h>
4
+ #include <fcntl.h>
5
+ #include <unistd.h>
6
+ #include <sys/stat.h>
7
+ #include <errno.h>
8
+
9
+ #include "stats/error.h"
10
+ #include "stats/shared_mem.h"
11
+ #include "stats/debug.h"
12
+
13
+ int shared_memory_create(const char *name, int flags, int size, struct shared_memory **shmem_out )
14
+ {
15
+ struct shared_memory * shmem;
16
+ int err;
17
+
18
+ if (shmem_out == NULL)
19
+ return ERROR_INVALID_PARAMETERS;
20
+
21
+ shmem = malloc(sizeof(struct shared_memory));
22
+ if (!shmem)
23
+ return ERROR_MEMORY;
24
+
25
+ err = shared_memory_init(shmem,name,flags,size);
26
+ if (err == S_OK)
27
+ {
28
+ *shmem_out = shmem;
29
+ }
30
+ else
31
+ {
32
+ free(shmem);
33
+ *shmem_out = NULL;
34
+ }
35
+
36
+ return err;
37
+ }
38
+
39
+ int shared_memory_init(struct shared_memory *shmem, const char *name, int flags, int size)
40
+ {
41
+ if (shmem == NULL)
42
+ return ERROR_INVALID_PARAMETERS;
43
+
44
+ if (strlen(name) >= SHARED_MEMORY_MAX_NAME_LEN)
45
+ return ERROR_SHARED_MEM_NAME_TOO_LONG;
46
+
47
+ memset(shmem, 0, sizeof(struct shared_memory));
48
+
49
+ shmem->magic = SHARED_MEMORY_MAGIC;
50
+ shmem->shmkey = -1;
51
+ shmem->shmid = -1;
52
+ shmem->flags = flags;
53
+ shmem->size = size;
54
+ strcpy(shmem->name, name);
55
+
56
+ return S_OK;
57
+ }
58
+
59
+ int shared_memory_open(struct shared_memory * shmem)
60
+ {
61
+ struct stat s;
62
+ char path[MAX_PATH];
63
+ int mode;
64
+ struct shmid_ds ds;
65
+ int omode;
66
+
67
+ if (shmem == NULL || shmem->magic != SHARED_MEMORY_MAGIC)
68
+ return ERROR_INVALID_PARAMETERS;
69
+
70
+ /* extract open mode from flags */
71
+ omode = shmem->flags & OMODE_MASK;
72
+
73
+ /* check for existance of the directory */
74
+ if (stat(SHARED_MEMORY_DIRECTORY,&s) != 0)
75
+ {
76
+ /* try to create the directory */
77
+ if (mkdir(SHARED_MEMORY_DIRECTORY, 0755) != 0)
78
+ return ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY;
79
+ }
80
+ else
81
+ {
82
+ if ((s.st_mode & S_IFDIR) != S_IFDIR)
83
+ return ERROR_SHARED_MEM_PATH_NOT_DIRECTORY;
84
+ }
85
+
86
+ sprintf(path, "%s/%s", SHARED_MEMORY_DIRECTORY, shmem->name);
87
+
88
+ if (access(path, W_OK) == -1)
89
+ {
90
+ int fd = creat(path, 0644 );
91
+ if (fd == -1)
92
+ return ERROR_SHARED_MEM_CANNOT_CREATE_PATH;
93
+ close(fd);
94
+ }
95
+
96
+ shmem->shmkey = ftok(path, 1);
97
+ if (shmem->shmkey == -1)
98
+ {
99
+ return ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN;
100
+ }
101
+
102
+ switch (omode)
103
+ {
104
+ case OMODE_CREATE:
105
+ mode = IPC_CREAT | IPC_EXCL | 0644;
106
+ break;
107
+ case OMODE_OPEN_OR_CREATE:
108
+ mode = IPC_CREAT | 0644;
109
+ break;
110
+ case OMODE_OPEN_EXISTING:
111
+ default:
112
+ mode = 0644;
113
+ break;
114
+ }
115
+
116
+ DPRINTF("Opening shmkey %08x for %s (absolute path: %s), mode 0%o, size %d\n", shmem->shmkey, shmem->name, path, mode, shmem->size);
117
+
118
+ shmem->shmid = shmget(shmem->shmkey, shmem->size, mode) ;
119
+ if (shmem->shmid == -1)
120
+ {
121
+ if (errno == EEXIST && omode == OMODE_CREATE)
122
+ return ERROR_SHARED_MEM_ALREADY_EXISTS;
123
+ if (errno == ENOENT && omode == OMODE_OPEN_EXISTING)
124
+ return ERROR_SHARED_MEM_DOES_NOT_EXIST;
125
+ if (errno == EINVAL)
126
+ return ERROR_SHARED_MEM_INVALID_SIZE;
127
+ return ERROR_SHARED_MEM_CANNOT_OPEN;
128
+ }
129
+
130
+ switch (omode)
131
+ {
132
+ case OMODE_CREATE:
133
+ shmem->created = TRUE;
134
+ break;
135
+ case OMODE_OPEN_OR_CREATE:
136
+ if (shmctl(shmem->shmid, IPC_STAT, &ds) != 0)
137
+ return ERROR_SHARED_MEM_CANNOT_STAT;
138
+ if (ds.shm_cpid == getpid())
139
+ shmem->created = TRUE;
140
+ else
141
+ shmem->created = FALSE;
142
+ break;
143
+ case OMODE_OPEN_EXISTING:
144
+ shmem->created = FALSE;
145
+ break;
146
+ }
147
+
148
+ shmem->ptr = shmat(shmem->shmid, NULL, 0);
149
+ if ((intptr_t)shmem->ptr == -1)
150
+ {
151
+ shmem->ptr = NULL;
152
+ return ERROR_SHARED_MEM_CANNOT_ATTACH;
153
+ }
154
+
155
+ DPRINTF("Successfylly openend shm 0x%08x for %s. Attached at 0x%016lx\n", shmem->shmkey, shmem->name, (intptr_t) shmem->ptr);
156
+
157
+ return S_OK;
158
+ }
159
+
160
+
161
+ int shared_memory_nattach(struct shared_memory *shmem, int *attach_count_out)
162
+ {
163
+ struct shmid_ds ds;
164
+
165
+ if (!shmem || shmem->magic != SHARED_MEMORY_MAGIC || shmem->shmid == -1)
166
+ return ERROR_INVALID_PARAMETERS;
167
+
168
+ if (shmctl(shmem->shmid, IPC_STAT, &ds) != 0)
169
+ return ERROR_SHARED_MEM_CANNOT_STAT;
170
+
171
+ *attach_count_out = ds.shm_nattch;
172
+ return S_OK;
173
+ }
174
+
175
+
176
+ int shared_memory_close(struct shared_memory *shmem, int *did_destroy)
177
+ {
178
+ struct shmid_ds ds;
179
+ int destroy_mode;
180
+ int destroy = FALSE;
181
+ char path[MAX_PATH];
182
+
183
+ if (shmem == NULL || shmem->magic != SHARED_MEMORY_MAGIC)
184
+ return ERROR_INVALID_PARAMETERS;
185
+
186
+ destroy_mode = shmem->flags & DESTROY_MASK;
187
+
188
+ if (shmem->ptr)
189
+ {
190
+ shmdt(shmem->ptr);
191
+ shmem->ptr = NULL;
192
+ }
193
+
194
+ if (shmem->shmid != -1)
195
+ {
196
+ if (destroy_mode == DESTROY_ON_CLOSE_IF_LAST)
197
+ {
198
+ /* need to stat to see if there are no more attaches */
199
+ if (shmctl(shmem->shmid, IPC_STAT, &ds) == 0)
200
+ {
201
+ if (ds.shm_nattch == 0)
202
+ destroy = TRUE;
203
+ }
204
+ }
205
+
206
+ if (destroy_mode == DESTROY_ON_CLOSE)
207
+ destroy = TRUE;
208
+
209
+ if (destroy)
210
+ {
211
+ DPRINTF("Destroying shared memory %s 0x%08x (%d).\n", shmem->name, shmem->shmkey, shmem->shmid);
212
+ shmctl(shmem->shmid, IPC_RMID, NULL);
213
+ sprintf(path, "%s/%s", SHARED_MEMORY_DIRECTORY, shmem->name);
214
+ unlink(path);
215
+
216
+ if (did_destroy != NULL)
217
+ *did_destroy = 1;
218
+ }
219
+ else
220
+ {
221
+ if (did_destroy != NULL)
222
+ *did_destroy = 0;
223
+ }
224
+
225
+ shmem->shmid = -1;
226
+ }
227
+
228
+ shmem->shmkey = -1;
229
+
230
+ return S_OK;
231
+ }
232
+
233
+ void shared_memory_free(struct shared_memory *shmem)
234
+ {
235
+ free(shmem);
236
+ }
237
+
238
+
@@ -0,0 +1,8 @@
1
+ /* debug.h */
2
+
3
+ #ifndef _DEBUG_H_INCLUDED_
4
+ #define _DEBUG_H_INCLUDED_
5
+
6
+ #define DPRINTF if (DEBUG) printf
7
+
8
+ #endif
@@ -0,0 +1,50 @@
1
+ /* error.h */
2
+
3
+ #ifndef _ERROR_H_INCLUDED_
4
+ #define _ERROR_H_INCLUDED_
5
+
6
+ #define TRUE 1
7
+ #define FALSE 0
8
+
9
+ #define S_OK 0x00000000
10
+ #define ERROR_FLAG 0x80000000
11
+
12
+ #define ERROR_FACILITY_GENERAL 0x00010000
13
+ #define ERROR_FACILITY_SHARED_MEM 0x00020000
14
+ #define ERROR_FACILITY_SEMAPHORE 0x00030000
15
+ #define ERROR_FACILITY_STATS 0x00040000
16
+
17
+ #define ERROR_FAIL (ERROR_FLAG | ERROR_FACILITY_GENERAL | 0x0000)
18
+ #define ERROR_INVALID_PARAMETERS (ERROR_FLAG | ERROR_FACILITY_GENERAL | 0x0001)
19
+ #define ERROR_MEMORY (ERROR_FLAG | ERROR_FACILITY_GENERAL | 0x0002)
20
+
21
+ #define ERROR_SHARED_MEM_NAME_TOO_LONG (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0001)
22
+ #define ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0002)
23
+ #define ERROR_SHARED_MEM_PATH_NOT_DIRECTORY (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0003)
24
+ #define ERROR_SHARED_MEM_CANNOT_CREATE_PATH (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0004)
25
+ #define ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0005)
26
+ #define ERROR_SHARED_MEM_ALREADY_EXISTS (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0006)
27
+ #define ERROR_SHARED_MEM_DOES_NOT_EXIST (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0007)
28
+ #define ERROR_SHARED_MEM_INVALID_SIZE (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0008)
29
+ #define ERROR_SHARED_MEM_CANNOT_STAT (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x0009)
30
+ #define ERROR_SHARED_MEM_CANNOT_OPEN (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x000A)
31
+ #define ERROR_SHARED_MEM_CANNOT_ATTACH (ERROR_FLAG | ERROR_FACILITY_SHARED_MEM | 0x000B)
32
+
33
+
34
+ #define ERROR_SEMAPHORE_NAME_TOO_LONG (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0001)
35
+ #define ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0002)
36
+ #define ERROR_SEMAPHORE_PATH_NOT_DIRECTORY (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0003)
37
+ #define ERROR_SEMAPHORE_CANNOT_CREATE_PATH (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0004)
38
+ #define ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0005)
39
+ #define ERROR_SEMAPHORE_ALREADY_EXISTS (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0006)
40
+ #define ERROR_SEMAPHORE_DOES_NOT_EXIST (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0007)
41
+ #define ERROR_SEMAPHORE_INVALID_SIZE (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x0008)
42
+ #define ERROR_SEMAPHORE_CANNOT_OPEN (ERROR_FLAG | ERROR_FACILITY_SEMAPHORE | 0x000A)
43
+
44
+ #define ERROR_STATS_CANNOT_ALLOCATE_COUNTER (ERROR_FLAG | ERROR_FACILITY_STATS | 0x0001)
45
+ #define ERROR_STATS_KEY_TOO_LONG (ERROR_FLAG | ERROR_FACILITY_STATS | 0x0002)
46
+
47
+ const char * error_message(int code);
48
+
49
+
50
+ #endif
@@ -0,0 +1,10 @@
1
+ /* hash.h */
2
+
3
+ #ifndef _HASH_H_
4
+ #define _HASH_H_
5
+
6
+ #include <stdint.h>
7
+
8
+ uint32_t fast_hash(const char * data, int len);
9
+
10
+ #endif
@@ -0,0 +1,27 @@
1
+ /* lock.h */
2
+
3
+ #ifndef _LOCK_H_INCLUDED_
4
+ #define _LOCK_H_INCLUDED_
5
+
6
+ #include "semaphore.h"
7
+
8
+ struct lock {
9
+ struct semaphore sem;
10
+ };
11
+
12
+ int lock_create(const char * name, struct lock **lock_out);
13
+
14
+ int lock_acquire(struct lock *lock);
15
+ int lock_release(struct lock *lock);
16
+
17
+ int lock_close(struct lock *lock);
18
+ void lock_free(struct lock *lock);
19
+
20
+ #define lock_init(lock,name) semaphore_init(&(lock)->sem,name,1)
21
+ #define lock_open(lock) semaphore_open_and_set(&(lock)->sem,1)
22
+ #define lock_acquire(lock) semaphore_P(&(lock)->sem,0)
23
+ #define lock_release(lock) semaphore_V(&(lock)->sem,0)
24
+ #define lock_close(lock,remove) semaphore_close(&(lock)->sem,(remove));
25
+ #define lock_is_open(lock) semaphore_is_open(&((lock)->sem))
26
+
27
+ #endif
@@ -0,0 +1,11 @@
1
+ /* omode.h */
2
+
3
+ #ifndef _OMODE_H_INCLUDED_
4
+ #define _OMODE_H_INCLUDED_
5
+
6
+ #define OMODE_CREATE 0x0000001
7
+ #define OMODE_OPEN_OR_CREATE 0x0000002
8
+ #define OMODE_OPEN_EXISTING 0x0000004
9
+ #define OMODE_MASK 0x0000007
10
+
11
+ #endif
@@ -0,0 +1,53 @@
1
+ /* semaphore.h */
2
+
3
+ #ifndef _SEMAPHORE_H_
4
+ #define _SEMAPHORE_H_
5
+
6
+ #include <sys/ipc.h>
7
+ #include <sys/sem.h>
8
+
9
+ #include "omode.h"
10
+
11
+ #define SEMAPHORE_MAGIC 'semm'
12
+
13
+ #define SEMAPHORE_MAX_NAME_LEN 31
14
+
15
+ struct semaphore {
16
+ int magic;
17
+ unsigned short size;
18
+ char name[SEMAPHORE_MAX_NAME_LEN + 1];
19
+ key_t semkey;
20
+ int semid;
21
+ };
22
+
23
+ #ifdef LINUX
24
+ union semun {
25
+ int val; /* Value for SETVAL */
26
+ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
27
+ unsigned short *array; /* Array for GETALL, SETALL */
28
+ struct seminfo *__buf; /* Buffer for IPC_INFO
29
+ (Linux-specific) */
30
+ };
31
+ #endif
32
+
33
+ int semaphore_create(const char *name, unsigned short size, struct semaphore **sem_out);
34
+ int semaphore_init(struct semaphore *sem, const char *name, unsigned short size);
35
+ int semaphore_open(struct semaphore *sem, int flags);
36
+ int semaphore_open_and_set(struct semaphore *sem, ... );
37
+ int semaphore_set_value(struct semaphore *sem, unsigned short nsem, unsigned short value);
38
+ int semaphore_set_values(struct semaphore *sem, unsigned short *values);
39
+ int semaphore_P(struct semaphore *sem, unsigned short nsem);
40
+ int semaphore_V(struct semaphore *sem, unsigned short nsem);
41
+ int semaphore_close(struct semaphore *sem, int remove);
42
+ void semaphore_free(struct semaphore *sem);
43
+
44
+ #define semaphore_signal semaphore_V
45
+ #define semaphore_wait semaphore_P
46
+
47
+ #define semaphore_size(s) ((s)->size)
48
+ #define semaphore_name(s) ((s)->name)
49
+ #define semaphore_is_open(s) ((s)->semid != -1)
50
+ #define SEMAPHORE_DIRECTORY "/tmp"
51
+ #define MAX_PATH 255
52
+
53
+ #endif