1

In iTron there is lighter version of the message-queue called data-queue. They are very easy to use for fast message transfers. Is there any equivalent synchronization primitive in Linux?

Definition of data-queue: It is a queue for one word messages

1
  • For those of us that are not familiar with iTron, could you please point us to a link that describes this "data-queue"? Commented Apr 13, 2009 at 19:27

2 Answers 2

2

On the few occassions I have needed to implement my own message queue, I tend to use 1 semaphore and 1 mutex(or semaphore) for each queue. I have only dealt with thread level queues, so this probably doesn't apply if you want a queue between two processes.

The semaphore is used to count the number of messages in the queue and provide the OS mechanism for thread to suspend/wait upon new messages.

The mutex is used to protect the overall queue structure.

So, it might look a bit like this (very much pseudo code):

DataQueueRx( Queue*, WORD*, timeout? )
{
   WaitOnSemaphore( Queue->sema, timeout? );  //get token
   LockMutex
   {
      //manipulate your queue, and transfer the data to WORD
   } 
   ReleaseMutex
}

DataQueueTx( Queue*, WORD )
{
   LockMutex
   {
      //manipulate your queue, inserting new WORD msg

      ReleaseSemaphore(Queue->sema);  //increment semaphore count
   }
   UnlockMutex
}

However, perhaps this isn't very "light weight". This also assumes that queues are not destroyed while in use. Also, I suspect that with a "WORD" only queue, there could be some optimizations.

If you are seeking "Lock-free code", then I suggest spending a day or two reading through these articles by Sutter.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know of any such primitive. I implemented an atomic queue using POSIX semaphores.

1 Comment

Thanks for the answer, A data-queue is actually a queue of size int (normally, 32 bits). And this is probably different from the atomic queue of Semaphores.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.