Tuesday, August 13, 2013

Data Queue Vs Mailbox: Creation

①Difference between Data queue and Mail box

In Data queue the data size is just one word and it is copied into the queue maintained by the kernel. Again when the receiver receives the data, it is copied to the receiver and removed from the queue. And, the queue size is limited (specified by the user). Since the queue size is limited, when there is no space, the sender will wait till one slot getting emptied. And, when there is no data, the receive task also kept waiting. So, two more queues are needed for the waiting tasks. But, receive task waits only in FIFO order. Sending task can be selected to wait either in Priority or FIFO order. When the data queue size is zero, the sending task will directly pass the data to the receive task. Which one coming earlier will wait for the other one. This is called 'synchronous message passing'.

But, in mailbox, the data is not copied, but the pointer to the data is passed to the receiver. And, the data buffer should include the space for the header to be maintained by the kernel. The number of entries are unlimited, since the kernel just ties the data buffers together into a list. So, there is no waiting on sender side. But, in mailbox, each message has a priority. So, when there is more than one priority level, the messages are delivered in priority order. And, the receive tasks also can be queued in priority order.
 
In data queue, the data does not have any priority.
 
Implementation wise, the data queue takes arguments of ① Number of data elements (data queue size) ② Memory area for the Data queue (if specified NULL, it will be allocated by kernel automatically) ③ Whether to order sending tasks in FIFO or Priority based order
 
Mailbox takes arguments of ① Number of priority levels of messages ② Memory for area for implementing the priority queues (if specified NULL, it will be allocated by kernel automatically) ③ Attribute to specify whether to order waiting receive tasks in FIFO or Priority based order

② ITRON implementation of creating data queue

ER cre_dtq(ID dtqid, T_CDTQ *pk_cdtq)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        Allocate memory for the data queue and send task queue;
        (for data queue, each element is sizeof(void pointer))
        Initialize the data queue structure;(data queue structure is as follows)

        In this function, start, head, tail parameters are initialized to start address
        end parameter is initialized to end address
        Counter for number of valid data elements is initialized to zero
        Initialize the receiving task waiting queue structure;(Only FIFO order. so,)
                Initialize queue as follows:
                ┏━━━━━┯━━━━━━┓
                ┃Queue   │Queue     ┃
                ┃[0]     │Tail       ┃
                ┗━━━━━┷━━━━━━┛
        Initialize the sending task waiting queue structure;
        If (tasks are queued in FIFO order)
                Initialize queue as follows:
                ┏━━━━━┯━━━━━━┓
                ┃Queue   │Queue     ┃
                ┃[0]     │Tail       ┃
                ┗━━━━━┷━━━━━━┛
        else (tasks are queued as priority based)
                Initialize as follows:
                ┏━━━━━━━━┯━━┯━━━━┯━━━━━━┓
                ┃Priority0      │1   │Max   │Queue    ┃
                ┃            │    │Pri    │Tail       ┃
                ┗━━━━━━━━┷━━┷━━━━┷━━━━━━┛
        Exit critical section (Interrupt Restore)
        return E_OK;
}

② ITRON implementation of creating mail box

ER cre_mbx(ID mbxid, const T_CMBX *pk_cmbx)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments;
        Enter critical section (Interrupt Disable);
        If maximum message priority has NOT been specified, keep it as 1;
        Allocate memory for the message priority queue and send task queue;
        Remember the points for message queue, maximum priority, etc;
        Initialize the message priority queue; Message queue is managed as follows:

        Initialize the task waiting queue structure;
        If (tasks are queued in FIFO order)
                Initialize queue as follows:
                ┏━━━━━┯━━━━━━┓
                ┃Queue   │Queue     ┃
                ┃[0]     │Tail       ┃
                ┗━━━━━┷━━━━━━┛
        else (tasks are queued as priority based)
                Initialize as follows:
                ┏━━━━━━━━┯━━┯━━━━┯━━━━━━┓
                ┃Priority0      │1   │Max   │Queue    ┃
                ┃            │    │Pri    │Tail       ┃
                ┗━━━━━━━━┷━━┷━━━━┷━━━━━━┛
        Exit critical section (Interrupt Restore)
        return E_OK;
}
 

No comments: