Commit Graph

315 Commits

Author SHA1 Message Date
ligd fc50e57a26 pthread_mutx: remove unused critical_secton lock
Caused the 'nlocks' are remove from mutex, so
here don't need do critical_secton lock like nxmutex_lock

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-15 17:32:33 +08:00
ligd ff1b54bff3 pthread_cleanup: move clenup down to tls
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-14 20:15:12 +08:00
ligd 6a2c03732f clock: Replace all ts and tick conversion functions
Using the ts/tick conversion functions provided in clock.h

Do this caused we want speed up the time calculation, so change:
clock_time2ticks, clock_ticks2time, clock_timespec_add,
clock_timespec_compare, clock_timespec_subtract... to MACRO

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-12 18:14:39 +08:00
Alin Jerpelea eb9030c891 sched: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-09-12 01:10:14 +08:00
makejian 3240540952 pthread/realtime: export interfaces about pthread ceiling priority
pthread_mutex_setprioceiling and pthread_mutex_getprioceiling refers
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_setprioceiling.html
Signed-off-by: makejian <makejian@xiaomi.com>
2024-09-10 01:26:31 +08:00
makejian 85f8677e21 nxmutex: export priority ceiling interfaces with nxsem
Signed-off-by: makejian <makejian@xiaomi.com>
2024-09-10 01:26:31 +08:00
yinshengkai d7f02a8cb6 sched: change pthread_mutex implementation from sem to mutex
Since pthread_mutex is implemented by sem, it is impossible to see in ps who holds the lock and causes the wait.
Replace sem with mutex implementation to solve the above problems

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-09-06 09:42:53 +08:00
yinshengkai 2fd3981a8e pthread: remove pshared parameter from pthread_mutex_init
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-09-06 09:42:53 +08:00
yinshengkai 4feb418e36 pthread: remove the code which save and restore mutex state in pthread_condwait
type and flags are only initialized in mutex_init and should not change in the middle

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-09-06 09:42:53 +08:00
chao an cd169e9b2c sched/pthread: simplify pthread_create() branch logic
remove redundant branch logic

Signed-off-by: chao an <anchao@lixiang.com>
2024-06-07 01:32:42 +08:00
Yanfeng Liu d019828d3c sched/pthread: rename pthread_initialize.c as pthread_sem.c
This is to better match the contents of the source file.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-22 19:06:38 +08:00
Yanfeng Liu 7ec304e6a8 pthread/join: catch null pexit_value case
This adds handling for null pointer in errout route as it crashes my
ostest sometimes.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-05-10 19:42:13 +08:00
chao an 09e5dca965 sched/pthread: detached thread should destroy the join info
In order to ensure the detached thread obtain the correct return
value from pthread_join()/pthread_cancel(), the detached thread
will create joininfo to save the detached status after thread
destroyed.  If there are too many of detached threads in the
process group, the joininfo will consume too much memory.
This is not friendly to embedded MCU devices.
This commit keep the semantics as #11898 was introduced,
will no longer save joininfo for detached threads to avoid wasting memory.

Signed-off-by: chao an <anchao@lixiang.com>
2024-04-09 13:46:51 +08:00
chao an 306c1c0b7d sched/tasklist: replace task status list with macro definition
replace to macro will help to extend the scheduling implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 11:23:46 +09:00
chao an df30a1f8d3 sched/pthread/join: refactor pthread join to support join task
1. add support to join main task

| static pthread_t self;
|
| static void *join_task(void *arg)
| {
|   int ret;
|   ret = pthread_join(self, NULL);          <---  /* Fix Task could not be joined */
|   return NULL;
| }
|
| int main(int argc, char *argv[])
| {
|   pthread_t thread;
|
|   self = pthread_self();
|
|   pthread_create(&thread, NULL, join_task, NULL);
|   sleep(1);
|
|   pthread_exit(NULL);
|   return 0;
| }

2. Detach active thread will not alloc for additional join, just update the task flag.
3. Remove the return value waiting lock logic (data_sem),
   the return value will be stored in the waiting tcb.
4. Revise the return value of pthread_join(), consistent with linux
   e.g:
       Joining a detached and canceled thread should return EINVAL, not ESRCH

   https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html

   [EINVAL]
      The value specified by thread does not refer to a joinable thread.

