添加rtthread相关代码

This commit is contained in:
2025-06-13 14:38:32 +08:00
parent caf2d9f0c5
commit 160f9f8201
1809 changed files with 648100 additions and 10 deletions

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,281 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <dfs_file.h>
#include <unistd.h>
#include "mqueue.h"
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);
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);
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);
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);
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);
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);
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);
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);
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,393 @@
/*
* 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);
rt_inline void posix_sem_insert(sem_t *psem)
{
psem->next = posix_sem_list;
posix_sem_list = psem;
}
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 ;
}
}
}
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;
}
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);
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);
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);
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);
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);
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);
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);
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);
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);
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