mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-12-26 01:07:21 +00:00
the support for PSE51 (#5534)
* [add] the function realization of signal for posix. * [update] the posix support for armclang. * [add] the new macro "RT_USING_POSIX_TIMER". * [modify] select "RT_USING_SOFT_TIMER" when use posix'timer. * [bug] optimize the logic for the "time_xxx" functions. * [modify] use "RT_USING_POSIX_TIMER" to protect the macro definition. * [modify] error code when except happened. * [delete] the "environ" is useless at this time.
This commit is contained in:
@@ -28,6 +28,8 @@ typedef signed int ssize_t; /* Used for a count of bytes or an error
|
||||
#else
|
||||
typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */
|
||||
#endif
|
||||
typedef unsigned long __timer_t;
|
||||
typedef __timer_t timer_t;
|
||||
typedef long suseconds_t; /* microseconds. */
|
||||
typedef unsigned long useconds_t; /* microseconds (unsigned) */
|
||||
|
||||
|
||||
@@ -19,10 +19,13 @@
|
||||
#define STDOUT_FILENO 1 /* standard output file descriptor */
|
||||
#define STDERR_FILENO 2 /* standard error file descriptor */
|
||||
|
||||
unsigned alarm(unsigned __secs);
|
||||
ssize_t read(int fd, void *buf, size_t len);
|
||||
ssize_t write(int fd, const void *buf, size_t len);
|
||||
off_t lseek(int fd, off_t offset, int whence);
|
||||
int pause(void);
|
||||
int fsync(int fildes);
|
||||
long sysconf(int __name);
|
||||
int unlink(const char *pathname);
|
||||
int close(int d);
|
||||
int ftruncate(int fd, off_t length);
|
||||
|
||||
@@ -17,6 +17,19 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
/* sigev_notify values
|
||||
NOTE: P1003.1c/D10, p. 34 adds SIGEV_THREAD. */
|
||||
|
||||
#define SIGEV_NONE 1 /* No asynchronous notification shall be delivered */
|
||||
/* when the event of interest occurs. */
|
||||
#define SIGEV_SIGNAL 2 /* A queued signal, with an application defined */
|
||||
/* value, shall be delivered when the event of */
|
||||
/* interest occurs. */
|
||||
#define SIGEV_THREAD 3 /* A notification function shall be called to */
|
||||
/* perform notification. */
|
||||
|
||||
/* Signal Generation and Delivery, P1003.1b-1993, p. 63
|
||||
NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
|
||||
@@ -62,6 +75,16 @@ struct sigaction
|
||||
int sa_flags;
|
||||
};
|
||||
|
||||
/*
|
||||
* Structure used in sigaltstack call.
|
||||
*/
|
||||
typedef struct sigaltstack
|
||||
{
|
||||
void *ss_sp; /* Stack base or pointer. */
|
||||
int ss_flags; /* Flags. */
|
||||
size_t ss_size; /* Stack size. */
|
||||
} stack_t;
|
||||
|
||||
#define SIG_SETMASK 0 /* set mask with sigprocmask() */
|
||||
#define SIG_BLOCK 1 /* set of signals to block */
|
||||
#define SIG_UNBLOCK 2 /* set of signals to, well, unblock */
|
||||
@@ -73,6 +96,15 @@ struct sigaction
|
||||
#define sigismember(what,sig) (((*(what)) & (1<<(sig))) != 0)
|
||||
|
||||
int sigprocmask (int how, const sigset_t *set, sigset_t *oset);
|
||||
int sigpending (sigset_t *set);
|
||||
int sigsuspend (const sigset_t *set);
|
||||
|
||||
#include "time.h"
|
||||
int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout);
|
||||
int sigwait(const sigset_t *set, int *sig);
|
||||
int sigwaitinfo(const sigset_t *set, siginfo_t *info);
|
||||
int raise(int sig);
|
||||
int sigqueue(pid_t pid, int signo, const union sigval value);
|
||||
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
|
||||
|
||||
#ifdef __ARMCC_VERSION
|
||||
@@ -100,6 +132,9 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
|
||||
#define SIGTTOU 22
|
||||
#define SIGPOLL 23
|
||||
#define SIGWINCH 24
|
||||
#define SIGXCPU 24 /* exceeded CPU time limit */
|
||||
#define SIGXFSZ 25 /* exceeded file size limit */
|
||||
#define SIGVTALRM 26 /* virtual time alarm */
|
||||
/* #define SIGUSR1 25 */
|
||||
/* #define SIGUSR2 26 */
|
||||
#define SIGRTMIN 27
|
||||
@@ -133,6 +168,9 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
|
||||
#define SIGTTOU 22
|
||||
#define SIGPOLL 23
|
||||
#define SIGWINCH 24
|
||||
#define SIGXCPU 24 /* exceeded CPU time limit */
|
||||
#define SIGXFSZ 25 /* exceeded file size limit */
|
||||
#define SIGVTALRM 26 /* virtual time alarm */
|
||||
#define SIGUSR1 25
|
||||
#define SIGUSR2 26
|
||||
#define SIGRTMIN 27
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@@ -60,6 +60,16 @@ struct timespec
|
||||
time_t tv_sec; /* seconds */
|
||||
long tv_nsec; /* and nanoseconds */
|
||||
};
|
||||
|
||||
/*
|
||||
* Structure defined by POSIX.1b to be like a itimerval, but with
|
||||
* timespecs. Used in the timer_*() system calls.
|
||||
*/
|
||||
struct itimerspec
|
||||
{
|
||||
struct timespec it_interval;
|
||||
struct timespec it_value;
|
||||
};
|
||||
#endif
|
||||
|
||||
int stime(const time_t *t);
|
||||
@@ -68,6 +78,9 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
|
||||
int settimeofday(const struct timeval *tv, const struct timezone *tz);
|
||||
#if defined(__ARMCC_VERSION) || defined (__ICCARM__)
|
||||
struct tm *gmtime_r(const time_t *timep, struct tm *r);
|
||||
struct tm* localtime_r(const time_t* t, struct tm* r);
|
||||
char* asctime_r(const struct tm *t, char *buf);
|
||||
char *ctime_r(const time_t * tim_p, char * result);
|
||||
#elif defined(_WIN32)
|
||||
struct tm* gmtime_r(const time_t* timep, struct tm* r);
|
||||
struct tm* gmtime(const time_t* t);
|
||||
@@ -84,7 +97,7 @@ time_t time(time_t* t);
|
||||
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
|
||||
#endif /* RT_USING_POSIX_DELAY */
|
||||
|
||||
#ifdef RT_USING_POSIX_CLOCK
|
||||
#if defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER)
|
||||
/* POSIX clock and timer */
|
||||
#define MILLISECOND_PER_SECOND 1000UL
|
||||
#define MICROSECOND_PER_SECOND 1000000UL
|
||||
@@ -110,7 +123,9 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
|
||||
#ifndef CLOCK_MONOTONIC
|
||||
#define CLOCK_MONOTONIC 4
|
||||
#endif
|
||||
#endif /* defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) */
|
||||
|
||||
#ifdef RT_USING_POSIX_CLOCK
|
||||
int clock_getres (clockid_t clockid, struct timespec *res);
|
||||
int clock_gettime (clockid_t clockid, struct timespec *tp);
|
||||
int clock_settime (clockid_t clockid, const struct timespec *tp);
|
||||
@@ -118,6 +133,16 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s
|
||||
int rt_timespec_to_tick(const struct timespec *time);
|
||||
#endif /* RT_USING_POSIX_CLOCK */
|
||||
|
||||
#ifdef RT_USING_POSIX_TIMER
|
||||
#include "signal.h"
|
||||
int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
|
||||
int timer_delete(timer_t timerid);
|
||||
int timer_getoverrun(timer_t timerid);
|
||||
int timer_gettime(timer_t timerid, struct itimerspec *its);
|
||||
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
|
||||
struct itimerspec *ovalue);
|
||||
#endif /* RT_USING_POSIX_TIMER */
|
||||
|
||||
/* timezone */
|
||||
void tz_set(int8_t tz);
|
||||
int8_t tz_get(void);
|
||||
|
||||
@@ -718,6 +718,269 @@ RTM_EXPORT(rt_timespec_to_tick);
|
||||
|
||||
#endif /* RT_USING_POSIX_CLOCK */
|
||||
|
||||
#ifdef RT_USING_POSIX_TIMER
|
||||
|
||||
#define ACTIVE 1
|
||||
#define NOT_ACTIVE 0
|
||||
|
||||
struct timer_obj
|
||||
{
|
||||
struct rt_timer timer;
|
||||
void (*sigev_notify_function)(union sigval val);
|
||||
union sigval val;
|
||||
struct timespec interval; /* Reload value */
|
||||
rt_uint32_t reload; /* Reload value in ms */
|
||||
rt_uint32_t status;
|
||||
};
|
||||
|
||||
static void rtthread_timer_wrapper(void *timerobj)
|
||||
{
|
||||
struct timer_obj *timer;
|
||||
|
||||
timer = (struct timer_obj *)timerobj;
|
||||
|
||||
if (timer->reload == 0U)
|
||||
{
|
||||
timer->status = NOT_ACTIVE;
|
||||
}
|
||||
|
||||
if(timer->sigev_notify_function != RT_NULL)
|
||||
{
|
||||
(timer->sigev_notify_function)(timer->val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a per-process timer.
|
||||
*
|
||||
* This API does not accept SIGEV_THREAD as valid signal event notification
|
||||
* type.
|
||||
*
|
||||
* See IEEE 1003.1
|
||||
*/
|
||||
int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
|
||||
{
|
||||
static int num = 0;
|
||||
struct timer_obj *timer;
|
||||
char timername[RT_NAME_MAX] = {0};
|
||||
|
||||
if (clockid != CLOCK_MONOTONIC || evp == NULL ||
|
||||
(evp->sigev_notify != SIGEV_NONE &&
|
||||
evp->sigev_notify != SIGEV_SIGNAL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
timer = rt_malloc(sizeof(struct timer_obj));
|
||||
if(timer == RT_NULL)
|
||||
{
|
||||
rt_set_errno(ENOMEM);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
RT_ASSERT(evp->sigev_notify_function != RT_NULL);
|
||||
rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
|
||||
num %= 100;
|
||||
timer->sigev_notify_function = evp->sigev_notify_function;
|
||||
timer->val = evp->sigev_value;
|
||||
timer->interval.tv_sec = 0;
|
||||
timer->interval.tv_nsec = 0;
|
||||
timer->reload = 0U;
|
||||
timer->status = NOT_ACTIVE;
|
||||
|
||||
if (evp->sigev_notify == SIGEV_NONE)
|
||||
{
|
||||
rt_timer_init(&timer->timer, timername, RT_NULL, RT_NULL, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_timer_init(&timer->timer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
|
||||
}
|
||||
|
||||
*timerid = (timer_t)timer;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(timer_create);
|
||||
|
||||
/**
|
||||
* @brief Delete a per-process timer.
|
||||
*
|
||||
* See IEEE 1003.1
|
||||
*/
|
||||
int timer_delete(timer_t timerid)
|
||||
{
|
||||
struct timer_obj *timer = (struct timer_obj *)timerid;
|
||||
|
||||
if (timer == RT_NULL)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (timer->status == ACTIVE)
|
||||
{
|
||||
timer->status = NOT_ACTIVE;
|
||||
rt_timer_stop(&timer->timer);
|
||||
}
|
||||
|
||||
rt_free(timer);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(timer_delete);
|
||||
|
||||
/**
|
||||
*
|
||||
* Return the overrun count for the last timer expiration.
|
||||
* It is subefficient to create a new structure to get overrun count.
|
||||
**/
|
||||
int timer_getoverrun(timer_t timerid)
|
||||
{
|
||||
rt_set_errno(ENOSYS);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get amount of time left for expiration on a per-process timer.
|
||||
*
|
||||
* See IEEE 1003.1
|
||||
*/
|
||||
int timer_gettime(timer_t timerid, struct itimerspec *its)
|
||||
{
|
||||
struct timer_obj *timer = (struct timer_obj *)timerid;
|
||||
rt_tick_t remaining;
|
||||
rt_uint32_t seconds, nanoseconds;
|
||||
rt_int64_t nsecs, secs;
|
||||
|
||||
if (timer == NULL)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (its == NULL)
|
||||
{
|
||||
rt_set_errno(EFAULT);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (timer->status == ACTIVE)
|
||||
{
|
||||
rt_tick_t remain_tick;
|
||||
|
||||
rt_timer_control(&timer->timer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
|
||||
|
||||
/* 'remain_tick' is minimum-unit in the RT-Thread' timer,
|
||||
* so the seconds, nanoseconds will be calculated by 'remain_tick'.
|
||||
*/
|
||||
remaining = remain_tick - rt_tick_get();
|
||||
|
||||
/* calculate 'second' */
|
||||
seconds = remaining / RT_TICK_PER_SECOND;
|
||||
|
||||
/* calculate 'nanosecond'; To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
|
||||
*
|
||||
* remain_tick millisecond remain_tick * MILLISECOND_PER_SECOND
|
||||
* ------------------------- = -------------------------- ---> millisecond = -------------------------------------------
|
||||
* RT_TICK_PER_SECOND MILLISECOND_PER_SECOND RT_TICK_PER_SECOND
|
||||
*
|
||||
* remain_tick * MILLISECOND_PER_SECOND remain_tick * MILLISECOND_PER_SECOND * MICROSECOND_PER_SECOND
|
||||
* millisecond = ---------------------------------------- ---> nanosecond = -------------------------------------------------------------------
|
||||
* RT_TICK_PER_SECOND RT_TICK_PER_SECOND
|
||||
*
|
||||
*/
|
||||
nanoseconds = (((remaining % RT_TICK_PER_SECOND) * MILLISECOND_PER_SECOND) * MICROSECOND_PER_SECOND) / RT_TICK_PER_SECOND ;
|
||||
|
||||
its->it_value.tv_sec = (rt_int32_t)seconds;
|
||||
its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Timer is disarmed */
|
||||
its->it_value.tv_sec = 0;
|
||||
its->it_value.tv_nsec = 0;
|
||||
}
|
||||
|
||||
/* The interval last set by timer_settime() */
|
||||
its->it_interval = timer->interval;
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(timer_gettime);
|
||||
|
||||
/**
|
||||
* @brief Sets expiration time of per-process timer.
|
||||
*
|
||||
* See IEEE 1003.1
|
||||
*/
|
||||
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
|
||||
struct itimerspec *ovalue)
|
||||
{
|
||||
struct timer_obj *timer = (struct timer_obj *)timerid;
|
||||
rt_uint32_t duration, current;
|
||||
|
||||
if (timer == NULL ||
|
||||
value->it_interval.tv_nsec < 0 ||
|
||||
value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
|
||||
value->it_value.tv_nsec < 0 ||
|
||||
value->it_value.tv_nsec >= NANOSECOND_PER_SECOND)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (value == NULL || ovalue == NULL)
|
||||
{
|
||||
rt_set_errno(EFAULT);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* Save time to expire and old reload value. */
|
||||
if (ovalue != NULL)
|
||||
{
|
||||
timer_gettime(timerid, ovalue);
|
||||
}
|
||||
|
||||
/* Stop the timer if the value is 0 */
|
||||
if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
|
||||
{
|
||||
if (timer->status == ACTIVE)
|
||||
{
|
||||
rt_timer_stop(&timer->timer);
|
||||
}
|
||||
|
||||
timer->status = NOT_ACTIVE;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/* calculate timer period(tick); To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
|
||||
*
|
||||
* tick nanosecond nanosecond * RT_TICK_PER_SECOND
|
||||
* ------------------------- = -------------------------- ---> tick = -------------------------------------
|
||||
* RT_TICK_PER_SECOND NANOSECOND_PER_SECOND NANOSECOND_PER_SECOND
|
||||
*
|
||||
*/
|
||||
timer->reload = (value->it_interval.tv_sec * RT_TICK_PER_SECOND) + (value->it_interval.tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
|
||||
timer->interval.tv_sec = value->it_interval.tv_sec;
|
||||
timer->interval.tv_nsec = value->it_interval.tv_nsec;
|
||||
|
||||
if (timer->status == ACTIVE)
|
||||
{
|
||||
rt_timer_stop(&timer->timer);
|
||||
}
|
||||
|
||||
timer->status = ACTIVE;
|
||||
rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, (void *)timer->reload);
|
||||
rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
|
||||
rt_timer_start(&timer->timer);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(timer_settime);
|
||||
#endif /* RT_USING_POSIX_TIMER */
|
||||
|
||||
|
||||
/* timezone */
|
||||
#ifndef RT_LIBC_DEFAULT_TIMEZONE
|
||||
#define RT_LIBC_DEFAULT_TIMEZONE 8
|
||||
|
||||
Reference in New Issue
Block a user