NOTE:

This PR will not increase stack usage, but struct tcb_s will increase 32 bytes.

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-13 18:06:56 +09:00
chao an be482cd830 sched/pthread: replace pthread_sem_give() to nxsem_post() to unify the post method
replace pthread_sem_give() to nxsem_post() to unify the post method

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-11 22:57:26 +08:00
chao an 68c21df444 sched/pthread/join: remove unused joininfo
remove unused joininfo, minor issue found from code reading

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-11 22:05:33 +08:00
chao an 29e50ffa73 sched/group: move task group into task_tcb_s to improve performance
move task group into task_tcb_s to avoid access allocator to improve performance

for Task Termination, the time consumption will be reduced ~2us (Tricore TC397 300MHZ):
15.97(us) -> 13.55(us)

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-10 11:45:46 -03:00
chao an 89bd6ab74a sched/pthread: fix memory leak of pthread_tcb_s
pthread tcb should be released appropriately

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-06 20:37:06 +08:00
Ville Juven 0dedbcd4ae task/pthread_cancelpt: Move cancel point handling to libc, data to TLS
This moves task / thread cancel point logic from the NuttX kernel into
libc, while the data needed by the cancel point logic is moved to TLS.

The change is an enabler to move user-space APIs to libc as well, for
a coherent user/kernel separation.
2023-11-15 08:52:04 -08:00
chao an 87d6084f31 sched: fix the minor style issue
and remove the unused return value

Signed-off-by: chao an <anchao@xiaomi.com>
2023-10-25 15:46:45 +08:00
chao an cdec5c80c2 sched/pthread/barrierwait: replace syscall(2) to kernel api
syscall(2) cannot be called from kernel space

Signed-off-by: chao an <anchao@xiaomi.com>
2023-10-25 09:18:55 +08:00
xuxin19 d93d377257 cmake:complete missing changes during cmake reforming for sched
Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
2023-09-08 21:20:16 +03:00
chao an 664927c86e mm/alloc: remove all unnecessary cast for alloc
Fix the minor style issue and remove unnecessary cast

Signed-off-by: chao an <anchao@xiaomi.com>
2023-08-30 14:34:20 +08:00
ligd e59f161fc2 pthread: remove unused temp change sched_priority
old:
pthread_create:

init_priority = prio;
if (sched_priority < parent_prio)
    sched_priority = parent_prio;    // don't need

pthread_start:

if (sched_priority > init_priority)
    sched_priority = init_priority

Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-08-11 21:45:16 +08:00
zhangyuan21 671c5dc3d8 sched/pthread: Don't do cancel when it is already in the exit process
When the task is already in the exit process,
do not execute pthread cancel and return ESRCH.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-08-03 03:12:36 -07:00
guanyi 55a727b1ce ltp: sigprocmask fix
1. change signal/sig_procmask.c and pthread/pthread_sigmask.c together
2. clear signals unconditionally

Signed-off-by: guanyi <guanyi@xiaomi.com>
2023-07-31 22:29:31 -07:00
yangyalei 30367fd4cd sched: inherit parent priority by default, except idle
Signed-off-by: yangyalei <yangyalei@xiaomi.com>

asdfas

Signed-off-by: yangyalei <yangyalei@xiaomi.com>
2023-07-31 07:50:10 -07:00
yangyalei b2a9c6a3e5 sched: inherit parent priority by default
fix ltp pthread_cond_wait_1 test question, child should inherit parent
priority by default.
nsh> ltp_pthread_cond_wait_1
Error: the policy or priority not correct

Signed-off-by: yangyalei <yangyalei@xiaomi.com>
2023-07-31 07:50:10 -07:00
chao an 6ee9ec7656 build: add initial cmake build system
1. Update all CMakeLists.txt to adapt to new layout
2. Fix cmake build break
3. Update all new file license
4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

------------------

How to test

From within nuttx/. Configure:

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

(or full path in custom board) :
cmake -B build -DBOARD_CONFIG=$PWD/boards/sim/sim/sim/configs/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

--------------------------

2. cmake/build: reformat the cmake style by cmake-format

