mirror of
https://github.com/tailix/libshmemq.git
synced 2025-02-17 15:45:41 -05:00
Use semaphores
This commit is contained in:
parent
59299ffcba
commit
42c7c3c1e6
2 changed files with 43 additions and 4 deletions
|
@ -39,6 +39,7 @@ AC_CHECK_HEADERS([sys/mman.h])
|
||||||
AC_CHECK_HEADERS([sys/stat.h])
|
AC_CHECK_HEADERS([sys/stat.h])
|
||||||
AC_CHECK_HEADERS([sys/types.h])
|
AC_CHECK_HEADERS([sys/types.h])
|
||||||
AC_CHECK_HEADERS([sys/wait.h])
|
AC_CHECK_HEADERS([sys/wait.h])
|
||||||
|
AC_CHECK_HEADERS([time.h])
|
||||||
AC_CHECK_HEADERS([unistd.h])
|
AC_CHECK_HEADERS([unistd.h])
|
||||||
|
|
||||||
AC_CHECK_FUNCS([close])
|
AC_CHECK_FUNCS([close])
|
||||||
|
@ -53,7 +54,10 @@ AC_CHECK_FUNCS([strcpy])
|
||||||
AC_CHECK_FUNCS([strlen])
|
AC_CHECK_FUNCS([strlen])
|
||||||
AC_CHECK_FUNCS([waitpid])
|
AC_CHECK_FUNCS([waitpid])
|
||||||
|
|
||||||
|
AC_SEARCH_LIBS([sem_getvalue], [pthread])
|
||||||
AC_SEARCH_LIBS([sem_init], [pthread])
|
AC_SEARCH_LIBS([sem_init], [pthread])
|
||||||
|
AC_SEARCH_LIBS([sem_post], [pthread])
|
||||||
|
AC_SEARCH_LIBS([sem_timedwait], [pthread])
|
||||||
AC_SEARCH_LIBS([shm_open], [rt])
|
AC_SEARCH_LIBS([shm_open], [rt])
|
||||||
AC_SEARCH_LIBS([shm_unlink], [rt])
|
AC_SEARCH_LIBS([shm_unlink], [rt])
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include <shmemq.h>
|
#include <shmemq.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
@ -6,6 +8,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static const char name[] = "/foobar";
|
static const char name[] = "/foobar";
|
||||||
|
@ -34,7 +37,13 @@ int main()
|
||||||
const ShmemqFrame frame = shmemq_pop_start(consumer);
|
const ShmemqFrame frame = shmemq_pop_start(consumer);
|
||||||
|
|
||||||
if (frame == NULL) {
|
if (frame == NULL) {
|
||||||
sleep(1);
|
struct timespec tspec;
|
||||||
|
const int clock_result = clock_gettime(CLOCK_REALTIME, &tspec);
|
||||||
|
assert(clock_result == 0);
|
||||||
|
|
||||||
|
tspec.tv_nsec += 1000;
|
||||||
|
|
||||||
|
sem_timedwait(&consumer->buffer->header.read_sem, &tspec);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +62,23 @@ int main()
|
||||||
|
|
||||||
shmemq_pop_end(consumer, &error);
|
shmemq_pop_end(consumer, &error);
|
||||||
assert(error == SHMEMQ_ERROR_NONE);
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
int sem_value;
|
||||||
|
const int sem_getvalue_result =
|
||||||
|
sem_getvalue(&consumer->buffer->header.write_sem, &sem_value);
|
||||||
|
assert(sem_getvalue_result == 0);
|
||||||
|
|
||||||
|
if (sem_value == 0) {
|
||||||
|
const int sem_post_result =
|
||||||
|
sem_post(&consumer->buffer->header.write_sem);
|
||||||
|
assert(sem_post_result == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
atexit(on_exit);
|
atexit(on_exit);
|
||||||
signal(SIGABRT, on_signal);
|
signal(SIGABRT, on_signal);
|
||||||
|
|
||||||
sleep(1);
|
|
||||||
|
|
||||||
producer = shmemq_new(name, false, &error);
|
producer = shmemq_new(name, false, &error);
|
||||||
assert(error == SHMEMQ_ERROR_NONE);
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
@ -68,7 +86,13 @@ int main()
|
||||||
const ShmemqFrame frame = shmemq_push_start(producer);
|
const ShmemqFrame frame = shmemq_push_start(producer);
|
||||||
|
|
||||||
if (frame == NULL) {
|
if (frame == NULL) {
|
||||||
sleep(1);
|
struct timespec tspec;
|
||||||
|
const int clock_result = clock_gettime(CLOCK_REALTIME, &tspec);
|
||||||
|
assert(clock_result == 0);
|
||||||
|
|
||||||
|
tspec.tv_nsec += 1000;
|
||||||
|
|
||||||
|
sem_timedwait(&producer->buffer->header.write_sem, &tspec);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +108,17 @@ int main()
|
||||||
|
|
||||||
shmemq_push_end(producer, sizeof(unsigned), &error);
|
shmemq_push_end(producer, sizeof(unsigned), &error);
|
||||||
assert(error == SHMEMQ_ERROR_NONE);
|
assert(error == SHMEMQ_ERROR_NONE);
|
||||||
|
|
||||||
|
int sem_value;
|
||||||
|
const int sem_getvalue_result =
|
||||||
|
sem_getvalue(&producer->buffer->header.read_sem, &sem_value);
|
||||||
|
assert(sem_getvalue_result == 0);
|
||||||
|
|
||||||
|
if (sem_value == 0) {
|
||||||
|
const int sem_post_result =
|
||||||
|
sem_post(&producer->buffer->header.read_sem);
|
||||||
|
assert(sem_post_result == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue