This commit is contained in:
2024-08-05 20:57:09 +08:00
commit 46d9ee7795
3020 changed files with 1725767 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
menu "POSIX (Portable Operating System Interface) layer"
config RT_USING_POSIX_FS
bool "Enable POSIX file system and I/O"
select RT_USING_DFS
select DFS_USING_POSIX
default n
if RT_USING_POSIX_FS
config RT_USING_POSIX_DEVIO
bool "Enable devices as file descriptors"
select RT_USING_DFS_DEVFS
default n
config RT_USING_POSIX_STDIO
bool "Enable standard I/O devices, e.g. STDOUT_FILENO"
select RT_USING_POSIX_DEVIO
default n
config RT_USING_POSIX_POLL
bool "Enable I/O Multiplexing poll() <poll.h>"
default y if RT_USING_SMART
default n
config RT_USING_POSIX_SELECT
bool "Enable I/O Multiplexing select() <sys/select.h>"
select RT_USING_POSIX_POLL
default y if RT_USING_SMART
default n
config RT_USING_POSIX_EVENTFD
bool "Enable I/O event eventfd <sys/eventfd.h>"
select RT_USING_POSIX_POLL
default y if RT_USING_SMART
default n
if RT_USING_SMART
config RT_USING_POSIX_EPOLL
bool "Enable I/O Multiplexing epoll <sys/epoll.h>"
select RT_USING_POSIX_POLL
default y
config RT_USING_POSIX_SIGNALFD
bool "Enable Signalfd <sys/signalfd.h>"
select RT_USING_POSIX_POLL
default y
if RT_USING_POSIX_SIGNALFD
config RT_SIGNALFD_MAX_NUM
int "signaled The maximum number of concurrent firing signals"
range 1 20
default 10
endif
endif
config RT_USING_POSIX_TIMERFD
bool "Enable I/O timerfd <sys/timerfd.h>"
default y if RT_USING_SMART
default n
config RT_USING_POSIX_SOCKET
bool "Enable BSD Socket I/O <sys/socket.h> <netdb.h>"
select RT_USING_POSIX_SELECT
select RT_USING_SAL
default n
config RT_USING_POSIX_TERMIOS
bool "Enable Terminal I/O <termios.h>"
select RT_USING_POSIX_STDIO
default n
config RT_USING_POSIX_AIO
bool "Enable Asynchronous I/O <aio.h>"
default n
config RT_USING_POSIX_MMAN
bool "Enable Memory-Mapped I/O <sys/mman.h>"
default n
endif
config RT_USING_POSIX_DELAY
select RT_USING_KTIME
bool "Enable delay APIs, sleep()/usleep()/msleep() etc"
default n
config RT_USING_POSIX_CLOCK
bool "Enable clock/time APIs, clock_gettime()/clock_settime() etc"
select RT_USING_POSIX_DELAY
default n
config RT_USING_POSIX_TIMER
select RT_USING_KTIME
select RT_USING_RESOURCE_ID
bool "Enable timer APIs, timer_create()/timer_gettime() etc"
default n
config RT_USING_PTHREADS
bool "Enable pthreads APIs"
select RT_USING_POSIX_CLOCK
default n
if RT_USING_PTHREADS
config PTHREAD_NUM_MAX
int "Maximum number of pthreads"
default 8
endif
config RT_USING_MODULE
bool "Enable dynamic module APIs, dlopen()/dlsym()/dlclose() etc"
default n
if RT_USING_MODULE
config RT_USING_CUSTOM_DLMODULE
bool "Enable load dynamic module by custom"
default n
endif
rsource "ipc/Kconfig"
endmenu

View File

@@ -0,0 +1,15 @@
# RT-Thread building script for bridge
import os
from building import *
cwd = GetCurrentDir()
objs = []
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(d, 'SConscript'))
Return('objs')

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

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-05-07 Meco Man first Version
*/
#include <sys/types.h>
#include <rtthread.h>
#include <rthw.h>
/**
* @brief Delays the execution of the current thread for the specified number of milliseconds.
*
* @param msecs The number of milliseconds to sleep.
*/
void msleep(unsigned int msecs)
{
rt_thread_mdelay(msecs);
}
RTM_EXPORT(msleep);
/**
* @brief Delays the execution of the current thread for the specified number of seconds.
*
* @param seconds The number of seconds to sleep.
*/
void ssleep(unsigned int seconds)
{
msleep(seconds * 1000);
}
RTM_EXPORT(ssleep);
/**
* @brief Delays the execution of the current thread for the specified number of milliseconds.
*
* @param msecs The number of milliseconds to delay.
*/
void mdelay(unsigned long msecs)
{
rt_hw_us_delay(msecs * 1000);
}
RTM_EXPORT(mdelay);
/**
* @brief Delays the execution of the current thread for the specified number of microseconds.
*
* @param usecs The number of microseconds to delay.
*/
void udelay(unsigned long usecs)
{
rt_hw_us_delay(usecs);
}
RTM_EXPORT(udelay);
/**
* @brief Delays the execution of the current thread for approximately one microsecond.
*
* @param nsecs This parameter is ignored.
*/
void ndelay(unsigned long nsecs)
{
rt_hw_us_delay(1);
}
RTM_EXPORT(ndelay);
/**
* @brief Delays the execution of the current thread for the specified number of seconds.
*
* @param seconds The number of seconds to sleep.
*
* @return Returns 0 on success.
*/
unsigned int sleep(unsigned int seconds)
{
if (rt_thread_self() != RT_NULL)
{
ssleep(seconds);
}
else /* scheduler has not run yet */
{
while(seconds > 0)
{
udelay(1000000u);
seconds --;
}
}
return 0;
}
RTM_EXPORT(sleep);
/**
* @brief Delays the execution of the current thread for the specified number of microseconds.
*
* @param usec The number of microseconds to sleep.
*
* @return Returns 0 on success.
*/
int usleep(useconds_t usec)
{
if (rt_thread_self() != RT_NULL)
{
msleep(usec / 1000u);
udelay(usec % 1000u);
}
else /* scheduler has not run yet */
{
udelay(usec);
}
return 0;
}
RTM_EXPORT(usleep);

View File

@@ -0,0 +1,21 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-05-07 Meco Man first Version
*/
#ifndef __DELAY_H__
#define __DELAY_H__
unsigned int sleep(unsigned int seconds);
void msleep(unsigned int msecs);
void ssleep(unsigned int seconds);
void mdelay(unsigned long msecs);
void udelay(unsigned long usecs);
void ndelay(unsigned long nsecs);
#endif

View File

@@ -0,0 +1,10 @@
This folder contains:
| sub-folders | description |
| ----------- | ------------------------- |
| aio | Asynchronous I/O |
| mman | Memory-Mapped I/O |
| poll | Nonblocking I/O |
| stdio | Standard Input/Output I/O |
| termios | Terminal I/O |

View File

@@ -0,0 +1,15 @@
# RT-Thread building script for component
import os
from building import *
cwd = GetCurrentDir()
group = []
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
group = group + SConscript(os.path.join(d, 'SConscript'))
Return('group')

View File

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

View File

@@ -0,0 +1,506 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/12/30 Bernard The first version.
* 2024/03/26 TroyMitchelle Added some function comments
* 2024/03/27 TroyMitchelle Fix the issue of incorrect return of invalid parameters in aio_write
*/
#include <rtthread.h>
#include <rthw.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include "aio.h"
struct rt_workqueue* aio_queue = NULL;
/**
* The aio_cancel() function shall attempt to cancel one or more asynchronous I/O
* requests currently outstanding against file descriptor fildes. The aiocbp
* argument points to the asynchronous I/O control block for a particular request
* to be canceled. If aiocbp is NULL, then all outstanding cancelable asynchronous
* I/O requests against fildes shall be canceled.
*
* Normal asynchronous notification shall occur for asynchronous I/O operations
* that are successfully canceled. If there are requests that cannot be canceled,
* then the normal asynchronous completion process shall take place for those
* requests when they are completed.
*
* For requested operations that are successfully canceled, the associated error
* status shall be set to [ECANCELED] and the return status shall be -1. For
* requested operations that are not successfully canceled, the aiocbp shall not
* be modified by aio_cancel().
*
* If aiocbp is not NULL, then if fildes does not have the same value as the file
* descriptor with which the asynchronous operation was initiated, unspecified results occur.
*
* Which operations are cancelable is implementation-defined.
*/
int aio_cancel(int fd, struct aiocb *cb)
{
rt_err_t ret;
if (!cb) return -EINVAL;
if (cb->aio_fildes != fd) return -EINVAL;
ret = rt_workqueue_cancel_work_sync(aio_queue, &(cb->aio_work));
if (ret == RT_EOK)
{
errno = -ECANCELED;
return -1;
}
return 0;
}
/**
* The aio_error() function shall return the error status associated with the
* aiocb structure referenced by the aiocbp argument. The error status for an
* asynchronous I/O operation is the errno value that would be set by the corresponding
* read(), write(),
*/
int aio_error(const struct aiocb *cb)
{
if (cb)
{
return cb->aio_result;
}
return -EINVAL;
}
/**
* The aio_fsync() function shall asynchronously perform a file synchronization
* operation, as specified by the op argument, for I/O operations associated with
* the file indicated by the file descriptor aio_fildes member of the aiocb
* structure referenced by the aiocbp argument and queued at the time of the
* call to aio_fsync(). The function call shall return when the synchronization
* request has been initiated or queued to the file or device (even when the data
* cannot be synchronized immediately).
*
* option: If op is O_DSYNC, all currently queued I/O operations shall be completed
* as if by a call to fdatasync(); that is, as defined for synchronized I/O data
* integrity completion.
*
* option: If op is O_SYNC, all currently queued I/O operations shall be completed
* as if by a call to fsync(); that is, as defined for synchronized I/O file integrity
* completion. If the aio_fsync() function fails, or if the operation queued by
* aio_fsync() fails, then outstanding I/O operations are not guaranteed to have
* been completed.
*
* If aio_fsync() succeeds, then it is only the I/O that was queued at the time
* of the call to aio_fsync() that is guaranteed to be forced to the relevant
* completion state. The completion of subsequent I/O on the file descriptor is
* not guaranteed to be completed in a synchronized fashion.
*
* The aiocbp argument refers to an asynchronous I/O control block. The aiocbp
* value may be used as an argument to aio_error() and aio_return() in order to
* determine the error status and return status, respectively, of the asynchronous
* operation while it is proceeding. When the request is queued, the error status
* for the operation is [EINPROGRESS]. When all data has been successfully transferred,
* the error status shall be reset to reflect the success or failure of the operation.
* If the operation does not complete successfully, the error status for the
* operation shall be set to indicate the error. The aio_sigevent member determines
* the asynchronous notification to occur as specified in Signal Generation and
* Delivery when all operations have achieved synchronized I/O completion. All
* other members of the structure referenced by aiocbp are ignored. If the control
* block referenced by aiocbp becomes an illegal address prior to asynchronous
* I/O completion, then the behavior is undefined.
*
* If the aio_fsync() function fails or aiocbp indicates an error condition,
* data is not guaranteed to have been successfully transferred.
*/
static void aio_fync_work(struct rt_work* work, void* work_data)
{
int result;
rt_base_t level;
struct aiocb *cb = (struct aiocb*)work_data;
RT_ASSERT(cb != RT_NULL);
result = fsync(cb->aio_fildes);
/* modify result */
level = rt_hw_interrupt_disable();
if (result < 0)
cb->aio_result = errno;
else
cb->aio_result = 0;
rt_hw_interrupt_enable(level);
return ;
}
/**
* @brief Initiates an asynchronous fsync operation.
*
* This function initiates an asynchronous fsync operation on the file associated
* with the specified aiocb structure. The operation is queued to the workqueue
* for execution.
*
* @param op The operation to be performed. This parameter is ignored.
* @param cb Pointer to the aiocb structure representing the asynchronous fsync operation.
*
* @return Returns 0 on success.
*/
int aio_fsync(int op, struct aiocb *cb)
{
rt_base_t level;
if (!cb) return -EINVAL;
level = rt_hw_interrupt_disable();
cb->aio_result = -EINPROGRESS;
rt_hw_interrupt_enable(level);
rt_work_init(&(cb->aio_work), aio_fync_work, cb);
rt_workqueue_dowork(aio_queue, &(cb->aio_work));
return 0;
}
/**
* @brief Worker function for asynchronous read operation.
*
* This function performs the actual reading of data from the file associated with
* the specified aiocb structure. It sets the result of the operation in the
* aio_result field of the aiocb structure.
*
* @param work Pointer to the work item.
* @param work_data Pointer to the aiocb structure representing the asynchronous read operation.
*/
static void aio_read_work(struct rt_work* work, void* work_data)
{
int len;
rt_base_t level;
uint8_t *buf_ptr;
struct aiocb *cb = (struct aiocb*)work_data;
buf_ptr = (uint8_t*)cb->aio_buf;
/* seek to offset */
lseek(cb->aio_fildes, cb->aio_offset, SEEK_SET);
len = read(cb->aio_fildes, &buf_ptr[cb->aio_offset], cb->aio_nbytes);
/* modify result */
level = rt_hw_interrupt_disable();
if (len <= 0)
cb->aio_result = errno;
else
cb->aio_result = len;
rt_hw_interrupt_enable(level);
return ;
}
/**
* The aio_read() function shall read aiocbp->aio_nbytes from the file associated
* with aiocbp->aio_fildes into the buffer pointed to by aiocbp->aio_buf. The
* function call shall return when the read request has been initiated or queued
* to the file or device (even when the data cannot be delivered immediately).
*
* If prioritized I/O is supported for this file, then the asynchronous operation
* shall be submitted at a priority equal to a base scheduling priority minus
* aiocbp->aio_reqprio. If Thread Execution Scheduling is not supported, then
* the base scheduling priority is that of the calling process;
*
* otherwise, the base scheduling priority is that of the calling thread.
*
* The aiocbp value may be used as an argument to aio_error() and aio_return()
* in order to determine the error status and return status, respectively, of
* the asynchronous operation while it is proceeding. If an error condition is
* encountered during queuing, the function call shall return without having
* initiated or queued the request. The requested operation takes place at the
* absolute position in the file as given by aio_offset, as if lseek() were called
* immediately prior to the operation with an offset equal to aio_offset and a
* whence equal to SEEK_SET. After a successful call to enqueue an asynchronous
* I/O operation, the value of the file offset for the file is unspecified.
*
* The aio_sigevent member specifies the notification which occurs when the
* request is completed.
*
* The aiocbp->aio_lio_opcode field shall be ignored by aio_read().
*
* The aiocbp argument points to an aiocb structure. If the buffer pointed to by
* aiocbp->aio_buf or the control block pointed to by aiocbp becomes an illegal
* address prior to asynchronous I/O completion, then the behavior is undefined.
*
* Simultaneous asynchronous operations using the same aiocbp produce undefined
* results.
*
* If synchronized I/O is enabled on the file associated with aiocbp->aio_fildes,
* the behavior of this function shall be according to the definitions of synchronized
* I/O data integrity completion and synchronized I/O file integrity completion.
*
* For any system action that changes the process memory space while an asynchronous
* I/O is outstanding to the address range being changed, the result of that action
* is undefined.
*
* For regular files, no data transfer shall occur past the offset maximum
* established in the open file description associated with aiocbp->aio_fildes.
*
*/
int aio_read(struct aiocb *cb)
{
rt_base_t level;
if (!cb) return -EINVAL;
if (cb->aio_offset < 0) return -EINVAL;
level = rt_hw_interrupt_disable();
cb->aio_result = -EINPROGRESS;
rt_hw_interrupt_enable(level);
/* en-queue read work */
rt_work_init(&(cb->aio_work), aio_read_work, cb);
rt_workqueue_dowork(aio_queue, &(cb->aio_work));
return 0;
}
/**
* The aio_return() function shall return the return status associated with the
* aiocb structure referenced by the aiocbp argument. The return status for an
* asynchronous I/O operation is the value that would be returned by the corresponding
* read(), write(), or fsync() function call. If the error status for the operation
* is equal to [EINPROGRESS], then the return status for the operation is undefined.
* The aio_return() function may be called exactly once to retrieve the return
* status of a given asynchronous operation; thereafter, if the same aiocb structure
* is used in a call to aio_return() or aio_error(), an error may be returned.
* When the aiocb structure referred to by aiocbp is used to submit another asynchronous
* operation, then aio_return() may be successfully used to retrieve the return
* status of that operation.
*/
ssize_t aio_return(struct aiocb *cb)
{
if (cb)
{
if (cb->aio_result < 0)
rt_set_errno(cb->aio_result);
return cb->aio_result;
}
return -EINVAL;
}
/**
* The aio_suspend() function shall suspend the calling thread until at least
* one of the asynchronous I/O operations referenced by the list argument has
* completed, until a signal interrupts the function, or, if timeout is not NULL,
* until the time interval specified by timeout has passed. If any of the aiocb
* structures in the list correspond to completed asynchronous I/O operations
* (that is, the error status for the operation is not equal to [EINPROGRESS])
* at the time of the call, the function shall return without suspending the
* calling thread. The list argument is an array of pointers to asynchronous I/O
* control blocks. The nent argument indicates the number of elements in the
* array. Each aiocb structure pointed to has been used in initiating an asynchronous
* I/O request via aio_read(), aio_write(), or lio_listio(). This array may
* contain null pointers, which are ignored. If this array contains pointers
* that refer to aiocb structures that have not been used in submitting asynchronous
* I/O, the effect is undefined.
*
* If the time interval indicated in the timespec structure pointed to by timeout
* passes before any of the I/O operations referenced by list are completed, then
* aio_suspend() shall return with an error.
*/
int aio_suspend(const struct aiocb *const list[], int nent,
const struct timespec *timeout)
{
return -ENOSYS;
}
/**
* @brief Worker function for asynchronous write operation.
*
* This function performs the actual writing of data to the file associated with
* the specified aiocb structure. It sets the result of the operation in the
* aio_result field of the aiocb structure.
*
* @param work Pointer to the work item.
* @param work_data Pointer to the aiocb structure representing the asynchronous write operation.
*/
static void aio_write_work(struct rt_work* work, void* work_data)
{
rt_base_t level;
int len, oflags;
uint8_t *buf_ptr;
struct aiocb *cb = (struct aiocb*)work_data;
buf_ptr = (uint8_t*)cb->aio_buf;
/* whether seek offset */
oflags = fcntl(cb->aio_fildes, F_GETFL, 0);
if ((oflags & O_APPEND) == 0)
{
lseek(cb->aio_fildes, SEEK_SET, cb->aio_offset);
}
/* write data */
len = write(cb->aio_fildes, buf_ptr, cb->aio_nbytes);
/* modify result */
level = rt_hw_interrupt_disable();
if (len <= 0)
cb->aio_result = errno;
else
cb->aio_result = len;
rt_hw_interrupt_enable(level);
return;
}
/**
* The aio_write() function shall write aiocbp->aio_nbytes to the file associated
* with aiocbp->aio_fildes from the buffer pointed to by aiocbp->aio_buf. The
* function shall return when the write request has been initiated or, at a minimum,
* queued to the file or device.
*
* The aiocbp argument may be used as an argument to aio_error() and aio_return()
* in order to determine the error status and return status, respectively, of the
* asynchronous operation while it is proceeding.
*
* The aiocbp argument points to an aiocb structure. If the buffer pointed to by
* aiocbp->aio_buf or the control block pointed to by aiocbp becomes an illegal
* address prior to asynchronous I/O completion, then the behavior is undefined.
*
* If O_APPEND is not set for the file descriptor aio_fildes, then the requested
* operation shall take place at the absolute position in the file as given by
* aio_offset, as if lseek() were called immediately prior to the operation with
* an offset equal to aio_offset and a whence equal to SEEK_SET. If O_APPEND is
* set for the file descriptor, or if aio_fildes is associated with a device that
* is incapable of seeking, write operations append to the file in the same order
* as the calls were made, except under circumstances described in Asynchronous
* I/O. After a successful call to enqueue an asynchronous I/O operation, the value
* of the file offset for the file is unspecified.
*
* The aio_sigevent member specifies the notification which occurs when the request
* is completed.
*
* The aiocbp->aio_lio_opcode field shall be ignored by aio_write().
*
* Simultaneous asynchronous operations using the same aiocbp produce undefined
* results.
*
* If synchronized I/O is enabled on the file associated with aiocbp->aio_fildes,
* the behavior of this function shall be according to the definitions of synchronized
* I/O data integrity completion, and synchronized I/O file integrity completion.
*
* For regular files, no data transfer shall occur past the offset maximum established
* in the open file description associated with aiocbp->aio_fildes.
*/
int aio_write(struct aiocb *cb)
{
int oflags;
rt_base_t level;
if (!cb || (cb->aio_buf == NULL)) return -EINVAL;
/* check access mode */
oflags = fcntl(cb->aio_fildes, F_GETFL, 0);
/* If the flag is not in write only or read-write mode, it cannot be written then an invalid parameter is returned */
if ((oflags & O_ACCMODE) != O_WRONLY &&
(oflags & O_ACCMODE) != O_RDWR)
return -EINVAL;
level = rt_hw_interrupt_disable();
cb->aio_result = -EINPROGRESS;
rt_hw_interrupt_enable(level);
rt_work_init(&(cb->aio_work), aio_write_work, cb);
rt_workqueue_dowork(aio_queue, &(cb->aio_work));
return 0;
}
/**
* The lio_listio() function shall initiate a list of I/O requests with a single
* function call.
*
* The mode argument takes one of the values LIO_WAIT or LIO_NOWAIT declared in
* <aio.h> and determines whether the function returns when the I/O operations
* have been completed, or as soon as the operations have been queued. If the
* mode argument is LIO_WAIT, the function shall wait until all I/O is complete
* and the sig argument shall be ignored.
*
* If the mode argument is LIO_NOWAIT, the function shall return immediately, and
* asynchronous notification shall occur, according to the sig argument, when all
* the I/O operations complete. If sig is NULL, then no asynchronous notification
* shall occur. If sig is not NULL, asynchronous notification occurs as specified
* in Signal Generation and Delivery when all the requests in list have completed.
*
* The I/O requests enumerated by list are submitted in an unspecified order.
*
* The list argument is an array of pointers to aiocb structures. The array contains
* nent elements. The array may contain NULL elements, which shall be ignored.
*
* If the buffer pointed to by list or the aiocb structures pointed to by the
* elements of the array list become illegal addresses before all asynchronous I/O
* completed and, if necessary, the notification is sent, then the behavior is
* undefined. If the buffers pointed to by the aio_buf member of the aiocb structure
* pointed to by the elements of the array list become illegal addresses prior to
* the asynchronous I/O associated with that aiocb structure being completed, the
* behavior is undefined.
*
* The aio_lio_opcode field of each aiocb structure specifies the operation to be
* performed. The supported operations are LIO_READ, LIO_WRITE, and LIO_NOP; these
* symbols are defined in <aio.h>. The LIO_NOP operation causes the list entry to
* be ignored. If the aio_lio_opcode element is equal to LIO_READ, then an I/O operation
* is submitted as if by a call to aio_read() with the aiocbp equal to the address
* of the aiocb structure. If the aio_lio_opcode element is equal to LIO_WRITE, then
* an I/O operation is submitted as if by a call to aio_write() with the aiocbp equal
* to the address of the aiocb structure.
*
* The aio_fildes member specifies the file descriptor on which the operation is to
* be performed.
*
* The aio_buf member specifies the address of the buffer to or from which the data
* is transferred.
*
* The aio_nbytes member specifies the number of bytes of data to be transferred.
*
* The members of the aiocb structure further describe the I/O operation to be
* performed, in a manner identical to that of the corresponding aiocb structure
* when used by the aio_read() and aio_write() functions.
*
* The nent argument specifies how many elements are members of the list; that is,
* the length of the array.
*
* The behavior of this function is altered according to the definitions of synchronized
* I/O data integrity completion and synchronized I/O file integrity completion if
* synchronized I/O is enabled on the file associated with aio_fildes.
*
* For regular files, no data transfer shall occur past the offset maximum established
* in the open file description associated with aiocbp->aio_fildes.
*
* If sig->sigev_notify is SIGEV_THREAD and sig->sigev_notify_attributes is a
* non-null pointer and the block pointed to by this pointer becomes an illegal
* address prior to all asynchronous I/O being completed, then the behavior is
* undefined.
*/
int lio_listio(int mode, struct aiocb * const list[], int nent,
struct sigevent *sig)
{
return -ENOSYS;
}
/**
* @brief Initializes the asynchronous I/O system.
*
* This function initializes the asynchronous I/O system by creating a workqueue
* for asynchronous I/O operations.
*
* @return Returns 0 on success.
*/
int aio_system_init(void)
{
aio_queue = rt_workqueue_create("aio", 2048, RT_THREAD_PRIORITY_MAX/2);
RT_ASSERT(aio_queue != NULL);
return 0;
}
INIT_COMPONENT_EXPORT(aio_system_init);

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/12/30 Bernard The first version.
* 2024/03/26 TroyMitchelle Align comments within the aiocb structure
*/
#ifndef __AIO_H__
#define __AIO_H__
#include <stdio.h>
#include <sys/signal.h>
#include <rtdevice.h>
struct aiocb
{
int aio_fildes; /* File descriptor. */
off_t aio_offset; /* File offset. */
volatile void *aio_buf; /* Location of buffer. */
size_t aio_nbytes; /* Length of transfer. */
int aio_reqprio; /* Request priority offset. */
struct sigevent aio_sigevent; /* Signal number and value. */
int aio_lio_opcode; /* Operation to be performed. */
int aio_result;
struct rt_work aio_work;
};
int aio_cancel(int fd, struct aiocb *cb);
int aio_error (const struct aiocb *cb);
int aio_fsync(int op, struct aiocb *cb);
int aio_read(struct aiocb *cb);
ssize_t aio_return(struct aiocb *cb);
int aio_suspend(const struct aiocb *const list[], int nent,
const struct timespec *timeout);
int aio_write(struct aiocb *cb);
int lio_listio(int mode, struct aiocb * const list[], int nent,
struct sigevent *sig);
#endif

View File

@@ -0,0 +1,14 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_USING_POSIX_EPOLL'):
src += ['epoll.c']
group = DefineGroup('POSIX', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,14 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_USING_POSIX_EVENTFD'):
src += ['eventfd.c']
group = DefineGroup('POSIX', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,345 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-07-20 zmq810150896 first version
* 2024-03-28 TroyMitchell Add comments for all functions
*/
#include <rtthread.h>
#include <fcntl.h>
#include <rtdevice.h>
#include <stdint.h>
#include <unistd.h>
#include <dfs_file.h>
#include <dfs.h>
#include "poll.h"
#include "eventfd.h"
#define EFD_SEMAPHORE (1 << 0)
#define EFD_CLOEXEC O_CLOEXEC
#define EFD_NONBLOCK O_NONBLOCK
#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE)
#define EFD_ULLONG_MAX (~0ULL)
#define EVENTFD_MUTEX_NAME "eventfd"
struct eventfd_ctx
{
rt_wqueue_t reader_queue;
rt_wqueue_t writer_queue;
rt_uint64_t count;
unsigned int flags;
struct rt_mutex lock;
};
#ifndef RT_USING_DFS_V2
static int eventfd_close(struct dfs_file *file);
static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count);
static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count);
#else
static int eventfd_close(struct dfs_file *file);
static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos);
#endif
static const struct dfs_file_ops eventfd_fops =
{
.close = eventfd_close,
.poll = eventfd_poll,
.read = eventfd_read,
.write = eventfd_write,
};
/**
* @brief Closes an event file descriptor.
* @param file Pointer to the file descriptor structure.
* @return 0 on success, otherwise an error code.
*/
static int eventfd_close(struct dfs_file *file)
{
struct eventfd_ctx *ctx = file->vnode->data;
if (file->vnode->ref_count == 1)
{
rt_mutex_detach(&ctx->lock);
rt_free(ctx);
}
return 0;
}
/**
* @brief Polls an event file descriptor for events.
* @param file Pointer to the file descriptor structure.
* @param req Pointer to the poll request structure.
* @return Events that occurred on the file descriptor.
*/
static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req)
{
struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
int events = 0;
rt_uint64_t count;
count = ctx->count;
rt_poll_add(&ctx->reader_queue, req);
if (count > 0)
events |= POLLIN;
if (count == EFD_ULLONG_MAX)
events |= POLLERR;
if ((EFD_ULLONG_MAX - 1) > count)
events |= POLLOUT;
return events;
}
#ifndef RT_USING_DFS_V2
/**
* @brief Reads data from an event file descriptor.
* @param file Pointer to the file descriptor structure.
* @param buf Pointer to the buffer to read data into.
* @param count Maximum number of bytes to read.
* @return Number of bytes read on success, otherwise an error code.
*/
static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count)
#else
/**
* @brief Reads data from an event file descriptor.
* @param file Pointer to the file descriptor structure.
* @param buf Pointer to the buffer to read data into.
* @param count Maximum number of bytes to read.
* @param pos Pointer to the file position (not used).
* @return Number of bytes read on success, otherwise an error code.
*/
static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
#endif
{
struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
rt_uint64_t counter_num = 0;
rt_uint64_t *buffer;
if (count < sizeof(counter_num))
return -EINVAL;
buffer = (rt_uint64_t *)buf;
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
if (ctx->count <= 0)
{
if (file->flags & O_NONBLOCK)
{
rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT);
rt_mutex_release(&ctx->lock);
return -EAGAIN;
}
else
{
/* In this case, when the data is read in blocked mode, when ctx->count is 0, the mutex needs to be released and wait for writing */
rt_mutex_release(&ctx->lock);
rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT);
rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER);
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
}
}
if (ctx->flags & EFD_SEMAPHORE)
{
counter_num = 1;
}
else
{
counter_num = ctx->count;
}
ctx->count -= counter_num;
(*buffer) = counter_num;
rt_mutex_release(&ctx->lock);
return sizeof(counter_num);
}
#ifndef RT_USING_DFS_V2
/**
* @brief Writes data to an event file descriptor.
* @param file Pointer to the file descriptor structure.
* @param buf Pointer to the buffer containing data to write.
* @param count Number of bytes to write.
* @return Number of bytes written on success, otherwise an error code.
*/
static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count)
#else
/**
* @brief Writes data to an event file descriptor.
* @param file Pointer to the file descriptor structure.
* @param buf Pointer to the buffer containing data to write.
* @param count Number of bytes to write.
* @param pos Pointer to the file position (not used).
* @return Number of bytes written on success, otherwise an error code.
*/
static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
#endif
{
struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
rt_ssize_t ret = 0;
rt_uint64_t counter_num;
if (count < sizeof(counter_num))
return -EINVAL;
counter_num = *(rt_uint64_t *)buf;
if (counter_num == EFD_ULLONG_MAX)
return -EINVAL;
ret = -EAGAIN;
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
if ((EFD_ULLONG_MAX - ctx->count) > counter_num)
{
ret = sizeof(counter_num);
}
else if (!(file->flags & O_NONBLOCK))
{
for (;;)
{
if ((EFD_ULLONG_MAX - ctx->count) >= counter_num)
{
ret = sizeof(counter_num);
break;
}
/* Release the mutex to avoid a deadlock */
rt_mutex_release(&ctx->lock);
rt_wqueue_wait(&ctx->writer_queue, 0, RT_WAITING_FOREVER);
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
}
}
if (ret > 0)
{
ctx->count += counter_num;
rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN);
}
rt_mutex_release(&ctx->lock);
return ret;
}
/**
* @brief Creates an event file descriptor.
* @param df Pointer to the file descriptor structure.
* @param count Initial value of the event counter.
* @param flags Flags for the event file descriptor.
* @return 0 on success, otherwise an error code.
*/
static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags)
{
struct eventfd_ctx *ctx = RT_NULL;
rt_err_t ret = 0;
ctx = (struct eventfd_ctx *)rt_malloc(sizeof(struct eventfd_ctx));
if (ctx == RT_NULL)
{
ret = -ENOMEM;
}
else
{
ctx->count = count;
ctx->flags = flags;
flags &= EFD_SHARED_FCNTL_FLAGS;
flags |= O_RDWR;
rt_mutex_init(&ctx->lock, EVENTFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
rt_wqueue_init(&ctx->reader_queue);
rt_wqueue_init(&ctx->writer_queue);
df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
if (df->vnode)
{
dfs_vnode_init(df->vnode, FT_NONLOCK, &eventfd_fops);
df->vnode->data = ctx;
df->flags = flags;
}
else
{
rt_mutex_detach(&ctx->lock);
rt_free(ctx);
ret = -ENOMEM;
}
#ifdef RT_USING_DFS_V2
df->fops = &eventfd_fops;
#endif
}
return ret;
}
/**
* @brief Internal function to create an event file descriptor.
* @param count Initial value of the event counter.
* @param flags Flags for the event file descriptor.
* @return File descriptor on success, otherwise an error code.
*/
static int do_eventfd(unsigned int count, int flags)
{
struct dfs_file *file;
int fd;
int status;
rt_ssize_t ret = 0;
if (flags & ~EFD_FLAGS_SET)
{
rt_set_errno(EINVAL);
return -1;
}
fd = fd_new();
if (fd >= 0)
{
ret = fd;
file = fd_get(fd);
status = rt_eventfd_create(file, count, flags);
if (status < 0)
{
fd_release(fd);
rt_set_errno(-status);
ret = -1;
}
}
else
{
rt_set_errno(-fd);
ret = -1;
}
return ret;
}
/**
* @brief Creates an event file descriptor with the specified count and flags.
* @param count Initial value of the event counter.
* @param flags Flags for the event file descriptor.
* @return File descriptor on success, otherwise an error code.
*/
int eventfd(unsigned int count, int flags)
{
return do_eventfd(count, flags);
}

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-07-20 zmq810150896 First version
*/
#ifndef __EVENTFD_H__
#define __EVENTFD_H__
int eventfd(unsigned int count, int flags);
#endif /* __EVENTFD_H__ */

View File

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

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/11/30 Bernard The first version.
* 2024/03/29 TroyMitchelle Add all function comments
*/
#include <stdint.h>
#include <stdio.h>
#include <rtthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/errno.h>
#include "sys/mman.h"
/**
* @brief Maps a region of memory into the calling process's address space.
* @param addr Desired starting address of the mapping.
* @param length Length of the mapping.
* @param prot Protection of the mapped memory region.
* @param flags Type of the mapped memory region.
* @param fd File descriptor of the file to be mapped.
* @param offset Offset within the file to start the mapping.
* @return Upon success, returns a pointer to the mapped region; otherwise, MAP_FAILED is returned.
*/
void *mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
uint8_t *mem;
if (addr)
{
mem = addr;
}
else mem = (uint8_t *)malloc(length);
if (mem)
{
off_t cur;
size_t read_bytes;
cur = lseek(fd, 0, SEEK_SET);
lseek(fd, offset, SEEK_SET);
read_bytes = read(fd, mem, length);
if (read_bytes != length)
{
if (addr == RT_NULL)
{
/* read failed */
free(mem);
mem = RT_NULL;
}
}
lseek(fd, cur, SEEK_SET);
return mem;
}
errno = ENOMEM;
return MAP_FAILED;
}
/**
* @brief Unmaps a mapped region of memory.
* @param addr Starting address of the mapping to be unmapped.
* @param length Length of the mapping.
* @return Upon success, returns 0; otherwise, -1 is returned.
*/
int munmap(void *addr, size_t length)
{
if (addr)
{
free(addr);
return 0;
}
return -1;
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/11/30 Bernard The first version.
* 2024/03/29 TroyMitchelle Add comments for all macros
*/
#ifndef __SYS_MMAN_H__
#define __SYS_MMAN_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#define MAP_FAILED ((void *) -1)
/* mmap flags */
#define MAP_SHARED 0x01 /**< Share the mapping with other processes. */
#define MAP_PRIVATE 0x02 /**< Create a private copy-on-write mapping. */
#define MAP_TYPE 0x0f /**< Mask for type of mapping. */
#define MAP_FIXED 0x10 /**< Interpret addr exactly. */
#define MAP_ANON 0x20 /**< Anonymous mapping. */
#define MAP_ANONYMOUS MAP_ANON /**< Synonym for MAP_ANON. */
#define MAP_NORESERVE 0x4000 /**< Don't reserve swap space for this mapping. */
#define MAP_GROWSDOWN 0x0100 /**< Stack-like segment. */
#define MAP_DENYWRITE 0x0800 /**< ETXTBSY. */
#define MAP_EXECUTABLE 0x1000 /**< Mark it as an executable. */
#define MAP_LOCKED 0x2000 /**< Lock the mapping's pages. */
#define MAP_POPULATE 0x8000 /**< Populate (prefault) pagetables. */
#define MAP_NONBLOCK 0x10000 /**< Do not block on IO. */
#define MAP_STACK 0x20000 /**< Allocation is a stack segment. */
#define MAP_HUGETLB 0x40000 /**< Create a huge page mapping. */
#define MAP_FILE 0 /**< Compatibility */
/* mmap protections */
#define PROT_NONE 0 /**< No access. */
#define PROT_READ 1 /**< Page can be read. */
#define PROT_WRITE 2 /**< Page can be written. */
#define PROT_EXEC 4 /**< Page can be executed. */
#define PROT_GROWSDOWN 0x01000000/**< Extend change to start of growsdown vma (mprotect only). */
#define PROT_GROWSUP 0x02000000/**< Extend change to start of growsup vma (mprotect only). */
/* msync flags */
#define MS_ASYNC 1 /**< Perform asynchronous writes. */
#define MS_INVALIDATE 2 /**< Invalidate mappings after writing. */
#define MS_SYNC 4 /**< Perform synchronous writes. */
/* mlockall flags */
#define MCL_CURRENT 1 /**< Lock all pages which are currently mapped into the address space of the process. */
#define MCL_FUTURE 2 /**< Lock all pages which will become mapped into the address space of the process in the future. */
#define MCL_ONFAULT 4 /**< Lock all pages which are currently mapped into the address space of the process on access. */
void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off);
int munmap (void *start, size_t len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,17 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_USING_POSIX_POLL'):
src += ['poll.c']
if GetDepend('RT_USING_POSIX_SELECT'):
src += ['select.c']
group = DefineGroup('POSIX', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,359 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016-12-28 Bernard first version
* 2018-03-09 Bernard Add protection for pt->triggered.
* 2023-12-04 Shell Fix return code and error verification
* 2023-12-14 Shell When poll goes to sleep before the waitqueue has added a
* record and finished enumerating all the fd's, it may be
* incorrectly woken up. This is basically because the poll
* mechanism wakeup algorithm does not correctly distinguish
* the current wait state.
* 2024-03-29 TroyMitchelle Add all function comments and comments to structure members
*/
#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>
#include <dfs_file.h>
#include "poll.h"
enum rt_poll_status
{
RT_POLL_STAT_INIT, /**< Poll operation initialization status. */
RT_POLL_STAT_TRIG, /**< Poll operation triggered status. */
RT_POLL_STAT_WAITING /**< Poll operation waiting status. */
};
struct rt_poll_table
{
rt_pollreq_t req; /**< Poll request. */
enum rt_poll_status status; /**< Status of the poll operation. */
rt_thread_t polling_thread; /**< Polling thread associated with the table. */
struct rt_poll_node *nodes; /**< Linked list of poll nodes. */
};
struct rt_poll_node
{
struct rt_wqueue_node wqn; /**< Wait queue node for the poll node. */
struct rt_poll_table *pt; /**< Pointer to the parent poll table. */
struct rt_poll_node *next; /**< Pointer to the next poll node. */
};
static RT_DEFINE_SPINLOCK(_spinlock);
/**
* @brief Wake-up function for the wait queue.
*
* This function is invoked when a node in the wait queue needs to be woken up.
*
* @param wait Pointer to the wait queue node.
* @param key Key associated with the wake-up operation.
* @return Upon successful wake-up, returns 0; otherwise, -1 is returned.
*/
static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
{
rt_ubase_t level;
struct rt_poll_node *pn;
int is_waiting;
if (key && !((rt_ubase_t)key & wait->key))
return -1;
pn = rt_container_of(wait, struct rt_poll_node, wqn);
level = rt_spin_lock_irqsave(&_spinlock);
is_waiting = (pn->pt->status == RT_POLL_STAT_WAITING);
pn->pt->status = RT_POLL_STAT_TRIG;
rt_spin_unlock_irqrestore(&_spinlock, level);
if (is_waiting)
return __wqueue_default_wake(wait, key);
return -1;
}
/**
* @brief Adds a poll request to the wait queue.
*
* This function adds a poll request to the wait queue associated with the specified
* wait queue and poll request.
*
* @param wq Pointer to the wait queue.
* @param req Pointer to the poll request.
*/
static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
{
struct rt_poll_table *pt;
struct rt_poll_node *node;
node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node));
if (node == RT_NULL)
return;
pt = rt_container_of(req, struct rt_poll_table, req);
node->wqn.key = req->_key;
rt_list_init(&(node->wqn.list));
node->wqn.polling_thread = pt->polling_thread;
node->wqn.wakeup = __wqueue_pollwake;
node->next = pt->nodes;
node->pt = pt;
pt->nodes = node;
rt_wqueue_add(wq, &node->wqn);
}
/**
* @brief Initializes a poll table.
*
* This function initializes a poll table with the provided poll request, status,
* and polling thread.
*
* @param pt Pointer to the poll table to be initialized.
*/
static void poll_table_init(struct rt_poll_table *pt)
{
pt->req._proc = _poll_add;
pt->status = RT_POLL_STAT_INIT;
pt->nodes = RT_NULL;
pt->polling_thread = rt_thread_self();
}
/**
* @brief Waits for events on the poll table with a specified timeout.
*
* This function waits for events on the poll table with the specified timeout
* in milliseconds.
*
* @param pt Pointer to the poll table.
* @param msec Timeout value in milliseconds.
* @return Upon successful completion, returns 0. If the timeout expires, -RT_ETIMEOUT
* is returned. If the operation is interrupted by a signal, -RT_EINTR is
* returned.
*/
static int poll_wait_timeout(struct rt_poll_table *pt, int msec)
{
rt_int32_t timeout;
int ret = 0;
struct rt_thread *thread;
rt_base_t level;
thread = pt->polling_thread;
timeout = rt_tick_from_millisecond(msec);
level = rt_spin_lock_irqsave(&_spinlock);
if (timeout != 0 && pt->status != RT_POLL_STAT_TRIG)
{
if (rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE) == RT_EOK)
{
if (timeout > 0)
{
rt_timer_control(&(thread->thread_timer),
RT_TIMER_CTRL_SET_TIME,
&timeout);
rt_timer_start(&(thread->thread_timer));
rt_set_errno(RT_ETIMEOUT);
}
else
{
rt_set_errno(0);
}
pt->status = RT_POLL_STAT_WAITING;
rt_spin_unlock_irqrestore(&_spinlock, level);
rt_schedule();
level = rt_spin_lock_irqsave(&_spinlock);
if (pt->status == RT_POLL_STAT_WAITING)
pt->status = RT_POLL_STAT_INIT;
}
}
ret = rt_get_errno();
if (ret == RT_EINTR)
ret = -RT_EINTR;
else if (pt->status == RT_POLL_STAT_TRIG)
ret = RT_EOK;
else
ret = -RT_ETIMEOUT;
rt_spin_unlock_irqrestore(&_spinlock, level);
return ret;
}
/**
* @brief Performs poll operation for a single file descriptor.
*
* This function performs a poll operation for a single file descriptor and updates
* the revents field of the pollfd structure accordingly.
*
* @param pollfd Pointer to the pollfd structure.
* @param req Pointer to the poll request.
* @return Upon successful completion, returns the bitmask of events that occurred.
* If an error occurs, -1 is returned.
*/
static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
{
int mask = 0;
int fd;
fd = pollfd->fd;
if (fd >= 0)
{
struct dfs_file *f = fd_get(fd);
mask = POLLNVAL;
if (f)
{
mask = POLLMASK_DEFAULT;
if (f->vnode->fops->poll)
{
req->_key = pollfd->events | POLLERR | POLLHUP;
mask = f->vnode->fops->poll(f, req);
/* dealwith the device return error -1*/
if (mask < 0)
{
pollfd->revents = 0;
return mask;
}
}
/* Mask out unneeded events. */
mask &= pollfd->events | POLLERR | POLLHUP;
}
}
pollfd->revents = mask;
return mask;
}
/**
* @brief Performs the poll operation on an array of file descriptors.
*
* This function performs the poll operation on an array of file descriptors and
* waits for events with the specified timeout.
*
* @param fds Pointer to the array of pollfd structures.
* @param nfds Number of file descriptors in the array.
* @param pt Pointer to the poll table.
* @param msec Timeout value in milliseconds.
* @return Upon successful completion, returns the number of file descriptors
* for which events were received. If the timeout expires, -RT_ETIMEOUT
* is returned. If the operation is interrupted by a signal, -RT_EINTR is
* returned.
*/
static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
{
int num;
int istimeout = 0;
nfds_t n;
struct pollfd *pf;
int ret = 0;
if (msec == 0)
{
pt->req._proc = RT_NULL;
istimeout = 1;
}
while (1)
{
pf = fds;
num = 0;
pt->status = RT_POLL_STAT_INIT;
for (n = 0; n < nfds; n ++)
{
ret = do_pollfd(pf, &pt->req);
if(ret < 0)
{
/*dealwith the device return error -1 */
pt->req._proc = RT_NULL;
return ret;
}
else if(ret > 0)
{
num ++;
pt->req._proc = RT_NULL;
}
pf ++;
}
pt->req._proc = RT_NULL;
if (num || istimeout)
break;
ret = poll_wait_timeout(pt, msec);
if (ret == -RT_EINTR)
return -EINTR;
else if (ret == -RT_ETIMEOUT)
istimeout = 1;
else
istimeout = 0;
}
return num;
}
/**
* @brief Tears down the poll table.
*
* This function tears down the poll table by removing all poll nodes associated
* with it.
*
* @param pt Pointer to the poll table.
*/
static void poll_teardown(struct rt_poll_table *pt)
{
struct rt_poll_node *node, *next;
next = pt->nodes;
while (next)
{
node = next;
rt_wqueue_remove(&node->wqn);
next = node->next;
rt_free(node);
}
}
/**
* @brief Performs the poll operation on a set of file descriptors.
*
* This function performs the poll operation on a set of file descriptors and
* waits for events with the specified timeout.
*
* @param fds Pointer to the array of pollfd structures.
* @param nfds Number of file descriptors in the array.
* @param timeout Timeout value in milliseconds.
* @return Upon successful completion, returns the number of file descriptors
* for which events were received. If the timeout expires, 0 is returned.
* If an error occurs, -1 is returned.
*/
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
int num;
struct rt_poll_table table;
poll_table_init(&table);
num = poll_do(fds, nfds, &table, timeout);
poll_teardown(&table);
return num;
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-09-11 Meco Man First version
* 2024-03-29 TroyMitchelle Add all macro comments and comments to structure members
*/
#ifndef __POLL_H__
#define __POLL_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef RT_USING_MUSLLIBC
#if !defined(POLLIN) && !defined(POLLOUT)
#define POLLIN 0x001 /**< There is data to read. */
#define POLLPRI 0x002 /**< There is urgent data to read. */
#define POLLOUT 0x004 /**< Writing is now possible. */
#define POLLERR 0x008 /**< Error condition. */
#define POLLHUP 0x010 /**< Hang up. */
#define POLLNVAL 0x020 /**< Invalid polling request. */
#define POLLRDNORM 0x040 /**< Normal data may be read. */
#define POLLRDBAND 0x080 /**< Priority data may be read. */
#define POLLWRNORM 0x100 /**< Writing normal data is possible. */
#define POLLWRBAND 0x200 /**< Writing priority data is possible. */
typedef unsigned int nfds_t;
struct pollfd
{
int fd; /**< File descriptor. */
short events; /**< Requested events. */
short revents; /**< Returned events. */
};
#endif
#else
#if !defined(POLLIN) && !defined(POLLOUT)
#define POLLIN 0x1 /**< There is data to read. */
#define POLLOUT 0x2 /**< Writing is now possible. */
#define POLLERR 0x4 /**< Error condition. */
#define POLLNVAL 0x8 /**< Invalid polling request. */
/* Below values are unimplemented */
#define POLLRDNORM 0x10 /**< Normal data may be read. */
#define POLLRDBAND 0x20 /**< Priority data may be read. */
#define POLLPRI 0x40 /**< There is urgent data to read. */
#define POLLWRNORM 0x80 /**< Writing normal data is possible. */
#define POLLWRBAND 0x100 /**< Writing priority data is possible. */
#define POLLHUP 0x200 /**< Hang up. */
typedef unsigned int nfds_t;
struct pollfd
{
int fd; /**< File descriptor. */
short events; /**< Requested events. */
short revents; /**< Returned events. */
};
#endif
#endif /* !defined(POLLIN) && !defined(POLLOUT) */
#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#ifdef __cplusplus
}
#endif
#endif /* __POLL_H__ */

View File

@@ -0,0 +1,193 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016-12-28 Bernard first version
* 2024-04-08 TroyMitchell Add all function comments
*/
#include <rtthread.h>
#include <poll.h>
#include <sys/select.h>
/**
* @brief Initialize the file descriptor set to have zero bits for all file descriptors.
* @param set Pointer to the file descriptor set to be initialized.
* @param nfds The maximum file descriptor in the set plus one.
* @note The actual size of the 'fd_set' is determined based on the parameter 'nfds'.
*/
static void fdszero(fd_set *set, int nfds)
{
fd_mask *m;
int n;
/*
The 'sizeof(fd_set)' of the system space may differ from user space,
so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
*/
m = (fd_mask *)set;
for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
{
rt_memset(m, 0, sizeof(fd_mask));
m ++;
}
}
/**
* @brief Synchronous I/O multiplexing: multiplex input/output over a set of file descriptors.
* @param nfds The highest-numbered file descriptor in any of the three sets, plus 1.
* @param readfds A pointer to a set of file descriptors to be checked for read readiness.
* @param writefds A pointer to a set of file descriptors to be checked for write readiness.
* @param exceptfds A pointer to a set of file descriptors to be checked for exceptions.
* @param timeout The maximum time to wait for any of the specified file descriptors to become ready.
* @return Upon successful completion, the total number of file descriptors in all the sets that are ready for the requested operation is returned; otherwise, -1 is returned on error.
*/
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
int fd;
int npfds;
int msec;
int ndx;
int ret;
struct pollfd *pollset = RT_NULL;
/* How many pollfd structures do we need to allocate? */
for (fd = 0, npfds = 0; fd < nfds; fd++)
{
/* Check if any monitor operation is requested on this fd */
if ((readfds && FD_ISSET(fd, readfds)) ||
(writefds && FD_ISSET(fd, writefds)) ||
(exceptfds && FD_ISSET(fd, exceptfds)))
{
npfds++;
}
}
/* Allocate the descriptor list for poll() */
if (npfds > 0)
{
pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
if (!pollset)
{
return -1;
}
}
/* Initialize the descriptor list for poll() */
for (fd = 0, ndx = 0; fd < nfds; fd++)
{
int incr = 0;
/* The readfs set holds the set of FDs that the caller can be assured
* of reading from without blocking. Note that POLLHUP is included as
* a read-able condition. POLLHUP will be reported at the end-of-file
* or when a connection is lost. In either case, the read() can then
* be performed without blocking.
*/
if (readfds && FD_ISSET(fd, readfds))
{
pollset[ndx].fd = fd;
pollset[ndx].events |= POLLIN;
incr = 1;
}
if (writefds && FD_ISSET(fd, writefds))
{
pollset[ndx].fd = fd;
pollset[ndx].events |= POLLOUT;
incr = 1;
}
if (exceptfds && FD_ISSET(fd, exceptfds))
{
pollset[ndx].fd = fd;
incr = 1;
}
ndx += incr;
}
RT_ASSERT(ndx == npfds);
/* Convert the timeout to milliseconds */
if (timeout)
{
msec = (int)timeout->tv_sec * 1000 + (int)timeout->tv_usec / 1000;
}
else
{
msec = -1;
}
/* Then let poll do all of the real work. */
ret = poll(pollset, npfds, msec);
/* Now set up the return values */
if (readfds)
{
fdszero(readfds, nfds);
}
if (writefds)
{
fdszero(writefds, nfds);
}
if (exceptfds)
{
fdszero(exceptfds, nfds);
}
/* Convert the poll descriptor list back into selects 3 bitsets */
if (ret > 0)
{
ret = 0;
for (ndx = 0; ndx < npfds; ndx++)
{
/* Check for read conditions. Note that POLLHUP is included as a
* read condition. POLLHUP will be reported when no more data will
* be available (such as when a connection is lost). In either
* case, the read() can then be performed without blocking.
*/
if (readfds)
{
if (pollset[ndx].revents & (POLLIN | POLLHUP))
{
FD_SET(pollset[ndx].fd, readfds);
ret++;
}
}
/* Check for write conditions */
if (writefds)
{
if (pollset[ndx].revents & POLLOUT)
{
FD_SET(pollset[ndx].fd, writefds);
ret++;
}
}
/* Check for exceptions */
if (exceptfds)
{
if (pollset[ndx].revents & POLLERR)
{
FD_SET(pollset[ndx].fd, exceptfds);
ret++;
}
}
}
}
if (pollset) rt_free(pollset);
return ret;
}

View File

@@ -0,0 +1,14 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_USING_POSIX_SIGNALFD'):
src += ['signalfd.c']
group = DefineGroup('POSIX', src, depend = ['RT_USING_SMART','RT_USING_POSIX_SIGNALFD'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,385 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-08-29 zmq810150896 first version
* 2024-04-08 TroyMitchell Add all function comments
*/
#include <rtthread.h>
#include <sys/signalfd.h>
#include <dfs_file.h>
#include <signal.h>
#include <rthw.h>
#include <sys/time.h>
#include <lwp_signal.h>
#include <lwp.h>
#include <poll.h>
#define SIGNALFD_MUTEX_NAME "signalfd"
#define SIGINFO_MAX 10
#define SIGNALFD_SHART_MAX RT_SIGNALFD_MAX_NUM
static int is_head_init = 0;
struct rt_signalfd_ctx
{
sigset_t sigmask;
struct rt_mutex lock;
siginfo_t info[SIGINFO_MAX];
int sig_num;
rt_wqueue_t signalfd_queue;
struct rt_lwp *lwp[SIGNALFD_SHART_MAX];
};
static int signalfd_close(struct dfs_file *file);
static int signalfd_poll(struct dfs_file *file, struct rt_pollreq *req);
#ifndef RT_USING_DFS_V2
static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count);
#else
static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
#endif
static int signalfd_add_notify(struct rt_signalfd_ctx *sfd);
static const struct dfs_file_ops signalfd_fops =
{
.close = signalfd_close,
.poll = signalfd_poll,
.read = signalfd_read,
};
/**
* @brief Closes the file descriptor associated with a signalfd file.
* @param file Pointer to the file structure.
* @return Upon successful completion, returns 0; otherwise, returns an error code.
*/
static int signalfd_close(struct dfs_file *file)
{
struct rt_signalfd_ctx *sfd;
if (file->vnode->ref_count != 1)
return 0;
sfd = file->vnode->data;
if (sfd)
{
rt_mutex_detach(&sfd->lock);
rt_free(sfd);
}
return 0;
}
/**
* @brief Adds a signalfd file descriptor to the poll queue and checks for pending events.
* @param file Pointer to the file structure.
* @param req Pointer to the poll request structure.
* @return The events that are ready on the file descriptor.
*/
static int signalfd_poll(struct dfs_file *file, struct rt_pollreq *req)
{
struct rt_signalfd_ctx *sfd;
int events = 0;
if (file->vnode)
{
sfd = file->vnode->data;
rt_poll_add(&sfd->signalfd_queue, req);
signalfd_add_notify(sfd);
rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
if (sfd->sig_num)
events |= POLLIN;
rt_mutex_release(&sfd->lock);
}
return events;
}
#ifndef RT_USING_DFS_V2
/**
* @brief Reads signals from a signalfd file descriptor.
* @param file Pointer to the file structure.
* @param buf Pointer to the buffer to store the signals.
* @param count Maximum number of bytes to read.
* @return Upon successful completion, returns the number of bytes read; otherwise, returns an error code.
*/
static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count)
#else
/**
* @brief Reads signals from a signalfd file descriptor with file offset.
* @param file Pointer to the file structure.
* @param buf Pointer to the buffer to store the signals.
* @param count Maximum number of bytes to read.
* @param pos Pointer to the file offset.
* @return Upon successful completion, returns the number of bytes read; otherwise, returns an negative error code.
*/
static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
#endif
{
struct rt_signalfd_ctx *sfd = RT_NULL;
struct signalfd_siginfo *buffer = RT_NULL;
int user_buf_num = 0;
int sig_num = 0;
int i = 0;
rt_err_t ret = -1;
if (sizeof(struct signalfd_siginfo) > count)
return -1;
if (buf == RT_NULL)
return -1;
buffer = (struct signalfd_siginfo *)buf;
user_buf_num = count / sizeof(struct signalfd_siginfo);
if (file->vnode)
{
sfd = file->vnode->data;
signalfd_add_notify(sfd);
if ((sfd->sig_num == 0) && (file->flags & O_NONBLOCK))
{
ret = -EAGAIN;
}
else
{
if (sfd->sig_num == 0)
{
rt_wqueue_wait(&sfd->signalfd_queue, 0, RT_WAITING_FOREVER);
}
rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
for (i = 0; i < sfd->sig_num; i++)
{
if (i < user_buf_num)
{
memcpy(&buffer[i], &sfd->info[i], sizeof(struct signalfd_siginfo));
sfd->sig_num -= 1;
sig_num += 1;
}
else
{
break;
}
}
for (int j = 0; j < sfd->sig_num; j ++)
{
memcpy(&sfd->info[j], &sfd->info[i ++], sizeof(struct signalfd_siginfo));
}
rt_mutex_release(&sfd->lock);
ret = sizeof(struct signalfd_siginfo) * sig_num;
}
}
return ret;
}
/**
* @brief Callback function for signalfd file descriptor notifications.
* @param signalfd_queue Pointer to the signalfd queue.
* @param signum Signal number.
*/
static void signalfd_callback(rt_wqueue_t *signalfd_queue, int signum)
{
struct rt_signalfd_ctx *sfd;
sfd = rt_container_of(signalfd_queue, struct rt_signalfd_ctx, signalfd_queue);
if (sfd)
{
if (sigismember(&sfd->sigmask, signum))
{
rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
if (sfd->sig_num < SIGINFO_MAX)
{
sfd->info[sfd->sig_num].si_signo = signum;
sfd->sig_num += 1;
}
rt_mutex_release(&sfd->lock);
rt_wqueue_wakeup(signalfd_queue, (void*)POLLIN);
}
}
}
/**
* @brief Adds a signal file descriptor notification.
* @param sfd Pointer to the signalfd context.
* @return Upon successful completion, returns 0; otherwise, returns an error code.
*/
static int signalfd_add_notify(struct rt_signalfd_ctx *sfd)
{
struct rt_lwp_notify *lwp_notify;
rt_err_t ret = -1;
rt_slist_t *node;
int is_lwp = 0;
rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
for (int i = 0; i < is_head_init; i++)
{
if (sfd->lwp[i])
{
if (sfd->lwp[i] == lwp_self())
{
is_lwp = 1;
}
}
}
if (is_lwp == 0)
{
sfd->lwp[is_head_init] = lwp_self();
if (is_head_init == 0)
{
rt_slist_init(&sfd->lwp[is_head_init]->signalfd_notify_head);
}
lwp_notify = (struct rt_lwp_notify *)rt_malloc(sizeof(struct rt_lwp_notify));
if (lwp_notify)
{
lwp_notify->notify = signalfd_callback;
lwp_notify->signalfd_queue = &sfd->signalfd_queue;
rt_slist_append(&sfd->lwp[is_head_init]->signalfd_notify_head, &(lwp_notify->list_node));
is_head_init ++;
ret = 0;
}
else
{
rt_slist_for_each(node, &sfd->lwp[is_head_init]->signalfd_notify_head)
{
struct rt_lwp_notify *n = rt_slist_entry(node, struct rt_lwp_notify, list_node);
rt_slist_remove(&sfd->lwp[is_head_init]->signalfd_notify_head, &n->list_node);
rt_free(n);
}
rt_set_errno(ENOMEM);
}
}
rt_mutex_release(&sfd->lock);
return ret;
}
/**
* @brief Creates a new signalfd file descriptor or modifies an existing one.
* @param fd File descriptor to modify (-1 to create a new one).
* @param mask Signal mask.
* @param flags File descriptor flags.
* @return Upon successful completion, returns the file descriptor number; otherwise, returns an error code.
*/
static int signalfd_do(int fd, const sigset_t *mask, int flags)
{
struct dfs_file *df;
struct rt_signalfd_ctx *sfd;
rt_err_t ret = 0;
if (fd == -1)
{
fd = fd_new();
if (fd < 0)
return -1;
ret = fd;
df = fd_get(fd);
if (df)
{
sfd = (struct rt_signalfd_ctx *)rt_malloc(sizeof(struct rt_signalfd_ctx));
if (sfd)
{
df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
if (df->vnode)
{
dfs_vnode_init(df->vnode, FT_REGULAR, &signalfd_fops);
df->vnode->data = sfd;
for (int i = 0; i < is_head_init; i++)
{
sfd->lwp[i] = RT_NULL;
}
sigemptyset(&sfd->sigmask);
memcpy(&sfd->sigmask, mask, sizeof(sigset_t));
rt_mutex_init(&sfd->lock, SIGNALFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
rt_wqueue_init(&sfd->signalfd_queue);
if (signalfd_add_notify(sfd) < 0)
{
is_head_init = 0;
fd_release(fd);
rt_free(sfd);
ret = -1;
}
sfd->sig_num = 0;
df->flags |= flags;
#ifdef RT_USING_DFS_V2
df->fops = &signalfd_fops;
#endif
}
else
{
fd_release(fd);
rt_free(sfd);
ret = -1;
}
}
else
{
fd_release(fd);
ret = -1;
}
}
else
{
fd_release(fd);
}
}
else
{
df = fd_get(fd);
if (df)
{
sfd = df->vnode->data;
df->flags = flags;
sigemptyset(&sfd->sigmask);
memcpy(&sfd->sigmask, mask, sizeof(sigset_t));
ret = fd;
}
else
{
rt_set_errno(EBADF);
ret = -1;
}
}
return ret;
}
/**
* @brief Creates a new signalfd file descriptor or modifies an existing one.
* @param fd File descriptor to modify (-1 to create a new one).
* @param mask Signal mask.
* @param flags File descriptor flags.
* @return Upon successful completion, returns the file descriptor number; otherwise, returns an error code.
*/
int signalfd(int fd, const sigset_t *mask, int flags)
{
return signalfd_do(fd, mask, flags);
}

View File

@@ -0,0 +1,6 @@
from building import *
src = ['stdio.c']
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_STDIO'], CPPPATH = [GetCurrentDir()])
Return('group')

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#ifndef __POSIX_STDIO_H__
#define __POSIX_STDIO_H__
#include <rtconfig.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
int rt_posix_stdio_init(void);
int rt_posix_stdio_get_console(void);
int rt_posix_stdio_set_console(const char* device_name, int mode);
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
#ifdef __cplusplus
}
#endif
#endif /* __POSIX_STDIO_H__ */

View File

