void task_exithook(FAR _TCB *tcb, int status) { /* Under certain conditions, task_exithook() can be called multiple times. * A bit in the TCB was set the first time this function was called. If * that bit is set, then just ext doing nothing more.. */ if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) { return; } /* If exit function(s) were registered, call them now before we do any un- * initialization. NOTE: In the case of task_delete(), the exit function * will *not* be called on the thread execution of the task being deleted! */ task_atexit(tcb); /* Call any registered on_exit function(s) */ task_onexit(tcb, status); /* Leave the task group */ task_leavegroup(tcb, status); /* Wakeup any tasks waiting for this task to exit */ task_exitwakeup(tcb, status); /* Flush all streams (File descriptors will be closed when * the TCB is deallocated). */ #if CONFIG_NFILE_STREAMS > 0 (void)lib_flushall(&tcb->group->tg_streamlist); #endif /* Leave the task group. Perhaps discarding any un-reaped child * status (no zombies here!) */ #ifdef HAVE_TASK_GROUP group_leave(tcb); #endif /* Deallocate anything left in the TCB's queues */ #ifndef CONFIG_DISABLE_SIGNALS sig_cleanup(tcb); /* Deallocate Signal lists */ #endif /* This function can be re-entered in certain cases. Set a flag * bit in the TCB to not that we have already completed this exit * processing. */ tcb->flags |= TCB_FLAG_EXIT_PROCESSING; }
void task_exithook(FAR _TCB *tcb, int status) { /* If exit function(s) were registered, call them now before we do any un- * initialization. NOTE: In the case of task_delete(), the exit function * will *not* be called on the thread execution of the task being deleted! */ task_atexit(tcb); /* Call any registered on_exit function(s) */ task_onexit(tcb, status); /* Send SIGCHLD to the parent of the exit-ing task */ task_sigchild(tcb, status); /* Wakeup any tasks waiting for this task to exit */ task_exitwakeup(tcb, status); /* Flush all streams (File descriptors will be closed when * the TCB is deallocated). */ #if CONFIG_NFILE_STREAMS > 0 (void)lib_flushall(tcb->streams); #endif /* Discard any un-reaped child status (no zombies here!) */ #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS) task_removechildren(tcb); #endif /* Free all file-related resources now. This gets called again * just be be certain when the TCB is delallocated. However, we * really need to close files as soon as possible while we still * have a functioning task. */ (void)sched_releasefiles(tcb); /* Deallocate anything left in the TCB's queues */ #ifndef CONFIG_DISABLE_SIGNALS sig_cleanup(tcb); /* Deallocate Signal lists */ #endif }
void task_exithook(FAR struct tcb_s *tcb, int status, bool nonblocking) { /* Under certain conditions, task_exithook() can be called multiple times. * A bit in the TCB was set the first time this function was called. If * that bit is set, then just exit doing nothing more.. */ if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) { return; } #if defined(CONFIG_SCHED_ATEXIT) || defined(CONFIG_SCHED_ONEXIT) /* If exit function(s) were registered, call them now before we do any un- * initialization. * * NOTES: * * 1. In the case of task_delete(), the exit function will *not* be called * on the thread execution of the task being deleted! That is probably * a bug. * 2. We cannot call the exit functions if nonblocking is requested: These * functions might block. * 3. This function will only be called with with non-blocking == true * only when called through _exit(). _exit() behaviors requires that * the exit functions *not* be called. */ if (!nonblocking) { task_atexit(tcb); /* Call any registered on_exit function(s) */ task_onexit(tcb, status); } #endif /* If the task was terminated by another task, it may be in an unknown * state. Make some feeble effort to recover the state. */ task_recover(tcb); /* Send the SIGCHILD signal to the parent task group */ task_signalparent(tcb, status); /* Wakeup any tasks waiting for this task to exit */ task_exitwakeup(tcb, status); /* If this is the last thread in the group, then flush all streams (File * descriptors will be closed when the TCB is deallocated). * * NOTES: * 1. We cannot flush the buffered I/O if nonblocking is requested. * that might cause this logic to block. * 2. This function will only be called with with non-blocking == true * only when called through _exit(). _exit() behavior does not * require that the streams be flushed */ if (!nonblocking) { task_flushstreams(tcb); } #ifdef HAVE_TASK_GROUP /* Leave the task group. Perhaps discarding any un-reaped child * status (no zombies here!) */ group_leave(tcb); #endif #ifndef CONFIG_DISABLE_SIGNALS /* Deallocate anything left in the TCB's queues */ sig_cleanup(tcb); /* Deallocate Signal lists */ #endif /* This function can be re-entered in certain cases. Set a flag * bit in the TCB to not that we have already completed this exit * processing. */ tcb->flags |= TCB_FLAG_EXIT_PROCESSING; }
static inline void task_atexit(FAR _TCB *tcb) { #if defined(CONFIG_SCHED_ATEXIT_MAX) && CONFIG_SCHED_ATEXIT_MAX > 1 int index; /* Call each atexit function in reverse order of registration atexit() * functions are registered from lower to higher arry indices; they must * be called in the reverse order of registration when task exists, i.e., * from higher to lower indices. */ for (index = CONFIG_SCHED_ATEXIT_MAX-1; index >= 0; index--) { if (tcb->atexitfunc[index]) { /* Call the atexit function */ (*tcb->atexitfunc[index])(); /* Nullify the atexit function. task_exithook may be called more then * once in most task exit scenarios. Nullifying the atext function * pointer will assure that the callback is performed only once. */ tcb->atexitfunc[index] = NULL; } } #else if (tcb->atexitfunc) { /* Call the atexit function */ (*tcb->atexitfunc)(); /* Nullify the atexit function. task_exithook may be called more then * once in most task exit scenarios. Nullifying the atext function * pointer will assure that the callback is performed only once. */ tcb->atexitfunc = NULL; } #endif #else # define task_atexit(tcb) #endif /**************************************************************************** * Name: task_onexit * * Description: * Call any registerd on)exit function(s) * ****************************************************************************/ #ifdef CONFIG_SCHED_ONEXIT static inline void task_onexit(FAR _TCB *tcb, int status) { #if defined(CONFIG_SCHED_ONEXIT_MAX) && CONFIG_SCHED_ONEXIT_MAX > 1 int index; /* Call each on_exit function in reverse order of registration. on_exit() * functions are registered from lower to higher arry indices; they must * be called in the reverse order of registration when task exists, i.e., * from higher to lower indices. */ for (index = CONFIG_SCHED_ONEXIT_MAX-1; index >= 0; index--) { if (tcb->onexitfunc[index]) { /* Call the on_exit function */ (*tcb->onexitfunc[index])(status, tcb->onexitarg[index]); /* Nullify the on_exit function. task_exithook may be called more then * once in most task exit scenarios. Nullifying the atext function * pointer will assure that the callback is performed only once. */ tcb->onexitfunc[index] = NULL; } } #else if (tcb->onexitfunc) { /* Call the on_exit function */ (*tcb->onexitfunc)(status, tcb->onexitarg); /* Nullify the on_exit function. task_exithook may be called more then * once in most task exit scenarios. Nullifying the on_exit function * pointer will assure that the callback is performed only once. */ tcb->onexitfunc = NULL; } #endif #else # define task_onexit(tcb,status) #endif /**************************************************************************** * Name: task_exitwakeup * * Description: * Wakeup any tasks waiting for this task to exit * ****************************************************************************/ #ifdef CONFIG_SCHED_WAITPID static inline void task_exitwakeup(FAR _TCB *tcb, int status) { /* Wakeup any tasks waiting for this task to exit */ while (tcb->exitsem.semcount < 0) { /* "If more than one thread is suspended in waitpid() awaiting * termination of the same process, exactly one thread will return * the process status at the time of the target process termination." * Hmmm.. what do we return to the others? */ if (tcb->stat_loc) { *tcb->stat_loc = status << 8; tcb->stat_loc = NULL; } /* Wake up the thread */ sem_post(&tcb->exitsem); } } #else # define task_exitwakeup(tcb, status) #endif /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** * Name: task_hook * * Description: * This function implements some of the internal logic of exit() and * task_delete(). This function performs some cleanup and other actions * required when a task exists: * * - All open streams are flushed and closed. * - All functions registered with atexit() and on_exit() are called, in * the reverse order of their registration. * * When called from exit(), the tcb still resides at the head of the ready- * to-run list. The following logic is safe because we will not be * returning from the exit() call. * * When called from task_delete() we are operating on a different thread; * on the thread that called task_delete(). In this case, task_delete * will have already removed the tcb from the ready-to-run list to prevent * any further action on this task. * ****************************************************************************/ void task_exithook(FAR _TCB *tcb, int status) { /* If exit function(s) were registered, call them now before we do any un- * initialization. NOTE: In the case of task_delete(), the exit function * will *not* be called on the thread execution of the task being deleted! */ task_atexit(tcb); /* Call any registered on_exit function(s) */ task_onexit(tcb, status); /* Wakeup any tasks waiting for this task to exit */ task_exitwakeup(tcb, status); /* Flush all streams (File descriptors will be closed when * the TCB is deallocated). */ #if CONFIG_NFILE_STREAMS > 0 (void)lib_flushall(tcb->streams); #endif /* Free all file-related resources now. This gets called again * just be be certain when the TCB is delallocated. However, we * really need to close files as soon as possible while we still * have a functioning task. */ (void)sched_releasefiles(tcb); /* Deallocate anything left in the TCB's queues */ #ifndef CONFIG_DISABLE_SIGNALS sig_cleanup(tcb); /* Deallocate Signal lists */ #endif }