信号量
在线程运行的过程中,他们之间有的需要按先后顺序进行同步,有的需要互斥进行资源访问,有时需要线程间进行数据交换等,这些在操作系统中称为进(线)程间通信(Internal Process Communication,简称IPC ),RT-Thread中的IPC机制包括:信号量、互斥量、事件、邮箱、消息队列;
-
信号量是一种轻型的用于解决线程间同步问题的内核对象,通过线程的获取和释放达到同步或互斥的目的。
信号量工作示意图:
从示意图可看到,每个信号量对象都有一个信号量值和一个线程等待队列,信号量的值对应信号量对象的实例数目(资源数目),假如信号量值为N,则表示共有N个信号量实例(资源)可以被使用,当信号量实例数目为零时,再申请该信号量的线程就会被挂起到该信号量的等待队列上,等待可用的信号量实例(资源); -
PS:可以想象成,信号量是停车场管理员(包含空余停车位数量、满/空等信息),需要进入停车场的车辆为各个线程,没空位就按停车场管理员的要求排队,有车位就根据管理员反馈的数量进入停车场;
-
信号量控制块
struct rt_semaphore
{
struct rt_ipc_object parent; /**<inherit from ipc_object */
rt_uint16_t value; /**<value of semaphore */
}
定义静态信号量:struct_semaphore static_sem
定义动态信号量:rt_sem_t dynamic_sem
- 信号量的操作
初始化与脱离:
rt_err_t rt_sem_init(rt_sem_t sem , const char *name , rt_uint32_t value , rt_uint8_t flag);
rt_err_t rt_sem_detach(rt_sem_t sem);
创建与删除:
rt_sem_t rt_sem_create(const char *name , rt_uint32_t value , rt_uint8_t flag);
//flag: RT_IPC_FLAG_FIFO、RT_IPC_FLAG_PRIO
rt_err_t rt_sem_delete(rt_sem_t sem);
获取信号量:
rt_err_t rt_sem_take(rt_sem_t sem , rt_int32_t time);//RT_WAITING_FOREVER = -1
rt_err_t rt_sem_trytake(rt_sem_t sem);
释放信号量:
rt_err_t rt_sem_release(rt_sem_t sem);
官方示例代码:
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-24 yangjie the first version
*/
/*
* 程序清单:信号量例程
*
* 该例程创建了一个动态信号量,初始化两个线程,线程1在count每计数10次时,
* 发送一个信号量,线程2在接收信号量后,对number进行加1操作
*/
#include <rtthread.h>
#define THREAD_PRIORITY 25
#define THREAD_TIMESLICE 5
/* 指向信号量的指针 */
static rt_sem_t dynamic_sem = RT_NULL;
ALIGN(RT_ALIGN_SIZE)
static char thread1_stack[1024];
static struct rt_thread thread1;
static void rt_thread1_entry(void *parameter)
{
static rt_uint8_t count = 0;
while(1)
{
if(count <= 100)
{
count++;
}
else
return;
/* count每计数10次,就释放一次信号量 */
if(0 == (count % 10))
{
rt_kprintf("t1 release a dynamic semaphore.\n" );
rt_sem_release(dynamic_sem);
}
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static struct rt_thread thread2;
static void rt_thread2_entry(void *parameter)
{
static rt_err_t result;
static rt_uint8_t number = 0;
while(1)
{
/* 永久方式等待信号量,获取到信号量,则执行number自加的操作 */
result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
rt_kprintf("t2 take a dynamic semaphore, failed.\n");
rt_sem_delete(dynamic_sem);
return;
}
else
{
number++;
rt_kprintf("t2 take a dynamic semaphore. number = %d\n" ,number);
}
}
}
/* 信号量示例的初始化 */
int semaphore_sample()
{
/* 创建一个动态信号量,初始值是0 */
dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
if (dynamic_sem == RT_NULL)
{
rt_kprintf("create dynamic semaphore failed.\n");
return -1;
}
else
{
rt_kprintf("create done. dynamic semaphore value = 0.\n");
}
rt_thread_init(&thread1,
"thread1",
rt_thread1_entry,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
rt_thread_startup(&thread1);
rt_thread_init(&thread2,
"thread2",
rt_thread2_entry,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY-1, THREAD_TIMESLICE);
rt_thread_startup(&thread2);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(semaphore_sample, semaphore sample);
运行结果: