示例#1
0
static inline void task_flushstreams(FAR struct tcb_s *tcb)
{
  FAR struct task_group_s *group = tcb->group;

  /* Have we already left the group?  Are we the last thread in the group? */

  if (group && group->tg_nmembers == 1)
    {
#if (defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)) && \
     defined(CONFIG_MM_KERNEL_HEAP)
      (void)lib_flushall(tcb->group->tg_streamlist);
#else
      (void)lib_flushall(&tcb->group->tg_streamlist);
#endif
    }
}
示例#2
0
int fflush(FAR FILE *stream)
{
    int ret;

    /* Is the stream argument NULL? */

    if (!stream)
    {
        /* Yes... then this is a request to flush all streams */

        ret = lib_flushall(sched_getstreams());
    }
    else
    {
        ret = lib_fflush(stream, true);
    }

    /* Check the return value */

    if (ret < 0)
    {
        /* An error occurred during the flush AND/OR we were unable to flush
         * all of the buffered write data. Set the errno value.
         */

        set_errno(-ret);

        /* And return EOF on failure. */

        return EOF;
    }

    return OK;
}
示例#3
0
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;
}
示例#4
0
void task_exithook(FAR _TCB *tcb, int status)
{
  /* Inform the instrumentation layer that the task has stopped */

  sched_note_stop(tcb);

  /* Flush all streams (File descriptors will be closed when
   * the TCB is deallocated).
   */

#if CONFIG_NFILE_STREAMS > 0
  (void)lib_flushall(tcb->streams);
#endif

  /* Deallocate anything left in the TCB's queues */

#ifndef CONFIG_DISABLE_SIGNALS
  sig_cleanup(tcb); /* Deallocate Signal lists */
#endif

  /* Wakeup any tasks waiting for this task to exit */

#ifdef CONFIG_SCHED_WAITPID /* Experimental */
  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);
    }
#endif

  /* If an exit function was registered, call it now.  NOTE:  In the case
   * of task_delete(), the exit function will *not* be called on the thread
   * execution of the task being deleted!
   */

#ifdef CONFIG_SCHED_ATEXIT
  if (tcb->exitfunc)
    {
      (*tcb->exitfunc)();
    }
#endif
}
示例#5
0
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
}
示例#6
0
int fflush(FAR FILE *stream)
{
  /* Is the stream argument NULL? */

  if (!stream)
    {
      /* Yes... then this is a request to flush all streams */

      return lib_flushall(sched_getstreams());
    }
  else if (lib_fflush(stream, true) != 0)
    {
      /* An error occurred during the flush AND/OR we were unable to flush all
       * of the buffered write data.  Return EOF on failure.
       */

      return EOF;
    }
  return OK;
}
示例#7
0
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
}