Pure Novice DSP programming--5.30--Communication and synchronization of tasks

Source: Internet
Author: User
Tags data structures reserved semaphore

The management module provided in Dsp/bios for coordination between threads

1 MBX Mailbox Management module

The MBX module uses a set of functions to manage access to a mailbox through a handle to a mailbox object.

Usually mbx_pend function to wait for the message of the mailbox. This function can take a timeout parameter to control when the task is allowed to wait. If the parameter is set to Sys_forever, it causes the calling task to wait indefinitely for information in the mailbox. If the timeout value is 0, the Mbx_pend function returns immediately. The return value gives the mailbox whether the information is stored.

The function mbx_post is used to send messages to the mailbox, and if there is a task waiting for the message, the task becomes ready and the Mbx_post function will generate a task switch based on the priority level. If no task is waiting or there is room in the mailbox, the function Mbx_post simply stores the information in the mailbox and returns it.

Two task threads can exchange data through a mailbox.

The mailbox size is in: Word

(1) Mbx_handle MBX = mbx_create (uns msgsize, uns mbxlength, Mbx_attrs * attrs)

Information size, mailbox length, current mailbox parameters not defined (reserved), this function calls the Mem_alloc function to create a mailbox data structure

struct mbx_attrs{

int segid; Default is 0

}

(2) void Mbx_delete (Mbx_handle MBX)

Delete the specified mailbox, call Mem_free to free storage space

(3) BOOL status = Mbx_pend (Mbx_handle MBX, Ptr msg, Uns timeout)

MSG is a pointer to the mailbox information that is returned if the wait time is greater than timeout. If there is information in the mailbox, this function copies the first message to the storage space referred to by MSG, and returns True, otherwise the function suspends the current task until it times out or calls the Mbx_post function.

If timeout takes sys_forever, the current task is suspended until Mbx_post is called, and if timeout is 0, it is returned directly.

(4) BOOL status = Mbx_post (Mbx_handle MBX, Ptr msg, Uns timeout)

Before writing the information to the mailbox, this function needs to check whether the mailbox has space to accommodate the new information, and if so, write and return it. When a function is called, the task switches if a higher priority task is in place, or if the mailbox is full and timeout is not 0.

Similarly, if timeout takes sys_forever, the current task is suspended until Mbx_pend is called, and if timeout is 0, it is returned directly.

2 SEM semaphore management module

The SEM semaphore module uses a set of functions to manage the use of semaphores through a handle to a semaphore object.

The semaphore provided by Dsp/bios is actually a semaphore semaphore, which completes the synchronization and interaction of the task thread by counting the semaphore.

Sem_post is similar to Mbx_post. The difference is that if no task is waiting for the semaphore, the function simply adds 1 to the semaphore counter and returns.

Semaphores are data structures defined by the Dsp/bios kernel, used for communication between task threads, synchronizing and accessing shared data, and when a task thread waits for a semaphore signal, it pauses execution and generates a task switch. The thread task transitions to ready state, pending execution, only when another task sends a semaphore signal.

(1) int count = Sem_count (Sem_handle SEM)

Returns the current value of the semaphore counter developed by SEM

(2) int count = sem_create (int count, Sem_attrs * attrs)

Incoming semaphore semaphore signal meter value and attribute parameter (currently undefined, reserved) Success returns the object handle, otherwise null

(3) void Sem_delete (Sem_handle SEM)

Deletes the specified semaphore, and calls the Mem_free function to free up space

(4) void Sem_ipost (Sem_handle SEM)

Makes the task waiting for the semaphore to be ready by blocking state (Blocked). If there is no task waiting for the semaphore, the function simply adds 1 to the semaphore counter and returns.

This function is similar to the Sem_post function, which generally uses the Sem_ipost function in SWI or HWI and uses the Sem_post function in the task thread.

(5) void Sem_new (Sem_handle SEM, int count)

Initializes a counter for the specified semaphore object, which can only be initialized with a statically created semaphore counter, which does not occur when the task is switched.

(6) BOOL status = Sem_pend (Sem_handle SEM, Uns timeout)

If the semaphore counter is greater than 0, this function returns true for the semaphore minus 1, otherwise pauses the current task until the semaphore of the function is reached.

After timeout time, the paused task becomes ready, and if timeout equals sys_forever, you must have the Sem_post function to cancel.

If timed out, the function returns FALSE. If the semaphore counter is 0 and the timeout parameter is not 0, the task switch

(7) void Sem_post (Sem_handle SEM)

Similar to the Sem_ipost function.

(8) void Sem_reset (Sem_handle SEM, int count)

Reset semaphore counter and start counting again, this function does not occur task switching.

3 que queue management module

Overview: The Que module manages a series of queue operation functions through access to queue handles.

Each queue contains 0 or more ordered element items, where each element item is a struct variable.

Its first member is a variable of type Que_elem, which is used as an internal pointer.

(1) Que_handle queue = que_create (Que_attrs * attrs)

Queue attribute parameters are currently reserved. The new queue object handle was successfully returned, and Null was returned for failure.

(2) void Que_delete (Que_handle queue)

Delete queue

(3) Ptr Elem = que_dequeue (que_handle queue)

Deletes the first element item in the queue and returns a pointer to the item, which is a pointer to the struct body, which must be a member of the

A member of the Que_elem type.

Note: When multitasking shares a queue: Use the Que_get function, which disables interrupts when the function takes elements.

(4) bool empty = Que_empty (Que_handle queue)

Determine if the queue is empty

(5) void Que_enqueue (Que_handle queue, Ptr elem)

Inserts an element item at the end of the queue, and the parameter elem is a pointer to the struct body.

Note: When multitasking shares a queue: Use the Que_put function, which disables interrupts when the function takes elements.

(6) void * Elem = que_get (que_handle queue)

If the queue is not empty, this function deletes the first element item and returns a pointer to it, returning the queue itself if the queue is empty.

method to determine whether the queue is empty:

if ((que_handle) (Elem = Que_get (q))!=q)//queue non-empty

(7) Que_elem * Elem = que_head (que_handle queue)

Returns a pointer to the most forward element in the queue, with the queue empty, returning the queue itself.

(8) void Que_insert (ptr qelem, ptr elem)

When you insert a new element item in front of the qelem of the original queue Elem, a multitasking Shared queue, this function should be used in conjunction with some functions that avoid conflicts.

(9) void Que_new (Que_handle queue)

Initializes the specified queue object so that the queue becomes empty.

This queue is initialized when the queue is created statically using the variable description method. If the queue turns out to be empty, its elements are not processed, but abandoned.

(10) PTR elem = Que_next (ptr qelem)

Returns a pointer to the next element item of the element Qelem, which is used in conjunction with a number of functions that avoid conflicts when multitasking shares a queue.

(11) PTR elem = Que_prev (ptr qelem)

Returns a pointer to the previous element item of an element Qelem, when a multitasking Shared queue, this function should be used in conjunction with some functions that avoid conflicts.

(a) void Que_put (Que_handle queue, void * elem)

add element items at the end of the queue, automatically disable interrupts

(+) void Que_remove (Ptr qelem)

Delete the element items in the queue, since the queue is a doubly linked list, do not delete the head node.

Shared resources and synchronization between tasks



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.