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. 

wKioL1R5WrnQcwl7AAGgpp9_iEA737.jpg









 

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

wKioL1R5WrrxOwhFAAFj3gxgoW0168.jpg 

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);

wKiom1R5WjXjXxjaAADtUPbUEP0582.jpg 

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.

 

wKioL1R5WrvykEOfAAFrPmGGiSw311.jpg

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.

wKioL1R5WrehmdObAAFyk5zBLe0738.jpg 

wKiom1R5WjKjnxfBAADL6U2jTtQ478.jpg

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.

wKiom1R5WjPiMZ9DAAFhzqM3nZE355.jpg 

上述例程所涉及代碼可參考mqx/examples例程。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章