[posix] POSIX standard implementation for PSE51 (#5384)

* [posix] POSIX standard implementation for PSE51

- add some posix's interfaces that we haven't before.
- these PR have passed the interface definition test across gcc platfrom;
- have tested base on qemu-a9 and stm32h750-art-pi.

* [newlib] only enable POSIX.1-1990

* update projects
This commit is contained in:
Man, Jianting (Meco)
2021-12-17 02:34:17 -05:00
committed by GitHub
parent 417efc370f
commit 6369e89502
480 changed files with 25486 additions and 21117 deletions

View File

@@ -72,6 +72,7 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz);
struct tm *gmtime_r(const time_t *timep, struct tm *r);
#endif
#ifdef RT_USING_POSIX_CLOCK
/* POSIX clock and timer */
#define MILLISECOND_PER_SECOND 1000UL
#define MICROSECOND_PER_SECOND 1000000UL
@@ -101,7 +102,10 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r);
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);
int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
int rt_timespec_to_tick(const struct timespec *time);
#endif /* RT_USING_POSIX_CLOCK */
/* timezone */
void tz_set(int8_t tz);

View File

@@ -486,6 +486,8 @@ RTM_EXPORT(settimeofday);
RTM_EXPORT(difftime);
RTM_EXPORT(strftime);
#ifdef RT_USING_POSIX_CLOCK
#include <delay.h>
#ifdef RT_USING_RTC
static volatile struct timeval _timevalue;
static int _rt_clock_time_system_init()
@@ -612,6 +614,17 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
}
RTM_EXPORT(clock_gettime);
int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
{
if ((clockid != CLOCK_REALTIME) || (rqtp == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
return nanosleep(rqtp, rmtp);
}
int clock_settime(clockid_t clockid, const struct timespec *tp)
{
#ifndef RT_USING_RTC
@@ -655,6 +668,33 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
}
RTM_EXPORT(clock_settime);
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
uint32_t time_ms = rqtp->tv_sec * 1000;
uint32_t time_us = rqtp->tv_nsec / 1000;
time_ms += time_us / 1000 ;
time_us = time_us % 1000;
if (rt_thread_self() != RT_NULL)
{
rt_thread_mdelay(time_ms);
}
else /* scheduler has not run yet */
{
while(time_ms > 0)
{
udelay(1000u);
time_ms -= 1;
}
}
udelay(time_us);
return 0;
}
RTM_EXPORT(nanosleep);
int rt_timespec_to_tick(const struct timespec *time)
{
int tick;
@@ -688,6 +728,8 @@ int rt_timespec_to_tick(const struct timespec *time)
}
RTM_EXPORT(rt_timespec_to_tick);
#endif /* RT_USING_POSIX_CLOCK */
/* timezone */
#ifndef RT_LIBC_DEFAULT_TIMEZONE
#define RT_LIBC_DEFAULT_TIMEZONE 8

View File

@@ -5,7 +5,6 @@ src = []
cwd = GetCurrentDir()
group = []
LIBS = ['m'] # link libm
CPPDEFINES = ['RT_USING_NEWLIB']
CPPPATH = [cwd]
if rtconfig.PLATFORM == 'gcc':
@@ -15,6 +14,9 @@ if rtconfig.PLATFORM == 'gcc':
else:
src += ['syscalls.c']
# identify this is Newlib, and only enable POSIX.1-1990
CPPDEFINES = ['RT_USING_NEWLIB', '_POSIX_C_SOURCE=1']
group = DefineGroup('libc', src, depend = [], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS)
Return('group')

View File

@@ -4,9 +4,4 @@
#include <rtconfig.h>
#define _CLOCKS_PER_SEC_ RT_TICK_PER_SECOND
#ifdef __SPU__
#include <sys/_timespec.h>
int nanosleep (const struct timespec *, struct timespec *);
#endif
#endif /* _MACHTIME_H_ */

View File

@@ -39,6 +39,12 @@ config RT_USING_POSIX_DELAY
bool "Enable delay APIs, sleep()/usleep()/msleep() etc"
default n
config RT_USING_POSIX_CLOCK
bool "Enable clock/time functions, clock_gettime()/clock_settime()/clock_getres() etc"
select RT_LIBC_USING_TIME if !RT_USING_LIBC
select RT_USING_POSIX_DELAY
default n
config RT_USING_POSIX_GETLINE
bool "Enable getline()/getdelim()"
select RT_USING_LIBC
@@ -47,6 +53,7 @@ config RT_USING_POSIX_GETLINE
config RT_USING_PTHREADS
bool "Enable pthreads APIs"
select RT_USING_POSIX_CLOCK
default n
if RT_USING_PTHREADS

View File

@@ -0,0 +1,11 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_DELAY'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -17,9 +17,6 @@
#include "libc.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef RT_USING_PTHREADS
#include <pthread.h>
#endif
int libc_system_init(void)
{
@@ -32,11 +29,6 @@ int libc_system_init(void)
libc_stdio_set_console(dev_console->parent.name, O_RDWR);
}
#endif /* RT_USING_POSIX_DEVIO */
#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT
pthread_system_init();
#endif
return 0;
}
INIT_COMPONENT_EXPORT(libc_system_init);

