[posix][dfs] move dfs_posix dfs_poll dfs_select.c to posix folder

This commit is contained in:
Meco Man
2021-10-21 19:05:53 -04:00
parent bcdd4a6256
commit f7548b934e
28 changed files with 183 additions and 217 deletions
+7 -5
View File
@@ -2,6 +2,8 @@ menu "POSIX layer and C standard library"
config RT_USING_LIBC
bool "Enable libc APIs from toolchain"
select RT_USING_DFS
select RT_USING_POSIX
default y
config RT_USING_PTHREADS
@@ -14,9 +16,9 @@ if RT_USING_PTHREADS
default 8
endif
if RT_USING_LIBC && RT_USING_DFS
if RT_USING_DFS
config RT_USING_POSIX
bool "Enable POSIX layer for compatibility with UNIX APIs, poll/select etc"
bool "Enable POSIX layer, open/read/write/select etc"
select RT_USING_DFS_DEVFS
default y
@@ -49,9 +51,9 @@ if RT_USING_LIBC
default n
if RT_USING_MODULE
config RT_USING_CUSTOM_DLMODULE
bool "Enable load dynamic module by custom"
default n
config RT_USING_CUSTOM_DLMODULE
bool "Enable load dynamic module by custom"
default n
endif
endif
+2 -5
View File
@@ -6,18 +6,15 @@ src = []
cwd = GetCurrentDir()
group = []
CPPPATH = [cwd]
CPPDEFINES = []
if GetDepend('RT_USING_LIBC'):
src += Glob('*.c')
if GetDepend('RT_USING_POSIX') == False:
SrcRemove(src, ['unistd.c', 'delay.c'])
elif GetDepend('RT_LIBC_USING_TIME'):
src += ['time.c']
if rtconfig.CROSS_TOOL == 'keil':
CPPDEFINES = ['__CLK_TCK=RT_TICK_PER_SECOND']
else:
CPPDEFINES = []
CPPDEFINES += ['__CLK_TCK=RT_TICK_PER_SECOND']
group = DefineGroup('libc', src, depend = [], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
+1 -10
View File
@@ -1,10 +1 @@
## Attentions
1. This folder is "common" for all toolchains.
2. If you want to add new `.c` files, please do not forget to fix SConscript file too. eg:
```python
if GetDepend('RT_USING_POSIX') == False:
SrcRemove(src, ['unistd.c'])
```
This folder is "common" for all toolchains.
-119
View File
@@ -1,119 +0,0 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-09-01 Meco Man first Version
* 2021-02-12 Meco Man move all functions located in <pthread_sleep.c> to this file
*/
#include <unistd.h>
#include <rtthread.h>
#include <rthw.h>
#ifdef RT_USING_POSIX_TERMIOS
#include "termios.h"
int isatty(int fd)
{
struct termios ts;
return(tcgetattr(fd, &ts) != -1); /*true if no error (is a tty)*/
}
#else
int isatty(int fd)
{
if (fd >=0 && fd < 3)
{
return 1;
}
else
{
return 0;
}
}
#endif
RTM_EXPORT(isatty);
char *ttyname(int fd)
{
return "/dev/tty"; /* TODO: need to add more specific */
}
RTM_EXPORT(ttyname);
unsigned int sleep(unsigned int seconds)
{
if (rt_thread_self() != RT_NULL)
{
rt_thread_delay(seconds * RT_TICK_PER_SECOND);
}
else /* scheduler has not run yet */
{
while(seconds > 0)
{
rt_hw_us_delay(1000000u);
seconds --;
}
}
return 0;
}
RTM_EXPORT(sleep);
int usleep(useconds_t usec)
{
if (rt_thread_self() != RT_NULL)
{
rt_thread_mdelay(usec / 1000u);
}
else /* scheduler has not run yet */
{
rt_hw_us_delay(usec / 1000u);
}
rt_hw_us_delay(usec % 1000u);
return 0;
}
RTM_EXPORT(usleep);
pid_t gettid(void)
{
/*TODO*/
return 0;
}
pid_t getpid(void)
{
return gettid();
}
RTM_EXPORT(getpid);
pid_t getppid(void)
{
return 0;
}
RTM_EXPORT(getppid);
uid_t getuid(void)
{
return 0; /*ROOT*/
}
RTM_EXPORT(getuid);
uid_t geteuid(void)
{
return 0; /*ROOT*/
}
RTM_EXPORT(geteuid);
gid_t getgid(void)
{
return 0; /*ROOT*/
}
RTM_EXPORT(getgid);
gid_t getegid(void)
{
return 0; /*ROOT*/
}
RTM_EXPORT(getegid);
+2 -2
View File
@@ -3,9 +3,9 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('libc', src, depend = ['RT_USING_POSIX', 'RT_USING_POSIX_AIO'], CPPPATH = CPPPATH)
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX', 'RT_USING_POSIX_AIO'], CPPPATH = CPPPATH)
Return('group')
+1 -1
View File
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
group = DefineGroup('POSIX', src,
depend = ['RT_USING_LIBC', 'RT_USING_POSIX','RT_USING_POSIX_GETLINE'],
CPPPATH = CPPPATH)
+1 -1
View File
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
group = DefineGroup('POSIX', src,
depend = ['RT_USING_DFS', 'RT_USING_POSIX_MMAP'],
CPPPATH = CPPPATH)
+1 -1
View File
@@ -16,7 +16,7 @@ try:
except :
pass
group = DefineGroup('libc', src,
group = DefineGroup('POSIX', src,
depend = ['RT_USING_PTHREADS', 'RT_USING_LIBC'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
Return('group')
+1 -1
View File
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
group = DefineGroup('POSIX', src,
depend = ['RT_USING_SIGNALS', 'RT_USING_PTHREADS', 'RT_USING_LIBC'],
CPPPATH = CPPPATH)
+10
View File
@@ -0,0 +1,10 @@
# RT-Thread building script for component
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX'])
Return('group')
@@ -9,6 +9,15 @@
*/
#include <rtthread.h>
#include <rthw.h>
#define DBG_TAG "POSIX.delay"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
RT_WEAK void rt_hw_us_delay(rt_uint32_t us)
{
(void) us;
LOG_W("Please consider implementing rt_hw_us_delay() in another file.");
}
void msleep(unsigned int msecs)
{
+230
View File
@@ -0,0 +1,230 @@
/*
* 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.
*/
#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>
#include <dfs_file.h>
#include <poll.h>
#ifdef RT_USING_POSIX
struct rt_poll_node;
struct rt_poll_table
{
rt_pollreq_t req;
rt_uint32_t triggered; /* the waited thread whether triggered */
rt_thread_t polling_thread;
struct rt_poll_node *nodes;
};
struct rt_poll_node
{
struct rt_wqueue_node wqn;
struct rt_poll_table *pt;
struct rt_poll_node *next;
};
static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
{
struct rt_poll_node *pn;
if (key && !((rt_ubase_t)key & wait->key))
return -1;
pn = rt_container_of(wait, struct rt_poll_node, wqn);
pn->pt->triggered = 1;
return __wqueue_default_wake(wait, key);
}
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);
}
static void poll_table_init(struct rt_poll_table *pt)
{
pt->req._proc = _poll_add;
pt->triggered = 0;
pt->nodes = RT_NULL;
pt->polling_thread = rt_thread_self();
}
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_hw_interrupt_disable();
if (timeout != 0 && !pt->triggered)
{
rt_thread_suspend(thread);
if (timeout > 0)
{
rt_timer_control(&(thread->thread_timer),
RT_TIMER_CTRL_SET_TIME,
&timeout);
rt_timer_start(&(thread->thread_timer));
}
rt_hw_interrupt_enable(level);
rt_schedule();
level = rt_hw_interrupt_disable();
}
ret = !pt->triggered;
rt_hw_interrupt_enable(level);
return ret;
}
static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
{
int mask = 0;
int fd;
fd = pollfd->fd;
if (fd >= 0)
{
struct dfs_fd *f = fd_get(fd);
mask = POLLNVAL;
if (f)
{
mask = POLLMASK_DEFAULT;
if (f->fops->poll)
{
req->_key = pollfd->events | POLLERR | POLLHUP;
mask = f->fops->poll(f, req);
/* dealwith the device return error -1*/
if (mask < 0)
{
fd_put(f);
pollfd->revents = 0;
return mask;
}
}
/* Mask out unneeded events. */
mask &= pollfd->events | POLLERR | POLLHUP;
fd_put(f);
}
}
pollfd->revents = mask;
return mask;
}
static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
{
int num;
int istimeout = 0;
int n;
struct pollfd *pf;
int ret = 0;
if (msec == 0)
{
pt->req._proc = RT_NULL;
istimeout = 1;
}
while (1)
{
pf = fds;
num = 0;
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;
if (poll_wait_timeout(pt, msec))
istimeout = 1;
}
return num;
}
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);
}
}
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;
}
#endif
+180
View File
@@ -0,0 +1,180 @@
/*
* 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
*/
#include <rtthread.h>
#include <poll.h>
#include <sys/select.h>
#ifdef RT_USING_POSIX
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 ++;
}
}
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 = timeout->tv_sec * 1000 + 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;
}
#endif
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('libc', src,
group = DefineGroup('POSIX', src,
depend = ['RT_USING_LIBC', 'RT_USING_POSIX', 'RT_USING_POSIX_TERMIOS'],
CPPPATH = CPPPATH)