Blame src/general/sem.c

nsz 1be84a
#include <pthread.h>
nsz 1be84a
#include <stdlib.h>
nsz 1be84a
#include <unistd.h>
nsz 1be84a
#include <stdio.h>
nsz 1be84a
#include <errno.h>
nsz 1be84a
#include <string.h>
nsz 1be84a
#include <semaphore.h>
nsz 1be84a
#include <time.h>
nsz 1be84a
#include <fcntl.h>
nsz 1be84a
#include "test.h"
nsz 1be84a
nsz 1be84a
#define TEST(r, f, x, m) ( \
nsz 1be84a
	((r) = (f)) == (x) || \
nsz 1be84a
	(error("%s failed (" m ")\n", #f, r, x), 0) )
nsz 1be84a
nsz 1be84a
#define TEST_S(s, x, m) ( \
nsz 1be84a
	!strcmp((s),(x)) || \
nsz 1be84a
	(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
nsz 1be84a
nsz 462b4f
int main(void)
nsz 1be84a
{
nsz 1be84a
	int r;
nsz 1be84a
	char buf[100];
nsz 1be84a
	struct timespec ts;
nsz 1be84a
	sem_t *sem, *sem2;
nsz 1be84a
	int val;
nsz 1be84a
nsz 1be84a
	clock_gettime(CLOCK_REALTIME, &ts);
nsz 1be84a
	snprintf(buf, sizeof buf, "/testsuite-%d-%d", (int)getpid(), (int)ts.tv_nsec);
nsz 1be84a
nsz 1be84a
	TEST(r, !(sem=sem_open(buf, O_CREAT|O_EXCL, 0700, 1)), 0, "could not open sem");
nsz 1be84a
nsz 1be84a
	TEST(r, sem_getvalue(sem, &val), 0, "failed to get sem value");
nsz 1be84a
	TEST(r, val, 1, "wrong initial semaphore value");
nsz 1be84a
nsz 1be84a
	TEST(r, !(sem2=sem_open(buf, 0)), 0, "could not reopen sem");
nsz 1be84a
	TEST(r, sem!=sem2, 0, "reopened sem has different address");
nsz 1be84a
nsz 1be84a
	TEST(r, sem_wait(sem), 0, "failed on sem wait");
nsz 1be84a
	TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value");
nsz 1be84a
	TEST(r, val, 0, "wrong semaphore value on second handle");
nsz 1be84a
nsz 1be84a
	TEST(r, sem_post(sem), 0, "failed on sem post");
nsz 1be84a
	TEST(r, sem_getvalue(sem2, &val), 0, "failed to get sem value");
nsz 1be84a
	TEST(r, val, 1, "wrong semaphore value on second handle");
nsz 1be84a
nsz 1be84a
	TEST(r, sem_close(sem), 0, "failed to close sem");
nsz 1be84a
	TEST(r, sem_close(sem), 0, "failed to close sem second time");
nsz 1be84a
	TEST(r, sem_unlink(buf), 0, "failed to unlink sem");
nsz 462b4f
	return test_status;
nsz 1be84a
}