关于MQX操做系统,也使用了一段时间了,一直想总结一下,今天就算开个头吧,但愿后续整理一下思路,多作一些关于MQX的专题总结。言归正传!app
在MQX应用程序中可采用以下方式实现任务间的资源共享:ide
1. 事件、轻量级事件oop
2. 信号量、轻量级信号post
3. 互斥锁ui
4. 消息、轻量级消息spa
事件和轻量级事件是MQX的可选组件。他们是经过改变状态位实现。优势是低开销,反应快。适用于传递简单信息。以下:操作系统
一个任务建立一个事件组,同时建立一个任务。打开事件组。等待事件发生,当任务发生时清除任务标志位,执行事件发生后的操做。在另外一个任务中,打开事件组,作该任务须要作的任务,当须要让这个事件发生时,设置事件位。code
The exampleserver
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.
轻量级信号量是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.
互斥锁是一个可选组件。当任务访问共享资源时,互斥锁提供了任务之间的相互排斥。互斥对象提供轮询、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.
消息是一个可选组件,任务能够经过消息队列相互通讯。每一个任务打开本身的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例程。