You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Background

Under certain conditions, it may be necessary to create a kernel thread whose stack likes in some custom memory.  This page provides and example of how that would be done:

Example

Here is the body of some function.  It expects to have the following inputs:

  1. taskname:  The name of the task to be started
  2. stacksize:  The size of the custom stack
  3. priority:  The priority of the task to be started
  4. entry_point:  The entry point of the task to be started
  5. argv:  An optional array of argument strings passed to the task


  /* Allocate a TCB for the new task.  kmm_zalloc() is used so
* that all fields of the new TCB will be zeroed.
*/ tcb = (FAR struct task_tcb_s *)kmm_zalloc(sizeof(struct task_tcb_s)); if (tcb == NULL) { return -ENOMEM; }
/* Indicate (1) that this is a kernel thread and that a custom
* stack will be used.
*/
tcb->flags = TCB_FLAG_TTYPE_KERNEL | TCB_FLAG_CUSTOM_STACK; /* Allocate the custom stack for the new task. * * Do whatever it takes to get a reference to the custom stack.
* Here custom_alloc() is used as a placeholder for whatever
* that may be. */ stack = (FAR uint32_t *)custom_alloc(stacksize); if (stack == NULL) { kmm_free(tcb); return -ENOMEM; } /* Initialize the TCB. This will initialize all remaining
* fields of the TCB, associate the stack to the TCB, allocate
* any additional resources needed by the task, and place the
* TCB in a list of inactive tasks.
*/ ret = task_init((FAR struct tcb_s *)tcb, progname, priority, stack, stacksize, entry_point, argv); if (ret < 0) {
kmm_free(tcb); custom_free(stack);
return ret;
}
  /* Then activate the task at the provided priority */

  ret = task_activate((FAR struct tcb_s *)tcb);
  if (ret < 0)
    {
      nxtask_uninit(tcb);
      custom_free(stack);
return ret;
}

return OK;



  • No labels