@@ -0,0 +1,275 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
* 2023/08/07 Meco Man rename as posix/stdio.c
*/
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/errno.h>
#include "posix/stdio.h"
#define STDIO_DEVICE_NAME_MAX 32
int sys_dup2(int oldfd, int new);
int rt_posix_stdio_init(void)
{
rt_device_t dev_console;
dev_console = rt_console_get_device();
if (dev_console)
{
int fd = rt_posix_stdio_set_console(dev_console->parent.name, O_RDWR);
if (fd < 0)
{
return -1;
}
/* set fd (0, 1, 2) */
sys_dup2(fd, 0);
sys_dup2(fd, 1);
sys_dup2(fd, 2);
}
return 0;
}
INIT_ENV_EXPORT(rt_posix_stdio_init);
#if defined(RT_USING_NEWLIBC)
#define NEWLIB_VERSION_NUM (__NEWLIB__ * 10000U + __NEWLIB_MINOR__ * 100U + __NEWLIB_PATCHLEVEL__)
static FILE* std_console = NULL;
int rt_posix_stdio_set_console(const char* device_name, int mode)
{
FILE *fp;
char name[STDIO_DEVICE_NAME_MAX];
char *file_mode;
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
if (mode == O_RDWR)
{
file_mode = "r+";
}
else if (mode == O_WRONLY)
{
file_mode = "wb";
}
else
{
file_mode = "rb";
}
fp = fopen(name, file_mode);
if (fp)
{
setvbuf(fp, NULL, _IONBF, 0);
if (std_console)
{
fclose(std_console);
std_console = NULL;
}
std_console = fp;
if (mode == O_RDWR)
{
_GLOBAL_REENT->_stdin = std_console;
}
else
{
_GLOBAL_REENT->_stdin = NULL;
}
if (mode == O_RDONLY)
{
_GLOBAL_REENT->_stdout = NULL;
_GLOBAL_REENT->_stderr = NULL;
}
else
{
_GLOBAL_REENT->_stdout = std_console;
_GLOBAL_REENT->_stderr = std_console;
}
#if (NEWLIB_VERSION_NUM < 30400U) || (NEWLIB_VERSION_NUM >= 40000U && NEWLIB_VERSION_NUM < 40300U)
_GLOBAL_REENT->__sdidinit = 1; /* __sdidinit is obselete */
#endif
}
if (std_console)
return fileno(std_console);
return -1;
}
int rt_posix_stdio_get_console(void)
{
if (std_console)
return fileno(std_console);
else
return -1;
}
#elif defined(RT_USING_MUSLLIBC)
static FILE* std_console = NULL;
int rt_posix_stdio_set_console(const char* device_name, int mode)
{
FILE *fp;
char name[STDIO_DEVICE_NAME_MAX];
char *file_mode;
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
if (mode == O_RDWR) file_mode = "r+";
else if (mode == O_WRONLY) file_mode = "wb";
else file_mode = "rb";
fp = fopen(name, file_mode);
if (fp)
{
setvbuf(fp, NULL, _IONBF, 0);
if (std_console)
{
fclose(std_console);
std_console = NULL;
}
std_console = fp;
}
if (std_console)
{
int fd = fileno(std_console);
return fd;
}
return -1;
}
int rt_posix_stdio_get_console(void)
{
int ret = -1;
if (std_console)
{
ret = fileno(std_console);
}
return ret;
}
#else
static int std_fd = -1;
int rt_posix_stdio_set_console(const char* device_name, int mode)
{
int fd;
char name[STDIO_DEVICE_NAME_MAX];
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
fd = open(name, mode, 0);
if (fd >= 0)
{
if (std_fd >= 0)
{
close(std_fd);
}
std_fd = fd;
}
return std_fd;
}
int rt_posix_stdio_get_console(void) {
return std_fd;
}
#endif /* defined(RT_USING_NEWLIBC) */
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
{
char *cur_pos, *new_lineptr;
size_t new_lineptr_len;
int c;
if (lineptr == NULL || n == NULL || stream == NULL)
{
errno = EINVAL;
return -1;
}
if (*lineptr == NULL)
{
*n = 128; /* init len */
if ((*lineptr = (char *)malloc(*n)) == NULL)
{
errno = ENOMEM;
return -1;
}
}
cur_pos = *lineptr;
for (;;)
{
c = getc(stream);
if (ferror(stream) || (c == EOF && cur_pos == *lineptr))
return -1;
if (c == EOF)
break;
if ((*lineptr + *n - cur_pos) < 2)
{
if (LONG_MAX / 2 < *n)
{
#ifdef EOVERFLOW
errno = EOVERFLOW;
#else
errno = ERANGE; /* no EOVERFLOW defined */
#endif
return -1;
}
new_lineptr_len = *n * 2;
if ((new_lineptr = (char *)realloc(*lineptr, new_lineptr_len)) == NULL)
{
errno = ENOMEM;
return -1;
}
cur_pos = new_lineptr + (cur_pos - *lineptr);
*lineptr = new_lineptr;
*n = new_lineptr_len;
}
*cur_pos++ = (char)c;
if (c == delim)
break;
}
*cur_pos = '\0';
return (ssize_t)(cur_pos - *lineptr);
}
ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
return getdelim(lineptr, n, '\n', stream);
}

View File

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

View File

@@ -0,0 +1,273 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-08-30 Bernard The first version
* 2024-04-26 TroyMitchell Add comments for all functions
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include "termios.h"
#include <rtthread.h>
/**
* @brief Gets the current attributes of a terminal device.
* @param fd File descriptor of the terminal device.
* @param tio Pointer to a struct termios where the attributes will be stored.
* @return Upon successful completion, returns 0; otherwise, returns -1.
*
* @note This function retrieves the current attributes of a terminal device specified by the file descriptor fd.
* It uses the ioctl system call with the TCGETA command to obtain the attributes and stores them in the
* struct termios pointed to by tio.
* If the ioctl operation fails, the function returns -1 to indicate an error.
*/
int tcgetattr(int fd, struct termios *tio)
{
/* Get the current serial port settings. */
if (ioctl(fd, TCGETA, tio))
return -1;
return 0;
}
/**
* @brief Sets the attributes of a terminal device.
* @param fd File descriptor of the terminal device.
* @param act Action to be taken for the attribute change (TCSANOW, TCSADRAIN, or TCSAFLUSH).
* @param tio Pointer to a struct termios containing the new attributes.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function sets the attributes of a terminal device specified by the file descriptor fd.
* The act parameter determines when the attribute change takes effect:
* - TCSANOW: Make the change immediately.
* - TCSADRAIN: Make the change after all currently written data has been transmitted.
* - TCSAFLUSH: Make the change after all currently written data has been transmitted, and discard
* any received but unread data.
* The new attributes are specified in the struct termios pointed to by tio.
* The ioctl system call is used to set the attributes based on the value of act.
* If the ioctl operation fails or an invalid action is specified, errno is set to indicate the error,
* and the function returns -1.
*/
int tcsetattr(int fd, int act, const struct termios *tio)
{
switch (act)
{
case TCSANOW:
/* make the change immediately */
return (ioctl(fd, TCSETA, (void*)tio));
case TCSADRAIN:
/*
* Don't make the change until all currently written data
* has been transmitted.
*/
return (ioctl(fd, TCSETAW, (void*)tio));
case TCSAFLUSH:
/* Don't make the change until all currently written data
* has been transmitted, at which point any received but
* unread data is also discarded.
*/
return (ioctl(fd, TCSETAF, (void*)tio));
default:
errno = EINVAL;
return (-1);
}
}
/**
* @brief Gets the session ID of a terminal.
* @param fd File descriptor of the terminal device.
* @return Always returns 0.
*
* @note This function is a stub and always returns 0.
* In POSIX systems, tcgetsid() is used to get the session ID of the terminal associated with the file descriptor fd.
* However, this function does not provide this functionality and simply returns 0 as a placeholder.
*/
pid_t tcgetsid(int fd)
{
return 0;
}
/**
* @brief Gets the output baud rate from a termios structure.
* @param tio Pointer to a struct termios containing the terminal attributes.
* @return Output baud rate extracted from the terminal attributes.
*
* @note This function extracts the output baud rate from the termios structure pointed to by tio.
* It retrieves the baud rate from the c_cflag member of the termios structure using the CBAUD mask.
* The output baud rate is returned as a speed_t type.
*/
speed_t cfgetospeed(const struct termios *tio)
{
return tio->c_cflag & CBAUD;
}
/**
* @brief Gets the input baud rate from a termios structure.
* @param tio Pointer to a struct termios containing the terminal attributes.
* @return Input baud rate extracted from the terminal attributes.
*
* @note This function is a wrapper for the cfgetospeed() function.
* It returns the input baud rate by calling cfgetospeed() with the termios structure pointer tio.
*/
speed_t cfgetispeed(const struct termios *tio)
{
return cfgetospeed(tio);
}
/**
* @brief Sets the output baud rate in a termios structure.
* @param tio Pointer to a struct termios where the output baud rate will be set.
* @param speed Output baud rate to be set.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function sets the output baud rate in the termios structure pointed to by tio.
* The speed parameter specifies the baud rate to be set.
* If the specified speed exceeds the CBAUD mask, indicating an invalid baud rate value,
* errno is set to EINVAL, and the function returns -1.
* Otherwise, the function clears the CBAUD bits in the c_cflag member of the termios structure
* and sets them to the specified speed value. It then returns 0 to indicate success.
*/
int cfsetospeed(struct termios *tio, speed_t speed)
{
if (speed & ~CBAUD)
{
errno = EINVAL;
return -1;
}
tio->c_cflag &= ~CBAUD;
tio->c_cflag |= speed;
return 0;
}
/**
* @brief Sets the input baud rate in a termios structure.
* @param tio Pointer to a struct termios where the input baud rate will be set.
* @param speed Input baud rate to be set.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function sets the input baud rate in the termios structure pointed to by tio.
* The speed parameter specifies the input baud rate to be set.
* If the speed parameter is non-zero, indicating a valid baud rate, the function calls
* cfsetospeed() to set both the input and output baud rates to the specified value.
* If speed is zero, indicating that no baud rate needs to be set for input, the function returns 0
* without making any changes to the termios structure.
*/
int cfsetispeed(struct termios *tio, speed_t speed)
{
return speed ? cfsetospeed(tio, speed) : 0;
}
/**
* @brief Sends a break signal on a terminal.
* @param fd File descriptor of the terminal device.
* @param dur Duration of the break signal (ignored).
* @return Always returns 0.
*
* @note This function is a stub and does not send an actual break signal.
* Sending a break signal with a nonzero duration is implementation-defined,
* so this function ignores the duration parameter and always returns 0.
*/
int tcsendbreak(int fd, int dur)
{
/* nonzero duration is implementation-defined, so ignore it */
return 0;
}
/**
* @brief Flushes data from a terminal input or output queue.
* @param fd File descriptor of the terminal device.
* @param queue Queue to be flushed (TCIFLUSH, TCOFLUSH, or TCIOFLUSH).
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function flushes data from the input or output queue of the terminal device specified by the file descriptor fd.
* The queue parameter specifies which queue to flush:
* - TCIFLUSH: Flushes data from the input queue.
* - TCOFLUSH: Flushes data from the output queue.
* - TCIOFLUSH: Flushes data from both the input and output queues.
* The ioctl system call is used with the TCFLSH command to perform the flush operation.
* If the ioctl operation fails, errno is set to indicate the error, and the function returns -1.
*/
int tcflush(int fd, int queue)
{
return ioctl(fd, TCFLSH, (void*)(rt_ubase_t)queue);
}
/**
* @brief Controls the terminal flow control.
* @param fd File descriptor of the terminal device.
* @param action Action to be taken for flow control (TCOOFF, TCOON, TCIOFF, or TCION).
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function controls the flow of data on the terminal device specified by the file descriptor fd.
* The action parameter determines the flow control action to be taken:
* - TCOOFF: Suspends output transmission.
* - TCOON: Restarts output transmission.
* - TCIOFF: Suspends input transmission.
* - TCION: Restarts input transmission.
* The ioctl system call is used with the TCXONC command to perform the flow control operation.
* If the ioctl operation fails, errno is set to indicate the error, and the function returns -1.
*/
int tcflow(int fd, int action)
{
return ioctl(fd, TCXONC, (void*)(rt_ubase_t)action);
}
/**
* @brief Waits until all output written to the terminal is transmitted.
* @param fd File descriptor of the terminal device.
* @return Always returns 0.
*
* @note This function is a stub and does not perform any action.
* In POSIX systems, tcdrain() is used to wait until all output written to the terminal
* is transmitted, but this function does not provide this functionality and simply returns 0 as a placeholder.
*/
int tcdrain(int fd)
{
return 0;
}
/**
* @brief Sets the terminal attributes to raw mode.
* @param t Pointer to a struct termios where the terminal attributes will be set.
*
* @note This function sets the terminal attributes pointed to by t to raw mode.
* Raw mode disables special input and output processing features, making the terminal behave more like a data stream.
* The following modifications are made to the termios structure:
* - Input flags (c_iflag) are cleared of various processing flags.
* - Output flags (c_oflag) are cleared to disable output processing.
* - Local flags (c_lflag) are cleared to disable canonical mode, echoing, and signal handling.
* - Control flags (c_cflag) are modified to set character size to 8 bits and disable parity.
* - The VMIN and VTIME control characters are set to 1 and 0, respectively, for non-blocking read behavior.
*/
void cfmakeraw(struct termios *t)
{
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t->c_oflag &= ~OPOST;
t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
t->c_cflag &= ~(CSIZE|PARENB);
t->c_cflag |= CS8;
t->c_cc[VMIN] = 1;
t->c_cc[VTIME] = 0;
}
/**
* @brief Sets the input and output baud rates in a termios structure.
* @param tio Pointer to a struct termios where the input and output baud rates will be set.
* @param speed Baud rate to be set for both input and output.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function is a wrapper for the cfsetospeed() function.
* It sets both the input and output baud rates in the termios structure pointed to by tio to the specified speed.
* The function returns the result of cfsetospeed(), which sets the baud rate for both input and output.
*/
int cfsetspeed(struct termios *tio, speed_t speed)
{
return cfsetospeed(tio, speed);
}

View File

