RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
+ Semaphore 的协作图:

结构体

struct  rt_semaphore
 

类型定义

typedef struct rt_semaphorert_sem_t
 

函数

rt_err_t rt_sem_init (rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
 
 RTM_EXPORT (rt_sem_init)
 
rt_err_t rt_sem_detach (rt_sem_t sem)
 
 RTM_EXPORT (rt_sem_detach)
 
rt_sem_t rt_sem_create (const char *name, rt_uint32_t value, rt_uint8_t flag)
 
 RTM_EXPORT (rt_sem_create)
 
rt_err_t rt_sem_delete (rt_sem_t sem)
 
 RTM_EXPORT (rt_sem_delete)
 
rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time)
 
 RTM_EXPORT (rt_sem_take)
 
rt_err_t rt_sem_take_interruptible (rt_sem_t sem, rt_int32_t time)
 
 RTM_EXPORT (rt_sem_take_interruptible)
 
rt_err_t rt_sem_take_killable (rt_sem_t sem, rt_int32_t time)
 
 RTM_EXPORT (rt_sem_take_killable)
 
rt_err_t rt_sem_trytake (rt_sem_t sem)
 
 RTM_EXPORT (rt_sem_trytake)
 
rt_err_t rt_sem_release (rt_sem_t sem)
 
 RTM_EXPORT (rt_sem_release)
 
rt_err_t rt_sem_control (rt_sem_t sem, int cmd, void *arg)
 
 RTM_EXPORT (rt_sem_control)
 

详细描述

类型定义说明

◆ rt_sem_t

typedef struct rt_semaphore* rt_sem_t

在文件 rtdef.h1010 行定义.

函数说明

◆ rt_sem_init()

rt_err_t rt_sem_init ( rt_sem_t sem,
const char * name,
rt_uint32_t value,
rt_uint8_t flag )

This function will initialize a static semaphore object.

注解
For the static semaphore object, its memory space is allocated by the compiler during compiling, and shall placed on the read-write data segment or on the uninitialized data segment. By contrast, the rt_sem_create() function will allocate memory space automatically and initialize the semaphore.
参见
rt_sem_create()
参数
semis a pointer to the semaphore to initialize. It is assumed that storage for the semaphore will be allocated in your application.
nameis a pointer to the name you would like to give the semaphore.
valueis the initial value for the semaphore. If used to share resources, you should initialize the value as the number of available resources. If used to signal the occurrence of an event, you should initialize the value as 0.
flagis the semaphore flag, which determines the queuing way of how multiple threads wait when the semaphore is not available. The semaphore flag can be ONE of the following values:
RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.

RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
                          (also known as first-come-first-served (FCFS) scheduling strategy).

NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
the first-in-first-out principle, and you clearly understand that all threads involved in
this semaphore will become non-real-time threads.
返回
Return the operation status. When the return value is RT_EOK, the initialization is successful. If the return value is any other values, it represents the initialization failed.
警告
This function can ONLY be called from threads.

在文件 ipc.c376 行定义.

380{
381 RT_ASSERT(sem != RT_NULL);
382 RT_ASSERT(value < 0x10000U);
383 RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
384
385 /* initialize object */
387
388 _sem_object_init(sem, value, flag, RT_SEM_VALUE_MAX);
389
390 return RT_EOK;
391}
#define RT_IPC_FLAG_FIFO
#define RT_IPC_FLAG_PRIO
void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name)
This function will initialize an object and add it to object system management.
@ RT_Object_Class_Semaphore
#define RT_ASSERT(EX)
#define RT_SEM_VALUE_MAX
#define RT_NULL
struct rt_object parent
struct rt_ipc_object parent

引用了 rt_ipc_object::parent, rt_semaphore::parent, RT_ASSERT, RT_IPC_FLAG_FIFO, RT_IPC_FLAG_PRIO, RT_NULL, RT_Object_Class_Semaphore, rt_object_init() , 以及 RT_SEM_VALUE_MAX.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [1/10]