View File

@@ -12,6 +12,25 @@ config RT_USING_POSIX_PIPE_SIZE
depends on RT_USING_POSIX_PIPE
default 512
# We have't implement of 'systemv ipc', so hide it firstly.
#
# config RT_USING_POSIX_IPC_SYSTEM_V
# bool "Enable System V IPC"
# default n
# help
# System V supplies an alternative form of interprocess communication consisting of thress
# features: shared memory, message, and semaphores.
config RT_USING_POSIX_MESSAGE_QUEUE
bool "Enable posix message queue <mqueue.h>"
select RT_USING_POSIX_CLOCK
default n
config RT_USING_POSIX_MESSAGE_SEMAPHORE
bool "Enable posix semaphore <semaphore.h>"
select RT_USING_POSIX_CLOCK
default n
comment "Socket is in the 'Network' category"
endmenu

View File

@@ -0,0 +1,20 @@
from building import *
cwd = GetCurrentDir()
src = []
inc = [cwd]
# We have't implement of 'systemv ipc', so hide it firstly.
# if GetDepend('RT_USING_POSIX_IPC_SYSTEM_V'):
# src += Glob('system-v/*.c')
# inc += [cwd + '/system-v']
if GetDepend('RT_USING_POSIX_MESSAGE_QUEUE'):
src += ['mqueue.c']
if GetDepend('RT_USING_POSIX_MESSAGE_SEMAPHORE'):
src += ['semaphore.c']
group = DefineGroup('POSIX', src, depend = [], CPPPATH = inc)
Return('group')

View File