@@ -0,0 +1,248 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/08/30 Bernard The first version
* 2021/12/10 linzhenxing put tty system
*/
#ifndef __TERMIOS_H__
#define __TERMIOS_H__
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned char cc_t;
typedef unsigned int speed_t;
typedef unsigned int tcflag_t;
#define NCCS 32
struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_line;
cc_t c_cc[NCCS];
speed_t __c_ispeed;
speed_t __c_ospeed;
};
#ifndef NCC
#define NCC 8
struct termio
{
unsigned short c_iflag; /* input mode flags */
unsigned short c_oflag; /* output mode flags */
unsigned short c_cflag; /* control mode flags */
unsigned short c_lflag; /* local mode flags */
unsigned char c_line; /* line discipline */
unsigned char c_cc[NCC]; /* control characters */
};
#endif
/* c_cc characters */
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define VTIME 5
#define VMIN 6
#define VSWTC 7
#define VSTART 8
#define VSTOP 9
#define VSUSP 10
#define VEOL 11
#define VREPRINT 12
#define VDISCARD 13
#define VWERASE 14
#define VLNEXT 15
#define VEOL2 16
/* c_iflag bits */
#define IGNBRK 0000001
#define BRKINT 0000002
#define IGNPAR 0000004
#define PARMRK 0000010
#define INPCK 0000020
#define ISTRIP 0000040
#define INLCR 0000100
#define IGNCR 0000200
#define ICRNL 0000400
#define IUCLC 0001000
#define IXON 0002000
#define IXANY 0004000
#define IXOFF 0010000
#define IMAXBEL 0020000
#define IUTF8 0040000
/* c_oflag bits */
#define OPOST 0000001
#define OLCUC 0000002
#define ONLCR 0000004
#define OCRNL 0000010
#define ONOCR 0000020
#define ONLRET 0000040
#define OFILL 00000100
#define OFDEL 00000200
#define NLDLY 00001400
#define NL0 00000000
#define NL1 00000400
#define NL2 00001000
#define NL3 00001400
#define TABDLY 00006000
#define TAB0 00000000
#define TAB1 00002000
#define TAB2 00004000
#define TAB3 00006000
#define CRDLY 00030000
#define KCR0 00000000
#define KCR1 00010000
#define KCR2 00020000
#define KCR3 00030000
#define FFDLY 00040000
#define FF0 00000000
#define FF1 00040000
#define BSDLY 00100000
#define BS0 00000000
#define BS1 00100000
#define VTDLY 00200000
#define VT0 00000000
#define VT1 00200000
#define XTABS 01000000
#define B0 0000000
#define B50 0000001
#define B75 0000002
#define B110 0000003
#define B134 0000004
#define B150 0000005
#define B200 0000006
#define B300 0000007
#define B600 0000010
#define B1200 0000011
#define B1800 0000012
#define B2400 0000013
#define B4800 0000014
#define B9600 0000015
#define B19200 0000016
#define B38400 0000017
#define B57600 0010001
#define B115200 0010002
#define B230400 0010003
#define B460800 0010004
#define B500000 0010005
#define B576000 0010006
#define B921600 0010007
#define B1000000 0010010
#define B1152000 0010011
#define B1500000 0010012
#define B2000000 0010013
#define B2500000 0010014
#define B3000000 0010015
#define B3500000 0010016
#define B4000000 0010017
#define CSIZE 0000060
#define CS5 0000000
#define CS6 0000020
#define CS7 0000040
#define CS8 0000060
#define CSTOPB 0000100
#define CREAD 0000200
#define PARENB 0000400
#define PARODD 0001000
#define HUPCL 0002000
#define CLOCAL 0004000
/* c_lflag bits */
#define ISIG 0000001
#define ICANON 0000002
#define XCASE 0000004
#define ECHO 0000010
#define ECHOE 0000020
#define ECHOK 0000040
#define ECHONL 0000100
#define NOFLSH 0000200
#define TOSTOP 0000400
#define ECHOCTL 0001000
#define ECHOPRT 0002000
#define ECHOKE 0004000
#define FLUSHO 0010000
#define PENDIN 0040000
#define IEXTEN 0100000
#define EXTPROC 0200000
#define TCOOFF 0
#define TCOON 1
#define TCIOFF 2
#define TCION 3
#define TCIFLUSH 0
#define TCOFLUSH 1
#define TCIOFLUSH 2
#define TCSANOW 0
#define TCSADRAIN 1
#define TCSAFLUSH 2
#define EXTA 0000016
#define EXTB 0000017
#define CBAUD 0010017
#define CBAUDEX 0010000
#define CIBAUD 002003600000
#define CMSPAR 010000000000
#define CRTSCTS 020000000000
#define XCASE 0000004
#define ECHOCTL 0001000
#define ECHOPRT 0002000
#define ECHOKE 0004000
#define FLUSHO 0010000
#define PENDIN 0040000
#define EXTPROC 0200000
/* intr=^C quit=^| erase=del kill=^U
eof=^D vtime=\0 vmin=\1 sxtc=\0
start=^Q stop=^S susp=^Z eol=\0
reprint=^R discard=^U werase=^W lnext=^V
eol2=\0
*/
#define INIT_C_CC "\003\034\177\025\004\0\1\0\021\023\032\0\022\017\027\026\0"
speed_t cfgetospeed (const struct termios *);
speed_t cfgetispeed (const struct termios *);
int cfsetospeed (struct termios *, speed_t);
int cfsetispeed (struct termios *, speed_t);
int tcgetattr (int, struct termios *);
int tcsetattr (int, int, const struct termios *);
int tcsendbreak (int, int);
int tcdrain (int);
int tcflush (int, int);
int tcflow (int, int);
pid_t tcgetsid (int);
void cfmakeraw(struct termios *);
int cfsetspeed(struct termios *, speed_t);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,14 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_USING_DFS'):
src += ['timerfd.c']
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_TIMERFD'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,562 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-09-20 zmq810150896 first version
*/
#include <rtthread.h>
#include <dfs_file.h>
#include <stdint.h>
#include <poll.h>
#include <sys/timerfd.h>
#define DBG_TAG "TIMERFD"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define INIT_PERIODIC 0
#define OPEN_PERIODIC 1
#define ENTER_PERIODIC 2
#define SEC_TO_MSEC 1000
#define MSEC_TO_NSEC 1000000
#define SEC_TO_NSEC 1000000000
#define TIME_INT32_MAX 0x7FFFFFFF
#define TIMERFD_MUTEX_NAME "TIMERFD"
#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
struct rt_timerfd
{
rt_wqueue_t timerfd_queue;
struct itimerspec ittimer;
rt_timer_t timer;
struct rt_mutex lock;
struct timespec pre_time;
rt_atomic_t timeout_num;
struct rt_wqueue_node wqn;
rt_atomic_t ticks;
int clockid;
int isperiodic;
int tick_out;
};
static int timerfd_close(struct dfs_file *file);
static int timerfd_poll(struct dfs_file *file, struct rt_pollreq *req);
#ifndef RT_USING_DFS_V2
static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count);
#else
static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
#endif
static const struct dfs_file_ops timerfd_fops =
{
.close = timerfd_close,
.poll = timerfd_poll,
.read = timerfd_read,
};
static int timerfd_close(struct dfs_file *file)
{
struct rt_timerfd *tfd;
if (file->vnode->ref_count != 1)
return 0;
tfd = file->vnode->data;
if (tfd)
{
if (tfd->timer != RT_NULL)
{
rt_timer_stop(tfd->timer);
rt_timer_delete(tfd->timer);
tfd->timer = RT_NULL;
}
if (tfd->wqn.wqueue)
{
rt_wqueue_remove(&tfd->wqn);
}
rt_mutex_detach(&tfd->lock);
rt_free(tfd);
}
return 0;
}
static int timerfd_poll(struct dfs_file *file, struct rt_pollreq *req)
{
struct rt_timerfd *tfd;
int events = 0;
tfd = file->vnode->data;
rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
rt_poll_add(&tfd->timerfd_queue, req);
rt_mutex_release(&tfd->lock);
if (rt_atomic_load(&(tfd->ticks)) > 0)
{
events |= POLLIN;
}
return events;
}
#ifndef RT_USING_DFS_V2
static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count)
#else
static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
#endif
{
struct rt_timerfd *tfd;
rt_uint64_t *buffer;
int ret = 0;
buffer = (rt_uint64_t *)buf;
if (sizeof(buffer) > count)
{
rt_set_errno(EINVAL);
return -1;
}
tfd = file->vnode->data;
if (!tfd)
{
rt_set_errno(EINVAL);
return -1;
}
if ((rt_atomic_load(&(tfd->ticks)) == 0) && (file->flags & O_NONBLOCK))
{
rt_set_errno(EAGAIN);
return -EAGAIN;
}
else
{
if (rt_atomic_load(&(tfd->ticks)) == 0)
{
tfd->wqn.polling_thread = rt_thread_self();
if (tfd->wqn.wqueue)
{
rt_wqueue_remove(&tfd->wqn);
}
rt_wqueue_add(&tfd->timerfd_queue, &tfd->wqn);
ret = rt_thread_suspend_with_flag(tfd->wqn.polling_thread, RT_INTERRUPTIBLE);
if (ret == RT_EOK)
{
rt_schedule();
}
else
{
return ret;
}
}
(*buffer) = rt_atomic_load(&(tfd->timeout_num));
rt_atomic_store(&(tfd->timeout_num), 0);
rt_atomic_store(&(tfd->ticks), 0);
}
return sizeof(buffer);
}
static int timerfd_wqueue_callback(struct rt_wqueue_node *wait, void *key)
{
return 0;
}
static int timerfd_do_create(int clockid, int flags)
{
struct rt_timerfd *tfd = RT_NULL;
struct dfs_file *df;
rt_err_t ret = -1;
int fd = -1;
if ((flags & ~TFD_SHARED_FCNTL_FLAGS) ||
(clockid != CLOCK_MONOTONIC &&
clockid != CLOCK_REALTIME &&
clockid != CLOCK_REALTIME_ALARM &&
clockid != CLOCK_BOOTTIME &&
clockid != CLOCK_BOOTTIME_ALARM))
{
rt_set_errno(EINVAL);
return -1;
}
if ((clockid == CLOCK_REALTIME_ALARM ||
clockid == CLOCK_BOOTTIME_ALARM))
{
rt_set_errno(EPERM);
return -1;
}
fd = fd_new();
if (fd < 0)
{
rt_set_errno(EINVAL);
return -1;
}
ret = fd;
df = fd_get(fd);
if (df)
{
df->flags |= flags;
tfd = (struct rt_timerfd *)rt_calloc(1, sizeof(struct rt_timerfd));
if (tfd)
{
rt_mutex_init(&tfd->lock, TIMERFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
rt_wqueue_init(&tfd->timerfd_queue);
tfd->isperiodic = INIT_PERIODIC;
tfd->ticks = 0;
tfd->timeout_num = 0;
tfd->tick_out = 0;
tfd->clockid = clockid;
tfd->timer = RT_NULL;
tfd->pre_time.tv_sec = 0;
tfd->pre_time.tv_nsec = 0;
tfd->wqn.polling_thread = rt_thread_self();
rt_list_init(&(tfd->wqn.list));
tfd->wqn.wakeup = timerfd_wqueue_callback;
df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
if (df->vnode)
{
dfs_vnode_init(df->vnode, FT_REGULAR, &timerfd_fops);
df->vnode->data = tfd;
#ifdef RT_USING_DFS_V2
df->fops = &timerfd_fops;
#endif
}
else
{
rt_free(tfd);
fd_release(fd);
rt_set_errno(ENOMEM);
ret = -1;
}
}
else
{
fd_release(fd);
rt_set_errno(ENOMEM);
ret = -1;
}
}
else
{
fd_release(fd);
ret = -1;
}
return ret;
}
static int get_current_time(struct rt_timerfd *tfd, struct timespec *time)
{
int ret = 0;
struct timespec *cur_time = RT_NULL;
if (time == RT_NULL)
{
cur_time = &tfd->pre_time;
}
else
{
cur_time = time;
}
if (tfd->clockid >= 0)
{
ret = clock_gettime(tfd->clockid, cur_time);
}
else
{
ret = clock_gettime(CLOCK_MONOTONIC, cur_time);
}
return ret;
}
static void timerfd_timeout(void *parameter)
{
struct rt_timerfd *tfd = RT_NULL;
tfd = (struct rt_timerfd *)parameter;
if (tfd == RT_NULL)
{
return ;
}
rt_wqueue_wakeup(&tfd->timerfd_queue, (void *)POLLIN);
rt_atomic_store(&(tfd->ticks), 1);
rt_atomic_add(&(tfd->timeout_num), 1);
rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
get_current_time(tfd, RT_NULL);
if (tfd->isperiodic == OPEN_PERIODIC)
{
if (tfd->timer)
{
rt_timer_stop(tfd->timer);
rt_timer_delete(tfd->timer);
tfd->timer = RT_NULL;
}
tfd->isperiodic = ENTER_PERIODIC;
tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
tfd, tfd->tick_out,
RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
if (tfd->timer == RT_NULL)
{
LOG_E("rt_timer_create fail \n");
rt_mutex_release(&tfd->lock);
return ;
}
rt_timer_start(tfd->timer);
}
rt_mutex_release(&tfd->lock);
}
static void timerfd_time_operation(time_t *sec, long *nsec)
{
if (*nsec < 0)
{
if (*sec > 0)
{
*sec -= 1;
*nsec = 1 * SEC_TO_NSEC + *nsec;
}
}
if (*sec < 0 || *nsec < 0)
{
*sec = 0;
*nsec = 0;
}
}
static int timerfd_do_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
{
int ret = 0;
struct rt_timerfd *tfd;
struct dfs_file *df;
struct timespec current_time;
int tick_out;
rt_int64_t value_msec;
rt_int64_t interval_msec;
rt_int64_t cur_time = 0;
if (fd < 0)
{
rt_set_errno(EINVAL);
return -EINVAL;
}
df = fd_get(fd);
if (!df)
return -EINVAL;
tfd = df->vnode->data;
rt_atomic_store(&(tfd->ticks), 0);
rt_atomic_store(&(tfd->timeout_num), 0);
rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
tfd->isperiodic = INIT_PERIODIC;
if (old)
{
old->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
old->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
old->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec;
old->it_value.tv_sec = tfd->ittimer.it_value.tv_sec;
}
if (new)
{
if (tfd->timer != RT_NULL)
{
rt_timer_stop(tfd->timer);
rt_timer_delete(tfd->timer);
tfd->timer = RT_NULL;
}
if (new->it_value.tv_nsec == 0 && new->it_value.tv_sec == 0)
{
rt_mutex_release(&tfd->lock);
return 0;
}
value_msec = (new->it_value.tv_nsec / MSEC_TO_NSEC) + (new->it_value.tv_sec * SEC_TO_MSEC);
interval_msec = (new->it_interval.tv_nsec / MSEC_TO_NSEC) + (new->it_interval.tv_sec * SEC_TO_MSEC);
current_time.tv_nsec = 0;
current_time.tv_sec = 0;
if (flags == TFD_TIMER_ABSTIME)
{
ret = get_current_time(tfd, &current_time);
if (ret < 0)
{
rt_mutex_release(&tfd->lock);
return ret;
}
cur_time = current_time.tv_sec * SEC_TO_MSEC + (current_time.tv_nsec / MSEC_TO_NSEC);
value_msec = value_msec - cur_time;
}
tfd->ittimer.it_interval.tv_nsec = new->it_interval.tv_nsec;
tfd->ittimer.it_interval.tv_sec = new->it_interval.tv_sec;
tfd->ittimer.it_value.tv_sec = new->it_value.tv_sec - current_time.tv_sec;
tfd->ittimer.it_value.tv_nsec = new->it_value.tv_nsec - current_time.tv_nsec;
timerfd_time_operation(&tfd->ittimer.it_value.tv_sec, &tfd->ittimer.it_value.tv_nsec);
if ((interval_msec > 0) && (interval_msec <= TIME_INT32_MAX))
{
tfd->tick_out = rt_tick_from_millisecond(interval_msec);
if (tfd->tick_out < 0)
{
rt_mutex_release(&tfd->lock);
return -EINVAL;
}
tfd->isperiodic = OPEN_PERIODIC;
}
get_current_time(tfd, RT_NULL);
if (value_msec > 0)
{
if (value_msec > TIME_INT32_MAX)
{
rt_mutex_release(&tfd->lock);
return -EINVAL;
}
tick_out = rt_tick_from_millisecond(value_msec);
if (tick_out < 0)
{
rt_mutex_release(&tfd->lock);
return -EINVAL;
}
tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
tfd, tick_out,
RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
if (tfd->timer == RT_NULL)
{
LOG_E("rt_timer_create fail \n");
rt_mutex_release(&tfd->lock);
return -ENOMEM;
}
rt_timer_start(tfd->timer);
}
else
{
timerfd_timeout(tfd);
}
}
else
{
rt_set_errno(EINVAL);
ret = -1;
}
rt_mutex_release(&tfd->lock);
return ret;
}
static int timerfd_do_gettime(int fd, struct itimerspec *cur)
{
struct rt_timerfd *tfd;
struct dfs_file *df = RT_NULL;
struct timespec cur_time;
rt_int64_t tv_sec = 0;
rt_int64_t tv_nsec = 0;
df = fd_get(fd);
if (df == RT_NULL)
{
rt_set_errno(EINVAL);
return -1;
}
tfd = df->vnode->data;
get_current_time(tfd, &cur_time);
rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
tv_sec = cur_time.tv_sec - tfd->pre_time.tv_sec;
tv_nsec = cur_time.tv_nsec - tfd->pre_time.tv_nsec;
timerfd_time_operation(&tv_sec, &tv_nsec);
cur->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
cur->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
if (tfd->isperiodic == ENTER_PERIODIC)
{
cur->it_value.tv_nsec = tfd->ittimer.it_interval.tv_nsec - tv_nsec;
cur->it_value.tv_sec = tfd->ittimer.it_interval.tv_sec - tv_sec;
timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
}
else
{
if (rt_atomic_load(&(tfd->timeout_num)) == 1)
{
cur->it_value.tv_nsec = 0;
cur->it_value.tv_sec = 0;
}
else
{
cur->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec - tv_nsec;
cur->it_value.tv_sec = tfd->ittimer.it_value.tv_sec - tv_sec;
timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
}
}
rt_mutex_release(&tfd->lock);
return 0;
}
int timerfd_create(int clockid, int flags)
{
return timerfd_do_create(clockid, flags);
}
int timerfd_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
{
return timerfd_do_settime(fd, flags, new, old);
}
int timerfd_gettime(int fd, struct itimerspec *cur)
{
return timerfd_do_gettime(fd, cur);
}

View File

@@ -0,0 +1,39 @@
menu "Interprocess Communication (IPC)"
config RT_USING_POSIX_PIPE
bool "Enable pipe and FIFO"
select RT_USING_POSIX_FS
select RT_USING_POSIX_DEVIO
select RT_USING_POSIX_POLL
select RT_USING_RESOURCE_ID
default n
config RT_USING_POSIX_PIPE_SIZE
int "Set pipe buffer 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
select RT_USING_MESSAGEQUEUE_PRIORITY
select RT_USING_DFS_MQUEUE
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', 'RT_USING_DFS_MQUEUE']):
src += ['mqueue.c']
if GetDepend('RT_USING_POSIX_MESSAGE_SEMAPHORE'):
src += ['semaphore.c']
group = DefineGroup('POSIX', src, depend = [''], CPPPATH = inc)
Return('group')

View File

@@ -0,0 +1,441 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-07-20 zmq810150896 first version
* 2024-04-30 TroyMitchell Add comments for all functions
*/
#include <dfs_file.h>
#include <unistd.h>
#include "mqueue.h"
/**
* @brief Sets the attributes of a message queue.
* @param id Identifier of the message queue.
* @param mqstat Pointer to a struct mq_attr containing the new attributes (ignored).
* @param omqstat Pointer to a struct mq_attr where the old attributes will be stored.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function sets the attributes of the message queue specified by id.
* The new attributes are provided in the mqstat parameter, but this implementation ignores it.
* Instead, the function calls mq_getattr() to retrieve the current attributes of the message queue
* and stores them in the struct mq_attr pointed to by omqstat.
* If mqstat is RT_NULL, the function behaves as a query and retrieves the attributes without setting new values.
* If an error occurs during the operation, errno is set to indicate the error, and the function returns -1.
*/
int mq_setattr(mqd_t id,
const struct mq_attr *mqstat,
struct mq_attr *omqstat)
{
if (mqstat == RT_NULL)
return mq_getattr(id, omqstat);
else
rt_set_errno(-RT_ERROR);
return -1;
}
RTM_EXPORT(mq_setattr);
/**
* @brief Gets the attributes of a message queue.
* @param id Identifier of the message queue.
* @param mqstat Pointer to a struct mq_attr where the attributes will be stored.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function retrieves the attributes of the message queue specified by id.
* The attributes include the maximum number of messages that can be queued (mq_maxmsg),
* the maximum size of each message (mq_msgsize), the number of messages currently in the queue (mq_curmsgs),
* and the flags associated with the queue (mq_flags).
* The attributes are stored in the struct mq_attr pointed to by mqstat.
* If the message queue identified by id does not exist or if mqstat is a null pointer, errno is set to EBADF,
* indicating a bad file descriptor, and the function returns -1.
* Otherwise, the function retrieves the attributes from the message queue and stores them in mqstat, returning 0 to indicate success.
*/
int mq_getattr(mqd_t id, struct mq_attr *mqstat)
{
rt_mq_t mq;
struct mqueue_file *mq_file;
mq_file = fd_get(id)->vnode->data;
mq = (rt_mq_t)mq_file->data;
if ((mq == RT_NULL) || mqstat == RT_NULL)
{
rt_set_errno(EBADF);
return -1;
}
mqstat->mq_maxmsg = mq->max_msgs;
mqstat->mq_msgsize = mq->msg_size;
mqstat->mq_curmsgs = 0;
mqstat->mq_flags = 0;
return 0;
}
RTM_EXPORT(mq_getattr);
/**
* @brief Opens or creates a message queue.
* @param name Name of the message queue.
* @param oflag Flags indicating the access mode and creation options (O_CREAT, O_EXCL, etc.).
* @param ... Additional arguments for creation options (mode, attr) (ignored).
* @return Upon successful completion, returns a message queue descriptor (mqd_t);
* otherwise, returns (mqd_t)(-1) and sets errno to indicate the error.
*
* @note This function opens or creates a message queue specified by name with the specified flags.
* If the name starts with '/', the leading '/' is ignored.
* The function then checks the length of the name and verifies if it exceeds the maximum allowed length.
* If the name is too long, errno is set to ENAMETOOLONG, indicating a name too long error.
* Next, the function attempts to find the message queue file corresponding to the name.
* If the file exists and O_CREAT and O_EXCL flags are both set, indicating exclusive creation,
* errno is set to EEXIST, indicating that the file already exists.
* If the file does not exist and O_CREAT flag is set, the function checks the message queue attributes.
* If the maximum number of messages (mq_maxmsg) in the attributes is less than or equal to 0,
* errno is set to EINVAL, indicating an invalid argument for the maximum number of messages.
* If the file does not exist and O_CREAT flag is not set, errno is set to ENOENT, indicating no such file or directory.
* If the message queue needs to be created (O_CREAT flag set), a new mqueue_file structure is allocated and initialized
* with the specified message queue attributes, and it is inserted into the message queue filesystem.
* Finally, the function constructs the path to the message queue device file, opens it with the specified flags,
* and returns the file descriptor as a message queue descriptor (mqd_t).
*/
mqd_t mq_open(const char *name, int oflag, ...)
{
int mq_fd;
va_list arg;
mode_t mode;
struct mq_attr *attr = RT_NULL;
va_start(arg, oflag);
mode = (mode_t)va_arg(arg, unsigned int);
mode = (mode_t)mode; /* self-assignment avoids compiler optimization */
attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
attr = (struct mq_attr *)attr; /* self-assignment avoids compiler optimization */
va_end(arg);
if(*name == '/')
{
name++;
}
int len = rt_strlen(name);
if (len > RT_NAME_MAX)
{
rt_set_errno(ENAMETOOLONG);
return (mqd_t)(-1);
}
rt_size_t size;
struct mqueue_file *mq_file;
mq_file = dfs_mqueue_lookup(name, &size);
if(mq_file != RT_NULL)
{
if (oflag & O_CREAT && oflag & O_EXCL)
{
rt_set_errno(EEXIST);
return (mqd_t)(-1);
}
}
else if (oflag & O_CREAT)
{
if (attr->mq_maxmsg <= 0)
{
rt_set_errno(EINVAL);
return (mqd_t)(-1);
}
struct mqueue_file *mq_file;
mq_file = (struct mqueue_file *) rt_malloc (sizeof(struct mqueue_file));
if (mq_file == RT_NULL)
{
rt_set_errno(ENFILE);
return (mqd_t)(-1);
}
mq_file->msg_size = attr->mq_msgsize;
mq_file->max_msgs = attr->mq_maxmsg;
mq_file->data = RT_NULL;
strncpy(mq_file->name, name, RT_NAME_MAX);
dfs_mqueue_insert_after(&(mq_file->list));
}
else
{
rt_set_errno(ENOENT);
return (mqd_t)(-1);
}
const char* mq_path = "/dev/mqueue/";
char mq_name[RT_NAME_MAX + 12] = {0};
rt_sprintf(mq_name, "%s%s", mq_path, name);
mq_fd = open(mq_name, oflag);
return (mqd_t)(mq_fd);
}
RTM_EXPORT(mq_open);
/**
* @brief Receives a message from a message queue.
* @param id Message queue descriptor.
* @param msg_ptr Pointer to the buffer where the received message will be stored.
* @param msg_len Maximum size of the message buffer.
* @param msg_prio Pointer to an unsigned integer where the priority of the received message will be stored (ignored).
* @return Upon successful completion, returns the number of bytes received;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function receives a message from the message queue identified by id.
* The received message is stored in the buffer pointed to by msg_ptr, with a maximum size of msg_len bytes.
* The priority of the received message is stored in the unsigned integer pointed to by msg_prio (ignored in this implementation).
* If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
* indicating an invalid argument, and the function returns -1.
* The function then attempts to receive a message from the message queue using the rt_mq_recv_prio() function
* with an infinite timeout and uninterruptible mode.
* If a message is successfully received, the function returns the number of bytes received.
* If an error occurs during the receive operation, errno is set to EBADF, indicating a bad file descriptor,
* and the function returns -1.
*/
ssize_t mq_receive(mqd_t id, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
{
rt_mq_t mq;
rt_err_t result;
struct mqueue_file *mq_file;
mq_file = fd_get(id)->vnode->data;
mq = (rt_mq_t)mq_file->data;
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE);
if (result >= 0)
return result;
rt_set_errno(EBADF);
return -1;
}
RTM_EXPORT(mq_receive);
/**
* @brief Sends a message to a message queue.
* @param id Message queue descriptor.
* @param msg_ptr Pointer to the buffer containing the message to be sent.
* @param msg_len Size of the message to be sent.
* @param msg_prio Priority of the message to be sent.
* @return Upon successful completion, returns 0;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function sends a message to the message queue identified by id.
* The message to be sent is contained in the buffer pointed to by msg_ptr, with a size of msg_len bytes.
* The priority of the message is specified by the msg_prio parameter.
* If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
* indicating an invalid argument, and the function returns -1.
* The function then attempts to send the message to the message queue using the rt_mq_send_wait_prio() function
* with zero timeout and uninterruptible mode.
* If the message is successfully sent, the function returns 0.
* If an error occurs during the send operation, errno is set to EBADF, indicating a bad file descriptor,
* and the function returns -1.
*/
int mq_send(mqd_t id, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
{
rt_mq_t mq;
rt_err_t result;
struct mqueue_file *mq_file;
mq_file = fd_get(id)->vnode->data;
mq = (rt_mq_t)mq_file->data;
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
result = rt_mq_send_wait_prio(mq, (void *)msg_ptr, msg_len, msg_prio, 0, RT_UNINTERRUPTIBLE);
if (result == RT_EOK)
return 0;
rt_set_errno(EBADF);
return -1;
}
RTM_EXPORT(mq_send);
/**
* @brief Receives a message from a message queue with a timeout.
* @param id Message queue descriptor.
* @param msg_ptr Pointer to the buffer where the received message will be stored.
* @param msg_len Maximum size of the message buffer.
* @param msg_prio Pointer to an unsigned integer where the priority of the received message will be stored.
* @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored if null).
* @return Upon successful completion, returns the number of bytes received;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function receives a message from the message queue identified by id with a specified timeout.
* The received message is stored in the buffer pointed to by msg_ptr, with a maximum size of msg_len bytes.
* The priority of the received message is stored in the unsigned integer pointed to by msg_prio.
* If either the message queue identified by id or the msg_ptr buffer is a null pointer, errno is set to EINVAL,
* indicating an invalid argument, and the function returns -1.
* The function then converts the absolute timeout value specified by abs_timeout to system ticks,
* or sets the timeout to RT_WAITING_FOREVER if abs_timeout is null.
* It attempts to receive a message from the message queue using the rt_mq_recv_prio() function
* with the specified timeout and uninterruptible mode.
* If a message is successfully received, the function returns the number of bytes received.
* If the receive operation times out, errno is set to ETIMEDOUT, indicating a timeout error.
* If the received message is too large for the specified buffer, errno is set to EMSGSIZE, indicating a message too large error.
* If an unknown error occurs during the receive operation, errno is set to EBADMSG, indicating a bad message error,
* and the function returns -1.
*/
ssize_t mq_timedreceive(mqd_t id,
char *msg_ptr,
size_t msg_len,
unsigned *msg_prio,
const struct timespec *abs_timeout)
{
rt_mq_t mq;
rt_err_t result;
int tick = 0;
struct mqueue_file *mq_file;
mq_file = fd_get(id)->vnode->data;
mq = (rt_mq_t)mq_file->data;
/* parameters check */
if ((mq == RT_NULL) || (msg_ptr == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
if (abs_timeout != RT_NULL)
tick = rt_timespec_to_tick(abs_timeout);
else
tick = RT_WAITING_FOREVER;
result = rt_mq_recv_prio(mq, msg_ptr, msg_len, (rt_int32_t *)msg_prio, tick, RT_UNINTERRUPTIBLE);
if (result >= 0)
return result;
if (result == -RT_ETIMEOUT)
rt_set_errno(ETIMEDOUT);
else if (result == -RT_ERROR)
rt_set_errno(EMSGSIZE);
else
rt_set_errno(EBADMSG);
return -1;
}
RTM_EXPORT(mq_timedreceive);
/**
* @brief Sends a message to a message queue with a timeout (not supported).
* @param id Message queue descriptor.
* @param msg_ptr Pointer to the buffer containing the message to be sent.
* @param msg_len Size of the message to be sent.
* @param msg_prio Priority of the message to be sent.
* @param abs_timeout Pointer to a struct timespec specifying the absolute timeout value (ignored).
* @return Upon successful completion, returns 0;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function attempts to send a message to the message queue identified by id with a specified timeout,
* but timed send is not supported in the RT-Thread environment.
* Therefore, the function simply delegates the message sending operation to the mq_send() function,
* which does not involve a timeout.
* The abs_timeout parameter is ignored, and the message is sent without waiting for a timeout to occur.
* The function returns the result of the mq_send() function, which indicates whether the message was successfully sent.
*/
int mq_timedsend(mqd_t id,
const char *msg_ptr,
size_t msg_len,
unsigned msg_prio,
const struct timespec *abs_timeout)
{
/* RT-Thread does not support timed send */
return mq_send(id, msg_ptr, msg_len, msg_prio);
}
RTM_EXPORT(mq_timedsend);
/**
* @brief Registers for notification when a message is available in a message queue (not supported).
* @param id Message queue descriptor.
* @param notification Pointer to a struct sigevent specifying the notification settings (ignored).
* @return Upon successful completion, returns 0;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function attempts to register for notification when a message is available in the message queue identified by id.
* However, message queue notification is not supported in the RT-Thread environment.
* Therefore, this function simply sets errno to EBADF, indicating a bad file descriptor,
* and returns -1 to indicate that the operation is not supported.
*/
int mq_notify(mqd_t id, const struct sigevent *notification)
{
rt_mq_t mq;
struct mqueue_file *mq_file;
mq_file = fd_get(id)->vnode->data;
mq = (rt_mq_t)mq_file->data;
if (mq == RT_NULL)
{
rt_set_errno(EBADF);
return -1;
}
rt_set_errno(-RT_ERROR);
return -1;
}
RTM_EXPORT(mq_notify);
/**
* @brief Closes a message queue descriptor.
* @param id Message queue descriptor to be closed.
* @return Upon successful completion, returns 0;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function closes the message queue descriptor specified by id.
* It delegates the closing operation to the close() function, which closes the file descriptor associated with the message queue.
* If the close operation is successful, the function returns 0.
* If an error occurs during the close operation, errno is set to indicate the error, and the function returns -1.
*/
int mq_close(mqd_t id)
{
return close(id);
}
RTM_EXPORT(mq_close);
/**
* @brief This function will remove a message queue (REALTIME).
*
* @note The mq_unlink() function shall remove the message queue named by the string name.
* If one or more processes have the message queue open when mq_unlink() is called,
* destruction of the message queue shall be postponed until all references to the message queue have been closed.
* However, the mq_unlink() call need not block until all references have been closed; it may return immediately.
*
* After a successful call to mq_unlink(), reuse of the name shall subsequently cause mq_open() to behave as if
* no message queue of this name exists (that is, mq_open() will fail if O_CREAT is not set,
* or will create a new message queue if O_CREAT is set).
*
* @param name is the name of the message queue.
*
* @return Upon successful completion, the function shall return a value of zero.
* Otherwise, the named message queue shall be unchanged by this function call,
* and the function shall return a value of -1 and set errno to indicate the error.
*
* @warning This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
* check the context.
* The mq_unlink() function shall fail if:
* [EACCES]
* Permission is denied to unlink the named message queue.
* [EINTR]
* The call to mq_unlink() blocked waiting for all references to the named message queue to be closed and a signal interrupted the call.
* [ENOENT]
* The named message queue does not exist.
* The mq_unlink() function may fail if:
* [ENAMETOOLONG]
* The length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option
* or exceeds {_XOPEN_PATH_MAX} on XSI systems,or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do
* not support the XSI option or longer than {_XOPEN_NAME_MAX} on XSI systems.A call to mq_unlink() with a name argument that contains
* the same message queue name as was previously used in a successful mq_open() call shall not give an [ENAMETOOLONG] error.
*/
int mq_unlink(const char *name)
{
if(*name == '/')
{
name++;
}
const char *mq_path = "/dev/mqueue/";
char mq_name[RT_NAME_MAX + 12] = {0};
rt_sprintf(mq_name, "%s%s", mq_path, name);
return unlink(mq_name);
}
RTM_EXPORT(mq_unlink);

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#ifndef __MQUEUE_H__
#define __MQUEUE_H__
#include <rtthread.h>
#include <sys/signal.h>
#ifdef RT_USING_DFS_MQUEUE
#include "dfs_mqueue.h"
#endif
typedef int mqd_t;
struct mq_attr
{
long mq_flags; /* Message queue flags. */
long mq_maxmsg; /* Maximum number of messages. */
long mq_msgsize; /* Maximum message size. */
long mq_curmsgs; /* Number of messages currently queued. */
};
int mq_close(mqd_t mqdes);
int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);
int mq_notify(mqd_t mqdes, const struct sigevent *notification);
mqd_t mq_open(const char *name, int oflag, ...);
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio);
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio);
int mq_setattr(mqd_t mqdes,
const struct mq_attr *mqstat,
struct mq_attr *omqstat);
ssize_t mq_timedreceive(mqd_t mqdes,
char *msg_ptr,
size_t msg_len,
unsigned *msg_prio,
const struct timespec *abs_timeout);
int mq_timedsend(mqd_t mqdes,
const char *msg_ptr,
size_t msg_len,
unsigned msg_prio,
const struct timespec *abs_timeout);
int mq_unlink(const char *name);
#endif

View File

@@ -0,0 +1,558 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <rtthread.h>
#include <string.h>
#include <fcntl.h>
#include <sys/errno.h>
#include "semaphore.h"
static sem_t *posix_sem_list = RT_NULL;
static struct rt_semaphore posix_sem_lock;
/* 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);
/**
* @brief Inserts a semaphore into the linked list of semaphores.
* @param psem Pointer to the semaphore structure to be inserted.
*
* @note This function inserts the specified semaphore into a linked list of semaphores.
* The newly inserted semaphore becomes the head of the list.
* It updates the 'next' pointer of the semaphore structure to link it to the
* current head of the list and then sets the head of the list to point to the
* newly inserted semaphore.
*/
rt_inline void posix_sem_insert(sem_t *psem)
{
psem->next = posix_sem_list;
posix_sem_list = psem;
}
/**
* @brief Deletes a semaphore from the linked list of semaphores.
* @param psem Pointer to the semaphore structure to be deleted.
*
* @note This function deletes the specified semaphore from a linked list of semaphores.
* If the semaphore to be deleted is the head of the list, it updates the head of the list
* to point to the next semaphore. Otherwise, it traverses the list to find the semaphore
* to be deleted and updates the 'next' pointer of the preceding semaphore to skip over
* the semaphore to be deleted.
* After deleting the semaphore, it also deletes the underlying RT-Thread semaphore if it exists
* and frees the memory associated with the semaphore structure if it's not unnamed.
*/
static void posix_sem_delete(sem_t *psem)
{
sem_t *iter;
if (posix_sem_list == psem)
{
posix_sem_list = psem->next;
rt_sem_delete(psem->sem);
if(psem->unamed == 0)
rt_free(psem);
return;
}
for (iter = posix_sem_list; iter->next != RT_NULL; iter = iter->next)
{
if (iter->next == psem)
{
/* delete this mq */
if (psem->next != RT_NULL)
iter->next = psem->next;
else
iter->next = RT_NULL;
/* delete RT-Thread mqueue */
rt_sem_delete(psem->sem);
if(psem->unamed == 0)
rt_free(psem);
return ;
}
}
}
/**
* @brief Finds a semaphore by name in the linked list of semaphores.
* @param name Pointer to the name of the semaphore to be found.
* @return Pointer to the semaphore structure if found; otherwise, RT_NULL.
*
* @note This function searches for a semaphore with the specified name in the linked list of semaphores.
* It iterates through the list and compares the name of each semaphore with the given name.
* If a semaphore with a matching name is found, a pointer to its structure is returned.
* Otherwise, RT_NULL is returned to indicate that no semaphore with the given name was found.
*/
static sem_t *posix_sem_find(const char* name)
{
sem_t *iter;
rt_object_t object;
for (iter = posix_sem_list; iter != RT_NULL; iter = iter->next)
{
object = (rt_object_t)iter->sem;
if (strncmp(object->name, name, RT_NAME_MAX) == 0)
{
return iter;
}
}
return RT_NULL;
}
/**
* @brief Closes a POSIX semaphore.
* @param sem Pointer to the semaphore to be closed.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function decreases the reference count of the specified semaphore.
* If the reference count reaches zero, the semaphore is removed from the list
* of POSIX semaphores if it was unlinked. The memory associated with the semaphore
* is not immediately freed; instead, it will be freed when the last reference to
* the semaphore is released.
*/
int sem_close(sem_t *sem)
{
if (sem == RT_NULL)
{
rt_set_errno(EINVAL);
return -1;
}
/* lock posix semaphore list */
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
sem->refcount --;
if (sem->refcount == 0)
{
/* delete from posix semaphore list */
if (sem->unlinked)
posix_sem_delete(sem);
sem = RT_NULL;
}
rt_sem_release(&posix_sem_lock);
return 0;
}
RTM_EXPORT(sem_close);
/**
* @brief Destroys a POSIX semaphore.
* @param sem Pointer to the semaphore to be destroyed.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function destroys an unnamed POSIX semaphore.
* It first checks if the semaphore pointer is valid and if the semaphore is unnamed.
* If the semaphore is still in use (i.e., there are threads waiting on it),
* the function returns with an error code (EBUSY) without destroying the semaphore.
* Otherwise, it removes the semaphore from the list of POSIX semaphores and frees
* the memory associated with the semaphore structure.
*/
int sem_destroy(sem_t *sem)
{
if ((!sem) || !(sem->unamed))
{
rt_set_errno(EINVAL);
return -1;
}
/* lock posix semaphore list */
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
if(rt_list_len(&sem->sem->parent.suspend_thread) != 0)
{
rt_sem_release(&posix_sem_lock);
rt_set_errno(EBUSY);
return -1;
}
/* destroy an unamed posix semaphore */
posix_sem_delete(sem);
rt_sem_release(&posix_sem_lock);
return 0;
}
RTM_EXPORT(sem_destroy);
/**
* @brief Unlinks a named POSIX semaphore.
* @param name Pointer to the name of the semaphore to be unlinked.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function unlinks a named POSIX semaphore identified by the given name.
* It first searches for the semaphore with the specified name in the list of
* POSIX semaphores. If the semaphore is found, it marks the semaphore as unlinked.
* If the reference count of the semaphore is zero, indicating that no threads are
* currently using the semaphore, it removes the semaphore from the list and frees
* the associated memory. Otherwise, the semaphore is not immediately removed; it
* will be removed when its reference count reaches zero.
*/
int sem_unlink(const char *name)
{
sem_t *psem;
/* lock posix semaphore list */
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
psem = posix_sem_find(name);
if (psem != RT_NULL)
{
psem->unlinked = 1;
if (psem->refcount == 0)
{
/* remove this semaphore */
posix_sem_delete(psem);
}
rt_sem_release(&posix_sem_lock);
return 0;
}
rt_sem_release(&posix_sem_lock);
/* no this entry */
rt_set_errno(ENOENT);
return -1;
}
RTM_EXPORT(sem_unlink);
/**
* @brief Retrieves the value of a POSIX semaphore.
* @param sem Pointer to the semaphore.
* @param sval Pointer to an integer where the semaphore value will be stored.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function retrieves the current value of the specified POSIX semaphore.
* It copies the semaphore value into the memory location pointed to by sval.
* If either sem or sval is a null pointer, the function sets errno to EINVAL
* to indicate an invalid argument and returns -1.
*/
int sem_getvalue(sem_t *sem, int *sval)
{
if (!sem || !sval)
{
rt_set_errno(EINVAL);
return -1;
}
*sval = sem->sem->value;
return 0;
}
RTM_EXPORT(sem_getvalue);
/**
* @brief Initializes a POSIX semaphore.
* @param sem Pointer to the semaphore structure to be initialized.
* @param pshared Flag indicating whether the semaphore is to be shared between processes.
* @param value Initial value of the semaphore.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function initializes a POSIX semaphore with the specified initial value.
* If sem is a null pointer, the function sets errno to EINVAL to indicate an invalid argument and returns -1.
* The pshared parameter is not used in this implementation, as all semaphores are created as local (unshared).
* The value parameter specifies the initial value of the semaphore.
* The semaphore is given a unique name using a static counter, and it is created using the RT-Thread semaphore
* creation function rt_sem_create(). If memory allocation fails during semaphore creation, errno is set to ENOMEM.
* After successful initialization, the semaphore structure is inserted into the linked list of POSIX semaphores.
*/
int sem_init(sem_t *sem, int pshared, unsigned int value)
{
char name[RT_NAME_MAX];
static rt_uint16_t psem_number = 0;
if (sem == RT_NULL)
{
rt_set_errno(EINVAL);
return -1;
}
rt_snprintf(name, sizeof(name), "psem%02d", psem_number++);
sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
if (sem->sem == RT_NULL)
{
rt_set_errno(ENOMEM);
return -1;
}
/* initialize posix semaphore */
sem->refcount = 1;
sem->unlinked = 0;
sem->unamed = 1;
/* lock posix semaphore list */
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
posix_sem_insert(sem);
rt_sem_release(&posix_sem_lock);
return 0;
}
RTM_EXPORT(sem_init);
/**
* @brief Opens or creates a POSIX semaphore.
* @param name Pointer to the name of the semaphore.
* @param oflag Bitwise OR of flags indicating the operation mode.
* @param ... Additional arguments (optional).
* @return Upon successful completion, returns a pointer to the semaphore;
* otherwise, returns RT_NULL and sets errno to indicate the error.
*
* @note This function opens or creates a POSIX semaphore specified by the name argument.
* If the oflag argument includes O_CREAT, the semaphore is created if it does not already exist.
* Additional arguments may include the mode (not used in this implementation) and the initial value of the semaphore.
* If the oflag argument includes O_EXCL along with O_CREAT, the function fails if the semaphore already exists.
* If memory allocation fails during semaphore creation, errno is set to ENFILE.
* After successful creation or opening, the semaphore's reference count is incremented, and it is inserted into
* the linked list of POSIX semaphores. If the semaphore already exists and is successfully opened, its reference
* count is incremented. If the semaphore cannot be opened or created for any reason, errno is set to indicate the error,
* and the function returns RT_NULL.
*/
sem_t *sem_open(const char *name, int oflag, ...)
{
sem_t* sem;
va_list arg;
mode_t mode;
unsigned int value;
sem = RT_NULL;
/* lock posix semaphore list */
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
if (oflag & O_CREAT)
{
va_start(arg, oflag);
mode = (mode_t) va_arg( arg, unsigned int); mode = mode;
value = va_arg( arg, unsigned int);
va_end(arg);
if (oflag & O_EXCL)
{
if (posix_sem_find(name) != RT_NULL)
{
rt_set_errno(EEXIST);
goto __return;
}
}
sem = (sem_t*) rt_malloc (sizeof(struct posix_sem));
if (sem == RT_NULL)
{
rt_set_errno(ENFILE);
goto __return;
}
/* create RT-Thread semaphore */
sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
if (sem->sem == RT_NULL) /* create failed */
{
rt_set_errno(ENFILE);
goto __return;
}
/* initialize reference count */
sem->refcount = 1;
sem->unlinked = 0;
sem->unamed = 0;
/* insert semaphore to posix semaphore list */
posix_sem_insert(sem);
}
else
{
/* find semaphore */
sem = posix_sem_find(name);
if (sem != RT_NULL)
{
sem->refcount ++; /* increase reference count */
}
else
{
rt_set_errno(ENOENT);
goto __return;
}
}
rt_sem_release(&posix_sem_lock);
return sem;
__return:
/* release lock */
rt_sem_release(&posix_sem_lock);
/* release allocated memory */
if (sem != RT_NULL)
{
/* delete RT-Thread semaphore */
if (sem->sem != RT_NULL)
rt_sem_delete(sem->sem);
rt_free(sem);
}
return RT_NULL;
}
RTM_EXPORT(sem_open);
/**
* @brief Posts (increments) a POSIX semaphore.
* @param sem Pointer to the semaphore.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function increments the value of the specified POSIX semaphore by one.
* If sem is a null pointer, errno is set to EINVAL to indicate an invalid argument, and the function returns -1.
* The semaphore is released using the RT-Thread semaphore release function rt_sem_release().
* If the semaphore release operation succeeds, the function returns 0; otherwise, errno is set to EINVAL,
* indicating an error, and the function returns -1.
*/
int sem_post(sem_t *sem)
{
rt_err_t result;
if (!sem)
{
rt_set_errno(EINVAL);
return -1;
}
result = rt_sem_release(sem->sem);
if (result == RT_EOK)
return 0;
rt_set_errno(EINVAL);
return -1;
}
RTM_EXPORT(sem_post);
/**
* @brief Waits for a POSIX semaphore with a timeout.
* @param sem Pointer to the semaphore.
* @param abs_timeout Pointer to the absolute timeout value.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function waits for the specified POSIX semaphore to become available within the specified timeout.
* If either sem or abs_timeout is a null pointer, the function returns EINVAL, indicating an invalid argument.
* The abs_timeout parameter specifies an absolute timeout value based on the CLOCK_REALTIME clock.
* The timeout is converted to RT-Thread ticks using the rt_timespec_to_tick() function.
* The semaphore is waited upon using the RT-Thread semaphore take function rt_sem_take().
* If the semaphore is successfully acquired within the specified timeout, the function returns 0.
* If the timeout expires before the semaphore becomes available, errno is set to ETIMEDOUT,
* and the function returns -1. If the semaphore wait operation is interrupted by a signal,
* errno is set to EINTR, and the function returns -1.
*/
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
{
rt_err_t result;
rt_int32_t tick;
if (!sem || !abs_timeout)
return EINVAL;
/* calculate os tick */
tick = rt_timespec_to_tick(abs_timeout);
result = rt_sem_take(sem->sem, tick);
if (result == -RT_ETIMEOUT)
{
rt_set_errno(ETIMEDOUT);
return -1;
}
if (result == RT_EOK)
return 0;
rt_set_errno(EINTR);
return -1;
}
RTM_EXPORT(sem_timedwait);
/**
* @brief Attempts to wait for a POSIX semaphore without blocking.
* @param sem Pointer to the semaphore.
* @return Upon successful completion, returns 0 if the semaphore was acquired;
* otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function attempts to acquire the specified POSIX semaphore without blocking.
* If sem is a null pointer, errno is set to EINVAL to indicate an invalid argument, and the function returns -1.
* The semaphore is waited upon using the RT-Thread semaphore take function rt_sem_take() with a timeout of 0,
* meaning that the function does not block if the semaphore is not available.
* If the semaphore is successfully acquired, the function returns 0. If the semaphore is not available,
* errno is set to EAGAIN to indicate that the operation would result in blocking, and the function returns -1.
* If the semaphore wait operation is interrupted by a signal, errno is set to EINTR, and the function returns -1.
*/
int sem_trywait(sem_t *sem)
{
rt_err_t result;
if (!sem)
{
rt_set_errno(EINVAL);
return -1;
}
result = rt_sem_take(sem->sem, 0);
if (result == -RT_ETIMEOUT)
{
rt_set_errno(EAGAIN);
return -1;
}
if (result == RT_EOK)
return 0;
rt_set_errno(EINTR);
return -1;
}
RTM_EXPORT(sem_trywait);
/**
* @brief Waits indefinitely for a POSIX semaphore to become available.
* @param sem Pointer to the semaphore.
* @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
*
* @note This function waits indefinitely for the specified POSIX semaphore to become available.
* If sem is a null pointer, errno is set to EINVAL to indicate an invalid argument, and the function returns -1.
* The semaphore is waited upon using the RT-Thread semaphore take function rt_sem_take() with a timeout
* value of RT_WAITING_FOREVER, indicating an infinite wait time.
* If the semaphore is successfully acquired, the function returns 0. If the semaphore wait operation is interrupted
* by a signal, errno is set to EINTR, and the function returns -1.
*/
int sem_wait(sem_t *sem)
{
rt_err_t result;
if (!sem)
{
rt_set_errno(EINVAL);
return -1;
}
result = rt_sem_take(sem->sem, RT_WAITING_FOREVER);
if (result == RT_EOK)
return 0;
rt_set_errno(EINTR);
return -1;
}
RTM_EXPORT(sem_wait);

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#ifndef __POSIX_SEMAPHORE_H__
#define __POSIX_SEMAPHORE_H__
#include <rtdef.h>
#include <sys/time.h>
struct posix_sem
{
/* reference count and unlinked */
rt_uint16_t refcount;
rt_uint8_t unlinked;
rt_uint8_t unamed;
/* RT-Thread semaphore */
rt_sem_t sem;
/* next posix semaphore */
struct posix_sem* next;
};
typedef struct posix_sem sem_t;
int sem_close(sem_t *sem);
int sem_destroy(sem_t *sem);
int sem_getvalue(sem_t *sem, int *sval);
int sem_init(sem_t *sem, int pshared, unsigned int value);
sem_t *sem_open(const char *name, int oflag, ...);
int sem_post(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
int sem_trywait(sem_t *sem);
int sem_unlink(const char *name);
int sem_wait(sem_t *sem);
#endif

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-07 Meco Man First version
*/
#ifndef __SYS_IPC_H__
#define __SYS_IPC_H__
#endif

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-07 Meco Man First version
*/
#ifndef __SYS_MSG_H__
#define __SYS_MSG_H__
#endif

View File

@@ -0,0 +1,14 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-07 Meco Man First version
*/
#ifndef __SYS_SEM_H__
#define __SYS_SEM_H__
#endif

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-12-07 Meco Man First version
*/
#ifndef __SYS_SHM_H__
#define __SYS_SHM_H__
#endif

View File

@@ -0,0 +1,12 @@
from building import *
Import('rtconfig')
src = Glob('*.c') + Glob('arch/*.c')
cwd = GetCurrentDir()
group = []
CPPPATH = [cwd]
if rtconfig.PLATFORM in ['gcc']:
group = DefineGroup('POSIX', src, depend = ['RT_USING_MODULE'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,126 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/29 Bernard first version
*/
#include "../dlmodule.h"
#include "../dlelf.h"
#ifdef __arm__
#define DBG_TAG "posix.libdl.arch"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
int dlmodule_relocate(struct rt_dlmodule *module, Elf32_Rel *rel, Elf32_Addr sym_val)
{
Elf32_Addr *where, tmp;
Elf32_Sword addend, offset;
rt_uint32_t upper, lower, sign, j1, j2;
where = (Elf32_Addr *)((rt_uint8_t *)module->mem_space
+ rel->r_offset
- module->vstart_addr);
switch (ELF32_R_TYPE(rel->r_info))
{
case R_ARM_NONE:
break;
case R_ARM_ABS32:
*where += (Elf32_Addr)sym_val;
LOG_D("R_ARM_ABS32: %x -> %x",
where, *where);
break;
case R_ARM_PC24:
case R_ARM_PLT32:
case R_ARM_CALL:
case R_ARM_JUMP24:
addend = *where & 0x00ffffff;
if (addend & 0x00800000)
addend |= 0xff000000;
tmp = sym_val - (Elf32_Addr)where + (addend << 2);
tmp >>= 2;
*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
LOG_D("R_ARM_PC24: %x -> %x",
where, *where);
break;
case R_ARM_REL32:
*where += sym_val - (Elf32_Addr)where;
LOG_D("R_ARM_REL32: %x -> %x, sym %x, offset %x",
where, *where, sym_val, rel->r_offset);
break;
case R_ARM_V4BX:
*where &= 0xf000000f;
*where |= 0x01a0f000;
break;
case R_ARM_GLOB_DAT:
case R_ARM_JUMP_SLOT:
*where = (Elf32_Addr)sym_val;
LOG_D("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x",
where, *where, sym_val);
break;
#if 0 /* To do */
case R_ARM_GOT_BREL:
temp = (Elf32_Addr)sym_val;
*where = (Elf32_Addr)&temp;
LOG_D("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x",
where, *where, sym_val);
break;
#endif
case R_ARM_RELATIVE:
*where = (Elf32_Addr)sym_val + *where;
LOG_D("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x",
where, *where, sym_val);
break;
case R_ARM_THM_CALL:
case R_ARM_THM_JUMP24:
upper = *(rt_uint16_t *)where;
lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
sign = (upper >> 10) & 1;
j1 = (lower >> 13) & 1;
j2 = (lower >> 11) & 1;
offset = (sign << 24) |
((~(j1 ^ sign) & 1) << 23) |
((~(j2 ^ sign) & 1) << 22) |
((upper & 0x03ff) << 12) |
((lower & 0x07ff) << 1);
if (offset & 0x01000000)
offset -= 0x02000000;
offset += sym_val - (Elf32_Addr)where;
if (!(offset & 1) ||
offset <= (rt_int32_t)0xff000000 ||
offset >= (rt_int32_t)0x01000000)
{
rt_kprintf("Module: Only Thumb addresses allowed\n");
return -1;
}
sign = (offset >> 24) & 1;
j1 = sign ^ (~(offset >> 23) & 1);
j2 = sign ^ (~(offset >> 22) & 1);
*(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
(sign << 10) |
((offset >> 12) & 0x03ff));
*(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
(j1 << 13) | (j2 << 11) |
((offset >> 1) & 0x07ff));
upper = *(rt_uint16_t *)where;
lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
break;
default:
return -1;
}
return 0;
}
#endif /* __arm__ */

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021/04/23 chunyexixiaoyu first version
*/
#include "../dlmodule.h"
#include "../dlelf.h"
#if (__riscv_xlen == 64)
#define R_RISCV_NONE 0
#define R_RISCV_32 1
#define R_RISCV_64 2
#define R_RISCV_RELATIVE 3
#define R_RISCV_COPY 4
#define R_RISCV_JUMP_SLOT 5
#define R_RISCV_TLS_DTPMOD32 6
#define R_RISCV_TLS_DTPMOD64 7
#define R_RISCV_TLS_DTPREL32 8
#define R_RISCV_TLS_DTPREL64 9
#define R_RISCV_TLS_TPREL32 10
#define R_RISCV_TLS_TPREL64 11
#define DBG_TAG "posix.libdl.arch"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
int dlmodule_relocate(struct rt_dlmodule *module, Elf_Rel *rel, Elf_Addr sym_val)
{
Elf64_Addr *where, tmp;
Elf64_Sword addend, offset;
rt_uint64_t upper, lower, sign, j1, j2;
where = (Elf64_Addr *)((rt_uint8_t *)module->mem_space
+ rel->r_offset
- module->vstart_addr);
switch (ELF64_R_TYPE(rel->r_info))
{
case R_RISCV_NONE:
break;
case R_RISCV_64:
*where = (Elf64_Addr)(sym_val + rel->r_addend);
LOG_D("R_RISCV_64: %x -> %x",where, *where);
break;
case R_RISCV_RELATIVE:
*where = (Elf64_Addr)((rt_uint8_t *)module->mem_space - module->vstart_addr + rel->r_addend);
LOG_D("R_RISCV_RELATIVE: %x -> %x",where, *where);
break;
case R_RISCV_JUMP_SLOT:
*where = (Elf64_Addr)sym_val;
LOG_D("R_RISCV_JUMP_SLOT: %x -> %x",where, *where);
break;
default:
LOG_D("__riscv__ELF: invalid relocate TYPE %d", ELF64_R_TYPE(rel->r_info));
return -1;
}
return 0;
}
#endif

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/09/15 parai first version
*/
#include "../dlmodule.h"
#include "../dlelf.h"
#ifdef __i386__
#define DBG_TAG "posix.libdl.arch"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
#define R_X86_64_RELATIVE 8 /* Adjust by program base */
int dlmodule_relocate(struct rt_dlmodule *module, Elf32_Rel *rel, Elf32_Addr sym_val)
{
Elf32_Addr *where, tmp;
Elf32_Sword addend, offset;
rt_uint32_t upper, lower, sign, j1, j2;
where = (Elf32_Addr *)((rt_uint8_t *)module->mem_space
+ rel->r_offset
- module->vstart_addr);
switch (ELF32_R_TYPE(rel->r_info))
{
case R_X86_64_GLOB_DAT:
case R_X86_64_JUMP_SLOT:
*where = (Elf32_Addr)sym_val;
LOG_D("R_X86_64_JUMP_SLOT: 0x%x -> 0x%x 0x%x",
(uint32_t)where, *where, sym_val);
break;
case R_X86_64_RELATIVE:
*where = (Elf32_Addr)sym_val + *where;
LOG_D("R_X86_64_RELATIVE: 0x%x -> 0x%x 0x%x",
(uint32_t)where, *where, sym_val);
break;
default:
LOG_D("X86ELF: invalid relocate TYPE %d", ELF32_R_TYPE(rel->r_info));
return -1;
}
return 0;
}
#endif /* __i386__ */

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 yi.qiu first version
*/
#include <rtthread.h>
#include <rtm.h>
#include "dlmodule.h"
int dlclose(void *handle)
{
struct rt_dlmodule *module;
RT_ASSERT(handle != RT_NULL);
module = (struct rt_dlmodule *)handle;
rt_enter_critical();
module->nref--;
if (module->nref <= 0)
{
rt_exit_critical();
dlmodule_destroy(module);
}
else
{
rt_exit_critical();
}
return RT_TRUE;
}
RTM_EXPORT(dlclose)

View File

@@ -0,0 +1,493 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/29 Bernard first version
* 2021/04/23 chunyexixiaoyu distinguish 32-bit and 64-bit
*/
#include "dlmodule.h"
#include "dlelf.h"
#define DBG_TAG "DLMD"
#define DBG_LVL DBG_INFO
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_ptr)
{
rt_bool_t linked = RT_FALSE;
rt_ubase_t index, module_size = 0;
Elf_Addr vstart_addr, vend_addr;
rt_bool_t has_vstart;
RT_ASSERT(module_ptr != RT_NULL);
if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
{
/* rtmlinker finished */
linked = RT_TRUE;
}
/* get the ELF image size */
has_vstart = RT_FALSE;
vstart_addr = vend_addr = RT_NULL;
for (index = 0; index < elf_module->e_phnum; index++)
{
if (phdr[index].p_type != PT_LOAD)
continue;
LOG_D("LOAD segment: %d, 0x%p, 0x%08x", index, phdr[index].p_vaddr, phdr[index].p_memsz);
if (phdr[index].p_memsz < phdr[index].p_filesz)
{
rt_kprintf("invalid elf: segment %d: p_memsz: %d, p_filesz: %d\n",
index, phdr[index].p_memsz, phdr[index].p_filesz);
return RT_NULL;
}
if (!has_vstart)
{
vstart_addr = phdr[index].p_vaddr;
vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
has_vstart = RT_TRUE;
if (vend_addr < vstart_addr)
{
LOG_E("invalid elf: segment %d: p_vaddr: %d, p_memsz: %d\n",
index, phdr[index].p_vaddr, phdr[index].p_memsz);
return RT_NULL;
}
}
else
{
if (phdr[index].p_vaddr < vend_addr)
{
LOG_E("invalid elf: segment should be sorted and not overlapped\n");
return RT_NULL;
}
if (phdr[index].p_vaddr > vend_addr + 16)
{
/* There should not be too much padding in the object files. */
LOG_W("warning: too much padding before segment %d", index);
}
vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
if (vend_addr < phdr[index].p_vaddr)
{
LOG_E("invalid elf: "
"segment %d address overflow\n", index);
return RT_NULL;
}
}
}
module_size = vend_addr - vstart_addr;
LOG_D("module size: %d, vstart_addr: 0x%p", module_size, vstart_addr);
if (module_size == 0)
{
LOG_E("Module: size error\n");
return -RT_ERROR;
}
module->vstart_addr = vstart_addr;
module->nref = 0;
/* allocate module space */
module->mem_space = rt_malloc(module_size);
if (module->mem_space == RT_NULL)
{
LOG_E("Module: allocate space failed.\n");
return -RT_ERROR;
}
module->mem_size = module_size;
/* zero all space */
rt_memset(module->mem_space, 0, module_size);
for (index = 0; index < elf_module->e_phnum; index++)
{
if (phdr[index].p_type == PT_LOAD)
{
rt_memcpy(module->mem_space + phdr[index].p_vaddr - vstart_addr,
(rt_uint8_t *)elf_module + phdr[index].p_offset,
phdr[index].p_filesz);
}
}
/* set module entry */
module->entry_addr = module->mem_space + elf_module->e_entry - vstart_addr;
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
rt_ubase_t i, nr_reloc;
Elf_Sym *symtab;
Elf_Rel *rel;
rt_uint8_t *strtab;
static rt_bool_t unsolved = RT_FALSE;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if (!IS_REL(shdr[index]))
continue;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if (!IS_RELA(shdr[index]))
continue;
#endif
/* get relocate item */
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .rel.plt and .rel.dyn section */
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
shdr[shdr[index].sh_link].sh_offset);
strtab = (rt_uint8_t *)module_ptr +
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
nr_reloc = (rt_ubase_t)(shdr[index].sh_size / sizeof(Elf_Rel));
/* relocate every items */
for (i = 0; i < nr_reloc; i ++)
{
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
Elf_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
Elf_Sym *sym = &symtab[ELF64_R_SYM(rel->r_info)];
#endif
LOG_D("relocate symbol %s shndx %d", strtab + sym->st_name, sym->st_shndx);
if ((sym->st_shndx != SHT_NULL) ||(ELF_ST_BIND(sym->st_info) == STB_LOCAL))
{
Elf_Addr addr;
addr = (Elf_Addr)(module->mem_space + sym->st_value - vstart_addr);
dlmodule_relocate(module, rel, addr);
}
else if (!linked)
{
Elf_Addr addr;
LOG_D("relocate symbol: %s", strtab + sym->st_name);
/* need to resolve symbol in kernel symbol table */
addr = dlmodule_symbol_find((const char *)(strtab + sym->st_name));
if (addr == 0)
{
LOG_E("Module: can't find %s in kernel symbol table", strtab + sym->st_name);
unsolved = RT_TRUE;
}
else
{
dlmodule_relocate(module, rel, addr);
}
}
rel ++;
}
if (unsolved)
return -RT_ERROR;
}
/* construct module symbol table */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* find .dynsym section */
rt_uint8_t *shstrab;
shstrab = (rt_uint8_t *)module_ptr +
shdr[elf_module->e_shstrndx].sh_offset;
if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
break;
}
/* found .dynsym section */
if (index != elf_module->e_shnum)
{
int i, count = 0;
Elf_Sym *symtab = RT_NULL;
rt_uint8_t *strtab = RT_NULL;
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
for (i = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
(ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
count ++;
}
module->symtab = (struct rt_module_symtab *)rt_malloc
(count * sizeof(struct rt_module_symtab));
module->nsym = count;
for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
rt_size_t length;
if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
(ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
continue;
length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
module->symtab[count].addr =
(void *)(module->mem_space + symtab[i].st_value - module->vstart_addr);
module->symtab[count].name = rt_malloc(length);
rt_memset((void *)module->symtab[count].name, 0, length);
rt_memcpy((void *)module->symtab[count].name,
strtab + symtab[i].st_name,
length);
count ++;
}
/* get priority & stack size params*/
rt_uint32_t flag = 0;
rt_uint16_t priority;
rt_uint32_t stacksize;
for (i = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
if (((flag & 0x01) == 0) &&
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_priority") == 0))
{
flag |= 0x01;
priority = *(rt_uint16_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
if (priority < RT_THREAD_PRIORITY_MAX)
{
module->priority = priority;
}
}
if (((flag & 0x02) == 0) &&
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_stacksize") == 0))
{
flag |= 0x02;
stacksize = *(rt_uint32_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
if ((stacksize < 2048) || (stacksize > 1024 * 32))
{
module->stack_size = stacksize;
}
}
if ((flag & 0x03) == 0x03)
{
break;
}
}
}
return RT_EOK;
}
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module_ptr)
{
rt_ubase_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
rt_ubase_t module_addr = 0, module_size = 0;
rt_uint8_t *ptr, *strtab, *shstrab;
/* get the ELF image size */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* text */
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
{
module_size += shdr[index].sh_size;
module_addr = shdr[index].sh_addr;
}
/* rodata */
if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
{
module_size += shdr[index].sh_size;
}
/* data */
if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
{
module_size += shdr[index].sh_size;
}
/* bss */
if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
{
module_size += shdr[index].sh_size;
}
}
/* no text, data and bss on image */
if (module_size == 0) return RT_NULL;
module->vstart_addr = 0;
/* allocate module space */
module->mem_space = rt_malloc(module_size);
if (module->mem_space == RT_NULL)
{
LOG_E("Module: allocate space failed.\n");
return -RT_ERROR;
}
module->mem_size = module_size;
/* zero all space */
ptr = module->mem_space;
rt_memset(ptr, 0, module_size);
/* load text and data section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* load text section */
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
LOG_D("load text 0x%x, size %d", ptr, shdr[index].sh_size);
ptr += shdr[index].sh_size;
}
/* load rodata section */
if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
rodata_addr = (rt_uint32_t)ptr;
LOG_D("load rodata 0x%x, size %d, rodata 0x%x", ptr,
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
ptr += shdr[index].sh_size;
}
/* load data section */
if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
data_addr = (rt_uint32_t)ptr;
LOG_D("load data 0x%x, size %d, data 0x%x", ptr,
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
ptr += shdr[index].sh_size;
}
/* load bss section */
if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
{
rt_memset(ptr, 0, shdr[index].sh_size);
bss_addr = (rt_uint32_t)ptr;
LOG_D("load bss 0x%x, size %d", ptr, shdr[index].sh_size);
}
}
/* set module entry */
module->entry_addr = (rt_dlmodule_entry_func_t)((rt_uint8_t *)module->mem_space + elf_module->e_entry - module_addr);
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
rt_ubase_t i, nr_reloc;
Elf_Sym *symtab;
Elf_Rel *rel;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if (!IS_REL(shdr[index]))
continue;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if (!IS_RELA(shdr[index]))
continue;
#endif
/* get relocate item */
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .dynsym and .dynstr */
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
shdr[shdr[index].sh_link].sh_offset);
strtab = (rt_uint8_t *)module_ptr +
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
shstrab = (rt_uint8_t *)module_ptr +
shdr[elf_module->e_shstrndx].sh_offset;
nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf_Rel));
/* relocate every items */
for (i = 0; i < nr_reloc; i ++)
{
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
Elf_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
Elf_Sym *sym = &symtab[ELF64_R_SYM(rel->r_info)];
#endif
LOG_D("relocate symbol: %s", strtab + sym->st_name);
if (sym->st_shndx != STN_UNDEF)
{
Elf_Addr addr = 0;
if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
(ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
{
if (rt_strncmp((const char *)(shstrab +
shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
{
/* relocate rodata section */
LOG_D("rodata");
addr = (Elf_Addr)(rodata_addr + sym->st_value);
}
else if (rt_strncmp((const char *)
(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
{
/* relocate bss section */
LOG_D("bss");
addr = (Elf_Addr)bss_addr + sym->st_value;
}
else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
ELF_DATA, 6) == 0)
{
/* relocate data section */
LOG_D("data");
addr = (Elf_Addr)data_addr + sym->st_value;
}
if (addr != 0) dlmodule_relocate(module, rel, addr);
}
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
{
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
/* relocate function */
dlmodule_relocate(module, rel, addr);
}
}
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
{
/* relocate function */
dlmodule_relocate(module, rel,
(Elf_Addr)((rt_uint8_t *)
module->mem_space
- module_addr
+ sym->st_value));
}
else
{
Elf_Addr addr;
if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
{
LOG_D("relocate symbol: %s", strtab + sym->st_name);
/* need to resolve symbol in kernel symbol table */
addr = dlmodule_symbol_find((const char *)(strtab + sym->st_name));
if (addr != (Elf_Addr)RT_NULL)
{
dlmodule_relocate(module, rel, addr);
LOG_D("symbol addr 0x%x", addr);
}
else
LOG_E("Module: can't find %s in kernel symbol table",
strtab + sym->st_name);
}
else
{
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
dlmodule_relocate(module, rel, addr);
}
}
rel ++;
}
}
return RT_EOK;
}

View File

@@ -0,0 +1,381 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/29 Bernard first version
* 2021/04/23 chunyexixiaoyu distinguish 32-bit and 64-bit
*/
#ifndef DL_ELF_H__
#define DL_ELF_H__
typedef rt_uint8_t Elf_Byte;
typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */
typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */
typedef rt_int32_t Elf32_Sword; /* Signed large integer */
typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */
typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
typedef rt_uint64_t Elf64_Addr;
typedef rt_uint16_t Elf64_Half;
typedef rt_int16_t Elf64_SHalf;
typedef rt_uint64_t Elf64_Off;
typedef rt_int32_t Elf64_Sword;
typedef rt_uint32_t Elf64_Word;
typedef rt_uint64_t Elf64_Xword;
typedef rt_int64_t Elf64_Sxword;
typedef rt_uint16_t Elf64_Section;
/* e_ident[] magic number */
#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
#define RTMMAG "\177RTM" /* magic */
#define ELFMAG "\177ELF" /* magic */
#define SELFMAG 4 /* size of magic */
#define EI_CLASS 4 /* file class */
#define EI_NIDENT 16 /* Size of e_ident[] */
/* e_ident[] file class */
#define ELFCLASSNONE 0 /* invalid */
#define ELFCLASS32 1 /* 32-bit objs */
#define ELFCLASS64 2 /* 64-bit objs */
#define ELFCLASSNUM 3 /* number of classes */
/* e_ident[] data encoding */
#define ELFDATANONE 0 /* invalid */
#define ELFDATA2LSB 1 /* Little-Endian */
#define ELFDATA2MSB 2 /* Big-Endian */
#define ELFDATANUM 3 /* number of data encode defines */
/* e_ident */
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
#define ET_NONE 0 /* No file type */
#define ET_REL 1 /* Relocatable file */
#define ET_EXEC 2 /* Executable file */
#define ET_DYN 3 /* Shared object file */
#define ET_CORE 4 /* Core file */
/* ELF Header */
typedef struct elfhdr
{
unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
Elf32_Half e_type; /* object file type */
Elf32_Half e_machine; /* machine */
Elf32_Word e_version; /* object file version */
Elf32_Addr e_entry; /* virtual entry point */
Elf32_Off e_phoff; /* program header table offset */
Elf32_Off e_shoff; /* section header table offset */
Elf32_Word e_flags; /* processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size */
Elf32_Half e_phentsize; /* program header entry size */
Elf32_Half e_phnum; /* number of program header entries */
Elf32_Half e_shentsize; /* section header entry size */
Elf32_Half e_shnum; /* number of section header entries */
Elf32_Half e_shstrndx; /* section header table's "section
header string table" entry offset */
} Elf32_Ehdr;
typedef struct elf64_hdr
{
unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
Elf64_Half e_type; /* object file type */
Elf64_Half e_machine; /* machine */
Elf64_Word e_version; /* object file version */
Elf64_Addr e_entry; /* virtual entry point */
Elf64_Off e_phoff; /* program header table offset */
Elf64_Off e_shoff; /* section header table offset */
Elf64_Word e_flags; /* processor-specific flags */
Elf64_Half e_ehsize; /* ELF header size */
Elf64_Half e_phentsize; /* program header entry size */
Elf64_Half e_phnum; /* number of program header entries */
Elf64_Half e_shentsize; /* section header entry size */
Elf64_Half e_shnum; /* number of section header entries */
Elf64_Half e_shstrndx; /* section header table's "section
header string table" entry offset */
} Elf64_Ehdr;
/* Section Header */
typedef struct
{
Elf32_Word sh_name; /* name - index into section header
string table section */
Elf32_Word sh_type; /* type */
Elf32_Word sh_flags; /* flags */
Elf32_Addr sh_addr; /* address */
Elf32_Off sh_offset; /* file offset */
Elf32_Word sh_size; /* section size */
Elf32_Word sh_link; /* section header table index link */
Elf32_Word sh_info; /* extra information */
Elf32_Word sh_addralign; /* address alignment */
Elf32_Word sh_entsize; /* section entry size */
} Elf32_Shdr;
typedef struct
{
Elf64_Word sh_name; /* Section name (string tbl index) */
Elf64_Word sh_type; /* Section type */
Elf64_Xword sh_flags; /* Section flags */
Elf64_Addr sh_addr; /* Section virtual addr at execution */
Elf64_Off sh_offset; /* Section file offset */
Elf64_Xword sh_size; /* Section size in bytes */
Elf64_Word sh_link; /* Link to another section */
Elf64_Word sh_info; /* Additional section information */
Elf64_Xword sh_addralign; /* Section alignment */
Elf64_Xword sh_entsize; /* Entry size if section holds table */
} Elf64_Shdr;
/* Section names */
#define ELF_BSS ".bss" /* uninitialized data */
#define ELF_DATA ".data" /* initialized data */
#define ELF_DEBUG ".debug" /* debug */
#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
#define ELF_DYNSTR ".dynstr" /* dynamic string table */
#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
#define ELF_FINI ".fini" /* termination code */
#define ELF_GOT ".got" /* global offset table */
#define ELF_HASH ".hash" /* symbol hash table */
#define ELF_INIT ".init" /* initialization code */
#define ELF_REL_DATA ".rel.data" /* relocation data */
#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
#define ELF_REL_TEXT ".rel.text" /* relocation code */
#define ELF_RODATA ".rodata" /* read-only data */
#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
#define ELF_STRTAB ".strtab" /* string table */
#define ELF_SYMTAB ".symtab" /* symbol table */
#define ELF_TEXT ".text" /* code */
#define ELF_RTMSYMTAB "RTMSymTab"
/* Symbol Table Entry */
typedef struct elf32_sym
{
Elf32_Word st_name; /* name - index into string table */
Elf32_Addr st_value; /* symbol value */
Elf32_Word st_size; /* symbol size */
unsigned char st_info; /* type and binding */
unsigned char st_other; /* 0 - no defined meaning */
Elf32_Half st_shndx; /* section header index */
} Elf32_Sym;
typedef struct
{
Elf64_Word st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
Elf64_Section st_shndx; /* Section index */
Elf64_Addr st_value; /* Symbol value */
Elf64_Xword st_size; /* Symbol size */
} Elf64_Sym;
#define STB_LOCAL 0 /* BIND */
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_NUM 3
#define STB_LOPROC 13 /* processor specific range */
#define STB_HIPROC 15
#define STT_NOTYPE 0 /* symbol type is unspecified */
#define STT_OBJECT 1 /* data object */
#define STT_FUNC 2 /* code object */
#define STT_SECTION 3 /* symbol identifies an ELF section */
#define STT_FILE 4 /* symbol's name is file name */
#define STT_COMMON 5 /* common data object */
#define STT_TLS 6 /* thread-local data object */
#define STT_NUM 7 /* # defined types in generic range */
#define STT_LOOS 10 /* OS specific range */
#define STT_HIOS 12
#define STT_LOPROC 13 /* processor specific range */
#define STT_HIPROC 15
#define STN_UNDEF 0 /* undefined */
#define ELF_ST_BIND(info) ((info) >> 4)
#define ELF_ST_TYPE(info) ((info) & 0xf)
#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
/* Relocation entry with implicit addend */
typedef struct
{
Elf32_Addr r_offset; /* offset of relocation */
Elf32_Word r_info; /* symbol table index and type */
} Elf32_Rel;
typedef struct
{
Elf64_Addr r_offset; /* Address */
Elf64_Xword r_info; /* Relocation type and symbol index */
} Elf64_Rel;
/* Relocation entry with explicit addend */
typedef struct
{
Elf32_Addr r_offset; /* offset of relocation */
Elf32_Word r_info; /* symbol table index and type */
Elf32_Sword r_addend;
} Elf32_Rela;
typedef struct
{
Elf64_Addr r_offset; /* Address */
Elf64_Xword r_info; /* Relocation type and symbol index */
Elf64_Sxword r_addend; /* Addend */
} Elf64_Rela;
/* Extract relocation info - r_info */
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((unsigned char) (i))
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
/*
* Relocation type for arm
*/
#define R_ARM_NONE 0
#define R_ARM_PC24 1
#define R_ARM_ABS32 2
#define R_ARM_REL32 3
#define R_ARM_THM_CALL 10
#define R_ARM_GLOB_DAT 21
#define R_ARM_JUMP_SLOT 22
#define R_ARM_RELATIVE 23
#define R_ARM_GOT_BREL 26
#define R_ARM_PLT32 27
#define R_ARM_CALL 28
#define R_ARM_JUMP24 29
#define R_ARM_THM_JUMP24 30
#define R_ARM_V4BX 40
/*
* Relocation type for x86
*/
#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
#define R_386_GOT32 3
#define R_386_PLT32 4
#define R_386_COPY 5
#define R_386_GLOB_DAT 6
#define R_386_JMP_SLOT 7
#define R_386_RELATIVE 8
#define R_386_GOTOFF 9
#define R_386_GOTPC 10
/* Program Header */
typedef struct
{
Elf32_Word p_type; /* segment type */
Elf32_Off p_offset; /* segment offset */
Elf32_Addr p_vaddr; /* virtual address of segment */
Elf32_Addr p_paddr; /* physical address - ignored? */
Elf32_Word p_filesz; /* number of bytes in file for seg. */
Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
Elf32_Word p_flags; /* flags */
Elf32_Word p_align; /* memory alignment */
} Elf32_Phdr;
typedef struct
{
Elf64_Word p_type; /* Segment type */
Elf64_Word p_flags; /* Segment flags */
Elf64_Off p_offset; /* Segment file offset */
Elf64_Addr p_vaddr; /* Segment virtual address */
Elf64_Addr p_paddr; /* Segment physical address */
Elf64_Xword p_filesz; /* Segment size in file */
Elf64_Xword p_memsz; /* Segment size in memory */
Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
/* p_type */
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6
#define PT_TLS 7
#define PT_NUM 8
#define PT_LOOS 0x60000000
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
/* p_flags */
#define PF_X 1
#define PF_W 2
#define PF_R 4
/* sh_type */
#define SHT_NULL 0 /* inactive */
#define SHT_PROGBITS 1 /* program defined information */
#define SHT_SYMTAB 2 /* symbol table section */
#define SHT_STRTAB 3 /* string table section */
#define SHT_RELA 4 /* relocation section with addends*/
#define SHT_HASH 5 /* symbol hash table section */
#define SHT_DYNAMIC 6 /* dynamic section */
#define SHT_NOTE 7 /* note section */
#define SHT_NOBITS 8 /* no space section */
#define SHT_REL 9 /* relocation section without addends */
#define SHT_SHLIB 10 /* reserved - purpose unknown */
#define SHT_DYNSYM 11 /* dynamic symbol table section */
#define SHT_NUM 12 /* number of section types */
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
#define SHT_HIPROC 0x7fffffff /* specific section header types */
#define SHT_LOUSER 0x80000000 /* reserved range for application */
#define SHT_HIUSER 0xffffffff /* specific indexes */
/* Section Attribute Flags - sh_flags */
#define SHF_WRITE 0x1 /* Writable */
#define SHF_ALLOC 0x2 /* occupies memory */
#define SHF_EXECINSTR 0x4 /* executable */
#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
/* specific section attributes */
#define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
#define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
#define IS_REL(s) (s.sh_type == SHT_REL)
#define IS_RELA(s) (s.sh_type == SHT_RELA)
#define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
#define elf_module ((Elf32_Ehdr *)module_ptr)
#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
typedef Elf32_Sym Elf_Sym;
typedef Elf32_Rel Elf_Rel;
typedef Elf32_Addr Elf_Addr;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
#define elf_module ((Elf64_Ehdr *)module_ptr)
#define shdr ((Elf64_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define phdr ((Elf64_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
typedef Elf64_Sym Elf_Sym;
typedef Elf64_Rela Elf_Rel;
typedef Elf64_Addr Elf_Addr;
#endif
int dlmodule_relocate(struct rt_dlmodule *module, Elf_Rel *rel, Elf_Addr sym_val);
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule *module, void *module_ptr);
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule *module, void *module_ptr);
#endif

View File

@@ -0,0 +1,18 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 yi.qiu first version
*/
#include <rtthread.h>
#include <rtm.h>
const char *dlerror(void)
{
return "TODO";
}
RTM_EXPORT(dlerror)

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 yi.qiu first version
*/
#ifndef __DLFCN_H_
#define __DLFCN_H_
#define RTLD_LAZY 0x00000
#define RTLD_NOW 0x00001
#define RTLD_LOCAL 0x00000
#define RTLD_GLOBAL 0x10000
#define RTLD_DEFAULT ((void*)1)
#define RTLD_NEXT ((void*)2)
void *dlopen (const char *filename, int flag);
const char *dlerror(void);
void *dlsym(void *handle, const char *symbol);
int dlclose (void *handle);
#endif

View File

@@ -0,0 +1,885 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/29 Bernard first version
*/
#include <rthw.h>
#include "dlfcn.h"
#include "dlmodule.h"
#include "dlelf.h"
#ifdef RT_USING_POSIX_FS
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#endif
#define DBG_TAG "DLMD"
#define DBG_LVL DBG_INFO
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
#if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
#pragma section="RTMSymTab"
#endif
/* set the name of module */
static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
{
int size;
struct rt_object *object;
const char *first, *end, *ptr;
object = &(module->parent);
ptr = first = (char *)path;
end = path + rt_strlen(path);
while (*ptr != '\0')
{
if (*ptr == '/')
first = ptr + 1;
if (*ptr == '.')
end = ptr - 1;
ptr ++;
}
size = end - first + 1;
if (size > RT_NAME_MAX) size = RT_NAME_MAX;
rt_strncpy(object->name, first, size);
object->name[size] = '\0';
}
#define RT_MODULE_ARG_MAX 8
static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
{
int argc = 0;
char *ptr = cmd;
while ((ptr - cmd) < length)
{
/* strip bank and tab */
while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
*ptr++ = '\0';
/* check whether it's the end of line */
if ((ptr - cmd) >= length) break;
/* handle string with quote */
if (*ptr == '"')
{
argv[argc++] = ++ptr;
/* skip this string */
while (*ptr != '"' && (ptr - cmd) < length)
if (*ptr ++ == '\\') ptr ++;
if ((ptr - cmd) >= length) break;
/* skip '"' */
*ptr ++ = '\0';
}
else
{
argv[argc++] = ptr;
while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
ptr ++;
}
if (argc >= RT_MODULE_ARG_MAX) break;
}
return argc;
}
/* invoked by main thread for exit */
static void _dlmodule_exit(void)
{
struct rt_dlmodule *module;
module = dlmodule_self();
if (!module) return; /* not a module thread */
rt_enter_critical();
if (module->stat == RT_DLMODULE_STAT_RUNNING)
{
struct rt_object *object = RT_NULL;
struct rt_list_node *node = RT_NULL;
/* set stat to closing */
module->stat = RT_DLMODULE_STAT_CLOSING;
/* suspend all threads in this module */
for (node = module->object_list.next; node != &(module->object_list); node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);
if ((object->type & ~RT_Object_Class_Static) == RT_Object_Class_Thread)
{
rt_thread_t thread = (rt_thread_t)object;
/* stop timer and suspend thread*/
if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
(RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
{
rt_timer_stop(&(thread->thread_timer));
rt_thread_suspend(thread);
}
}
}
}
rt_exit_critical();
return;
}
static void _dlmodule_thread_entry(void* parameter)
{
int argc = 0;
char *argv[RT_MODULE_ARG_MAX];
struct rt_dlmodule *module = (struct rt_dlmodule*)parameter;
if (module == RT_NULL || module->cmd_line == RT_NULL)
/* malloc for module_cmd_line failed. */
return;
if (module->cmd_line)
{
rt_memset(argv, 0x00, sizeof(argv));
argc = _rt_module_split_arg((char *)module->cmd_line, rt_strlen(module->cmd_line), argv);
if (argc == 0) goto __exit;
}
/* set status of module */
module->stat = RT_DLMODULE_STAT_RUNNING;
LOG_D("run main entry: 0x%p with %s",
module->entry_addr,
module->cmd_line);
if (module->entry_addr)
module->entry_addr(argc, argv);
__exit:
_dlmodule_exit();
return ;
}
struct rt_dlmodule *dlmodule_create(void)
{
struct rt_dlmodule *module = RT_NULL;
module = (struct rt_dlmodule*) rt_object_allocate(RT_Object_Class_Module, "module");
if (module)
{
module->stat = RT_DLMODULE_STAT_INIT;
/* set initial priority and stack size */
module->priority = RT_THREAD_PRIORITY_MAX - 1;
module->stack_size = 2048;
rt_list_init(&(module->object_list));
}
return module;
}
void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
{
RT_ASSERT(thread->parent.module_id== module);
/* lock scheduler to prevent scheduling in cleanup function. */
rt_enter_critical();
rt_thread_close(thread);
/* remove thread from thread_list (defunct thread list) */
rt_list_remove(&RT_THREAD_LIST_NODE(thread));
/* invoke thread cleanup */
if (thread->cleanup != RT_NULL)
thread->cleanup(thread);
rt_exit_critical();
#ifdef RT_USING_SIGNALS
rt_thread_free_sig(thread);
#endif
if (thread->parent.type & RT_Object_Class_Static)
{
/* detach object */
rt_object_detach((rt_object_t)thread);
}
#ifdef RT_USING_HEAP
else
{
/* release thread's stack */
RT_KERNEL_FREE(thread->stack_addr);
/* delete thread object */
rt_object_delete((rt_object_t)thread);
}
#endif
}
rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
{
int i;
RT_DEBUG_NOT_IN_INTERRUPT;
/* check parameter */
if (module == RT_NULL)
return -RT_ERROR;
/* can not destroy a running module */
if (module->stat == RT_DLMODULE_STAT_RUNNING)
return -RT_EBUSY;
/* do module cleanup */
if (module->cleanup_func)
{
rt_enter_critical();
module->cleanup_func(module);
rt_exit_critical();
}
// list_object(&(module->object_list));
/* cleanup for all kernel objects inside module*/
{
struct rt_object *object = RT_NULL;
struct rt_list_node *node = RT_NULL;
/* detach/delete all threads in this module */
for (node = module->object_list.next; node != &(module->object_list); )
{
int object_type;
object = rt_list_entry(node, struct rt_object, list);
object_type = object->type & ~RT_Object_Class_Static;
/* to next node */
node = node->next;
if (object->type & RT_Object_Class_Static)
{
switch (object_type)
{
case RT_Object_Class_Thread:
dlmodule_destroy_subthread(module, (rt_thread_t)object);
break;
#ifdef RT_USING_SEMAPHORE
case RT_Object_Class_Semaphore:
rt_sem_detach((rt_sem_t)object);
break;
#endif
#ifdef RT_USING_MUTEX
case RT_Object_Class_Mutex:
rt_mutex_detach((rt_mutex_t)object);
break;
#endif
#ifdef RT_USING_EVENT
case RT_Object_Class_Event:
rt_event_detach((rt_event_t)object);
break;
#endif
#ifdef RT_USING_MAILBOX
case RT_Object_Class_MailBox:
rt_mb_detach((rt_mailbox_t)object);
break;
#endif
#ifdef RT_USING_MESSAGEQUEUE
case RT_Object_Class_MessageQueue:
rt_mq_detach((rt_mq_t)object);
break;
#endif
#ifdef RT_USING_MEMHEAP
case RT_Object_Class_MemHeap:
rt_memheap_detach((struct rt_memheap*)object);
break;
#endif
#ifdef RT_USING_MEMPOOL
case RT_Object_Class_MemPool:
rt_mp_detach((struct rt_mempool*)object);
break;
#endif
case RT_Object_Class_Timer:
rt_timer_detach((rt_timer_t)object);
break;
default:
LOG_E("Unsupported oject type in module.");
break;
}
}
else
{
switch (object_type)
{
case RT_Object_Class_Thread:
dlmodule_destroy_subthread(module, (rt_thread_t)object);
break;
#ifdef RT_USING_SEMAPHORE
case RT_Object_Class_Semaphore:
rt_sem_delete((rt_sem_t)object);
break;
#endif
#ifdef RT_USING_MUTEX
case RT_Object_Class_Mutex:
rt_mutex_delete((rt_mutex_t)object);
break;
#endif
#ifdef RT_USING_EVENT
case RT_Object_Class_Event:
rt_event_delete((rt_event_t)object);
break;
#endif
#ifdef RT_USING_MAILBOX
case RT_Object_Class_MailBox:
rt_mb_delete((rt_mailbox_t)object);
break;
#endif
#ifdef RT_USING_MESSAGEQUEUE
case RT_Object_Class_MessageQueue:
rt_mq_delete((rt_mq_t)object);
break;
#endif
#ifdef RT_USING_MEMHEAP
/* no delete operation */
#endif
#ifdef RT_USING_MEMPOOL
case RT_Object_Class_MemPool:
rt_mp_delete((struct rt_mempool*)object);
break;
#endif
case RT_Object_Class_Timer:
rt_timer_delete((rt_timer_t)object);
break;
default:
LOG_E("Unsupported oject type in module.");
break;
}
}
}
}
if (module->cmd_line) rt_free(module->cmd_line);
/* release module symbol table */
for (i = 0; i < module->nsym; i ++)
{
rt_free((void *)module->symtab[i].name);
}
if (module->symtab != RT_NULL)
{
rt_free(module->symtab);
}
/* destory module */
rt_free(module->mem_space);
/* delete module object */
rt_object_delete((rt_object_t)module);
return RT_EOK;
}
struct rt_dlmodule *dlmodule_self(void)
{
rt_thread_t tid;
struct rt_dlmodule *ret = RT_NULL;
tid = rt_thread_self();
if (tid)
{
ret = (struct rt_dlmodule*) tid->parent.module_id;
}
return ret;
}
/*
* Compatible with old API
*/
struct rt_dlmodule *rt_module_self(void)
{
return dlmodule_self();
}
struct rt_dlmodule* dlmodule_load(const char* filename)
{
#ifdef RT_USING_POSIX_FS
int fd = -1, length = 0;
#endif
rt_err_t ret = RT_EOK;
rt_uint8_t *module_ptr = RT_NULL;
struct rt_dlmodule *module = RT_NULL;
#ifdef RT_USING_POSIX_FS
fd = open(filename, O_RDONLY, 0);
if (fd >= 0)
{
length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
if (length == 0) goto __exit;
module_ptr = (uint8_t*) rt_malloc (length);
if (!module_ptr) goto __exit;
if (read(fd, module_ptr, length) != length)
goto __exit;
/* close file and release fd */
close(fd);
fd = -1;
}
else
{
goto __exit;
}
#endif
if (!module_ptr) goto __exit;
/* check ELF header */
if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
{
rt_kprintf("Module: magic error\n");
goto __exit;
}
/* check ELF class */
if ((elf_module->e_ident[EI_CLASS] != ELFCLASS32)&&(elf_module->e_ident[EI_CLASS] != ELFCLASS64))
{
rt_kprintf("Module: ELF class error\n");
goto __exit;
}
module = dlmodule_create();
if (!module) goto __exit;
/* set the name of module */
_dlmodule_set_name(module, filename);
LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
if (elf_module->e_type == ET_REL)
{
ret = dlmodule_load_relocated_object(module, module_ptr);
}
else if (elf_module->e_type == ET_DYN)
{
ret = dlmodule_load_shared_object(module, module_ptr);
}
else
{
rt_kprintf("Module: unsupported elf type\n");
goto __exit;
}
/* check return value */
if (ret != RT_EOK) goto __exit;
/* release module data */
rt_free(module_ptr);
/* increase module reference count */
module->nref ++;
/* deal with cache */
#ifdef RT_USING_CACHE
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
#endif
/* set module initialization and cleanup function */
module->init_func = dlsym(module, "module_init");
module->cleanup_func = dlsym(module, "module_cleanup");
module->stat = RT_DLMODULE_STAT_INIT;
/* do module initialization */
if (module->init_func)
{
module->init_func(module);
}
return module;
__exit:
#ifdef RT_USING_POSIX_FS
if (fd >= 0) close(fd);
#endif
if (module_ptr) rt_free(module_ptr);
if (module) dlmodule_destroy(module);
return RT_NULL;
}
struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
{
struct rt_dlmodule *module = RT_NULL;
module = dlmodule_load(pgname);
if (module)
{
if (module->entry_addr)
{
/* exec this module */
rt_thread_t tid;
module->cmd_line = rt_strdup(cmd);
/* check stack size and priority */
if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
module->stack_size, module->priority, 10);
if (tid)
{
tid->parent.module_id= module;
module->main_thread = tid;
rt_thread_startup(tid);
}
else
{
/* destory dl module */
dlmodule_destroy(module);
module = RT_NULL;
}
}
}
return module;
}
#if defined(RT_USING_CUSTOM_DLMODULE)
struct rt_dlmodule* dlmodule_load_custom(const char* filename, struct rt_dlmodule_ops* ops)
{
#ifdef RT_USING_POSIX_FS
int fd = -1, length = 0;
#endif
rt_err_t ret = RT_EOK;
rt_uint8_t *module_ptr = RT_NULL;
struct rt_dlmodule *module = RT_NULL;
if (ops)
{
RT_ASSERT(ops->load);
RT_ASSERT(ops->unload);
module_ptr = ops->load(filename);
}
#ifdef RT_USING_POSIX_FS
else
{
fd = open(filename, O_RDONLY, 0);
if (fd >= 0)
{
length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
if (length == 0) goto __exit;
module_ptr = (uint8_t*) rt_malloc (length);
if (!module_ptr) goto __exit;
if (read(fd, module_ptr, length) != length)
goto __exit;
/* close file and release fd */
close(fd);
fd = -1;
}
else
{
goto __exit;
}
}
#endif
if (!module_ptr) goto __exit;
/* check ELF header */
if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
{
rt_kprintf("Module: magic error\n");
goto __exit;
}
/* check ELF class */
if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
{
rt_kprintf("Module: ELF class error\n");
goto __exit;
}
module = dlmodule_create();
if (!module) goto __exit;
/* set the name of module */
_dlmodule_set_name(module, filename);
LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
if (elf_module->e_type == ET_REL)
{
ret = dlmodule_load_relocated_object(module, module_ptr);
}
else if (elf_module->e_type == ET_DYN)
{
ret = dlmodule_load_shared_object(module, module_ptr);
}
else
{
rt_kprintf("Module: unsupported elf type\n");
goto __exit;
}
/* check return value */
if (ret != RT_EOK) goto __exit;
/* release module data */
if (ops)
{
ops->unload(module_ptr);
}
else
{
rt_free(module_ptr);
}
/* increase module reference count */
module->nref ++;
/* deal with cache */
#ifdef RT_USING_CACHE
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
#endif
/* set module initialization and cleanup function */
module->init_func = dlsym(module, "module_init");
module->cleanup_func = dlsym(module, "module_cleanup");
module->stat = RT_DLMODULE_STAT_INIT;
/* do module initialization */
if (module->init_func)
{
module->init_func(module);
}
return module;
__exit:
#ifdef RT_USING_POSIX_FS
if (fd >= 0) close(fd);
#endif
if (module_ptr)
{
if (ops)
{
ops->unload(module_ptr);
}
else
{
rt_free(module_ptr);
}
}
if (module) dlmodule_destroy(module);
return RT_NULL;
}
struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, int cmd_size, struct rt_dlmodule_ops* ops)
{
struct rt_dlmodule *module = RT_NULL;
module = dlmodule_load_custom(pgname, ops);
if (module)
{
if (module->entry_addr)
{
/* exec this module */
rt_thread_t tid;
module->cmd_line = rt_strdup(cmd);
/* check stack size and priority */
if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
module->stack_size, module->priority, 10);
if (tid)
{
tid->parent.module_id= module;
module->main_thread = tid;
rt_thread_startup(tid);
}
else
{
/* destory dl module */
dlmodule_destroy(module);
module = RT_NULL;
}
}
}
return module;
}
#endif
void dlmodule_exit(int ret_code)
{
rt_thread_t thread;
struct rt_dlmodule *module;
module = dlmodule_self();
if (!module) return;
/* disable scheduling */
rt_enter_critical();
/* module is not running */
if (module->stat != RT_DLMODULE_STAT_RUNNING)
{
/* restore scheduling */
rt_exit_critical();
return;
}
/* set return code */
module->ret_code = ret_code;
/* do exit for this module */
_dlmodule_exit();
/* the stat of module was changed to CLOSING in _dlmodule_exit */
thread = module->main_thread;
if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
/* main thread already closed */
rt_exit_critical();
return ;
}
/* delete thread: insert to defunct thread list */
rt_thread_delete(thread);
/* enable scheduling */
rt_exit_critical();
}
rt_uint32_t dlmodule_symbol_find(const char *sym_str)
{
/* find in kernel symbol table */
struct rt_module_symtab *index;
for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
{
if (rt_strcmp(index->name, sym_str) == 0)
return (rt_uint32_t)index->addr;
}
return 0;
}
int rt_system_dlmodule_init(void)
{
#if defined(__GNUC__) && !defined(__CC_ARM)
extern int __rtmsymtab_start;
extern int __rtmsymtab_end;
_rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
_rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
#elif defined (__CC_ARM)
extern int RTMSymTab$$Base;
extern int RTMSymTab$$Limit;
_rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
_rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
#elif defined (__IAR_SYSTEMS_ICC__)
_rt_module_symtab_begin = __section_begin("RTMSymTab");
_rt_module_symtab_end = __section_end("RTMSymTab");
#endif
return 0;
}
INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
/**
* This function will find the specified module.
*
* @param name the name of module finding
*
* @return the module
*/
struct rt_dlmodule *dlmodule_find(const char *name)
{
rt_object_t object;
struct rt_dlmodule *ret = RT_NULL;
object = rt_object_find(name, RT_Object_Class_Module);
if (object)
{
ret = (struct rt_dlmodule*) object;
}
return ret;
}
RTM_EXPORT(dlmodule_find);
int list_symbols(void)
{
extern int __rtmsymtab_start;
extern int __rtmsymtab_end;
/* find in kernel symbol table */
struct rt_module_symtab *index;
for (index = _rt_module_symtab_begin;
index != _rt_module_symtab_end;
index ++)
{
rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
}
return 0;
}
MSH_CMD_EXPORT(list_symbols, list symbols information);
int list_module(void)
{
struct rt_dlmodule *module;
struct rt_list_node *list, *node;
struct rt_object_information *info;
info = rt_object_get_information(RT_Object_Class_Module);
list = &info->object_list;
rt_kprintf("module ref address \n");
rt_kprintf("-------- -------- ------------\n");
for (node = list->next; node != list; node = node->next)
{
module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-*.*s %-04d 0x%08x\n",
RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
}
return 0;
}
MSH_CMD_EXPORT(list_module, list modules in system);

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/11 Bernard the first version
*/
#ifndef RT_DL_MODULE_H__
#define RT_DL_MODULE_H__
#include <rtthread.h>
#define RT_DLMODULE_STAT_INIT 0x00
#define RT_DLMODULE_STAT_RUNNING 0x01
#define RT_DLMODULE_STAT_CLOSING 0x02
#define RT_DLMODULE_STAT_CLOSED 0x03
struct rt_dlmodule;
typedef void* rt_addr_t;
typedef void (*rt_dlmodule_init_func_t)(struct rt_dlmodule *module);
typedef void (*rt_dlmodule_cleanup_func_t)(struct rt_dlmodule *module);
typedef int (*rt_dlmodule_entry_func_t)(int argc, char** argv);
struct rt_dlmodule
{
struct rt_object parent;
rt_list_t object_list; /* objects inside this module */
rt_uint8_t stat; /* status of module */
/* main thread of this module */
rt_uint16_t priority;
rt_uint32_t stack_size;
struct rt_thread *main_thread;
/* the return code */
int ret_code;
/* VMA base address for the first LOAD segment */
rt_uint32_t vstart_addr;
/* module entry, RT_NULL for dynamic library */
rt_dlmodule_entry_func_t entry_addr;
char *cmd_line; /* command line */
rt_addr_t mem_space; /* memory space */
rt_uint32_t mem_size; /* sizeof memory space */
/* init and clean function */
rt_dlmodule_init_func_t init_func;
rt_dlmodule_cleanup_func_t cleanup_func;
rt_uint16_t nref; /* reference count */
rt_uint16_t nsym; /* number of symbols in the module */
struct rt_module_symtab *symtab; /* module symbol table */
};
struct rt_dlmodule_ops
{
rt_uint8_t *(*load)(const char* filename); /* load dlmodule file data */
rt_err_t (*unload)(rt_uint8_t *param); /* unload dlmodule file data */
};
struct rt_dlmodule *dlmodule_create(void);
rt_err_t dlmodule_destroy(struct rt_dlmodule* module);
struct rt_dlmodule *dlmodule_self(void);
struct rt_dlmodule *dlmodule_load(const char* pgname);
struct rt_dlmodule *dlmodule_exec(const char* pgname, const char* cmd, int cmd_size);
#if defined(RT_USING_CUSTOM_DLMODULE)
struct rt_dlmodule* dlmodule_load_custom(const char* filename, struct rt_dlmodule_ops* ops);
struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, int cmd_size, struct rt_dlmodule_ops* ops);
#endif
void dlmodule_exit(int ret_code);
struct rt_dlmodule *dlmodule_find(const char *name);
rt_uint32_t dlmodule_symbol_find(const char *sym_str);
#endif

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 yi.qiu first version
*/
#include <rtthread.h>
#include <rtm.h>
#include <string.h>
#include "dlmodule.h"
#define MODULE_ROOT_DIR "/modules"
void* dlopen(const char *filename, int flags)
{
struct rt_dlmodule *module;
char *fullpath;
const char*def_path = MODULE_ROOT_DIR;
/* check parameters */
RT_ASSERT(filename != RT_NULL);
if (filename[0] != '/') /* it's a relative path, prefix with MODULE_ROOT_DIR */
{
fullpath = rt_malloc(strlen(def_path) + strlen(filename) + 2);
/* join path and file name */
rt_snprintf(fullpath, strlen(def_path) + strlen(filename) + 2,
"%s/%s", def_path, filename);
}
else
{
fullpath = (char*)filename; /* absolute path, use it directly */
}
rt_enter_critical();
/* find in module list */
module = dlmodule_find(fullpath);
if(module != RT_NULL)
{
rt_exit_critical();
module->nref++;
}
else
{
rt_exit_critical();
module = dlmodule_load(fullpath);
}
if(fullpath != filename)
{
rt_free(fullpath);
}
return (void*)module;
}
RTM_EXPORT(dlopen);

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 yi.qiu first version
*/
#include <rtthread.h>
#include <rtm.h>
#include "dlmodule.h"
void* dlsym(void *handle, const char* symbol)
{
int i;
struct rt_dlmodule *module;
RT_ASSERT(handle != RT_NULL);
module = (struct rt_dlmodule *)handle;
for(i=0; i<module->nsym; i++)
{
if (rt_strcmp(module->symtab[i].name, symbol) == 0)
return (void*)module->symtab[i].addr;
}
return RT_NULL;
}
RTM_EXPORT(dlsym)

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/15 bernard the first version
*/
#include <rtthread.h>
#include <rtm.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
RTM_EXPORT(strcpy);
RTM_EXPORT(strncpy);
RTM_EXPORT(strlen);
RTM_EXPORT(strcat);
RTM_EXPORT(strstr);
RTM_EXPORT(strchr);
RTM_EXPORT(strcmp);
RTM_EXPORT(strtol);
RTM_EXPORT(strtoul);
RTM_EXPORT(strncmp);
RTM_EXPORT(memcpy);
RTM_EXPORT(memcmp);
RTM_EXPORT(memmove);
RTM_EXPORT(memset);
RTM_EXPORT(memchr);
RTM_EXPORT(putchar);
RTM_EXPORT(puts);
RTM_EXPORT(printf);
RTM_EXPORT(sprintf);
RTM_EXPORT(snprintf);
RTM_EXPORT(fwrite);
#include <setjmp.h>
RTM_EXPORT(longjmp);
RTM_EXPORT(setjmp);
RTM_EXPORT(exit);
RTM_EXPORT(abort);
RTM_EXPORT(rand);
#include <assert.h>
RTM_EXPORT(__assert_func);

