📄 sem.c
字号:
* - Kernel module initialization/cleanup code * - Interrupt service routine * only if @a timeout is equal to TM_NONBLOCK. * * - Kernel-based task * - User-space task (switches to primary mode) * * Rescheduling: always unless the request is immediately satisfied or * @a timeout specifies a non-blocking operation. * * @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_sem_p (RT_SEM *sem, RTIME timeout){ int err = 0; spl_t s; xnlock_get_irqsave(&nklock,s); sem = xeno_h2obj_validate(sem,XENO_SEM_MAGIC,RT_SEM); if (!sem) { err = xeno_handle_error(sem,XENO_SEM_MAGIC,RT_SEM); goto unlock_and_exit; } if (timeout == TM_NONBLOCK) { if (sem->count > 0) sem->count--; else err = -EWOULDBLOCK; goto unlock_and_exit; } if (xnpod_unblockable_p()) { err = -EPERM; goto unlock_and_exit; } if (sem->count > 0) --sem->count; else { RT_TASK *task = xeno_current_task(); xnsynch_sleep_on(&sem->synch_base,timeout); if (xnthread_test_flags(&task->thread_base,XNRMID)) err = -EIDRM; /* Semaphore deleted while pending. */ else if (xnthread_test_flags(&task->thread_base,XNTIMEO)) err = -ETIMEDOUT; /* Timeout.*/ else if (xnthread_test_flags(&task->thread_base,XNBREAK)) err = -EINTR; /* Unblocked.*/ } unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_sem_v(RT_SEM *sem) * @brief Signal a semaphore. * * Release a semaphore unit. If the semaphore is pended, the first * waiting task (by queuing order) is immediately unblocked; * otherwise, the semaphore value is incremented by one. * * @param sem The descriptor address of the affected semaphore. * * @return 0 is returned upon success. Otherwise: * * - -EINVAL is returned if @a sem is not a semaphore descriptor. * * - -EIDRM is returned if @a sem is a deleted semaphore 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_sem_v (RT_SEM *sem){ int err = 0; spl_t s; xnlock_get_irqsave(&nklock,s); sem = xeno_h2obj_validate(sem,XENO_SEM_MAGIC,RT_SEM); if (!sem) { err = xeno_handle_error(sem,XENO_SEM_MAGIC,RT_SEM); goto unlock_and_exit; } if (xnsynch_wakeup_one_sleeper(&sem->synch_base) != NULL) xnpod_schedule(); else if (!(sem->mode & S_PULSE)) sem->count++; unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_sem_broadcast(RT_SEM *sem) * @brief Broadcast a semaphore. * * Unblock all tasks waiting on a semaphore. Awaken tasks return from * rt_sem_p() as if the semaphore has been signaled. The semaphore * count is zeroed as a result of the operation. * * @param sem The descriptor address of the affected semaphore. * * @return 0 is returned upon success. Otherwise: * * - -EINVAL is returned if @a sem is not a semaphore descriptor. * * - -EIDRM is returned if @a sem is a deleted semaphore 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_sem_broadcast (RT_SEM *sem){ int err = 0; spl_t s; xnlock_get_irqsave(&nklock,s); sem = xeno_h2obj_validate(sem,XENO_SEM_MAGIC,RT_SEM); if (!sem) { err = xeno_handle_error(sem,XENO_SEM_MAGIC,RT_SEM); goto unlock_and_exit; } if (xnsynch_flush(&sem->synch_base,0) == XNSYNCH_RESCHED) xnpod_schedule(); sem->count = 0; unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_sem_inquire(RT_SEM *sem, RT_SEM_INFO *info) * @brief Inquire about a semaphore. * * Return various information about the status of a given semaphore. * * @param sem The descriptor address of the inquired semaphore. * * @param info The address of a structure the semaphore 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 sem is not a semaphore descriptor. * * - -EIDRM is returned if @a sem is a deleted semaphore descriptor. * * Environments: * * This service can be called from: * * - Kernel module initialization/cleanup code * - Interrupt service routine * - Kernel-based task * - User-space task * * Rescheduling: never. */int rt_sem_inquire (RT_SEM *sem, RT_SEM_INFO *info){ int err = 0; spl_t s; xnlock_get_irqsave(&nklock,s); sem = xeno_h2obj_validate(sem,XENO_SEM_MAGIC,RT_SEM); if (!sem) { err = xeno_handle_error(sem,XENO_SEM_MAGIC,RT_SEM); goto unlock_and_exit; } strcpy(info->name,sem->name); info->count = sem->count; info->nwaiters = xnsynch_nsleepers(&sem->synch_base); unlock_and_exit: xnlock_put_irqrestore(&nklock,s); return err;}/** * @fn int rt_sem_bind(RT_SEM *sem,const char *name,RTIME timeout) * @brief Bind to a semaphore. * * This user-space only service retrieves the uniform descriptor of a * given Xenomai semaphore identified by its symbolic name. If the * semaphore does not exist on entry, this service blocks the caller * until a semaphore of the given name is created. * * @param name A valid NULL-terminated name which identifies the * semaphore to bind to. * * @param sem The address of a semaphore descriptor retrieved by the * operation. Contents of this memory is undefined upon failure. * * @param timeout The number of clock ticks to wait for the * registration to occur (see note). Passing TM_INFINITE causes the * caller to block indefinitely until the object is * registered. Passing TM_NONBLOCK causes the service to return * immediately without waiting if the object is not registered on * entry. * * @return 0 is returned upon success. Otherwise: * * - -EFAULT is returned if @a sem or @a name is referencing invalid * memory. * * - -EINTR is returned if rt_task_unblock() has been called for the * waiting task before the retrieval has completed. * * - -EWOULDBLOCK is returned if @a timeout is equal to TM_NONBLOCK * and the searched object is not registered on entry. * * - -ETIMEDOUT is returned if the object cannot be retrieved within * the specified amount of time. * * - -EPERM is returned if this service should block, but was called * from a context which cannot sleep (e.g. interrupt, non-realtime or * scheduler locked). * * Environments: * * This service can be called from: * * - User-space task (switches to primary mode) * * Rescheduling: always unless the request is immediately satisfied or * @a timeout specifies a non-blocking operation. * * @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. *//** * @fn int rt_sem_unbind(RT_SEM *sem) * * @brief Unbind from a semaphore. * * This user-space only service unbinds the calling task from the * semaphore object previously retrieved by a call to rt_sem_bind(). * * @param sem The address of a semaphore descriptor to unbind from. * * @return 0 is always returned. * * This service can be called from: * * - User-space task. * * Rescheduling: never. */int __native_sem_pkg_init (void){ return 0;}void __native_sem_pkg_cleanup (void){}/*@}*/EXPORT_SYMBOL(rt_sem_create);EXPORT_SYMBOL(rt_sem_delete);EXPORT_SYMBOL(rt_sem_p);EXPORT_SYMBOL(rt_sem_v);EXPORT_SYMBOL(rt_sem_inquire);EXPORT_SYMBOL(rt_sem_broadcast);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -