Skip to content

Commit

Permalink
libc: Implement nxtask_exit_cleanup
Browse files Browse the repository at this point in the history
This runs the I/O and FILE cleanup procedures
  • Loading branch information
pussuw committed May 5, 2022
1 parent 668fb72 commit d7eff92
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 68 deletions.
16 changes: 16 additions & 0 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,22 @@ void nxtask_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook,
void nxtask_startup(main_t entrypt, int argc, FAR char *argv[]);
#endif

/****************************************************************************
* Name: nxtask_exit_cleanup
*
* Description:
* Perform cleanup before task is closed.
*
* If SCHED_ATEXIT or SCHED_ONEXIT are defined, all functions registered
* with atexit() and on_exit() are called, in the reverse order of their
* registration.
*
* All open streams are flushed and closed.
*
****************************************************************************/

void nxtask_exit_cleanup(int status);

/****************************************************************************
* Internal vfork support. The overall sequence is:
*
Expand Down
6 changes: 3 additions & 3 deletions libs/libc/stdlib/lib_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
****************************************************************************/

#include <nuttx/config.h>
#include <stdlib.h>
#include <unistd.h>

#include "libc.h"

#include "atexit.h"

Expand All @@ -36,7 +36,7 @@ void exit(int status)
{
__atexit_call_exitfuncs(status);

/* REVISIT: Need to flush files and streams */
nxtask_exit_cleanup(status);

_exit(status);
}
Expand Down
67 changes: 2 additions & 65 deletions sched/task/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@

#include <nuttx/config.h>

#include <stdlib.h>
#include <unistd.h>
#include <debug.h>
#include <errno.h>

#include <nuttx/fs/fs.h>

#include "task/task.h"
#include "group/group.h"
#include "sched/sched.h"
#include "pthread/pthread.h"
#include <nuttx/arch.h>

/****************************************************************************
* Public Functions
Expand All @@ -53,58 +43,5 @@

void _exit(int status)
{
up_exit(status);
}

/****************************************************************************
* Name: exit
*
* Description:
* The exit() function causes normal process termination and the value of
* status & 0377 to be returned to the parent.
*
* All functions registered with atexit() and on_exit() are called, in the
* reverse order of their registration.
*
* All open streams are flushed and closed.
*
****************************************************************************/

void exit(int status)
{
FAR struct tcb_s *tcb = this_task();

/* Only the lower 8-bits of status are used */

status &= 0xff;

#ifdef HAVE_GROUP_MEMBERS
/* Kill all of the children of the group, preserving only this thread.
* exit() is normally called from the main thread of the task. pthreads
* exit through a different mechanism.
*/

group_kill_children(tcb);
#endif

#if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
/* Recover any mutexes still held by the canceled thread */

pthread_mutex_inconsistent(tcb);
#endif

/* Perform common task termination logic. This will get called again later
* through logic kicked off by _exit(). However, we need to call it before
* calling _exit() in order to handle atexit() and on_exit() callbacks and
* so that we can flush buffered I/O (both of which may required
* suspending).
*/

nxtask_exithook(tcb, status, false);

/* Then "really" exit. Only the lower 8 bits of the exit status are
* used.
*/

_exit(status);
up_exit(status & 0xff);
}
41 changes: 41 additions & 0 deletions sched/task/task_exithook.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "group/group.h"
#include "signal/signal.h"
#include "task/task.h"
#include "pthread/pthread.h"

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -643,3 +644,43 @@ void nxtask_exithook(FAR struct tcb_s *tcb, int status, bool nonblocking)

tcb->flags |= TCB_FLAG_EXIT_PROCESSING;
}

/****************************************************************************
* Name: nxtask_exit_cleanup
*
* Description:
* Perform cleanup before task is closed.
*
* If SCHED_ATEXIT or SCHED_ONEXIT are defined, all functions registered
* with atexit() and on_exit() are called, in the reverse order of their
* registration.
*
* All open streams are flushed and closed.
*
****************************************************************************/

void nxtask_exit_cleanup(int status)
{
FAR struct tcb_s *tcb = this_task();

/* Only the lower 8-bits of status are used */

status &= 0xff;

#ifdef HAVE_GROUP_MEMBERS
/* Kill all of the children of the group, preserving only this thread.
* exit() is normally called from the main thread of the task. pthreads
* exit through a different mechanism.
*/

group_kill_children(tcb);
#endif

#if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
/* Recover any mutexes still held by the canceled thread */

pthread_mutex_inconsistent(tcb);
#endif

nxtask_flushstreams(tcb);
}

0 comments on commit d7eff92

Please sign in to comment.