View File

@@ -0,0 +1,9 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('POSIX', src, depend = ['RT_USING_PTHREADS'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2013-12-23 Bernard Add the checking for ESHUTDOWN
*/
#ifndef __POSIX_TYPES_H__
#define __POSIX_TYPES_H__
#include <rtthread.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/errno.h>
#include <fcntl.h>
#endif

View File

@@ -0,0 +1,833 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-01-26 Bernard Fix pthread_detach issue for a none-joinable
* thread.
* 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
* 2022-05-10 xiangxistu Modify the recycle logic about resource of pthread.
* 2024-04-15 atwww Modify the recycle logic of TLS in function _pthread_data_destroy,
* make it safe for C++11's thread_local destructors.
*/
#include <rthw.h>
#include <pthread.h>
#include <sched.h>
#include <sys/time.h>
#include "pthread_internal.h"
RT_DEFINE_HW_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)
{
_pthread_data_t *ptd;
if (thread >= PTHREAD_NUM_MAX) return NULL;
rt_hw_spin_lock(&pth_lock);
ptd = pth_table[thread];
rt_hw_spin_unlock(&pth_lock);
if (ptd && ptd->magic == PTHREAD_MAGIC) return ptd;
return NULL;
}
pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
{
int index;
rt_hw_spin_lock(&pth_lock);
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
{
if (pth_table[index] == ptd) break;
}
rt_hw_spin_unlock(&pth_lock);
return index;
}
pthread_t _pthread_data_create(void)
{
int index;
_pthread_data_t *ptd = NULL;
ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
if (!ptd) return PTHREAD_NUM_MAX;
memset(ptd, 0x0, sizeof(_pthread_data_t));
ptd->canceled = 0;
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
ptd->magic = PTHREAD_MAGIC;
rt_hw_spin_lock(&pth_lock);
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
{
if (pth_table[index] == NULL)
{
pth_table[index] = ptd;
break;
}
}
rt_hw_spin_unlock(&pth_lock);
/* full of pthreads, clean magic and release ptd */
if (index == PTHREAD_NUM_MAX)
{
ptd->magic = 0x0;
rt_free(ptd);
}
return index;
}
static inline void _destroy_item(int index, _pthread_data_t *ptd)
{
extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
void *data;
if (_thread_keys[index].is_used)
{
data = ptd->tls[index];
if (data && _thread_keys[index].destructor)
{
_thread_keys[index].destructor(data);
}
}
}
#ifdef RT_USING_CPLUSPLUS11
#define NOT_USE_CXX_TLS -1
#endif
void _pthread_data_destroy(_pthread_data_t *ptd)
{
pthread_t pth;
if (ptd)
{
/* if this thread create the local thread data,
* destruct thread local key
*/
if (ptd->tls != RT_NULL)
{
int index;
#ifdef RT_USING_CPLUSPLUS11
/* If C++11 is enabled and emutls is used,
* destructors of C++ object must be called safely.
*/
extern pthread_key_t emutls_get_pthread_key(void);
pthread_key_t emutls_pthread_key = emutls_get_pthread_key();
if (emutls_pthread_key != NOT_USE_CXX_TLS)
{
/* If execution reaches here, C++ 'thread_local' may be used.
* Destructors of c++ class object must be called before emutls_key_destructor.
*/
int start = ((emutls_pthread_key - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX);
int i = 0;
for (index = start; i < PTHREAD_KEY_MAX; index = (index - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX, i ++)
{
_destroy_item(index, ptd);
}
}
else
#endif
{
/* If only C TLS is used, that is, POSIX TLS or __Thread_local,
* just iterate the _thread_keys from index 0.
*/
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
{
_destroy_item(index, ptd);
}
}
/* release tls area */
rt_free(ptd->tls);
ptd->tls = RT_NULL;
}
pth = _pthread_data_get_pth(ptd);
/* remove from pthread table */
rt_hw_spin_lock(&pth_lock);
pth_table[pth] = NULL;
rt_hw_spin_unlock(&pth_lock);
/* delete joinable semaphore */
if (ptd->joinable_sem != RT_NULL)
{
rt_sem_delete(ptd->joinable_sem);
ptd->joinable_sem = RT_NULL;
}
/* clean magic */
ptd->magic = 0x0;
/* clear the "ptd->tid->pthread_data" */
ptd->tid->pthread_data = RT_NULL;
/* free ptd */
rt_free(ptd);
}
}
static void _pthread_cleanup(rt_thread_t tid)
{
/* clear cleanup function */
tid->cleanup = RT_NULL;
/* restore tid stack */
rt_free(tid->stack_addr);
/* restore tid control block */
rt_free(tid);
}
static void pthread_entry_stub(void *parameter)
{
void *value;
_pthread_data_t *ptd;
ptd = (_pthread_data_t *)parameter;
/* execute pthread entry */
value = ptd->thread_entry(ptd->thread_parameter);
/* According to "detachstate" to whether or not to recycle resource immediately */
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
{
/* set value */
ptd->return_value = value;
rt_sem_release(ptd->joinable_sem);
}
else
{
/* release pthread resource */
_pthread_data_destroy(ptd);
}
}
int pthread_create(pthread_t *pid,
const pthread_attr_t *attr,
void *(*start)(void *), void *parameter)
{
int ret = 0;
void *stack;
char name[RT_NAME_MAX];
static rt_uint16_t pthread_number = 0;
pthread_t pth_id;
_pthread_data_t *ptd;
/* pid shall be provided */
RT_ASSERT(pid != RT_NULL);
/* allocate posix thread data */
pth_id = _pthread_data_create();
if (pth_id == PTHREAD_NUM_MAX)
{
ret = ENOMEM;
goto __exit;
}
/* get pthread data */
ptd = _pthread_get_data(pth_id);
RT_ASSERT(ptd != RT_NULL);
if (attr != RT_NULL)
{
ptd->attr = *attr;
}
else
{
/* use default attribute */
pthread_attr_init(&ptd->attr);
}
if (ptd->attr.stacksize == 0)
{
ret = EINVAL;
goto __exit;
}
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
/* pthread is a static thread object */
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
if (ptd->tid == RT_NULL)
{
ret = ENOMEM;
goto __exit;
}
memset(ptd->tid, 0, sizeof(struct rt_thread));
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
{
ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
if (ptd->joinable_sem == RT_NULL)
{
ret = ENOMEM;
goto __exit;
}
}
else
{
ptd->joinable_sem = RT_NULL;
}
/* set parameter */
ptd->thread_entry = start;
ptd->thread_parameter = parameter;
/* stack */
if (ptd->attr.stackaddr == 0)
{
stack = (void *)rt_malloc(ptd->attr.stacksize);
}
else
{
stack = (void *)(ptd->attr.stackaddr);
}
if (stack == RT_NULL)
{
ret = ENOMEM;
goto __exit;
}
/* initial this pthread to system */
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
stack, ptd->attr.stacksize,
ptd->attr.schedparam.sched_priority, 20) != RT_EOK)
{
ret = EINVAL;
goto __exit;
}
/* set pthread id */
*pid = pth_id;
/* set pthread cleanup function and ptd data */
ptd->tid->cleanup = _pthread_cleanup;
ptd->tid->pthread_data = (void *)ptd;
/* start thread */
if (rt_thread_startup(ptd->tid) == RT_EOK)
return 0;
/* start thread failed */
rt_thread_detach(ptd->tid);
ret = EINVAL;
__exit:
if (pth_id != PTHREAD_NUM_MAX)
{
_pthread_data_destroy(ptd);
}
return ret;
}
RTM_EXPORT(pthread_create);
int pthread_detach(pthread_t thread)
{
int ret = 0;
_pthread_data_t *ptd = _pthread_get_data(thread);
if (ptd == RT_NULL)
{
/* invalid pthread id */
ret = EINVAL;
goto __exit;
}
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
{
/* The implementation has detected that the value specified by thread does not refer
* to a joinable thread.
*/
ret = EINVAL;
goto __exit;
}
if ((RT_SCHED_CTX(ptd->tid).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
/* destroy this pthread */
_pthread_data_destroy(ptd);
goto __exit;
}
else
{
/* change to detach state */
ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
/* detach joinable semaphore */
if (ptd->joinable_sem)
{
rt_sem_delete(ptd->joinable_sem);
ptd->joinable_sem = RT_NULL;
}
}
__exit:
return ret;
}
RTM_EXPORT(pthread_detach);
int pthread_join(pthread_t thread, void **value_ptr)
{
_pthread_data_t *ptd;
rt_err_t result;
ptd = _pthread_get_data(thread);
if (ptd == RT_NULL)
{
return EINVAL; /* invalid pthread id */
}
if (ptd && ptd->tid == rt_thread_self())
{
/* join self */
return EDEADLK;
}
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
{
return EINVAL; /* join on a detached pthread */
}
result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
/* get return value */
if (value_ptr != RT_NULL)
*value_ptr = ptd->return_value;
/* destroy this pthread */
_pthread_data_destroy(ptd);
}
else
{
return ESRCH;
}
return 0;
}
RTM_EXPORT(pthread_join);
pthread_t pthread_self (void)
{
rt_thread_t tid;
_pthread_data_t *ptd;
tid = rt_thread_self();
if (tid == NULL) return PTHREAD_NUM_MAX;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
return _pthread_data_get_pth(ptd);
}
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;
_pthread_cleanup_t *cleanup;
rt_thread_t tid;
if (rt_thread_self() == RT_NULL)
{
return;
}
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
rt_enter_critical();
/* disable cancel */
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
/* set return value */
ptd->return_value = value;
rt_exit_critical();
/*
* When use pthread_exit to exit.
* invoke pushed cleanup
*/
while (ptd->cleanup != RT_NULL)
{
cleanup = ptd->cleanup;
ptd->cleanup = cleanup->next;
cleanup->cleanup_func(cleanup->parameter);
/* release this cleanup function */
rt_free(cleanup);
}
/* get the info aboult "tid" early */
tid = ptd->tid;
/* According to "detachstate" to whether or not to recycle resource immediately */
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
{
/* set value */
rt_sem_release(ptd->joinable_sem);
}
else
{
/* release pthread resource */
_pthread_data_destroy(ptd);
}
/*
* second: detach thread.
* this thread will be removed from scheduler list
* and because there is a cleanup function in the
* thread (pthread_cleanup), it will move to defunct
* thread list and wait for handling in idle thread.
*/
rt_thread_detach(tid);
/* reschedule thread */
rt_schedule();
}
RTM_EXPORT(pthread_exit);
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
RT_ASSERT(once_control != RT_NULL);
RT_ASSERT(init_routine != RT_NULL);
rt_enter_critical();
if (!(*once_control))
{
/* call routine once */
*once_control = 1;
rt_exit_critical();
init_routine();
}
rt_exit_critical();
return 0;
}
RTM_EXPORT(pthread_once);
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
{
return EOPNOTSUPP;
}
RTM_EXPORT(pthread_atfork);
int pthread_kill(pthread_t thread, int sig)
{
#ifdef RT_USING_SIGNALS
_pthread_data_t *ptd;
int ret;
ptd = _pthread_get_data(thread);
if (ptd)
{
ret = rt_thread_kill(ptd->tid, sig);
if (ret == -RT_EINVAL)
{
return EINVAL;
}
return ret;
}
return ESRCH;
#else
return ENOSYS;
#endif
}
RTM_EXPORT(pthread_kill);
#ifdef RT_USING_SIGNALS
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
return sigprocmask(how, set, oset);
}
#endif
void pthread_cleanup_pop(int execute)
{
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
if (rt_thread_self() == NULL) return;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
if (execute)
{
rt_enter_critical();
cleanup = ptd->cleanup;
if (cleanup)
ptd->cleanup = cleanup->next;
rt_exit_critical();
if (cleanup)
{
cleanup->cleanup_func(cleanup->parameter);
rt_free(cleanup);
}
}
}
RTM_EXPORT(pthread_cleanup_pop);
void pthread_cleanup_push(void (*routine)(void *), void *arg)
{
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
if (rt_thread_self() == NULL) return;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
if (cleanup != RT_NULL)
{
cleanup->cleanup_func = routine;
cleanup->parameter = arg;
rt_enter_critical();
cleanup->next = ptd->cleanup;
ptd->cleanup = cleanup;
rt_exit_critical();
}
}
RTM_EXPORT(pthread_cleanup_push);
/*
* According to IEEE Std 1003.1, 2004 Edition , following pthreads
* interface support cancellation point:
* mq_receive()
* mq_send()
* mq_timedreceive()
* mq_timedsend()
* msgrcv()
* msgsnd()
* msync()
* pthread_cond_timedwait()
* pthread_cond_wait()
* pthread_join()
* pthread_testcancel()
* sem_timedwait()
* sem_wait()
*
* A cancellation point may also occur when a thread is
* executing the following functions:
* pthread_rwlock_rdlock()
* pthread_rwlock_timedrdlock()
* pthread_rwlock_timedwrlock()
* pthread_rwlock_wrlock()
*
* The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
* functions are defined to be async-cancel safe.
*/
int pthread_setcancelstate(int state, int *oldstate)
{
_pthread_data_t *ptd;
if (rt_thread_self() == NULL) return EINVAL;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
{
if (oldstate)
*oldstate = ptd->cancelstate;
ptd->cancelstate = state;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_setcancelstate);
int pthread_setcanceltype(int type, int *oldtype)
{
_pthread_data_t *ptd;
if (rt_thread_self() == NULL) return EINVAL;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
return EINVAL;
if (oldtype)
*oldtype = ptd->canceltype;
ptd->canceltype = type;
return 0;
}
RTM_EXPORT(pthread_setcanceltype);
void pthread_testcancel(void)
{
int cancel = 0;
_pthread_data_t *ptd;
if (rt_thread_self() == NULL) return;
/* get pthread data from pthread_data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != RT_NULL);
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
cancel = ptd->canceled;
if (cancel)
pthread_exit((void *)PTHREAD_CANCELED);
}
RTM_EXPORT(pthread_testcancel);
int pthread_cancel(pthread_t thread)
{
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
rt_thread_t tid;
/* get posix thread data */
ptd = _pthread_get_data(thread);
if (ptd == RT_NULL)
{
return EINVAL;
}
tid = ptd->tid;
/* cancel self */
if (ptd->tid == rt_thread_self())
return 0;
/* set canceled */
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
{
ptd->canceled = 1;
if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
{
/*
* When use pthread_cancel to exit.
* invoke pushed cleanup
*/
while (ptd->cleanup != RT_NULL)
{
cleanup = ptd->cleanup;
ptd->cleanup = cleanup->next;
cleanup->cleanup_func(cleanup->parameter);
/* release this cleanup function */
rt_free(cleanup);
}
/* According to "detachstate" to whether or not to recycle resource immediately */
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
{
/* set value */
rt_sem_release(ptd->joinable_sem);
}
else
{
/* release pthread resource */
_pthread_data_destroy(ptd);
}
/*
* second: detach thread.
* this thread will be removed from scheduler list
* and because there is a cleanup function in the
* thread (pthread_cleanup), it will move to defunct
* thread list and wait for handling in idle thread.
*/
rt_thread_detach(tid);
}
}
return 0;
}
RTM_EXPORT(pthread_cancel);

View File

@@ -0,0 +1,296 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#ifndef __PTHREAD_H__
#define __PTHREAD_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <posix_types.h>
#include <sched.h>
#define PTHREAD_KEY_MAX 8
#define PTHREAD_COND_INITIALIZER {-1}
#define PTHREAD_RWLOCK_INITIALIZER {-1}
#define PTHREAD_MUTEX_INITIALIZER {-1}
#define PTHREAD_CREATE_JOINABLE 0x00
#define PTHREAD_CREATE_DETACHED 0x01
#define PTHREAD_EXPLICIT_SCHED 0
#define PTHREAD_INHERIT_SCHED 1
typedef long pthread_t;
typedef long pthread_condattr_t;
typedef long pthread_rwlockattr_t;
typedef long pthread_mutexattr_t;
typedef long pthread_barrierattr_t;
typedef int pthread_key_t;
typedef int pthread_once_t;
enum
{
PTHREAD_CANCEL_ASYNCHRONOUS = 0,
PTHREAD_CANCEL_ENABLE,
PTHREAD_CANCEL_DEFERRED,
PTHREAD_CANCEL_DISABLE,
PTHREAD_CANCELED
};
enum
{
PTHREAD_MUTEX_NORMAL = 0,
PTHREAD_MUTEX_RECURSIVE = 1,
PTHREAD_MUTEX_ERRORCHECK = 2,
PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
};
/* init value for pthread_once_t */
#define PTHREAD_ONCE_INIT 0
enum
{
PTHREAD_PRIO_INHERIT =0,
PTHREAD_PRIO_NONE,
PTHREAD_PRIO_PROTECT,
};
#define PTHREAD_PROCESS_PRIVATE 0
#define PTHREAD_PROCESS_SHARED 1
#define PTHREAD_SCOPE_PROCESS 0
#define PTHREAD_SCOPE_SYSTEM 1
struct sched_param
{
int sched_priority;
};
struct pthread_attr
{
void* stackaddr; /* stack address of thread */
int stacksize; /* stack size of thread */
int inheritsched; /* Inherit parent prio/policy */
int schedpolicy; /* scheduler policy */
struct sched_param schedparam; /* sched parameter */
int detachstate; /* detach state */
};
typedef struct pthread_attr pthread_attr_t;
struct pthread_mutex
{
pthread_mutexattr_t attr;
struct rt_mutex lock;
};
typedef struct pthread_mutex pthread_mutex_t;
struct pthread_cond
{
pthread_condattr_t attr;
struct rt_semaphore sem;
};
typedef struct pthread_cond pthread_cond_t;
struct pthread_rwlock
{
pthread_rwlockattr_t attr;
pthread_mutex_t rw_mutex; /* basic lock on this struct */
pthread_cond_t rw_condreaders; /* for reader threads waiting */
pthread_cond_t rw_condwriters; /* for writer threads waiting */
int rw_nwaitreaders; /* the number of reader threads waiting */
int rw_nwaitwriters; /* the number of writer threads waiting */
int rw_refcount; /* 0: unlocked, -1: locked by writer, > 0 locked by n readers */
};
typedef struct pthread_rwlock pthread_rwlock_t;
/* spinlock implementation, (ADVANCED REALTIME THREADS)*/
struct pthread_spinlock
{
int lock;
};
typedef struct pthread_spinlock pthread_spinlock_t;
struct pthread_barrier
{
int count;
pthread_cond_t cond;
pthread_mutex_t mutex;
};
typedef struct pthread_barrier pthread_barrier_t;
/* pthread thread interface */
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state);
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy);
int pthread_attr_setschedparam(pthread_attr_t *attr,struct sched_param const *param);
int pthread_attr_getschedparam(pthread_attr_t const *attr,struct sched_param *param);
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size);
int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size);
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr);
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr);
int pthread_attr_setstack(pthread_attr_t *attr,
void *stack_base,
size_t stack_size);
int pthread_attr_getstack(pthread_attr_t const *attr,
void **stack_base,
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 *scope);
int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
void *(*start) (void *), void *arg);
int pthread_detach (pthread_t thread);
int pthread_join (pthread_t thread, void **value_ptr);
rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
{
return t1 == 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));
#ifdef RT_USING_SIGNALS
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
#endif
/* pthread cleanup */
void pthread_cleanup_pop(int execute);
void pthread_cleanup_push(void (*routine)(void*), void *arg);
/* pthread cancel */
int pthread_cancel(pthread_t thread);
void pthread_testcancel(void);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
int pthread_kill(pthread_t thread, int sig);
/* pthread mutex interface */
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
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);
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);
int pthread_condattr_init(pthread_condattr_t *attr);
/* ADVANCED REALTIME feature in IEEE Std 1003.1, 2004 Edition */
int pthread_condattr_getclock(const pthread_condattr_t *attr,
clockid_t *clock_id);
int pthread_condattr_setclock(pthread_condattr_t *attr,
clockid_t clock_id);
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime);
/* pthread rwlock interface */
int pthread_rwlockattr_init (pthread_rwlockattr_t *attr);
int pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr);
int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared);
int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared);
int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock);
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime);
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);
/* pthread spinlock interface */
int pthread_spin_init (pthread_spinlock_t *lock, int pshared);
int pthread_spin_destroy (pthread_spinlock_t *lock);
int pthread_spin_lock (pthread_spinlock_t * lock);
int pthread_spin_trylock (pthread_spinlock_t * lock);
int pthread_spin_unlock (pthread_spinlock_t * lock);
/* pthread barrier interface */
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
int pthread_barrierattr_init(pthread_barrierattr_t *attr);
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
int pthread_barrier_destroy(pthread_barrier_t *barrier);
int pthread_barrier_init(pthread_barrier_t *barrier,
const pthread_barrierattr_t *attr,
unsigned count);
int pthread_barrier_wait(pthread_barrier_t *barrier);
int pthread_setspecific(pthread_key_t key, const void *value);
void *pthread_getspecific(pthread_key_t key);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
int pthread_key_delete(pthread_key_t key);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,615 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <rtthread.h>
#include "pthread.h"
#include "sched.h"
#include <string.h>
#define DEFAULT_STACK_SIZE 2048
#define DEFAULT_PRIORITY (RT_THREAD_PRIORITY_MAX/2 + RT_THREAD_PRIORITY_MAX/4)
const pthread_attr_t pthread_default_attr =
{
0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */
PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
SCHED_FIFO, /* scheduler policy */
{
DEFAULT_PRIORITY, /* scheduler priority */
},
PTHREAD_CREATE_JOINABLE, /* detach state */
};
/**
* @brief This function will initialize thread attributes object.
*
* @note The pthread_attr_t type should be treated as opaque: any access to the object other
* than via pthreads functions is nonportable and produces undefined results.
* The resulting attribute object (possibly modified by setting individual attribute values),
* when used by pthread_create(), defines the attributes of the thread created. A single attributes
* object can be used in multiple simultaneous calls to pthread_create().
*
* @see pthread_create()
*
* @param attr is a thread attributes object.
*
* @return Upon successful completion, pthread_attr_init() return a value of 0.
* Otherwise, it means that the event detach failed.
*
* @warning This function will fail if attr is null.
*/
int pthread_attr_init(pthread_attr_t *attr)
{
RT_ASSERT(attr != RT_NULL);
*attr = pthread_default_attr;
return 0;
}
RTM_EXPORT(pthread_attr_init);
/**
* @brief This function will destroy thread attributes object.
*
* @note When a thread attributes object is no longer required, it should be destroyed
* using the pthread_attr_destroy() function. Destroying a thread attributes object
* has no effect on threads that were created using that object.
* Once a thread attributes object has been destroyed, it can be reinitialized using pthread_attr_init().
* Any other use of a destroyed thread attributes object has undefined results.
*
* @see pthread_attr_init(), pthread_attr_getdetachstate(), pthread_create()
*
* @param attr is a thread attributes object.
*
* @return Upon successful completion, pthread_attr_destroy() and shall return a value of 0;
* Otherwise, an error number shall be returned to indicate the error.
*
* @warning This function will fail if attr is null.
*/
int pthread_attr_destroy(pthread_attr_t *attr)
{
RT_ASSERT(attr != RT_NULL);
memset(attr, 0, sizeof(pthread_attr_t));
return 0;
}
RTM_EXPORT(pthread_attr_destroy);
/**
* @brief This function set detach state attribute in thread attributes object.
*
* @note This function sets the detach state attribute of the thread attributes object
* referred to by attr to the value specified in detachstate. The detach state
* attribute determines whether a thread created using the thread attributes
* object attr will be created in a joinable or a detached state.
*
* @see pthread_attr_init(), pthread_create(), pthread_detach(), pthread_join(), pthreads()
*
* @param attr is a thread attributes object.
*
* @param state is attribute in the attr object.
* attribute controls whether the thread is created in a detached state.
* The detachstate can be ONE of the following values:
*
* PTHREAD_CREATE_DETACHED It causes all threads created with attr to be in the detached state.
*
* PTHREAD_CREATE_JOINABLE Default value, it causes all threads created with attr to be in the joinable state.
*
* @return Upon successful completion, pthread_attr_setdetachstate() and return a value of 0.
* Otherwise, an error number is returned to indicate the error.
*
* @warning The pthread_attr_setdetachstate() function will fail if:
* [EINVAL]
* The value of detach state was not valid
*/
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
{
RT_ASSERT(attr != RT_NULL);
if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
return EINVAL;
attr->detachstate = state;
return 0;
}
RTM_EXPORT(pthread_attr_setdetachstate);
/**
* @brief This function get detach state attribute in thread attributes object.
*
* @note The detachstate attribute controls whether the thread is created in a detached state.
* If the thread is created detached, then use of the ID of the newly created thread by
* the pthread_detach() or pthread_join() function is an error.
*
* @see pthread_attr_destroy(), pthread_attr_getstackaddr(), pthread_attr_getstacksize(), pthread_create()
*
* @param attr is a thread attributes object.
*
* @param state is attribute in the attr object.
* attribute controls whether the thread is created in a detached state.
* The detachstate can be ONE of the following values:
*
* PTHREAD_CREATE_DETACHED It causes all threads created with attr to be in the detached state.
*
* PTHREAD_CREATE_JOINABLE Default value, it causes all threads created with attr to be in the joinable state.
*
* @return Upon successful completion, pthread_attr_getdetachstate() and shall return a value of 0;
* otherwise, an error number shall be returned to indicate the error.
*
* The pthread_attr_getdetachstate() function stores the value of the detachstate
* attribute in detachstate if successful.
*/
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state)
{
RT_ASSERT(attr != RT_NULL);
*state = (int)attr->detachstate;
return 0;
}
RTM_EXPORT(pthread_attr_getdetachstate);
/**
* @brief This function sets schedpolicy attribute.
*
* @note The function function sets the scheduling policy attribute of the thread
* attributes object referred to by attr to the value specified in policy.
*
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedparam(), pthread_create()
*
* @param attr is a thread attributes object.
*
* @param policy is attribute in the attr object.
* The policy can be ONE of the following values:
*
* SCHED_FIFO First in-first out scheduling.
*
* SCHED_RR Round-robin scheduling.
*
* SCHED_OTHER Default Linux time-sharing scheduling.
*
* @return On success, these functions return 0.
*/
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
{
RT_ASSERT(attr != RT_NULL);
attr->schedpolicy = policy;
return 0;
}
RTM_EXPORT(pthread_attr_setschedpolicy);
/**
* @brief This function gets schedpolicy attribute.
*
* @note The function gets the schedpolicy attribute in the attr argument.
*
* @see pthread_attr_destroy(), pthread_attr_getscope(), pthread_attr_getinheritsched(), pthread_attr_getschedparam(), pthread_create()
*
* @param attr is a thread attributes object.
*
* @param policy is attribute in the attr object.
* The policy can be ONE of the following values:
*
* SCHED_FIFO First in-first out scheduling.
*
* SCHED_RR Round-robin scheduling.
*
* SCHED_OTHER Default Linux time-sharing scheduling.
*
* @return On success, these functions return 0.
*/
int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
{
RT_ASSERT(attr != RT_NULL);
*policy = (int)attr->schedpolicy;
return 0;
}
RTM_EXPORT(pthread_attr_getschedpolicy);
/**
* @brief This function set the scheduling parameter attributes in the attr argument.
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedpolicy()
*
* @param attr is a thread attributes object.
*
* @param param is scheduling parameter attributes in the attr argument.
* The contents of the param structure are defined in <pthread.h>.
* For the SCHED_FIFO and SCHED_RR policies, the only required member of param is sched_priority.
*
* @return On success, these functions return 0.
*/
int pthread_attr_setschedparam(pthread_attr_t *attr,
struct sched_param const *param)
{
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
attr->schedparam.sched_priority = param->sched_priority;
return 0;
}
RTM_EXPORT(pthread_attr_setschedparam);
/**
* @brief This function get the scheduling parameter attributes in the attr argument.
* @see pthread_attr_init(), pthread_attr_setscope(), pthread_attr_setinheritsched(), pthread_attr_setschedpolicy()
*
* @param attr is a thread attributes object.
*
* @param param is scheduling parameter attributes in the attr argument.
* The contents of the param structure are defined in <pthread.h>.
* For the SCHED_FIFO and SCHED_RR policies, the only required member of param is sched_priority.
*
* @return On success, these functions return 0.
*/
int pthread_attr_getschedparam(pthread_attr_t const *attr,
struct sched_param *param)
{
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
param->sched_priority = attr->schedparam.sched_priority;
return 0;
}
RTM_EXPORT(pthread_attr_getschedparam);
/**
* @brief This function set the thread creation stacksize attribute in the attr object.
*
* @see pthread_attr_init(), pthread_attr_setstackaddr(), pthread_attr_setdetachstate()
*
* @param attr is a thread attributes object.
*
* @param stack_size is the minimum stack size (in bytes) allocated for the created threads stack.
*
* @return Upon successful completion, This function return a value of 0.
*/
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
{
RT_ASSERT(attr != RT_NULL);
attr->stacksize = stack_size;
return 0;
}
RTM_EXPORT(pthread_attr_setstacksize);
/**
* @brief This function get the thread creation stacksize attribute in the attr object.
*
* @see pthread_attr_init(), pthread_attr_getstackaddr(), pthread_attr_getdetachstate()
*
* @param attr is a thread attributes object.
*
* @param stack_size is the minimum stack size (in bytes) allocated for the created threads stack.
*
* @return Upon successful completion, This function return a value of 0.
*/
int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
{
RT_ASSERT(attr != RT_NULL);
*stack_size = attr->stacksize;
return 0;
}
RTM_EXPORT(pthread_attr_getstacksize);
/**
* @brief This function sets the thread creation stackaddr attribute in the attr object.
*
* @see pthread_attr_init(), pthread_attr_setdetachstate(), pthread_attr_setstacksize()
*
* @param attr is a thread attributes object.
*
* @param The stack_addr attribute specifies the location of storage to be used for the created
* thread's stack.
*
* @return Upon successful completion, This function return a value of 0.
*/
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
{
RT_ASSERT(attr != RT_NULL);
return EOPNOTSUPP;
}
RTM_EXPORT(pthread_attr_setstackaddr);
/**
* @brief This function gets the thread creation stackaddr attribute in the attr object.
*
* @see pthread_attr_init(), pthread_attr_setdetachstate(), pthread_attr_setstacksize()
*
* @param attr is a thread attributes object.
*
* @param The stack_addr attribute specifies the location of storage to be used for the created
* thread's stack.
*
* @return Upon successful completion, This function return a value of 0.
*/
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr)
{
RT_ASSERT(attr != RT_NULL);
return EOPNOTSUPP;
}
RTM_EXPORT(pthread_attr_getstackaddr);
/**
* @brief This function set the thread creation stack attributes stackaddr and stacksize in the attr object.
*
* @note The stack attributes specify the area of storage to be used for the created thread's stack.
* The base (lowest addressable byte) of the storage shall be stack_base, and the size of the storage
* shall be stack_size bytes.
* All pages within the stack described by stackaddr and stacksize shall be both readable
* and writable by the thread.
*
* @see pthread_attr_destroy, pthread_attr_getdetachstate, pthread_attr_getstacksize, pthread_create
*
* @param attr is a thread attributes object.
*
* @param stack_base is the base (lowest addressable byte) of the storage.
*
* @param stack_size is the size of the storage.
*
* @return Upon successful completion, these functions shall return a value of 0;
* otherwise, an error number shall be returned to indicate the error.
*
* @warning The behavior is undefined if the value specified by the attr argument to or pthread_attr_setstack()
* does not refer to an initialized thread attributes object.
*/
int pthread_attr_setstack(pthread_attr_t *attr,
void *stack_base,
size_t stack_size)
{
RT_ASSERT(attr != RT_NULL);
attr->stackaddr = stack_base;
attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
return 0;
}
RTM_EXPORT(pthread_attr_setstack);
/**
* @brief This function shall get the thread creation stack attributes stackaddr and stacksize in the attr object.
*
* @note The stack attributes specify the area of storage to be used for the created thread's stack.
* The base (lowest addressable byte) of the storage shall be stack_base, and the size of the storage
* shall be stack_size bytes.
* All pages within the stack described by stack_base and stack_size shall be both readable
* and writable by the thread.
*
* @see pthread_attr_destroy, pthread_attr_getdetachstate, pthread_attr_getstacksize, pthread_create
*
* @param attr is a thread attributes object.
*
* @param stack_base is the base (lowest addressable byte) of the storage.
*
* @param stack_size is the size of the storage.
*
* @return Upon successful completion, these functions shall return a value of 0;
* otherwise, an error number shall be returned to indicate the error.
* This function shall store the stack attribute values in stack_base and stack_size if successful.
*/
int pthread_attr_getstack(pthread_attr_t const *attr,
void **stack_base,
size_t *stack_size)
{
RT_ASSERT(attr != RT_NULL);
*stack_base = attr->stackaddr;
*stack_size = attr->stacksize;
return 0;
}
RTM_EXPORT(pthread_attr_getstack);
/**
* @brief This function shall set the guardsize attribute in the attr object.
*
* @note The guardsize attribute controls the size of the guard area for the created thread's stack.
* The guardsize attribute provides protection against overflow of the stack pointer.
* If a thread's stack is created with guard protection, the implementation allocates extra
* memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer.
* If an application overflows into this buffer an error shall result (possibly in a SIGSEGV signal
* being delivered to the thread).
*
* @see <pthread.h>, <sys/mman.h>
*
* @param attr is a thread attributes object.
*
* @param guard_size is the size of the guard area for the created thread's stack.
*
* @return Upon successful completion, these functions shall return a value of 0;
*
* @warning The guardsize attribute is provided to the application for two reasons:
*
* 1. Overflow protection can potentially result in wasted system resources.
* An application that creates a large number of threads, and which knows its threads
* never overflow their stack, can save system resources by turning off guard areas.
*
* 2. When threads allocate large data structures on the stack, large guard areas may be
* needed to detect stack overflow.
*
* The default size of the guard area is left implementation-defined since on systems
* supporting very large page sizes, the overhead might be substantial if at least one guard
* page is required by default.
*/
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size)
{
return EOPNOTSUPP;
}
/**
* @brief This function get the guardsize attribute in the attr object.
* This attribute shall be returned in the guard_size parameter.
*
* @note The guardsize attribute controls the size of the guard area for the created thread's stack.
* The guardsize attribute provides protection against overflow of the stack pointer.
* If a thread's stack is created with guard protection, the implementation allocates extra
* memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer.
*
* @see <pthread.h>, <sys/mman.h>
*
* @param attr is a thread attributes object.
*
* @param guard_size is the size of the guard area for the created thread's stack.
*
* @return Upon successful completion, these functions shall return a value of 0;
*
* @warning The guardsize attribute is provided to the application for two reasons:
*
* 1. Overflow protection can potentially result in wasted system resources.
* An application that creates a large number of threads, and which knows its threads
* never overflow their stack, can save system resources by turning off guard areas.
*
* 2. When threads allocate large data structures on the stack, large guard areas may be
* needed to detect stack overflow.
*
* The default size of the guard area is left implementation-defined since on systems
* supporting very large page sizes, the overhead might be substantial if at least one guard
* page is required by default.
*/
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
{
return EOPNOTSUPP;
}
RTM_EXPORT(pthread_attr_getguardsize);
/**
* @brief This function sets inherit-scheduler attribute in thread attributes object.
*
* @note The function sets the inherit-scheduler attribute of the thread attributes object
* referred to by attr to the value specified in inheritsched.
* The inherit-scheduler attribute determines whether a thread created using the thread
* attributes object attr will inherit its scheduling attributes from the calling thread
* or whether it will take them from attr.
*
* @see pthread_attr_init(), pthread_attr_setschedpolicy(), pthread_attr_setschedparam()
*
* @param attr is a thread attributes object.
*
* @param inheritsched the inheritsched attribute determines how the other scheduling attributes of the created thread are to be set:
* The policy can be ONE of the following values:
*
* PTHREAD_INHERIT_SCHED Specifies that the scheduling policy and associated attributes are
* to be inherited from the creating thread, and the scheduling attributes
* in this attr argument are to be ignored.
*
* PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be
* set to the corresponding values from this attribute object.
*
* @return Upon successful completion, these functions shall return a value of 0;
*/
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);
/**
* @brief This function get and set the inheritsched attribute in the attr argument.
*
* @note The function sets the inherit-scheduler attribute of the thread attributes object
* referred to by attr to the value specified in inheritsched.
* The inherit-scheduler attribute determines whether a thread created using the thread
* attributes object attr will inherit its scheduling attributes from the calling thread
* or whether it will take them from attr.
*
* @see pthread_attr_init(), pthread_attr_getschedpolicy(), pthread_attr_getschedparam()
*
* @param attr is a thread attributes object.
*
* @param inheritsched the inheritsched attribute determines how the other scheduling attributes of the created thread are to be set:
* The inheritsched can be ONE of the following values:
*
* PTHREAD_INHERIT_SCHED Specifies that the scheduling policy and associated attributes are
* to be inherited from the creating thread, and the scheduling attributes
* in this attr argument are to be ignored.
*
* PTHREAD_EXPLICIT_SCHED Specifies that the scheduling policy and associated attributes are to be
* set to the corresponding values from this attribute object.
*
* @return Upon successful completion, these functions shall return a value of 0;
*/
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);
/**
* @brief This function set contentionscope attribute.
*
* @note The function are used to set the contentionscope attribute in the attr object.
*
* @param attr is a thread attributes object.
*
* @param scope is the value of contentionscope attribute.
* The scope can be ONE of the following values:
*
* PTHREAD_SCOPE_SYSTEM signifying system scheduling contention scope.
*
* PTHREAD_SCOPE_PROCESS signifying process scheduling contention scope.
*
* @return Upon successful completion, these functions shall return a value of 0;
*/
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
{
if (scope == PTHREAD_SCOPE_SYSTEM)
return 0;
if (scope == PTHREAD_SCOPE_PROCESS)
return EOPNOTSUPP;
return EINVAL;
}
RTM_EXPORT(pthread_attr_setscope);
/**
* @brief This function get contentionscope attribute.
*
* @note The function are used to get the contentionscope attribute in the attr object.
*
* @param attr is a thread attributes object.
*
* @param scope is the value of contentionscope attribute.
* The scope can be ONE of the following values:
*
* PTHREAD_SCOPE_SYSTEM signifying system scheduling contention scope.
*
* PTHREAD_SCOPE_PROCESS signifying process scheduling contention scope.
*
* @return Upon successful completion, these functions shall return a value of 0;
*/
int pthread_attr_getscope(pthread_attr_t const *attr, int *scope)
{
return PTHREAD_SCOPE_SYSTEM;
}
RTM_EXPORT(pthread_attr_getscope);