@@ -8,16 +8,23 @@
*/
#include <string.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <rtthread.h>
#include "mqueue.h"
#include "pthread_internal.h"
static mqd_t posix_mq_list = RT_NULL;
static struct rt_semaphore posix_mq_lock;
void posix_mq_system_init()
/* initialize posix mqueue */
static int posix_mq_system_init(void)
{
rt_sem_init(&posix_mq_lock, "pmq", 1, RT_IPC_FLAG_FIFO);
return 0;
}
INIT_COMPONENT_EXPORT(posix_mq_system_init);
rt_inline void posix_mq_insert(mqd_t pmq)
{

View File

@@ -10,8 +10,9 @@
#ifndef __MQUEUE_H__
#define __MQUEUE_H__
#include <rtthread.h>
#include <pthread.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <rtdef.h>
struct mqdes
{

View File

@@ -10,15 +10,20 @@
#include <rtthread.h>
#include <string.h>
#include <fcntl.h>
#include <sys/errno.h>
#include "semaphore.h"
#include "pthread_internal.h"
static sem_t *posix_sem_list = RT_NULL;
static struct rt_semaphore posix_sem_lock;
void posix_sem_system_init()
/* initialize posix semaphore */
static int posix_sem_system_init(void)
{
rt_sem_init(&posix_sem_lock, "psem", 1, RT_IPC_FLAG_FIFO);
return 0;
}
INIT_COMPONENT_EXPORT(posix_sem_system_init);
rt_inline void posix_sem_insert(sem_t *psem)
{

View File

@@ -11,8 +11,8 @@
#ifndef __POSIX_SEMAPHORE_H__
#define __POSIX_SEMAPHORE_H__
#include <rtthread.h>
#include <pthread.h>
#include <rtdef.h>
#include <sys/time.h>
struct posix_sem
{

View File

@@ -10,6 +10,4 @@
#ifndef __SYS_SEM_H__
#define __SYS_SEM_H__
#endif

View File

@@ -1,22 +1,9 @@
from building import *
from utils import VersionCmp
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
CPPDEFINES = []
# only enable POSIX.1b-1993 Real-time extensions
libc_ver = GetDepend('LIBC_VERSION')
try:
ver = libc_ver.split(' ')
ver = ver[1]
if VersionCmp(ver, "2.5.0") == 1:
CPPDEFINES = ['_POSIX_C_SOURCE=199309L']
except :
pass
group = DefineGroup('POSIX', src,
depend = ['RT_USING_PTHREADS', 'RT_USING_LIBC'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
group = DefineGroup('POSIX', src, depend = ['RT_USING_PTHREADS'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -24,4 +24,3 @@
#include <fcntl.h>
#endif

View File

@@ -18,6 +18,7 @@
RT_DEFINE_SPINLOCK(pth_lock);
_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
static int concurrency_level;
_pthread_data_t *_pthread_get_data(pthread_t thread)
{
@@ -151,19 +152,6 @@ void _pthread_data_destroy(pthread_t pth)
}
}
int pthread_system_init(void)
{
/* initialize key area */
pthread_key_system_init();
/* initialize posix mqueue */
posix_mq_system_init();
/* initialize posix semaphore */
posix_sem_system_init();
return 0;
}
INIT_COMPONENT_EXPORT(pthread_system_init);
static void _pthread_destroy(_pthread_data_t *ptd)
{
pthread_t pth = _pthread_data_get_pth(ptd);
@@ -451,6 +439,70 @@ pthread_t pthread_self (void)
}
RTM_EXPORT(pthread_self);
int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id)
{
if(_pthread_get_data(thread) == NULL)
{
return EINVAL;
}
*clock_id = (clockid_t)rt_tick_get();
return 0;
}
RTM_EXPORT(pthread_getcpuclockid);
int pthread_getconcurrency(void)
{
return concurrency_level;
}
RTM_EXPORT(pthread_getconcurrency);
int pthread_setconcurrency(int new_level)
{
concurrency_level = new_level;
return 0;
}
RTM_EXPORT(pthread_setconcurrency);
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
{
_pthread_data_t *ptd;
ptd = _pthread_get_data(thread);
pthread_attr_getschedpolicy(&ptd->attr, policy);
pthread_attr_getschedparam(&ptd->attr, param);
return 0;
}
RTM_EXPORT(pthread_getschedparam);
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
{
_pthread_data_t *ptd;
ptd = _pthread_get_data(thread);
pthread_attr_setschedpolicy(&ptd->attr, policy);
pthread_attr_setschedparam(&ptd->attr, param);
return 0;
}
RTM_EXPORT(pthread_setschedparam);
int pthread_setschedprio(pthread_t thread, int prio)
{
_pthread_data_t *ptd;
struct sched_param param;
ptd = _pthread_get_data(thread);
param.sched_priority = prio;
pthread_attr_setschedparam(&ptd->attr, &param);
return 0;
}
RTM_EXPORT(pthread_setschedprio);
void pthread_exit(void *value)
{
_pthread_data_t *ptd;

View File

@@ -158,9 +158,10 @@ int pthread_attr_getstack(pthread_attr_t const *attr,
size_t *stack_size);
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size);
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
int pthread_attr_getscope(pthread_attr_t const *attr);
int pthread_system_init(void);
int pthread_attr_getscope(pthread_attr_t const *attr, int *scope);
int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
void *(*start) (void *), void *arg);
@@ -174,6 +175,13 @@ rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
pthread_t pthread_self (void);
int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id);
int pthread_getconcurrency(void);
int pthread_setconcurrency(int new_level);
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);
int pthread_setschedprio(pthread_t thread, int prio);
void pthread_exit (void *value_ptr);
int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
@@ -196,6 +204,8 @@ int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling);
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
@@ -203,6 +213,11 @@ int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling);
int pthread_mutexattr_setprioceiling(const pthread_mutexattr_t *attr, int prioceiling);
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol);
int pthread_mutexattr_setprotocol(const pthread_mutexattr_t *attr, int protocol);
/* pthread condition interface */
int pthread_condattr_destroy(pthread_condattr_t *attr);