https://github.com/cheshirekow/cmake_format

$ pip install cmakelang

$ for i in `find -name CMakeLists.txt`;do cmake-format $i -o $i;done
$ for i in `find -name *\.cmake`;do cmake-format $i -o $i;done

Co-authored-by: Matias N <matias@protobits.dev>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-08 13:50:48 +08:00
Petro Karashchenko 3fe371f60f nuttx: replace getpid() with nxsched_getpid() in kernel code
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-07-07 17:39:39 -03:00
Petro Karashchenko a0b19226a1 sched/pthread: add missing FAR and fix alignment issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-07-07 17:39:39 -03:00
fangxinyong d08b81db9d sched: fix pthread_exit crash
A normal user task calls pthread_exit(), will crash at DEBUGASSERT.
Cause pthread_exit limit in user pthread task.

test case:
int main(int argc, FAR char *argv[])
{
  pthread_exit(NULL);
  return 0;
}
>> test
>> echo $?
>> 0

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2023-06-28 15:17:17 +08:00
chao an b9b615b425 sched/pthread: fix race condition on pthread_cond_wait()
pthread_cond_wait() should be an atomic operation in the mutex lock/unlock.
Since the sched_lock() has been wrongly deleted in the previous commit,
the context switch will occurred after the mutex was unlocked:

--------------------------------------------------------------------
  Task1(Priority 100)             |      Task2(Priority 101)
                                  |
  pthread_mutex_lock(mutex);      |
  |                               |
  pthread_cond_wait(cond, mutex)  |
  |  |                            |
  |  |                            |
  |  ->enter_critical_section()   |
  |  ->pthread_mutex_give(mutex)  | ----> pthread_mutex_lock(mutex);    // contex switch to high priority task
  |                               |       pthread_cond_signal(cond);    // signal before wait
  |                               | <---- pthread_mutex_unlock(mutex);  // switch back to original task
  |  ->pthread_sem_take(cond->sem)|                                     // try to wait the signal, Deadlock.
  |  ->leave_critical_section()   |
  |
  |  ->pthread_mutex_take(mutex)  |
  |                               |
  pthread_mutex_lock(mutex);      |
---------------------------------------------------------------------

This PR will bring back sched_lock()/sched_unlock() to avoid context switch to ensure atomicity

Signed-off-by: chao an <anchao@xiaomi.com>
2023-06-28 02:25:05 +08:00
zhangyuan21 fc9f87824c sched/pthread: Return ESRCH when the task is in the process of exit.
Resolving the issue with the ltp_interfaces_pthread_join_6_2 test case.

In SMP mode, the pthread may still be in the process of exiting when
pthread_join returns, and calling pthread_join again at this time will
result in an error. The error code returned should be ESRCH.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-06-15 10:12:25 -03:00
yanghuatao 29a336d6a8 sched/tls: remove PTHREAD_CLEANUP from Kconfig
use PTHREAD_CLEANUP_STACKSIZE to enable or disable interfaces pthread_cleanup_push() and pthread_cleanup_pop().
reasons:(1)same as TLS_TASK_NELEM (2)it is no need to use two variables

Signed-off-by: yanghuatao <yanghuatao@xiaomi.com>
2023-06-14 12:00:48 +08:00
Ville Juven a636edcbe4 addrenv/kstack: Allocate the kernel stack before initializing tcb
This is preparation to use kernel stack for everything when the user
process enters the kernel. Now the user stack is in use when the user
process runs a system call, which might not be the safest option.
2023-06-09 13:53:27 +08:00
chao an c1bcf59a85 libs/libc: fix build break on kernel mode
Signed-off-by: chao an <anchao@xiaomi.com>
2023-06-06 13:30:07 +08:00
zhangyuan21 ff6ba02378 sched/pthread: return ESRCH when thread not found at pthread_detach
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_detach.html

If an implementation detects that the value specified by the thread argument
to pthread_detach() does not refer to a joinable thread, it is recommended
that the function should fail and report an [EINVAL] error.