RTM_EXPORT ( rt_sem_init )

引用了 rt_sem_init().

+ 函数调用图:

◆ rt_sem_detach()

rt_err_t rt_sem_detach ( rt_sem_t sem)

This function will detach a static semaphore object.

注解
This function is used to detach a static semaphore object which is initialized by rt_sem_init() function. By contrast, the rt_sem_delete() function will delete a semaphore object. When the semaphore is successfully detached, it will resume all suspended threads in the semaphore list.
参见
rt_sem_delete()
参数
semis a pointer to a semaphore object to be detached.
返回
Return the operation status. When the return value is RT_EOK, the initialization is successful. If the return value is any other values, it means that the semaphore detach failed.
警告
This function can ONLY detach a static semaphore initialized by the rt_sem_init() function. If the semaphore is created by the rt_sem_create() function, you MUST NOT USE this function to detach it, ONLY USE the rt_sem_delete() function to complete the deletion.

在文件 ipc.c413 行定义.

414{
415 rt_base_t level;
416
417 /* parameter check */
418 RT_ASSERT(sem != RT_NULL);
421
422 level = rt_spin_lock_irqsave(&(sem->spinlock));
423 /* wakeup all suspended threads */
425 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
426
427 /* detach semaphore object */
429
430 return RT_EOK;
431}
rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
This function will disable the local interrupt and then lock the spinlock, will lock the thread sched...
rt_err_t rt_susp_list_resume_all(rt_list_t *susp_list, rt_err_t thread_error)
This function will resume all suspended threads in the IPC object list, including the suspended list ...
定义 ipc.c:165
void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
This function will unlock the spinlock and then restore current cpu interrupt status,...
rt_bool_t rt_object_is_systemobject(rt_object_t object)
This function will judge the object is system object or not.
rt_uint8_t rt_object_get_type(rt_object_t object)
This function will return the type of object without RT_Object_Class_Static flag.
void rt_object_detach(rt_object_t object)
This function will detach a static object from object system, and the memory of static object is not ...
rt_int32_t rt_base_t
rt_list_t suspend_thread
struct rt_spinlock spinlock

引用了 rt_ipc_object::parent, rt_semaphore::parent, RT_ASSERT, RT_NULL, RT_Object_Class_Semaphore, rt_object_detach(), rt_object_get_type(), rt_object_is_systemobject(), rt_spin_lock_irqsave(), rt_spin_unlock_irqrestore(), rt_susp_list_resume_all(), rt_semaphore::spinlock , 以及 rt_ipc_object::suspend_thread.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [2/10]

RTM_EXPORT ( rt_sem_detach )

引用了 rt_sem_detach().

+ 函数调用图:

◆ rt_sem_create()

rt_sem_t rt_sem_create ( const char * name,
rt_uint32_t value,
rt_uint8_t flag )

Creating a semaphore object.

注解
For the semaphore object, its memory space is allocated automatically. By contrast, the rt_sem_init() function will initialize a static semaphore object.
参见
rt_sem_init()
参数
nameis a pointer to the name you would like to give the semaphore.
valueis the initial value for the semaphore. If used to share resources, you should initialize the value as the number of available resources. If used to signal the occurrence of an event, you should initialize the value as 0.
flagis the semaphore flag, which determines the queuing way of how multiple threads wait when the semaphore is not available. The semaphore flag can be ONE of the following values:
RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.

RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
                          (also known as first-come-first-served (FCFS) scheduling strategy).

NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
the first-in-first-out principle, and you clearly understand that all threads involved in
this semaphore will become non-real-time threads.
返回
Return a pointer to the semaphore object. When the return value is RT_NULL, it means the creation failed.
警告
This function can NOT be called in interrupt context. You can use macor RT_DEBUG_NOT_IN_INTERRUPT to check it.

在文件 ipc.c467 行定义.