View File

@@ -189,6 +189,26 @@ int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
}
RTM_EXPORT(pthread_attr_getguardsize);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
{
RT_ASSERT(attr != RT_NULL);
attr->inheritsched = inheritsched;
return 0;
}
RTM_EXPORT(pthread_attr_setinheritsched);
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
{
RT_ASSERT(attr != RT_NULL);
*inheritsched = attr->inheritsched;
return 0;
}
RTM_EXPORT(pthread_attr_getinheritsched);
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
{
if (scope == PTHREAD_SCOPE_SYSTEM)
@@ -200,7 +220,7 @@ int pthread_attr_setscope(pthread_attr_t *attr, int scope)
}
RTM_EXPORT(pthread_attr_setscope);
int pthread_attr_getscope(pthread_attr_t const *attr)
int pthread_attr_getscope(pthread_attr_t const *attr, int *scope)
{
return PTHREAD_SCOPE_SYSTEM;
}

View File

@@ -63,8 +63,4 @@ typedef struct _pthread_data _pthread_data_t;
_pthread_data_t *_pthread_get_data(pthread_t thread);
void posix_mq_system_init(void);
void posix_sem_system_init(void);
void pthread_key_system_init(void);
#endif

View File

@@ -246,3 +246,45 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex)
return EBUSY;
}
RTM_EXPORT(pthread_mutex_trylock);
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
{
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_getprioceiling);
int pthread_mutexattr_setprioceiling(const pthread_mutexattr_t *attr, int prioceiling)
{
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_setprioceiling);
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
{
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_getprotocol);
int pthread_mutexattr_setprotocol(const pthread_mutexattr_t *attr, int protocol)
{
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_setprotocol);
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)
{
return pthread_mutexattr_getprioceiling(&mutex->attr, prioceiling);
}
RTM_EXPORT(pthread_mutex_getprioceiling);
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling)
{
*old_ceiling = pthread_mutexattr_getprioceiling(&mutex->attr, old_ceiling);
if(*old_ceiling != 0)
{
return EINVAL;
}
return pthread_mutexattr_setprioceiling(&mutex->attr, prioceiling);
}
RTM_EXPORT(pthread_mutex_setprioceiling);

View File

@@ -13,10 +13,13 @@
_pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
void pthread_key_system_init()
/* initialize key area */
static int pthread_key_system_init(void)
{
rt_memset(&_thread_keys[0], 0, sizeof(_thread_keys));
return 0;
}
INIT_COMPONENT_EXPORT(pthread_key_system_init);
void *pthread_getspecific(pthread_key_t key)
{

View File

@@ -40,3 +40,18 @@ int sched_setscheduler(pid_t pid, int policy)
return EOPNOTSUPP;
}
RTM_EXPORT(sched_setscheduler);
int sched_rr_get_interval(pid_t pid, struct timespec *tp)
{
if(pid != 0)
{
return EINVAL;
}
rt_set_errno(-EINVAL);
/* course model, don't support */
// TODO
return -1;
}
RTM_EXPORT(sched_rr_get_interval);

View File

@@ -31,6 +31,7 @@ extern "C"
int sched_yield(void);
int sched_get_priority_min(int policy);
int sched_get_priority_max(int policy);
int sched_rr_get_interval(pid_t pid, struct timespec *tp);
int sched_setscheduler(pid_t pid, int policy);
#ifdef __cplusplus

View File

@@ -1 +1,8 @@
This folder provides functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard.
This folder provides functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard.
## NOTE
1. For consistency of compilation results across the different of platforms(gcc, keil, iar) , you better use ``#include <sys/time.h>`` to instead of ``#include <time.h>``.

View File

@@ -10,10 +10,6 @@ group = []
flag = False
src += ['unistd.c'] #TODO
if GetDepend('RT_USING_POSIX_DELAY'):
src += ['delay.c']
flag = True
if flag == True:
group = DefineGroup('POSIX', src, depend = [], CPPPATH = CPPPATH)