コード例 #1
0
ファイル: task_exithook.c プロジェクト: daregger/Firmware
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
}
コード例 #2
0
int sched_releasetcb(FAR _TCB *tcb)
{
  int ret = OK;
  int i;

  if (tcb)
    {
      /* Relase any timers that the task might hold.  We do this
       * before release the PID because it may still be trying to
       * deliver signals (although interrupts are should be
       * disabled here).
       */

#ifndef CONFIG_DISABLE_POSIX_TIMERS
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
     if (timer_deleteall != NULL)
#endif
        {
          timer_deleteall(tcb->pid);
        }
#endif

      /* Release the task's process ID if one was assigned.  PID
       * zero is reserved for the IDLE task.  The TCB of the IDLE
       * task is never release so a value of zero simply means that
       * the process ID was never allocated to this TCB.
       */

      if (tcb->pid)
        {
          sched_releasepid(tcb->pid);
        }

      /* Delete the thread's stack if one has been allocated */

#ifndef CONFIG_CUSTOM_STACK
      if (tcb->stack_alloc_ptr)
        {
          up_release_stack(tcb);
        }
#endif

      /* Delete the tasks's allocated DSpace region (external modules only) */

#ifdef CONFIG_PIC
      if (tcb->dspace)
        {
          if (tcb->dspace->crefs <= 1)
            {
              sched_free(tcb->dspace);
            }
          else
            {
              tcb->dspace->crefs--;
            }
        }
#endif

      /* Release command line arguments that were allocated for task
       * start/re-start.
       */

      if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_TASK)
        {
          for (i = 1; i < CONFIG_MAX_TASK_ARGS+1 && tcb->argv[i]; i++)
            {
              sched_free((FAR void*)tcb->argv[i]);
            }
        }

      /* Release any allocated file structures */

      ret = sched_releasefiles(tcb);

      /* Release environment variables */

      (void)env_release(tcb);

      /* And, finally, release the TCB itself */

      sched_free(tcb);
    }
  return ret;
}
コード例 #3
0
ファイル: task_exithook.c プロジェクト: soonhooi/Firmware
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
}