1
0
Fork 0
mirror of https://github.com/tailix/libshmemq.git synced 2024-11-27 11:14:51 -05:00

Use semaphores

This commit is contained in:
Alex Kotov 2020-12-15 20:41:00 +05:00
parent 59299ffcba
commit 42c7c3c1e6
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 43 additions and 4 deletions

View file

@ -39,6 +39,7 @@ AC_CHECK_HEADERS([sys/mman.h])
AC_CHECK_HEADERS([sys/stat.h])
AC_CHECK_HEADERS([sys/types.h])
AC_CHECK_HEADERS([sys/wait.h])
AC_CHECK_HEADERS([time.h])
AC_CHECK_HEADERS([unistd.h])
AC_CHECK_FUNCS([close])
@ -53,7 +54,10 @@ AC_CHECK_FUNCS([strcpy])
AC_CHECK_FUNCS([strlen])
AC_CHECK_FUNCS([waitpid])
AC_SEARCH_LIBS([sem_getvalue], [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_unlink], [rt])

View file

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <shmemq.h>
#include <assert.h>
@ -6,6 +8,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
static const char name[] = "/foobar";
@ -34,7 +37,13 @@ int main()
const ShmemqFrame frame = shmemq_pop_start(consumer);
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;
}
@ -53,14 +62,23 @@ int main()
shmemq_pop_end(consumer, &error);
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 {
atexit(on_exit);
signal(SIGABRT, on_signal);
sleep(1);
producer = shmemq_new(name, false, &error);
assert(error == SHMEMQ_ERROR_NONE);
@ -68,7 +86,13 @@ int main()
const ShmemqFrame frame = shmemq_push_start(producer);
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;
}
@ -84,6 +108,17 @@ int main()
shmemq_push_end(producer, sizeof(unsigned), &error);
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);
}
}
}