468{
469 rt_sem_t sem;
470
471 RT_ASSERT(value < 0x10000U);
472 RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
473
475
476 /* allocate object */
478 if (sem == RT_NULL)
479 return sem;
480
481 _sem_object_init(sem, value, flag, RT_SEM_VALUE_MAX);
482
483 return sem;
484}
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
This function will allocate an object from object system.
#define RT_DEBUG_NOT_IN_INTERRUPT
struct rt_semaphore * rt_sem_t

引用了 RT_ASSERT, RT_DEBUG_NOT_IN_INTERRUPT, RT_IPC_FLAG_FIFO, RT_IPC_FLAG_PRIO, RT_NULL, rt_object_allocate(), RT_Object_Class_Semaphore , 以及 RT_SEM_VALUE_MAX.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [3/10]

RTM_EXPORT ( rt_sem_create )

引用了 rt_sem_create().

+ 函数调用图:

◆ rt_sem_delete()

rt_err_t rt_sem_delete ( rt_sem_t sem)

This function will delete a semaphore object and release the memory space.

注解
This function is used to delete a semaphore object which is created by the rt_sem_create() function. By contrast, the rt_sem_detach() function will detach a static semaphore object. When the semaphore is successfully deleted, it will resume all suspended threads in the semaphore list.
参见
rt_sem_detach()
参数
semis a pointer to a semaphore object to be deleted.
返回
Return the operation status. When the return value is RT_EOK, the operation is successful. If the return value is any other values, it means that the semaphore detach failed.
警告
This function can ONLY delete a semaphore initialized by the rt_sem_create() function. If the semaphore is initialized by the rt_sem_init() function, you MUST NOT USE this function to delete it, ONLY USE the rt_sem_detach() function to complete the detachment.

在文件 ipc.c506 行定义.

507{
508 rt_ubase_t level;
509
510 /* parameter check */
511 RT_ASSERT(sem != RT_NULL);
514
516
517 level = rt_spin_lock_irqsave(&(sem->spinlock));
518 /* wakeup all suspended threads */
520 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
521
522 /* delete semaphore object */
524
525 return RT_EOK;
526}
void rt_object_delete(rt_object_t object)
This function will delete an object and release object memory.
#define RT_FALSE
rt_uint32_t rt_ubase_t

引用了 rt_ipc_object::parent, rt_semaphore::parent, RT_ASSERT, RT_DEBUG_NOT_IN_INTERRUPT, RT_FALSE, RT_NULL, RT_Object_Class_Semaphore, rt_object_delete(), rt_object_get_type(), rt_object_is_systemobject(), rt_spin_lock_irqsave(), rt_spin_unlock_irqrestore(), rt_susp_list_resume_all(), rt_semaphore::spinlock , 以及 rt_ipc_object::suspend_thread.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [4/10]

RTM_EXPORT ( rt_sem_delete )

引用了 RT_NULL, RT_Object_Class_Semaphore , 以及 rt_sem_delete().

+ 函数调用图:

◆ rt_sem_take()

rt_err_t rt_sem_take ( rt_sem_t sem,
rt_int32_t time )

在文件 ipc.c644 行定义.

645{
646 return _rt_sem_take(sem, time, RT_UNINTERRUPTIBLE);
647}
@ RT_UNINTERRUPTIBLE

引用了 RT_UNINTERRUPTIBLE.

+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [5/10]

RTM_EXPORT ( rt_sem_take )

引用了 rt_sem_take().

+ 函数调用图:

◆ rt_sem_take_interruptible()

rt_err_t rt_sem_take_interruptible ( rt_sem_t sem,
rt_int32_t time )

在文件 ipc.c650 行定义.

651{
652 return _rt_sem_take(sem, time, RT_INTERRUPTIBLE);
653}
@ RT_INTERRUPTIBLE

引用了 RT_INTERRUPTIBLE.

+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [6/10]

RTM_EXPORT ( rt_sem_take_interruptible )

引用了 rt_sem_take_interruptible().

+ 函数调用图:

◆ rt_sem_take_killable()

rt_err_t rt_sem_take_killable ( rt_sem_t sem,
rt_int32_t time )

在文件 ipc.c656 行定义.

657{
658 return _rt_sem_take(sem, time, RT_KILLABLE);
659}
@ RT_KILLABLE

引用了 RT_KILLABLE.

+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [7/10]

RTM_EXPORT ( rt_sem_take_killable )

引用了 rt_sem_take_killable().

+ 函数调用图:

◆ rt_sem_trytake()

rt_err_t rt_sem_trytake ( rt_sem_t sem)

This function will try to take a semaphore, if the semaphore is unavailable, the thread returns immediately.

注解
This function is very similar to the rt_sem_take() function, when the semaphore is not available, the rt_sem_trytake() function will return immediately without waiting for a timeout. In other words, rt_sem_trytake(sem) has the same effect as rt_sem_take(sem, 0).
参见
rt_sem_take()
参数
semis a pointer to a semaphore object.
返回
Return the operation status. ONLY When the return value is RT_EOK, the operation is successful. If the return value is any other values, it means that the semaphore take failed.

在文件 ipc.c676 行定义.

677{
678 return rt_sem_take(sem, RT_WAITING_NO);
679}
#define RT_WAITING_NO
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
定义 ipc.c:644

引用了 rt_sem_take() , 以及 RT_WAITING_NO.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [8/10]

RTM_EXPORT ( rt_sem_trytake )

引用了 rt_sem_trytake().

+ 函数调用图:

◆ rt_sem_release()

rt_err_t rt_sem_release ( rt_sem_t sem)

This function will release a semaphore. If there is thread suspended on the semaphore, it will get resumed.

注解
If there are threads suspended on this semaphore, the first thread in the list of this semaphore object will be resumed, and a thread scheduling (rt_schedule) will be executed. If no threads are suspended on this semaphore, the count value sem->value of this semaphore will increase by 1.
参数
semis a pointer to a semaphore object.
返回
Return the operation status. When the return value is RT_EOK, the operation is successful. If the return value is any other values, it means that the semaphore release failed.

在文件 ipc.c695 行定义.

696{
697 rt_base_t level;
698 rt_bool_t need_schedule;
699
700 /* parameter check */
701 RT_ASSERT(sem != RT_NULL);
703
704 RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));
705
706 need_schedule = RT_FALSE;
707
708 level = rt_spin_lock_irqsave(&(sem->spinlock));
709
710 LOG_D("thread %s releases sem:%s, which value is: %d",
711 rt_thread_self()->parent.name,
712 sem->parent.parent.name,
713 sem->value);
714
716 {
717 /* resume the suspended thread */
719 need_schedule = RT_TRUE;
720 }
721 else
722 {
723 if(sem->value < sem->max_value)
724 {
725 sem->value ++; /* increase value */
726 }
727 else
728 {
729 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
730 return -RT_EFULL; /* value overflowed */
731 }
732 }
733
734 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
735
736 /* resume a thread, re-schedule */
737 if (need_schedule == RT_TRUE)
738 rt_schedule();
739
740 return RT_EOK;
741}
struct rt_thread * rt_susp_list_dequeue(rt_list_t *susp_list, rt_err_t thread_error)
Dequeue a thread from suspended list and set it to ready. The 2 are taken as an atomic operation,...
定义 ipc.c:105
#define RT_OBJECT_HOOK_CALL(func, argv)
rt_inline int rt_list_isempty(const rt_list_t *l)
tests whether a list is empty
rt_thread_t rt_thread_self(void)
This function will return self thread object.
void rt_schedule(void)
This function will perform one scheduling. It will select one thread with the highest priority level ...
#define LOG_D(...)
int rt_bool_t
#define RT_TRUE
const char * name
rt_uint16_t value
rt_uint16_t max_value

引用了 LOG_D, rt_semaphore::max_value, rt_object::name, rt_ipc_object::parent, rt_semaphore::parent, rt_thread::parent, RT_ASSERT, RT_FALSE, rt_list_isempty(), RT_NULL, RT_Object_Class_Semaphore, rt_object_get_type(), RT_OBJECT_HOOK_CALL, rt_schedule(), rt_spin_lock_irqsave(), rt_spin_unlock_irqrestore(), rt_susp_list_dequeue(), rt_thread_self(), RT_TRUE, rt_semaphore::spinlock, rt_ipc_object::suspend_thread , 以及 rt_semaphore::value.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [9/10]

RTM_EXPORT ( rt_sem_release )

引用了 rt_sem_release().

+ 函数调用图:

◆ rt_sem_control()

rt_err_t rt_sem_control ( rt_sem_t sem,
int cmd,
void * arg )

This function will set some extra attributions of a semaphore object.

注解
Currently this function only supports the RT_IPC_CMD_RESET command to reset the semaphore.
参数
semis a pointer to a semaphore object.
cmdis a command word used to configure some attributions of the semaphore.
argis the argument of the function to execute the command.
返回
Return the operation status. When the return value is RT_EOK, the operation is successful. If the return value is any other values, it means that this function failed to execute.

在文件 ipc.c759 行定义.

760{
761 rt_base_t level;
762
763 /* parameter check */
764 RT_ASSERT(sem != RT_NULL);
766
767 if (cmd == RT_IPC_CMD_RESET)
768 {
769 rt_ubase_t value;
770
771 /* get value */
772 value = (rt_uintptr_t)arg;
773 level = rt_spin_lock_irqsave(&(sem->spinlock));
774
775 /* resume all waiting thread */
777
778 /* set new value */
779 sem->value = (rt_uint16_t)value;
780 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
781 rt_schedule();
782
783 return RT_EOK;
784 }
785 else if (cmd == RT_IPC_CMD_SET_VLIMIT)
786 {
787 rt_ubase_t max_value;
788 rt_bool_t need_schedule = RT_FALSE;
789
790 max_value = (rt_uint16_t)((rt_uintptr_t)arg);
791 if (max_value > RT_SEM_VALUE_MAX || max_value < 1)
792 {
793 return -RT_EINVAL;
794 }
795
796 level = rt_spin_lock_irqsave(&(sem->spinlock));
797 if (max_value < sem->value)
798 {
800 {
801 /* resume all waiting thread */
803 need_schedule = RT_TRUE;
804 }
805 }
806 /* set new value */
807 sem->max_value = max_value;
808 rt_spin_unlock_irqrestore(&(sem->spinlock), level);
809
810 if (need_schedule)
811 {
812 rt_schedule();
813 }
814
815 return RT_EOK;
816 }
817
818 return -RT_ERROR;
819}
#define RT_IPC_CMD_RESET
#define RT_IPC_CMD_SET_VLIMIT
rt_base_t rt_uintptr_t
unsigned short rt_uint16_t

引用了 rt_semaphore::max_value, rt_ipc_object::parent, rt_semaphore::parent, RT_ASSERT, RT_FALSE, RT_IPC_CMD_RESET, RT_IPC_CMD_SET_VLIMIT, rt_list_isempty(), RT_NULL, RT_Object_Class_Semaphore, rt_object_get_type(), rt_schedule(), RT_SEM_VALUE_MAX, rt_spin_lock_irqsave(), rt_spin_unlock_irqrestore(), rt_susp_list_resume_all(), RT_TRUE, rt_semaphore::spinlock, rt_ipc_object::suspend_thread , 以及 rt_semaphore::value.

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ RTM_EXPORT() [10/10]

RTM_EXPORT ( rt_sem_control )

引用了 rt_sem_control().

+ 函数调用图: