首页 > 代码库 > MQX之任务间资源共享方式
MQX之任务间资源共享方式
关于MQX操作系统,也使用了一段时间了,一直想总结一下,今天就算开个头吧,希望后续整理一下思路,多做一些关于MQX的专题总结。言归正传!
在MQX应用程序中可采用如下方式实现任务间的资源共享:
1. 事件、轻量级事件
2. 信号量、轻量级信号
3. 互斥锁
4. 消息、轻量级消息
1.事件、轻量级事件
事件和轻量级事件是MQX的可选组件。他们是通过改变状态位实现。优点是低开销,反应快。适用于传递简单信息。如下:
一个任务创建一个事件组,同时创建一个任务。打开事件组。等待事件发生,当任务发生时清除任务标志位,执行事件发生后的操作。在另一个任务中,打开事件组,做该任务需要做的任务,当需要让这个事件发生时,设置事件位。
The example
The event example code shows how a task waits for an event. This event
can be set by any other process or interrupt in the system. The example
simulates an ISR event using another simple task.
Explanation of the example
The example application creates two tasks. The service_task task
creates an event group, opens it and enters a loop in which it waits
for an event bit. When the appropriate event bit is set, it clears it
and prints "Tick.". The simulated_ISR_task task opens a connection to
the event group and periodically sets the corresponding event bit with
a delay in between.
2.信号量、轻量级信号量
轻量级信号量是MQX的核心组件,通过低开销就可以实现对共享资源的同步访问。轻量级事件消耗的空间小并且反应快。仅通过FIFO方式统计轻量级信号量。
信号量是可选组件。计数信号量。可使用信号量同步任务,保护访问共享资源。信号量提供FIFO队列、优先级队列、优先级继承操作。
The lwsem example
The lwsem example code shows how lightweight semaphores works. The code is written in a way that two different semaphores are synchronized to ensure mutual exclusion of a common memory space.
Explaining the example
The light weight semaphores example uses four tasks:
1 read_task
3 write_task
The read_task starts by creating two lightweight semaphores (WRITE_SEM and
READ_SEM) that govern the access to a data memory location (FIFO.Data). The
WRITE_SEM starts enabled, while the REA_SEM is disabled.
Result = _lwsem_create(&fifo.READ_SEM, 0);
Result = _lwsem_create(&fifo.WRITE_SEM, 1);
Parameters:
&fifo.READ_SEM = pointer to the lightweight semaphore to create
0 = Initial semaphore counter
Then 3 write task are created with initial_data equal to ‘A’, ‘B’, ‘C’ correspondingly.
If the READ_SEM is available (_lwsem_wait), the read_task prints on the HyperTerminal
whatever the shared memory space contains.
Result = _lwsem_wait(&fifo.READ_SEM);
putchar(‘\n’);
putchar(fifo.DATA);
Finally the WRITE_SEM is posted
_lwsem_post(&fifo.WRITE_SEM);
The write task verifies if the WRITE_SEM is available (_lwsem_wait), then writes at the
shared memory space, task initial_value.
Result = _lwsem_wait(&fifo.WRITE_SEM)
fifo.DATA = (uchar)initial_data;
Finally the READ_SEM is posted
_lwsem_post(&fifo.READ_SEM);
The following figure shows how the tasks behave in the lwsem example.
3.互斥锁
互斥锁是一个可选组件。当任务访问共享资源时,互斥锁提供了任务之间的相互排斥。互斥对象提供轮询、FIFO排队、优先级排队、spin-only limited-spin排队,优先级继承,优先级保护。任务不能解锁互斥锁,除非它首次锁定的互斥锁。
The example
The mutex example code is used to demonstrate how to use a mutex to
synchronize two tasks. It creates a mutex and two tasks. Both tasks use
STDOUT to print out messages. Each task will lock the mutex before
printing and unlock it after printing to ensure that the outputs from
tasks are not mixed together.
Explanation of the example
The application demo creates the main task first. The main task then
creates two print tasks. The flows of the tasks are described in the
next figure.
The main task first initializes the mutex with the following line:
_mutex_init(&print_mutex, &mutexattr)
Then the main task creates a print task with parameter sting1, and
creates a second print task with parameter strings withthe following
line:
_task_create(0, PRINT_TASK, (uint_32)string1);
_task_create(0, PRINT_TASK, (uint_32)string2);
Then the main task blocks itself.
The two print tasks both use the STDOUT. If they used it in the same
time, there would be conflicts, so a mutex is used to synchronize the
two tasks.
Each print task will try to lock the mutex before printing the message;
it will wait for the mutex as long as needed. Once the mutex is locked
it prints the message and then unlocks the mutex so that the other task
can lock it. This process is repeated indefinitely.
4.消息、轻量级消息队列
消息是一个可选组件,任务可以通过消息队列相互通信。每个任务打开自己的input-message队列,MQX创建队列时给消息队列分配队列ID。只有打开这个消息队列的任务才可以从这个队列中接收消息。任何任务如果知道消息队列的ID都可以发送消息到这个消息队列。任务从消息池分配消息。
轻量级消息队列是一个可选组件。它低开销实现标准MQX消息机制。任务从轻量级消息队列发送和接收消息。消息在消息池中的大小是确定的,是4字节的倍数。
The lwmsgq example
This client/server model shows communication and task synchronization
using message passing.
Server task initializes the message queues, creates three client tasks,
and then waits for a message.
After receiving a message, the task returns the message to the sender.
Client task sends a message to the server_task and then waits for a
reply.
Explanation of the example
The flow of the tasks is described in the next figure. There is a
server task that receives all the incoming data and responds to them.
There are three client tasks. Each client sends a message to the server
and receives the answer back from it.
上述例程所涉及代码可参考mqx/examples例程。
本文出自 “KenoTu” 博客,请务必保留此出处http://kenotu.blog.51cto.com/4006069/1584451
MQX之任务间资源共享方式