事件集
举例说明事件集:
1、A坐公交车去某地,只有一趟公交车去该地,等此公交车即可出发;
2、A坐公交车去某地,有三趟公交车去该地,等其中任意一辆公交车即可出发;
3、A约B一起去某地,则A必须等到"B到公交站"和"公交车到底公交站"两个条件都满足后,才出发。
此处,可将A去某地的行为看做一个线程,将"到达目的地公交车到达公交站"、"B到公交站"看做事件发生,情况1是特定事件唤醒线程;情况2是任意单个事件唤醒线程;情况3是多个事件同时发生才能唤醒线程。
信号量主要用于"一对一"的线程同步;
事件集可用于"一对多"、“多对一”、“多对多"的线程同步;
RT-Thread中的事件集用一个32位无符号整型变量来表示,变量中的一个位代表一个事件,线程通过"逻辑与”、“逻辑或”,与一个或多个事件建立联系,形成一个事件组合。
事件的"逻辑或"(也称独立型同步),指的是线程和任何事件之一发生同步,只要有一个事件发生,即满足条件;
事件的"逻辑与"(也称关联型同步),指的是线程和若干事件都发生同步,只有这些事件都发生,才满足条件;
事件集控制块:
/*
* event structure
*/
struct rt_event
{
struct rt_ipc_object parent; /**< inherit from ipc_object */
rt_uint32_t set; /**< event set */
};
typedef struct rt_event *rt_event_t;
定义静态事件集:struct rt_event static_evt
定义动态事件集:rt_event_t dynamic_evt
初始化与脱离
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag);//RT_IPC_FLAG_FIFO、RT_IPC_FLAG_PRIO
rt_err_t rt_event_detach(rt_event_t event);
创建与删除
rt_event_t rt_event_create(const char *name, rt_uint8_t flag);
rt_err_t rt_event_delete(rt_event_t event);
发送事件
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set);
set:某位或某几位的与或操作后的值,如0x01|0x08
接收事件
rt_err_t rt_event_recv(rt_event_t event,
rt_uint32_t set,
rt_uint8_t option,
rt_int32_t timeout,
rt_uint32_t *recved);
option:
RT_EVENT_FLAG_AND 0x01 /**< logic and */
RT_EVENT_FLAG_OR 0x02 /**< logic or */
RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */
官方示例代码:
/*
* 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
*/
/*
* 程序清单:事件例程
*
* 程序会初始化2个线程及初始化一个静态事件对象
* 一个线程等待于事件对象上,以接收事件;
* 一个线程发送事件 (事件3/事件5)
*/
#include <rtthread.h>
#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5
#define EVENT_FLAG3 (1 << 3)
#define EVENT_FLAG5 (1 << 5)
/* 事件控制块 */
static struct rt_event event;
ALIGN(RT_ALIGN_SIZE)
static char thread1_stack[1024];
static struct rt_thread thread1;
/* 线程1入口函数 */
static void thread1_recv_event(void *param)
{
rt_uint32_t e;
/* 第一次接收事件,事件3或事件5任意一个可以触发线程1,接收完后清除事件标志 */
if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: OR recv event 0x%x\n", e);
}
rt_kprintf("thread1: delay 1s to prepare the second event\n");
rt_thread_mdelay(1000);
/* 第二次接收事件,事件3和事件5均发生时才可以触发线程1,接收完后清除事件标志 */
if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: AND recv event 0x%x\n", e);
}
rt_kprintf("thread1 leave.\n");
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static struct rt_thread thread2;
/* 线程2入口 */
static void thread2_send_event(void *param)
{
rt_kprintf("thread2: send event3\n");
rt_event_send(&event, EVENT_FLAG3);
rt_thread_mdelay(200);
rt_kprintf("thread2: send event5\n");
rt_event_send(&event, EVENT_FLAG5);
rt_thread_mdelay(200);
rt_kprintf("thread2: send event3\n");
rt_event_send(&event, EVENT_FLAG3);
rt_kprintf("thread2 leave.\n");
}
int event_sample(void)
{
rt_err_t result;
/* 初始化事件对象 */
result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
if (result != RT_EOK)
{
rt_kprintf("init event failed.\n");
return -1;
}
rt_thread_init(&thread1,
"thread1",
thread1_recv_event,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
rt_thread_startup(&thread1);
rt_thread_init(&thread2,
"thread2",
thread2_send_event,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
rt_thread_startup(&thread2);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(event_sample, event sample);
运行结果: