📄 task.c
字号:
/** * @fn int rt_task_sleep(RTIME delay) * @brief Delay the calling task (relative). * * Delay the execution of the calling task for a number of internal * clock ticks. * * @param delay The number of clock ticks to wait before resuming the * task (see note). Passing zero causes the task to return immediately * with no delay. * * @return 0 is returned upon success, otherwise: * * - -EINTR is returned if rt_task_unblock() has been called for the * sleeping task before the sleep time has elapsed. * * - -EWOULDBLOCK is returned if the system timer is inactive. * * - -EPERM is returned if this service was called from a context * which cannot sleep (e.g. interrupt, non-realtime or scheduler * locked). * * Environments: * * This service can be called from: * * - Kernel-based task * - User-space task (switches to primary mode) * * Rescheduling: always unless a null delay is given. * * @note This service is sensitive to the current operation mode of * the system timer, as defined by the rt_timer_start() service. In * periodic mode, clock ticks are interpreted as periodic jiffies. In * oneshot mode, clock ticks are interpreted as nanoseconds. */int rt_task_sleep (RTIME delay){ if (xnpod_unblockable_p()) return -EPERM; if (delay == 0) return 0; if (!testbits(nkpod->status,XNTIMED)) return -EWOULDBLOCK; /* Calling the suspension service on behalf of the current task implicitely calls the rescheduling procedure. */ xnpod_suspend_thread(&xeno_current_task()->thread_base, XNDELAY, delay, NULL); return xnthread_test_flags(&xeno_current_task()->thread_base,XNBREAK) ? -EINTR : 0;}/** * @fn int rt_task_sleep_until(RTIME date) * @brief Delay the calling task (absolute). * * Delay the execution of the calling task until a given date is * reached. * * @param date The absolute date in clock ticks to wait before * resuming the task (see note). Passing an already elapsed date * causes the task to return immediately with no delay. * * @return 0 is returned upon success. Otherwise: * * - -EINTR is returned if rt_task_unblock() has been called for the * sleeping task before the sleep time has elapsed. * * - -ETIMEDOUT is returned if @a date has already elapsed. * * - -EWOULDBLOCK is returned if the system timer is inactive. * * - -EPERM is returned if this service was called from a context * which cannot sleep (e.g. interrupt, non-realtime or scheduler * locked). * * Environments: * * This service can be called from: * * - Kernel-based task * - User-space task (switches to primary mode) * * Rescheduling: always unless a date in the past is given. * * @note This service is sensitive to the current operation mode of * the system timer, as defined by the rt_timer_start() service. In * periodic mode, clock ticks are interpreted as periodic jiffies. In * oneshot mode, clock ticks are interpreted as nanoseconds. */int rt_task_sleep_until (RTIME date){ int err = 0; SRTIME delay; spl_t s; if (xnpod_unblockable_p()) return -EPERM; if (!testbits(nkpod->status,XNTIMED)) return -EWOULDBLOCK; xnlock_get_irqsave(&nklock,s); /* Calling the suspension service on behalf of the current task implicitely calls the rescheduling procedure. */ delay = date - xnpod_get_time(); if (delay > 0) { xnpod_suspend_thread(&xeno_current_task()->thread_base, XNDELAY, delay, NULL); if (xnthread_test_flags(&xeno_current_task()->thread_base,XNBREAK)) err = -EINTR; } else err = -ETIMEDOUT; xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_task_unblock(RT_TASK *task) * @brief Unblock a real-time task. * * Break the task out of any wait it is currently in. This call * clears all delay and/or resource wait condition for the target * task. However, rt_task_unblock() does not resume a task which has * been forcibly suspended by a previous call to rt_task_suspend(). * If all suspensive conditions are gone, the task becomes eligible * anew for scheduling. * * @param task The descriptor address of the affected task. * * @return 0 is returned upon success. Otherwise: * * - -EINVAL is returned if @a task is not a task descriptor. * * - -EIDRM is returned if @a task is a deleted task descriptor. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * - Kernel-based task * - User-space task * * Rescheduling: possible. */int rt_task_unblock (RT_TASK *task){ int err = 0; spl_t s; xnlock_get_irqsave(&nklock,s); task = xeno_h2obj_validate(task,XENO_TASK_MAGIC,RT_TASK); if (!task) { err = xeno_handle_error(task,XENO_TASK_MAGIC,RT_TASK); goto unlock_and_exit; } xnpod_unblock_thread(&task->thread_base); xnpod_schedule(); unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_task_inquire(RT_TASK *task, RT_TASK_INFO *info) * @brief Inquire about a real-time task. * * Return various information about the status of a given task. * * @param task The descriptor address of the inquired task. If @a task * is NULL, the current task is inquired. * * @param info The address of a structure the task information will be * written to. * @return 0 is returned and status information is written to the * structure pointed at by @a info upon success. Otherwise: * * - -EINVAL is returned if @a task is not a task descriptor. * * - -EPERM is returned if @a task is NULL but not called from a task * context. * * - -EIDRM is returned if @a task is a deleted task descriptor. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * only if @a task is non-NULL. * * - Kernel-based task * - User-space task * * Rescheduling: never. */int rt_task_inquire (RT_TASK *task, RT_TASK_INFO *info){ int err = 0; spl_t s; if (!task) { if (!xnpod_primary_p()) return -EPERM; task = xeno_current_task(); } xnlock_get_irqsave(&nklock,s); task = xeno_h2obj_validate(task,XENO_TASK_MAGIC,RT_TASK); if (!task) { err = xeno_handle_error(task,XENO_TASK_MAGIC,RT_TASK); goto unlock_and_exit; } strcpy(info->name,xnthread_name(&task->thread_base)); info->bprio = xnthread_base_priority(&task->thread_base); info->cprio = xnthread_current_priority(&task->thread_base); info->status = xnthread_status_flags(&task->thread_base); info->relpoint = xntimer_get_date(&task->timer); unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_task_add_hook(int type, void (*routine)(void *cookie)); * @brief Install a task hook. * * The real-time kernel allows to register user-defined routines which * get called whenever a specific scheduling event occurs. Multiple * hooks can be chained for a single event type, and get called on a * FIFO basis. * * The scheduling is locked while a hook is executing. * * @param type Defines the kind of hook to install: * * - T_HOOK_START: The user-defined routine will be called on behalf * of the starter task whenever a new task starts. An opaque cookie is * passed to the routine which can use it to retrieve the descriptor * address of the started task through the T_DESC() macro. * * - T_HOOK_DELETE: The user-defined routine will be called on behalf * of the deletor task whenever a task is deleted. An opaque cookie is * passed to the routine which can use it to retrieve the descriptor * address of the deleted task through the T_DESC() macro. * * - T_HOOK_SWITCH: The user-defined routine will be called on behalf * of the resuming task whenever a context switch takes place. An * opaque cookie is passed to the routine which can use it to retrieve * the descriptor address of the task which has been switched in * through the T_DESC() macro. * * @param routine The address of the user-supplied routine to call. * * @return 0 is returned upon success. Otherwise, one of the following * error codes indicates the cause of the failure: * * - -EINVAL is returned if @a type is incorrect. * * - -ENOMEM is returned if not enough memory is available from the * system heap to add the new hook. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * - Kernel-based task * * Rescheduling: never. */int rt_task_add_hook (int type, void (*routine)(void *cookie)) { return xnpod_add_hook(type,(void (*)(xnthread_t *))routine);}/** * @fn int rt_task_remove_hook(int type, void (*routine)(void *cookie)); * @brief Remove a task hook. * * This service allows to remove a task hook previously registered * using rt_task_add_hook(). * * @param type Defines the kind of hook to uninstall. Possible values * are: * * - T_HOOK_START * - T_HOOK_DELETE * - T_HOOK_SWITCH * * @param routine The address of the user-supplied routine to remove * from the hook list. * * @return 0 is returned upon success. Otherwise, one of the following * error codes indicates the cause of the failure: * * - -EINVAL is returned if @a type is incorrect. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * - Kernel-based task * * Rescheduling: never. */int rt_task_remove_hook (int type, void (*routine)(void *cookie)) { return xnpod_remove_hook(type,(void (*)(xnthread_t *))routine);}/** * @fn int rt_task_catch(void (*handler)(rt_sigset_t)) * @brief Install a signal handler. * * This service installs a signal handler for the current * task. Signals are discrete events tasks can receive each time they * resume execution. When signals are pending upon resumption, @a * handler is fired to process them. Signals can be sent using * rt_task_notify(). A task can block the signal delivery by passing * the T_NOSIG bit to rt_task_set_mode(). * * Calling this service implicitely unblocks the signal delivery for * the caller. * * @param handler The address of the user-supplied routine to fire * when signals are pending for the task. This handler is passed the * set of pending signals as its first and only argument. * * @return 0 upon success, or: * * - -EPERM is returned if this service was not called from a * real-time task context. * * Environments: * * This service can be called from: * * - Kernel-based task * - User-space task * * Rescheduling: possible. */int rt_task_catch (void (*handler)(rt_sigset_t)){ spl_t s; if (!xnpod_primary_p()) return -EPERM; xnlock_get_irqsave(&nklock,s); xeno_current_task()->thread_base.asr = (xnasr_t)handler; xeno_current_task()->thread_base.asrmode &= ~XNASDI; xeno_current_task()->thread_base.asrimask = 0; xnlock_put_irqrestore(&nklock,s); /* The rescheduling procedure checks for pending signals. */ xnpod_schedule(); return 0;}/** * @fn int rt_task_notify(RT_TASK *task,rt_sigset_t signals) * @brief Send signals to a task. * * This service sends a set of signals to a given task. A task can * install a signal handler using the rt_task_catch() service to * process them. * * @param task The descriptor address of the affected task which must * have been previously created by the rt_task_create() service. * * @param signals The set of signals to make pending for the * task. This set is OR'ed with the current set of pending signals for * the task; there is no count of occurence maintained for each * available signal, which is either pending or cleared. * * @return 0 is returned upon success. Otherwise: * * - -EINVAL is returned if @a task is not a task descriptor. * * - -EPERM is returned if @a task is NULL but not called from a * real-time task context. * * - -EIDRM is returned if @a task is a deleted task descriptor. * * - -ESRCH is returned if @a task has not set any signal handler. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * only if @a task is non-NULL. * * - Kernel-based task * - User-space task * * Rescheduling: possible.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -