1
+ #define _POSIX_C_SOURCE 200809L
2
+
1
3
#include <shmemq.h>
2
4
3
5
#include <assert.h>
6
8
#include <stdlib.h>
7
9
#include <sys/types.h>
8
10
#include <sys/wait.h>
11
+ #include <time.h>
9
12
#include <unistd.h>
10
13
11
14
static const char name [] = "/foobar" ;
@@ -34,7 +37,13 @@ int main()
34
37
const ShmemqFrame frame = shmemq_pop_start (consumer );
35
38
36
39
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 );
38
47
continue ;
39
48
}
40
49
@@ -53,22 +62,37 @@ int main()
53
62
54
63
shmemq_pop_end (consumer , & error );
55
64
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
+ }
56
76
}
57
77
}
58
78
else {
59
79
atexit (on_exit );
60
80
signal (SIGABRT , on_signal );
61
81
62
- sleep (1 );
63
-
64
82
producer = shmemq_new (name , false, & error );
65
83
assert (error == SHMEMQ_ERROR_NONE );
66
84
67
85
for (unsigned index = 0 ; index < 1000 ;) {
68
86
const ShmemqFrame frame = shmemq_push_start (producer );
69
87
70
88
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 );
72
96
continue ;
73
97
}
74
98
@@ -84,6 +108,17 @@ int main()
84
108
85
109
shmemq_push_end (producer , sizeof (unsigned ), & error );
86
110
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
+ }
87
122
}
88
123
}
89
124
0 commit comments