View File

@@ -0,0 +1,112 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <pthread.h>
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
{
if (!attr)
return EINVAL;
return 0;
}
RTM_EXPORT(pthread_barrierattr_destroy);
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
{
if (!attr)
return EINVAL;
*attr = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_barrierattr_init);
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
int *pshared)
{
if (!attr)
return EINVAL;
*pshared = (int)*attr;
return 0;
}
RTM_EXPORT(pthread_barrierattr_getpshared);
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
{
if (!attr)
return EINVAL;
if (pshared == PTHREAD_PROCESS_PRIVATE)
attr = PTHREAD_PROCESS_PRIVATE;
return EINVAL;
}
RTM_EXPORT(pthread_barrierattr_setpshared);
int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
rt_err_t result;
if (!barrier)
return EINVAL;
result = pthread_cond_destroy(&(barrier->cond));
return result;
}
RTM_EXPORT(pthread_barrier_destroy);
int pthread_barrier_init(pthread_barrier_t *barrier,
const pthread_barrierattr_t *attr,
unsigned count)
{
if (!barrier)
return EINVAL;
if (attr && (*attr != PTHREAD_PROCESS_PRIVATE))
return EINVAL;
if (count == 0)
return EINVAL;
barrier->count = count;
pthread_cond_init(&(barrier->cond), NULL);
pthread_mutex_init(&(barrier->mutex), NULL);
return 0;
}
RTM_EXPORT(pthread_barrier_init);
int pthread_barrier_wait(pthread_barrier_t *barrier)
{
rt_err_t result;
if (!barrier)
return EINVAL;
result = pthread_mutex_lock(&(barrier->mutex));
if (result != 0)
return EINVAL;
if (barrier->count == 0)
result = EINVAL;
else
{
barrier->count -= 1;
if (barrier->count == 0) /* broadcast condition */
pthread_cond_broadcast(&(barrier->cond));
else
pthread_cond_wait(&(barrier->cond), &(barrier->mutex));
}
pthread_mutex_unlock(&(barrier->mutex));
return result;
}
RTM_EXPORT(pthread_barrier_wait);

