aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/posix-timer.c
blob: d0978188873c73147dd722d34d5343c70d786d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

int main() {
	struct sigevent evp;
	memset(&evp, 0, sizeof(evp));

	sigset_t set;
	sigemptyset(&set);
	sigaddset(&set, SIGUSR1);
	sigprocmask(SIG_BLOCK, &set, 0);
	evp.sigev_notify = SIGEV_SIGNAL;
	evp.sigev_signo = SIGUSR1;

	struct timeval start;
	gettimeofday(&start, NULL);

	timer_t timer;
	if (timer_create(CLOCK_MONOTONIC, &evp, &timer)) {
		perror("timer_create");
		exit(1);
	}

	struct itimerspec spec;
	memset(&spec, 0, sizeof(spec));
	spec.it_value.tv_sec = 1;
	spec.it_value.tv_nsec = 0;

	int sig;
	timer_settime(timer, 0, &spec, NULL);
	sigwait(&set, &sig);

	struct timeval end;
	gettimeofday(&end, NULL);
	timer_delete(timer);

	double diff = end.tv_sec - start.tv_sec;
	diff += (end.tv_usec - start.tv_usec) / 1000000.0;
	assert(diff >= 1.0);

	return 0;
}