Skip to content

Commit 42c7c3c

Browse files
Use semaphores
1 parent 59299ff commit 42c7c3c

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

configure.ac

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ AC_CHECK_HEADERS([sys/mman.h])
3939
AC_CHECK_HEADERS([sys/stat.h])
4040
AC_CHECK_HEADERS([sys/types.h])
4141
AC_CHECK_HEADERS([sys/wait.h])
42+
AC_CHECK_HEADERS([time.h])
4243
AC_CHECK_HEADERS([unistd.h])
4344

4445
AC_CHECK_FUNCS([close])
@@ -53,7 +54,10 @@ AC_CHECK_FUNCS([strcpy])
5354
AC_CHECK_FUNCS([strlen])
5455
AC_CHECK_FUNCS([waitpid])
5556

57+
AC_SEARCH_LIBS([sem_getvalue], [pthread])
5658
AC_SEARCH_LIBS([sem_init], [pthread])
59+
AC_SEARCH_LIBS([sem_post], [pthread])
60+
AC_SEARCH_LIBS([sem_timedwait], [pthread])
5761
AC_SEARCH_LIBS([shm_open], [rt])
5862
AC_SEARCH_LIBS([shm_unlink], [rt])
5963

tests/test_fork.c

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define _POSIX_C_SOURCE 200809L
2+
13
#include <shmemq.h>
24

35
#include <assert.h>
@@ -6,6 +8,7 @@
68
#include <stdlib.h>
79
#include <sys/types.h>
810
#include <sys/wait.h>
11+
#include <time.h>
912
#include <unistd.h>
1013

1114
static const char name[] = "/foobar";
@@ -34,7 +37,13 @@ int main()
3437
const ShmemqFrame frame = shmemq_pop_start(consumer);
3538

3639
if (frame == NULL) {
37-
sleep(1);
40+
struct timespec tspec;
41+
const int clock_result = clock_gettime(CLOCK_REALTIME, &tspec);
42+
assert(clock_result == 0);
43+
44+
tspec.tv_nsec += 1000;
45+
46+
sem_timedwait(&consumer->buffer->header.read_sem, &tspec);
3847
continue;
3948
}
4049

@@ -53,22 +62,37 @@ int main()
5362

5463
shmemq_pop_end(consumer, &error);
5564
assert(error == SHMEMQ_ERROR_NONE);
65+
66+
int sem_value;
67+
const int sem_getvalue_result =
68+
sem_getvalue(&consumer->buffer->header.write_sem, &sem_value);
69+
assert(sem_getvalue_result == 0);
70+
71+
if (sem_value == 0) {
72+
const int sem_post_result =
73+
sem_post(&consumer->buffer->header.write_sem);
74+
assert(sem_post_result == 0);
75+
}
5676
}
5777
}
5878
else {
5979
atexit(on_exit);
6080
signal(SIGABRT, on_signal);
6181

62-
sleep(1);
63-
6482
producer = shmemq_new(name, false, &error);
6583
assert(error == SHMEMQ_ERROR_NONE);
6684

6785
for (unsigned index = 0; index < 1000;) {
6886
const ShmemqFrame frame = shmemq_push_start(producer);
6987

7088
if (frame == NULL) {
71-
sleep(1);
89+
struct timespec tspec;
90+
const int clock_result = clock_gettime(CLOCK_REALTIME, &tspec);
91+
assert(clock_result == 0);
92+
93+
tspec.tv_nsec += 1000;
94+
95+
sem_timedwait(&producer->buffer->header.write_sem, &tspec);
7296
continue;
7397
}
7498

@@ -84,6 +108,17 @@ int main()
84108

85109
shmemq_push_end(producer, sizeof(unsigned), &error);
86110
assert(error == SHMEMQ_ERROR_NONE);
111+
112+
int sem_value;
113+
const int sem_getvalue_result =
114+
sem_getvalue(&producer->buffer->header.read_sem, &sem_value);
115+
assert(sem_getvalue_result == 0);
116+
117+
if (sem_value == 0) {
118+
const int sem_post_result =
119+
sem_post(&producer->buffer->header.read_sem);
120+
assert(sem_post_result == 0);
121+
}
87122
}
88123
}
89124

0 commit comments

Comments
 (0)