關於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例程。