View File

@@ -0,0 +1,372 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
* 2022-06-27 xiangxistu use atomic operation to protect pthread conditional variable
*/
#include <rthw.h>
#include <pthread.h>
#include "pthread_internal.h"
int pthread_condattr_destroy(pthread_condattr_t *attr)
{
if (!attr)
return EINVAL;
return 0;
}
RTM_EXPORT(pthread_condattr_destroy);
int pthread_condattr_init(pthread_condattr_t *attr)
{
if (!attr)
return EINVAL;
*attr = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_condattr_init);
int pthread_condattr_getclock(const pthread_condattr_t *attr,
clockid_t *clock_id)
{
return 0;
}
RTM_EXPORT(pthread_condattr_getclock);
int pthread_condattr_setclock(pthread_condattr_t *attr,
clockid_t clock_id)
{
return 0;
}
RTM_EXPORT(pthread_condattr_setclock);
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
{
if (!attr || !pshared)
return EINVAL;
*pshared = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_condattr_getpshared);
int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
{
if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
(pshared != PTHREAD_PROCESS_SHARED))
{
return EINVAL;
}
if (pshared != PTHREAD_PROCESS_PRIVATE)
return ENOSYS;
return 0;
}
RTM_EXPORT(pthread_condattr_setpshared);
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{
rt_err_t result;
char cond_name[RT_NAME_MAX];
static rt_uint16_t cond_num = 0;
/* parameter check */
if (cond == RT_NULL)
return EINVAL;
if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
return EINVAL;
rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
/* use default value */
if (attr == RT_NULL)
{
cond->attr = PTHREAD_PROCESS_PRIVATE;
}
else
{
cond->attr = *attr;
}
result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
if (result != RT_EOK)
{
return EINVAL;
}
/* detach the object from system object container */
rt_object_detach(&(cond->sem.parent.parent));
cond->sem.parent.parent.type = RT_Object_Class_Semaphore;
return 0;
}
RTM_EXPORT(pthread_cond_init);
int pthread_cond_destroy(pthread_cond_t *cond)
{
rt_err_t result;
if (cond == RT_NULL)
{
return EINVAL;
}
/* which is not initialized */
if (cond->attr == -1)
{
return 0;
}
if (!rt_list_isempty(&cond->sem.parent.suspend_thread))
{
return EBUSY;
}
__retry:
result = rt_sem_trytake(&(cond->sem));
if (result == EBUSY)
{
pthread_cond_broadcast(cond);
goto __retry;
}
/* clean condition */
rt_memset(cond, 0, sizeof(pthread_cond_t));
cond->attr = -1;
return 0;
}
RTM_EXPORT(pthread_cond_destroy);
int pthread_cond_broadcast(pthread_cond_t *cond)
{
rt_err_t result;
if (cond == RT_NULL)
return EINVAL;
if (cond->attr == -1)
pthread_cond_init(cond, RT_NULL);
while (1)
{
/* try to take condition semaphore */
result = rt_sem_trytake(&(cond->sem));
if (result == -RT_ETIMEOUT)
{
/* it's timeout, release this semaphore */
rt_sem_release(&(cond->sem));
}
else if (result == RT_EOK)
{
/* has taken this semaphore, release it */
rt_sem_release(&(cond->sem));
break;
}
else
{
return EINVAL;
}
}
return 0;
}
RTM_EXPORT(pthread_cond_broadcast);
int pthread_cond_signal(pthread_cond_t *cond)
{
rt_base_t temp;
rt_err_t result;
if (cond == RT_NULL)
return EINVAL;
if (cond->attr == -1)
pthread_cond_init(cond, RT_NULL);
/* disable interrupt */
temp = rt_hw_interrupt_disable();
if (rt_list_isempty(&cond->sem.parent.suspend_thread))
{
/* enable interrupt */
rt_hw_interrupt_enable(temp);
return 0;
}
else
{
/* enable interrupt */
rt_hw_interrupt_enable(temp);
result = rt_sem_release(&(cond->sem));
if (result == RT_EOK)
{
return 0;
}
return 0;
}
}
RTM_EXPORT(pthread_cond_signal);
rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
rt_int32_t timeout)
{
rt_err_t result = RT_EOK;
rt_sem_t sem;
rt_int32_t time;
sem = &(cond->sem);
if (sem == RT_NULL)
{
return -RT_ERROR;
}
time = timeout;
if (!cond || !mutex)
{
return -RT_ERROR;
}
/* check whether initialized */
if (cond->attr == -1)
{
pthread_cond_init(cond, RT_NULL);
}
/* The mutex was not owned by the current thread at the time of the call. */
if (mutex->lock.owner != rt_thread_self())
{
return -RT_ERROR;
}
{
register rt_base_t temp;
struct rt_thread *thread;
/* parameter check */
RT_ASSERT(sem != RT_NULL);
RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
/* disable interrupt */
temp = rt_hw_interrupt_disable();
if (sem->value > 0)
{
/* semaphore is available */
sem->value--;
/* enable interrupt */
rt_hw_interrupt_enable(temp);
}
else
{
/* no waiting, return with timeout */
if (time == 0)
{
rt_hw_interrupt_enable(temp);
return -RT_ETIMEOUT;
}
else
{
/* current context checking */
RT_DEBUG_IN_THREAD_CONTEXT;
/* semaphore is unavailable, push to suspend list */
/* get current thread */
thread = rt_thread_self();
/* reset thread error number */
thread->error = RT_EOK;
/* suspend thread */
rt_thread_suspend(thread);
/* Only support FIFO */
rt_list_insert_before(&(sem->parent.suspend_thread), &RT_THREAD_LIST_NODE(thread));
/**
rt_ipc_list_suspend(&(sem->parent.suspend_thread),
thread,
sem->parent.parent.flag);
*/
/* has waiting time, start thread timer */
if (time > 0)
{
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer),
RT_TIMER_CTRL_SET_TIME,
&time);
rt_timer_start(&(thread->thread_timer));
}
/* to avoid the lost of singal< cond->sem > */
if (pthread_mutex_unlock(mutex) != 0)
{
return -RT_ERROR;
}
/* enable interrupt */
rt_hw_interrupt_enable(temp);
/* do schedule */
rt_schedule();
result = thread->error;
/* lock mutex again */
pthread_mutex_lock(mutex);
}
}
}
return result;
}
RTM_EXPORT(_pthread_cond_timedwait);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
rt_err_t result;
__retry:
result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
return 0;
}
else if (result == -RT_EINTR)
{
/* https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html
* These functions shall not return an error code of [EINTR].
*/
goto __retry;
}
return EINVAL;
}
RTM_EXPORT(pthread_cond_wait);
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec *abstime)
{
int timeout;
rt_err_t result;
timeout = rt_timespec_to_tick(abstime);
result = _pthread_cond_timedwait(cond, mutex, timeout);
if (result == RT_EOK)
{
return 0;
}
if (result == -RT_ETIMEOUT)
{
return ETIMEDOUT;
}
return EINVAL;
}
RTM_EXPORT(pthread_cond_timedwait);

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#ifndef __PTHREAD_INTERNAL_H__
#define __PTHREAD_INTERNAL_H__
#include <rtthread.h>
#include <pthread.h>
#include <sys/time.h>
struct _pthread_cleanup
{
void (*cleanup_func)(void *parameter);
void *parameter;
struct _pthread_cleanup *next;
};
typedef struct _pthread_cleanup _pthread_cleanup_t;
struct _pthread_key_data
{
int is_used;
void (*destructor)(void *parameter);
};
typedef struct _pthread_key_data _pthread_key_data_t;
#ifndef PTHREAD_NUM_MAX
#define PTHREAD_NUM_MAX 32
#endif
#define PTHREAD_MAGIC 0x70746873
struct _pthread_data
{
rt_uint32_t magic;
pthread_attr_t attr;
rt_thread_t tid;
void* (*thread_entry)(void *parameter);
void *thread_parameter;
/* return value */
void *return_value;
/* semaphore for joinable thread */
rt_sem_t joinable_sem;
/* cancel state and type */
rt_uint8_t cancelstate;
volatile rt_uint8_t canceltype;
volatile rt_uint8_t canceled;
_pthread_cleanup_t *cleanup;
void** tls; /* thread-local storage area */
};
typedef struct _pthread_data _pthread_data_t;
_pthread_data_t *_pthread_get_data(pthread_t thread);
#endif

