libs/pthread/pthread_atfork: fulfill implement pthread_atfork function

1. add the pthread_atfork implementation
2. the pthread_atfork implementation are referred to https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
This commit is contained in:
guoshichao
2023-07-15 16:37:22 +08:00
committed by Alin Jerpelea
parent 3524f4b9ce
commit 9d7f349664
7 changed files with 217 additions and 5 deletions
+6
View File
@@ -14,4 +14,10 @@ config PTHREAD_SPINLOCKS
---help---
Enable support for pthread spinlocks.
config PTHREAD_ATFORK
bool "pthread_atfork support"
default n
---help---
Enable support for pthread_atfork.
endmenu # pthread support
+49 -5
View File
@@ -22,21 +22,65 @@
* Included Files
****************************************************************************/
#include <nuttx/tls.h>
#include <nuttx/lib/lib.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_atfork
*
* Description:
* To register the methods that need to be executed when fork is called
* by any thread in a process
*
* Input Parameters:
* prepare - the method that is executed in the parent process before
* fork() processing is started
* parent - the method that is executed in the parent process after fork()
* processing completes
* child - the method that is executed in the child process after fork()
* processing completes
*
* Returned Value:
* On success, pthread_atfork() returns 0.
* On error, pthread_atfork() returns -1.
*
* Assumptions:
*
****************************************************************************/
int pthread_atfork(CODE void (*prepare)(void),
CODE void (*parent)(void),
CODE void (*child)(void))
{
/* fork isn't supported yet, so the dummy implementation is enough. */
#ifdef CONFIG_PTHREAD_ATFORK
FAR struct task_info_s *info = task_get_info();
FAR struct list_node *list = &info->ta_atfork;
FAR struct pthread_atfork_s *entry =
(FAR struct pthread_atfork_s *)
lib_malloc(sizeof(struct pthread_atfork_s));
UNUSED(prepare);
UNUSED(parent);
UNUSED(child);
if (entry == NULL)
{
return ENOMEM;
}
return 0;
list_initialize(&entry->node);
entry->prepare = prepare;
entry->parent = parent;
entry->child = child;
nxmutex_lock(&info->ta_lock);
list_add_head(list, &entry->node);
nxmutex_unlock(&info->ta_lock);
#endif
return OK;
}