2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2021-06-10 07:51:27 +02:00
|
|
|
* apps/testing/ostest/posixtimer.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Included Files
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "ostest.h"
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Definitions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
# define NULL (void*)0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MY_TIMER_SIGNAL 17
|
|
|
|
#define SIGVALUE_INT 42
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Data
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
static sem_t sem;
|
|
|
|
static int g_nsigreceived = 0;
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
static void timer_expiration(int signo, siginfo_t *info, void *ucontext)
|
|
|
|
{
|
|
|
|
sigset_t oldset;
|
|
|
|
sigset_t allsigs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
printf("timer_expiration: Received signal %d\n" , signo);
|
|
|
|
|
|
|
|
g_nsigreceived++;
|
|
|
|
|
|
|
|
/* Check signo */
|
|
|
|
|
|
|
|
if (signo != MY_TIMER_SIGNAL)
|
|
|
|
{
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_expiration: ERROR expected signo=%d\n",
|
|
|
|
MY_TIMER_SIGNAL);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check siginfo */
|
|
|
|
|
|
|
|
if (info->si_value.sival_int != SIGVALUE_INT)
|
|
|
|
{
|
|
|
|
printf("timer_expiration: ERROR sival_int=%d expected %d\n",
|
|
|
|
info->si_value.sival_int, SIGVALUE_INT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("timer_expiration: sival_int=%d\n" , info->si_value.sival_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->si_signo != MY_TIMER_SIGNAL)
|
|
|
|
{
|
|
|
|
printf("timer_expiration: ERROR expected si_signo=%d, got=%d\n",
|
|
|
|
MY_TIMER_SIGNAL, info->si_signo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->si_code == SI_TIMER)
|
|
|
|
{
|
|
|
|
printf("timer_expiration: si_code=%d (SI_TIMER)\n" , info->si_code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("timer_expiration: ERROR si_code=%d, expected SI_TIMER=%d\n",
|
|
|
|
info->si_code, SI_TIMER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check ucontext_t */
|
|
|
|
|
|
|
|
printf("timer_expiration: ucontext=%p\n" , ucontext);
|
|
|
|
|
|
|
|
/* Check sigprocmask */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&allsigs);
|
2011-03-20 19:18:19 +01:00
|
|
|
status = sigprocmask(SIG_SETMASK, NULL, &oldset);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_expiration: ERROR sigprocmask failed, status=%d\n",
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldset != allsigs)
|
|
|
|
{
|
2020-11-13 04:33:34 +01:00
|
|
|
printf("timer_expiration: ERROR sigprocmask=%jx expected=%jx\n",
|
|
|
|
(uintmax_t)oldset, (uintmax_t)allsigs);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
void timer_test(void)
|
|
|
|
{
|
2015-07-31 21:31:44 +02:00
|
|
|
sigset_t set;
|
2011-03-20 19:18:19 +01:00
|
|
|
struct sigaction act;
|
|
|
|
struct sigaction oact;
|
|
|
|
struct sigevent notify;
|
|
|
|
struct itimerspec timer;
|
|
|
|
timer_t timerid;
|
|
|
|
int status;
|
|
|
|
int i;
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Initializing semaphore to 0\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
sem_init(&sem, 0, 0);
|
|
|
|
|
|
|
|
/* Start waiter thread */
|
|
|
|
|
|
|
|
printf("timer_test: Unmasking signal %d\n" , MY_TIMER_SIGNAL);
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, MY_TIMER_SIGNAL);
|
2015-07-31 21:31:44 +02:00
|
|
|
status = sigprocmask(SIG_UNBLOCK, &set, NULL);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_test: ERROR sigprocmask failed, status=%d\n",
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Registering signal handler\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
act.sa_sigaction = timer_expiration;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
sigdelset(&act.sa_mask, MY_TIMER_SIGNAL);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
status = sigaction(MY_TIMER_SIGNAL, &act, &oact);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_test: ERROR sigaction failed, status=%d\n" , status);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SDCC
|
2020-11-13 04:33:34 +01:00
|
|
|
printf("timer_test: oact.sigaction=%p oact.sa_flags=%x oact.sa_mask=%jx\n",
|
|
|
|
oact.sa_sigaction, oact.sa_flags, (uintmax_t)oact.sa_mask);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create the POSIX timer */
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Creating timer\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-12-30 23:29:21 +01:00
|
|
|
notify.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
notify.sigev_signo = MY_TIMER_SIGNAL;
|
|
|
|
notify.sigev_value.sival_int = SIGVALUE_INT;
|
|
|
|
#ifdef CONFIG_SIG_EVTHREAD
|
|
|
|
notify.sigev_notify_function = NULL;
|
|
|
|
notify.sigev_notify_attributes = NULL;
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
status = timer_create(CLOCK_REALTIME, ¬ify, &timerid);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_test: timer_create failed, errno=%d\n", errno);
|
|
|
|
goto errorout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the POSIX timer */
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Starting timer\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
timer.it_value.tv_sec = 2;
|
|
|
|
timer.it_value.tv_nsec = 0;
|
|
|
|
timer.it_interval.tv_sec = 2;
|
|
|
|
timer.it_interval.tv_nsec = 0;
|
|
|
|
|
|
|
|
status = timer_settime(timerid, 0, &timer, NULL);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_test: timer_settime failed, errno=%d\n", errno);
|
|
|
|
goto errorout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take the semaphore */
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
{
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Waiting on semaphore\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
FFLUSH();
|
|
|
|
status = sem_wait(&sem);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
int error = errno;
|
|
|
|
if (error == EINTR)
|
|
|
|
{
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: sem_wait() successfully interrupted "
|
|
|
|
"by signal\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: ERROR sem_wait failed, errno=%d\n", error);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: ERROR awakened with no error!\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2020-11-13 04:32:18 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("timer_test: g_nsigreceived=%d\n", g_nsigreceived);
|
|
|
|
}
|
|
|
|
|
|
|
|
errorout:
|
|
|
|
sem_destroy(&sem);
|
|
|
|
|
|
|
|
/* Then delete the timer */
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: Deleting timer\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
status = timer_delete(timerid);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
|
|
|
printf("timer_test: timer_create failed, errno=%d\n", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach the signal handler */
|
|
|
|
|
2016-06-09 02:02:37 +02:00
|
|
|
act.sa_handler = SIG_DFL;
|
2011-03-20 19:18:19 +01:00
|
|
|
status = sigaction(MY_TIMER_SIGNAL, &act, &oact);
|
|
|
|
|
2020-11-13 04:32:18 +01:00
|
|
|
printf("timer_test: done\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
FFLUSH();
|
|
|
|
}
|