View File

@@ -0,0 +1,290 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <rtthread.h>
#include "pthread.h"
#define MUTEXATTR_SHARED_MASK 0x0010
#define MUTEXATTR_TYPE_MASK 0x000f
const pthread_mutexattr_t pthread_default_mutexattr = PTHREAD_PROCESS_PRIVATE;
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
if (attr)
{
*attr = pthread_default_mutexattr;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_init);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
{
if (attr)
{
*attr = -1;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_destroy);
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
{
if (attr && type)
{
int atype = (*attr & MUTEXATTR_TYPE_MASK);
if (atype >= PTHREAD_MUTEX_NORMAL && atype <= PTHREAD_MUTEX_ERRORCHECK)
{
*type = atype;
return 0;
}
}
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_gettype);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_ERRORCHECK)
{
*attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_settype);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
{
if (!attr)
return EINVAL;
switch (pshared)
{
case PTHREAD_PROCESS_PRIVATE:
*attr &= ~MUTEXATTR_SHARED_MASK;
return 0;
case PTHREAD_PROCESS_SHARED:
*attr |= MUTEXATTR_SHARED_MASK;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_mutexattr_setpshared);
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
{
if (!attr || !pshared)
return EINVAL;
*pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
: PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_mutexattr_getpshared);
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
rt_err_t result;
char name[RT_NAME_MAX];
static rt_uint16_t pthread_mutex_number = 0;
if (!mutex)
return EINVAL;
/* build mutex name */
rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++);
if (attr == RT_NULL)
mutex->attr = pthread_default_mutexattr;
else
mutex->attr = *attr;
/* init mutex lock */
result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_PRIO);
if (result != RT_EOK)
return EINVAL;
/* detach the object from system object container */
rt_object_detach(&(mutex->lock.parent.parent));
mutex->lock.parent.parent.type = RT_Object_Class_Mutex;
return 0;
}
RTM_EXPORT(pthread_mutex_init);
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
if (!mutex || mutex->attr == -1)
return EINVAL;
/* it's busy */
if (mutex->lock.owner != RT_NULL)
return EBUSY;
rt_memset(mutex, 0, sizeof(pthread_mutex_t));
mutex->attr = -1;
return 0;
}
RTM_EXPORT(pthread_mutex_destroy);
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
int mtype;
rt_err_t result;
if (!mutex)
return EINVAL;
if (mutex->attr == -1)
{
/* init mutex */
pthread_mutex_init(mutex, RT_NULL);
}
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
rt_enter_critical();
if (mutex->lock.owner == rt_thread_self() &&
mtype != PTHREAD_MUTEX_RECURSIVE)
{
rt_exit_critical();
return EDEADLK;
}
rt_exit_critical();
result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
if (result == RT_EOK)
return 0;
return EINVAL;
}
RTM_EXPORT(pthread_mutex_lock);
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
rt_err_t result;
if (!mutex)
return EINVAL;
if (mutex->attr == -1)
{
/* init mutex */
pthread_mutex_init(mutex, RT_NULL);
}
if (mutex->lock.owner != rt_thread_self())
{
int mtype;
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
/* error check, return EPERM */
if (mtype == PTHREAD_MUTEX_ERRORCHECK)
return EPERM;
/* no thread waiting on this mutex */
if (mutex->lock.owner == RT_NULL)
return 0;
}
result = rt_mutex_release(&(mutex->lock));
if (result == RT_EOK)
return 0;
return EINVAL;
}
RTM_EXPORT(pthread_mutex_unlock);
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
rt_err_t result;
int mtype;
if (!mutex)
return EINVAL;
if (mutex->attr == -1)
{
/* init mutex */
pthread_mutex_init(mutex, RT_NULL);
}
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
rt_enter_critical();
if (mutex->lock.owner == rt_thread_self() &&
mtype != PTHREAD_MUTEX_RECURSIVE)
{
rt_exit_critical();
return EDEADLK;
}
rt_exit_critical();
result = rt_mutex_take(&(mutex->lock), 0);
if (result == RT_EOK) return 0;
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

@@ -0,0 +1,340 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <pthread.h>
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
{
if (!attr)
return EINVAL;
*attr = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_rwlockattr_init);
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
{
if (!attr)
return EINVAL;
return 0;
}
RTM_EXPORT(pthread_rwlockattr_destroy);
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
int *pshared)
{
if (!attr || !pshared)
return EINVAL;
*pshared = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_rwlockattr_getpshared);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
{
if (!attr || pshared != PTHREAD_PROCESS_PRIVATE)
return EINVAL;
return 0;
}
RTM_EXPORT(pthread_rwlockattr_setpshared);
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
const pthread_rwlockattr_t *attr)
{
if (!rwlock)
return EINVAL;
rwlock->attr = PTHREAD_PROCESS_PRIVATE;
pthread_mutex_init(&(rwlock->rw_mutex), NULL);
pthread_cond_init(&(rwlock->rw_condreaders), NULL);
pthread_cond_init(&(rwlock->rw_condwriters), NULL);
rwlock->rw_nwaitwriters = 0;
rwlock->rw_nwaitreaders = 0;
rwlock->rw_refcount = 0;
return 0;
}
RTM_EXPORT(pthread_rwlock_init);
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
return 0; /* rwlock is not initialized */
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
if (rwlock->rw_refcount != 0 ||
rwlock->rw_nwaitreaders != 0 ||
rwlock->rw_nwaitwriters != 0)
{
result = EBUSY;
return result;
}
else
{
/* check whether busy */
result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
if (result == RT_EOK)
{
result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
if (result == RT_EOK)
{
rt_sem_release(&(rwlock->rw_condreaders.sem));
rt_sem_release(&(rwlock->rw_condwriters.sem));
pthread_cond_destroy(&rwlock->rw_condreaders);
pthread_cond_destroy(&rwlock->rw_condwriters);
}
else
{
rt_sem_release(&(rwlock->rw_condreaders.sem));
result = EBUSY;
}
}
else
result = EBUSY;
}
pthread_mutex_unlock(&rwlock->rw_mutex);
if (result == 0)
pthread_mutex_destroy(&rwlock->rw_mutex);
return result;
}
RTM_EXPORT(pthread_rwlock_destroy);
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
/* give preference to waiting writers */
while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
{
rwlock->rw_nwaitreaders++;
/* rw_mutex will be released when waiting for rw_condreaders */
result = pthread_cond_wait(&rwlock->rw_condreaders, &rwlock->rw_mutex);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitreaders--;
if (result != 0) /* wait error */
break;
}
/* another reader has a read lock */
if (result == 0)
rwlock->rw_refcount++;
pthread_mutex_unlock(&rwlock->rw_mutex);
return (result);
}
RTM_EXPORT(pthread_rwlock_rdlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
if (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
result = EBUSY; /* held by a writer or waiting writers */
else
rwlock->rw_refcount++; /* increment count of reader locks */
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_tryrdlock);
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
const struct timespec *abstime)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
/* give preference to waiting writers */
while (rwlock->rw_refcount < 0 || rwlock->rw_nwaitwriters > 0)
{
rwlock->rw_nwaitreaders++;
/* rw_mutex will be released when waiting for rw_condreaders */
result = pthread_cond_timedwait(&rwlock->rw_condreaders, &rwlock->rw_mutex, abstime);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitreaders--;
if (result != 0)
break;
}
/* another reader has a read lock */
if (result == 0)
rwlock->rw_refcount++;
pthread_mutex_unlock(&rwlock->rw_mutex);
return (result);
}
RTM_EXPORT(pthread_rwlock_timedrdlock);
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
const struct timespec *abstime)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
while (rwlock->rw_refcount != 0)
{
rwlock->rw_nwaitwriters++;
/* rw_mutex will be released when waiting for rw_condwriters */
result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitwriters--;
if (result != 0)
break;
}
if (result == 0)
rwlock->rw_refcount = -1;
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_timedwrlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
if (rwlock->rw_refcount != 0)
result = EBUSY; /* held by either writer or reader(s) */
else
rwlock->rw_refcount = -1; /* available, indicate a writer has it */
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_trywrlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
if (rwlock->rw_refcount > 0)
rwlock->rw_refcount--; /* releasing a reader */
else if (rwlock->rw_refcount == -1)
rwlock->rw_refcount = 0; /* releasing a writer */
/* give preference to waiting writers over waiting readers */
if (rwlock->rw_nwaitwriters > 0)
{
if (rwlock->rw_refcount == 0)
result = pthread_cond_signal(&rwlock->rw_condwriters);
}
else if (rwlock->rw_nwaitreaders > 0)
{
result = pthread_cond_broadcast(&rwlock->rw_condreaders);
}
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_unlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{
int result;
if (!rwlock)
return EINVAL;
if (rwlock->attr == -1)
pthread_rwlock_init(rwlock, NULL);
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
while (rwlock->rw_refcount != 0)
{
rwlock->rw_nwaitwriters++;
/* rw_mutex will be released when waiting for rw_condwriters */
result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitwriters--;
if (result != 0)
break;
}
if (result == 0)
rwlock->rw_refcount = -1;
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_wrlock);

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <pthread.h>
int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
{
if (!lock)
return EINVAL;
lock->lock = 0;
return 0;
}
int pthread_spin_destroy (pthread_spinlock_t *lock)
{
if (!lock)
return EINVAL;
return 0;
}
int pthread_spin_lock (pthread_spinlock_t *lock)
{
if (!lock)
return EINVAL;
while (!(lock->lock))
{
lock->lock = 1;
}
return 0;
}
int pthread_spin_trylock (pthread_spinlock_t *lock)
{
if (!lock)
return EINVAL;
if (!(lock->lock))
{
lock->lock = 1;
return 0;
}
return EBUSY;
}
int pthread_spin_unlock (pthread_spinlock_t *lock)
{
if (!lock)
return EINVAL;
if (!(lock->lock))
return EPERM;
lock->lock = 0;
return 0;
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-10-26 Bernard the first version
*/
#include <pthread.h>
#include "pthread_internal.h"
_pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
/* 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)
{
struct _pthread_data* ptd;
if (rt_thread_self() == NULL) return NULL;
/* get pthread data from user data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != NULL);
if (ptd->tls == NULL)
return NULL;
if ((key < PTHREAD_KEY_MAX) && (_thread_keys[key].is_used))
return ptd->tls[key];
return NULL;
}
RTM_EXPORT(pthread_getspecific);
int pthread_setspecific(pthread_key_t key, const void *value)
{
struct _pthread_data* ptd;
if (rt_thread_self() == NULL) return EINVAL;
/* get pthread data from user data of thread */
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
RT_ASSERT(ptd != NULL);
/* check tls area */
if (ptd->tls == NULL)
{
ptd->tls = (void**)rt_malloc(sizeof(void*) * PTHREAD_KEY_MAX);
}
if ((key < PTHREAD_KEY_MAX) && _thread_keys[key].is_used)
{
ptd->tls[key] = (void *)value;
return 0;
}
return EINVAL;
}
RTM_EXPORT(pthread_setspecific);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
{
rt_uint32_t index;
rt_enter_critical();
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
{
if (_thread_keys[index].is_used == 0)
{
_thread_keys[index].is_used = 1;
_thread_keys[index].destructor = destructor;
*key = index;
rt_exit_critical();
return 0;
}
}
rt_exit_critical();
return EAGAIN;
}
RTM_EXPORT(pthread_key_create);
int pthread_key_delete(pthread_key_t key)
{
if (key >= PTHREAD_KEY_MAX)
return EINVAL;
rt_enter_critical();
_thread_keys[key].is_used = 0;
_thread_keys[key].destructor = 0;
rt_exit_critical();
return 0;
}
RTM_EXPORT(pthread_key_delete);

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <sched.h>
int sched_yield(void)
{
rt_thread_yield();
return 0;
}
RTM_EXPORT(sched_yield);
int sched_get_priority_min(int policy)
{
if (policy != SCHED_FIFO && policy != SCHED_RR)
return EINVAL;
return 0;
}
RTM_EXPORT(sched_get_priority_min);
int sched_get_priority_max(int policy)
{
if (policy != SCHED_FIFO && policy != SCHED_RR)
return EINVAL;
return RT_THREAD_PRIORITY_MAX - 1;
}
RTM_EXPORT(sched_get_priority_max);
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

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#ifndef __SCHED_H__
#define __SCHED_H__
#include <rtthread.h>
#include <pthread.h>
/* Thread scheduling policies */
enum
{
SCHED_OTHER = 0,
SCHED_FIFO,
SCHED_RR,
SCHED_MIN = SCHED_OTHER,
SCHED_MAX = SCHED_RR
};
#ifdef __cplusplus
extern "C"
{
#endif
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
}
#endif
#endif

View File

@@ -0,0 +1,11 @@
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) , use:
- `#include <sys/time.h>` to instead of `#include <time.h>`
- `#include <sys/errno.h>` to instead of `#include <errno.h>`
- `#include <sys/signal.h>` to instead of `#include <signal.h>`

View File

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

View File

@@ -0,0 +1,248 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/1 Bernard The first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <sys/time.h>
#include <sys/errno.h>
#include "posix_signal.h"
#define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
void (*signal(int sig, void (*func)(int))) (int)
{
return rt_signal_install(sig, func);
}
/**
* @brief This function will examines, changes, or examines and changes the signal mask of the calling thread.
*
* @param how indicates the way in which the existing set of blocked signals should be changed.
* The following are the possible values for option:
*
* SIG_BLOCK The set of blocked signals is the union of the current set and the set argument.
*
* SIG_UNBLOCK The signals in set are removed from the current set of blocked signals.
* It is permissible to attempt to unblock a signal which is not blocked.
*
* SIG_SETMASK The set of blocked signals is set to the argument set.
*
* @param set is a pointer to a sigset_t object that specifies the new set of blocked signals.
* If set is NULL, then the signal mask is unchanged (i.e., how is ignored)
*
* @param oset is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
* If oset is non-NULL, the previous value of the signal mask is stored in it.
*
* @return Returns 0 on success.
*/
int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
{
rt_base_t level;
rt_thread_t tid;
tid = rt_thread_self();
level = rt_hw_interrupt_disable();
if (oset) *oset = tid->sig_mask;
if (set)
{
switch(how)
{
case SIG_BLOCK:
tid->sig_mask |= *set;
break;
case SIG_UNBLOCK:
tid->sig_mask &= ~*set;
break;
case SIG_SETMASK:
tid->sig_mask = *set;
break;
default:
break;
}
}
rt_hw_interrupt_enable(level);
return 0;
}
/**
* @brief This function will examines the signal mask of the calling thread.
*
* @param set is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
* If set is non-NULL, the previous value of the signal mask is stored in it.
*
* @return Returns 0 on success.
*/
int sigpending (sigset_t *set)
{
sigprocmask(SIG_SETMASK, RT_NULL, set);
return 0;
}
/**
* @brief This function will temporarily replace the signal mask of the calling thread
* with the mask given and then suspends the thread until delivery of an expected signal
* or a signal whose action is to terminate a process.
*
* @param set is a pointer of a sigset_t object that is used to replace the original mask of the calling thread.
*
* @return Returns 0 on success.
* If the return value is any other values, it means that the signal wait failed.
*/
int sigsuspend (const sigset_t *set)
{
int ret = 0;
sigset_t origin_set;
sigset_t suspend_set;
siginfo_t info; /* unless paremeter */
/* get the origin signal information */
sigpending(&origin_set);
/* set the new signal information */
sigprocmask(SIG_BLOCK, set, RT_NULL);
sigpending(&suspend_set);
ret = rt_signal_wait(&suspend_set, &info, RT_WAITING_FOREVER);
/* restore the original sigprocmask */
sigprocmask(SIG_UNBLOCK, (sigset_t *)0xffffUL, RT_NULL);
sigprocmask(SIG_BLOCK, &origin_set, RT_NULL);
return ret;
}
/**
* @brief This function will install or confirm action for specified signal.
*
* @param signum is the signal to be handled.
*
* @param act is the new signal action, or NULL to restore default action.
*
* @param oldact returns the previous signal action, or NULL if not required.
*
* @return Returns 0 on success or -1 on failure.
*/
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
rt_sighandler_t old = RT_NULL;
if (!sig_valid(signum)) return -RT_ERROR;
if (act)
old = rt_signal_install(signum, act->sa_handler);
else
{
old = rt_signal_install(signum, RT_NULL);
rt_signal_install(signum, old);
}
if (oldact)
oldact->sa_handler = old;
return 0;
}
/**
* @brief This function will suspends execution of the calling thread until one of
* the signals in the given set is pending. If none of the signals specified
* are pending, it will wait for the specified time interval.
*
* @param set is the set of signal values to be waited for.
*
* @param info is a pointer to the received signal info.
*
* @param timeout is a pointer to a timespec structure that specifys the waiting time.
*
* @return Return 0 on success. Otherwise, return -1 and set errno to indicate the error.
*/
int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
{
int ret = 0;
int tick = RT_WAITING_FOREVER;
if (timeout)
{
tick = timeout->tv_sec * RT_TICK_PER_SECOND + timeout->tv_nsec * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
}
ret = rt_signal_wait(set, info, tick);
if (ret == 0) return 0;
errno = ret;
return -1;
}
/**
* @brief This function will suspend execution of the calling thread until one of
* the specified signal becomes pending and return the signal number.
*
* @param set is the set of signal values to be waited for.
*
* @param sig is a pointer to the received signal number.
*
* @return Return 0 on success or -1 on failure.
*/
int sigwait(const sigset_t *set, int *sig)
{
siginfo_t si;
if (sigtimedwait(set, &si, 0) < 0)
return -1;
*sig = si.si_signo;
return 0;
}
/**
* @brief This function will suspend execution of the calling thread until one of
* the specified signal is pending.
*
* @param set is the set of signal values to be waited for.
*
* @param info is a pointer to the received signal info.
*
* @return Return 0 on success or -1 on failure.
*/
int sigwaitinfo(const sigset_t *set, siginfo_t *info)
{
return sigtimedwait(set, info, NULL);
}
/**
* @brief This function willsend a signal to the caller
*
* @param sig is the signal that is to be sent.
*
* @return Returns 0 on success.
*/
int raise(int sig)
{
rt_thread_kill(rt_thread_self(), sig);
return 0;
}
#include <sys/types.h>
/**
* @brief Sends a signal to the caller.
*
* This function sends the signal specified by @p sig to the caller.
*
* @param sig The signal to be sent.
* This should be one of the standard signal macros such as SIGUSR1, SIGUSR2, etc.
*
* @return Returns 0 on success. If an error occurs, -1 is returned and errno is set appropriately.
*/
int sigqueue (pid_t pid, int signo, const union sigval value)
{
/* no support, signal queue */
return -1;
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017/10/1 Bernard The first version
*/
#ifndef POSIX_SIGNAL_H__
#define POSIX_SIGNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/signal.h>
enum rt_signal_value{
SIG1 = SIGHUP, // Hangup detected on controlling terminal or death of controlling process
SIG2 = SIGINT, // Interrupt from keyboard
SIG3 = SIGQUIT, // Quit from keyboard
SIG4 = SIGILL, // Illegal instruction
SIG5 = SIGTRAP, // Trace trap
SIG6 = SIGABRT, // Abort signal from abort(3)
SIG7 = SIGEMT, // Emulator trap
SIG8 = SIGFPE, // Floating-point exception
SIG9 = SIGKILL, // Kill signal
SIG10 = SIGBUS, // Bus error
SIG11 = SIGSEGV, // Segmentation fault
SIG12 = SIGSYS, // Bad system call
SIG13 = SIGPIPE, // Broken pipe
SIG14 = SIGALRM, // Timer signal from alarm(2)
SIG15 = SIGTERM, // Termination signal
SIG16 = SIGURG, // Urgent condition on socket
SIG17 = SIGSTOP, // Stop executing (cannot be caught or ignored)
SIG18 = SIGTSTP, // Stop signal from keyboard to suspend execution
SIG19 = SIGCONT, // Continue if stopped
SIG20 = SIGCHLD, // Child status has changed
SIG21 = SIGTTIN, // Background read from control terminal attempted by background process
SIG22 = SIGTTOU, // Background write to control terminal attempted by background process
SIG23 = SIGPOLL, // Pollable event
SIG24 = 24, // SIGXCPU: CPU time limit exceeded
SIG25 = 25, // SIGXFSZ: File size limit exceeded
SIG26 = 26, // SIGVTALRM: Virtual timer expired
SIG27 = 27, // SIGPROF: Profiling timer expired
SIG28 = SIGWINCH,// Window size changed
SIG29 = 29, // SIGLOST
SIG30 = SIGUSR1, // User-defined signal 1
SIG31 = SIGUSR2, // User-defined signal 2
SIGRT_MIN = 27, // SIGRTMIN: Minimum real-time signal number
SIGRT_MAX = 31, // SIGRTMAX: Maximum real-time signal number
SIGMAX = NSIG // Number of signals (total)
};
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,9 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('POSIX', src, depend = ['RT_USING_PTHREADS'], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,221 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-04-27 peterfan Add copyright header.
* 2024-04-15 atwww Add emutls_get_pthread_key to make c++11's thread_local destructe safely.
* Use emutls_pthread_key to determine whether C++11 thread_local is used.
* Move this file from components\libc\cplusplus\cpp11 to components\libc\posix\tls,
* because _Thread_local in C will also use emutls.
*/
/* ===---------- emutls.c - Implements __emutls_get_address ---------------===
*
* The LLVM Compiler Infrastructure
*
* This file is dual licensed under the MIT and the University of Illinois Open
* Source Licenses. See LICENSE.TXT for details.
*
* ===----------------------------------------------------------------------===
*/
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
extern int pthread_key_delete(pthread_key_t key);
extern void *pthread_getspecific(pthread_key_t key);
extern int pthread_setspecific(pthread_key_t key, const void *value);
/* Default is not to use posix_memalign, so systems like Android
* can use thread local data without heavier POSIX memory allocators.
*/
#ifndef EMUTLS_USE_POSIX_MEMALIGN
#define EMUTLS_USE_POSIX_MEMALIGN 0
#endif
/* For every TLS variable xyz,
* there is one __emutls_control variable named __emutls_v.xyz.
* If xyz has non-zero initial value, __emutls_v.xyz's "value"
* will point to __emutls_t.xyz, which has the initial value.
*/
typedef struct __emutls_control
{
size_t size; /* size of the object in bytes */
size_t align; /* alignment of the object in bytes */
union
{
uintptr_t index; /* data[index-1] is the object address */
void *address; /* object address, when in single thread env */
} object;
void *value; /* null or non-zero initial value for the object */
} __emutls_control;
static __inline void *emutls_memalign_alloc(size_t align, size_t size)
{
void *base;
#if EMUTLS_USE_POSIX_MEMALIGN
if (posix_memalign(&base, align, size) != 0)
abort();
#else
#define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void *))
char *object;
if ((object = (char *)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
abort();
base = (void *)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES)) & ~(uintptr_t)(align - 1));
((void **)base)[-1] = object;
#endif
return base;
}
static __inline void emutls_memalign_free(void *base)
{
#if EMUTLS_USE_POSIX_MEMALIGN
free(base);
#else
/* The mallocated address is in ((void**)base)[-1] */
free(((void **)base)[-1]);
#endif
}
/* Emulated TLS objects are always allocated at run-time. */
static __inline void *emutls_allocate_object(__emutls_control *control)
{
size_t size = control->size;
size_t align = control->align;
if (align < sizeof(void *))
align = sizeof(void *);
/* Make sure that align is power of 2. */
if ((align & (align - 1)) != 0)
abort();
void *base = emutls_memalign_alloc(align, size);
if (control->value)
memcpy(base, control->value, size);
else
memset(base, 0, size);
return base;
}
static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
static size_t emutls_num_object = 0; /* number of allocated TLS objects */
typedef struct emutls_address_array
{
uintptr_t size; /* number of elements in the 'data' array */
void *data[];
} emutls_address_array;
static pthread_key_t emutls_pthread_key = -1; /* -1 means that TLS in C or C++ is not used. */
pthread_key_t emutls_get_pthread_key(void)
{
/* If program uses C or C++ TLS keyword, _Thread_local、__thread or thread_local,
* the function emutls_get_index will ensure that emutls_pthread_key is initialized once
* when it is first used. Therefore, there is no need to use synchronization measures here.
*/
return emutls_pthread_key;
}
static void emutls_key_destructor(void *ptr)
{
emutls_address_array *array = (emutls_address_array *)ptr;
uintptr_t i;
for (i = 0; i < array->size; ++i)
{
if (array->data[i])
emutls_memalign_free(array->data[i]);
}
free(ptr);
}
static void emutls_init(void)
{
if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
abort();
}
/* Returns control->object.index; set index if not allocated yet. */
static __inline uintptr_t emutls_get_index(__emutls_control *control)
{
uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
if (!index)
{
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, emutls_init);
pthread_mutex_lock(&emutls_mutex);
index = control->object.index;
if (!index)
{
index = ++emutls_num_object;
__atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
}
pthread_mutex_unlock(&emutls_mutex);
}
return index;
}
/* Updates newly allocated thread local emutls_address_array. */
static __inline void emutls_check_array_set_size(emutls_address_array *array,
uintptr_t size)
{
if (array == NULL)
abort();
array->size = size;
pthread_setspecific(emutls_pthread_key, (void *)array);
}
/* Returns the new 'data' array size, number of elements,
* which must be no smaller than the given index.
*/
static __inline uintptr_t emutls_new_data_array_size(uintptr_t index)
{
/* Need to allocate emutls_address_array with one extra slot
* to store the data array size.
* Round up the emutls_address_array size to multiple of 16.
*/
return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
}
/* Returns the thread local emutls_address_array.
* Extends its size if necessary to hold address at index.
*/
static __inline emutls_address_array *
emutls_get_address_array(uintptr_t index)
{
emutls_address_array *array = pthread_getspecific(emutls_pthread_key);
if (array == NULL)
{
uintptr_t new_size = emutls_new_data_array_size(index);
array = (emutls_address_array *)calloc(new_size + 1, sizeof(void *));
emutls_check_array_set_size(array, new_size);
}
else if (index > array->size)
{
uintptr_t orig_size = array->size;
uintptr_t new_size = emutls_new_data_array_size(index);
array = (emutls_address_array *)realloc(array, (new_size + 1) * sizeof(void *));
if (array)
memset(array->data + orig_size, 0,
(new_size - orig_size) * sizeof(void *));
emutls_check_array_set_size(array, new_size);
}
return array;
}
void *__emutls_get_address(void *control)
{
uintptr_t index = emutls_get_index((__emutls_control *)control);
emutls_address_array *array = emutls_get_address_array(index);
if (array->data[index - 1] == NULL)
array->data[index - 1] = emutls_allocate_object((__emutls_control *)control);
return array->data[index - 1];
}