If an implementation detects use of a thread ID after the end of its lifetime,
it is recommended that the function should fail and report an [ESRCH] error.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-05-12 01:06:23 +08:00
Xiang Xiao 325f395300 Replace all strncpy with strlcpy
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-08 09:57:01 +02:00
hujun5 9cd8ff5a0b pthread: sched_lock should replace with enter_critical_secion
sched_lock cannot lock threads from other CPUs, use enter_critical_section

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-05-06 23:12:20 +08:00
hujun5 d189a86a35 system: pthread_barrierwait should be moved to kernel space
The current implementation requires the use of enter_critical_section, so the source code needs to be moved to kernel space

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-04-25 15:34:40 +08:00
hujun5 0477fb116a pthread: pthread_cond_wait dead lock
pthread_cond_wait is preempted after releasing the lock, sched_lock cannot lock threads from other CPUs, use enter_critical_section

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-04-24 12:53:56 +03:00
yinshengkai a40a802f9b sched/pthread: repalce sched_lock to enter_critical_section
After RR is enabled, an interrupt occurs during this period and the task cannot be switched

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-04-24 01:54:44 +08:00
Gregory Nutt 717bb04cb7 Increase the number of real time signals. Two is not enough.
Refer to issue #8867 for details and rational.

Convert sigset_t to an array type so that more than 32 signals can be supported.

Why not use a uin64_t?
- Using a uin32_t is more flexible if we decide to increase the number of signals beyound 64.
- 64-bit accesses are not atomic, at least not on 32-bit ARMv7-M and similar
- Keeping the base type as uint32_t does not introduce additional overhead due to padding to achieve 64-bit alignment of uin64_t
- Some architectures still supported by NuttX do not support uin64_t
  types,

Increased the number of signals to 64. This matches Linux. This will support all xsignals defined by Linux and also 32 real time signals (also like Linux).

This is is a work in progress; a draft PR that you are encouraged to comment on.
2023-03-27 16:59:04 +03:00
Petro Karashchenko 5651715486 signal: remove unused SIGCONDTIMEDOUT
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-23 17:17:25 -06:00
Ville Juven df1d7dd480 libc/exit: Purge calls to userspace API exit() from kernel
Remove calls to the userspace API exit() from the kernel. The problem
with doing such calls is that the exit functions are called with kernel
mode privileges which is a big security no-no.
2023-02-17 23:07:17 +08:00
zhangyuan21 a20bc77010 sched/pthread: change the wrong type cast
Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-02-13 23:17:29 +08:00
zhangyuan21 9017f70561 sched/pthread: check pthread group after find joininfo
Need check tcb or group info after find joininfo,
otherwise the wrong value will be returned.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-02-12 16:47:28 +08:00
Ville Juven 09e7987121 sched/addrenv: Fix system crash when process group has been deleted
There is currently a big problem in the address environment handling which
is that the address environment is released too soon when the process is
exiting. The current MMU mappings will always be the exiting process's, which means
the system needs them AT LEAST until the next context switch happens. If
the next thread is a kernel thread, the address environment is needed for
longer.

Kernel threads "lend" the address environment of the previous user process.
This is beneficial in two ways:
- The kernel processes do not need an allocated address environment
- When a context switch happens from user -> kernel or kernel -> kernel,
  the TLB does not need to be flushed. This must be done only when
  changing to a different user address environment.

Another issue is when a new process is created; the address environment
of the new process must be temporarily instantiated by up_addrenv_select().
However, the system scheduler does not know that the process has a different
address environment to its own and when / if a context restore happens, the
wrong MMU page directory is restored and the process will either crash or
do something horribly wrong.

The following changes are needed to fix the issues:
- Add mm_curr which is the current address environment of the process
- Add a reference counter to safeguard the address environment
- Whenever an address environment is mapped to MMU, its reference counter
  is incremented
- Whenever and address environment is unmapped from MMU, its reference
  counter is decremented, and tested. If no more references -> drop the
  address environment and release the memory as well
- To limit the context switch delay, the address environment is freed in
  a separate low priority clean-up thread (LPWORK)
- When a process temporarily instantiates another process's address
  environment, the scheduler will now know of this and will restore the
  correct mappings to MMU

Why is this not causing more noticeable issues ? The problem only happens
under the aforementioned special conditions, and if a context switch or
IRQ occurs during this time.
2023-02-08 02:51:23 +08:00