/* Tests for POSIX timer implementation.
Copyright (C) 2004-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2004
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, see <https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#if _POSIX_THREADS && defined SA_SIGINFO
# include <pthread.h>
# ifndef TEST_CLOCK
# define TEST_CLOCK CLOCK_REALTIME
# endif
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
timer_t timer_none, timer_sig1, timer_sig2, timer_thr1, timer_thr2;
int thr1_cnt, thr1_err;
union sigval thr1_sigval;
struct timespec thr1_ts;
static void
thr1 (union sigval sigval)
{
pthread_mutex_lock (&lock);
thr1_err = clock_gettime (TEST_CLOCK, &thr1_ts);
if (thr1_cnt >= 5)
{
struct itimerspec it = { };
thr1_err |= timer_settime (timer_thr1, 0, &it, NULL);
}
thr1_sigval = sigval;
++thr1_cnt;
pthread_cond_signal (&cond);
pthread_mutex_unlock (&lock);
}
int thr2_cnt, thr2_err;
union sigval thr2_sigval;
size_t thr2_guardsize;
struct timespec thr2_ts;
static void
thr2 (union sigval sigval)
{
pthread_attr_t nattr;
int err = 0;
size_t guardsize = -1;
int ret = pthread_getattr_np (pthread_self (), &nattr);
if (ret)
{
errno = ret;
printf ("*** pthread_getattr_np failed: %m\n");
err = 1;
}
else
{
ret = pthread_attr_getguardsize (&nattr, &guardsize);
if (ret)
{
errno = ret;
printf ("*** pthread_attr_getguardsize failed: %m\n");
err = 1;
}
if (pthread_attr_destroy (&nattr) != 0)
{
puts ("*** pthread_attr_destroy failed");
err = 1;
}
}
pthread_mutex_lock (&lock);
thr2_err = clock_gettime (TEST_CLOCK, &thr2_ts) | err;
if (thr2_cnt >= 5)
{
struct itimerspec it = { };
thr2_err |= timer_settime (timer_thr2, 0, &it, NULL);
}
thr2_sigval = sigval;
++thr2_cnt;
thr2_guardsize = guardsize;
pthread_cond_signal (&cond);
pthread_mutex_unlock (&lock);
}
volatile int sig1_cnt, sig1_err;
volatile union sigval sig1_sigval;
struct timespec sig1_ts;
static void
sig1_handler (int sig,