#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <sys/sysconfig.h>
#include <sys/sysmacros.h>
extern long _sysconfig(int);
static int
mktimer(timer_t *timer)
{
struct sigevent sev;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = timer;
return (timer_create(CLOCK_MONOTONIC, &sev, timer));
}
int
main(void)
{
long ncpu;
size_t limit;
timer_t *timers, timer_overage;
ncpu = _sysconfig(_CONFIG_NPROC_NCPU);
assert(ncpu > 0 && ncpu < INT32_MAX);
limit = 4 * ncpu;
timers = calloc(limit + 1, sizeof (timer_t));
if (timers == NULL)
err(EXIT_FAILURE, "failed to allocate %zu timers", limit + 1);
for (int i = 1; i <= limit; i = MIN(limit, i*2)) {
for (int j = 0; j < i; j++) {
assert(mktimer(&timers[j]) == 0);
}
if (i == limit) {
assert(mktimer(&timer_overage) == -1);
}
for (int j = 0; j < i; j++) {
assert(timer_delete(timers[j]) == 0);
}
if (i == limit)
break;
}
return (0);
}