Trouble with a mutex

paulgcoleman wrote on Friday, March 23, 2012:

I’ve got a simple RS232 library which I’m using for debug output but it doesn’t always do what I expect. I’ve created a function to initialise the port and within that I create a mutex to protect the resource. I have a number of threads, all of which have different priorities, and any of them are free to use the RS232 library calls. What I wanted to ensure was that if one thread is preempted by a higher priority thread whilst in the middle of sending a message, it’s able to finish its message before the higher priority one gets run.

Here is an over-simplified version of my code showing the mutex creation/calls…

static xSemaphoreHandle semRS232Tx = NULL; // mutex to protect the resource

RS232_STATUS RS232Init(void)
{

// create the mutex which protects writes and ensures reentrancy
semRS232Tx = xSemaphoreCreateMutex();
if(NULL == semRS232Tx)
return RS232_FATAL_ERROR;

return RS232_SUCCESS;
}

RS232_STATUS RS232WriteString(char *str)
{
if(pdFAIL == xSemaphoreTake(semRS232Tx, TIME_IN_MS(1000)))
return RS232_MUTEX_NOT_AVAILABLE;

// output character string here

xSemaphoreGive(semRS232Tx);

return RS232_SUCCESS;
}

Most of the time it works fine but occassionally I will get a higher priority thread message embedded in the middle of a low priority thread’s message and I don’t understand why. Can anybody see what I’ve done wrong?

Thanks, Paul.
P.S. Great little OS by the way. We looked at ThreadX which was going to cost us £4,000 just for the binaries and went with FreeRTOS in the end and it really has performed well :o)

richard_damon wrote on Friday, March 23, 2012:

I don’t see a problem here, but is there any possibility that the message was that was interrupted could have been created with two different calls to RS232WriteString? I know that I often use something like this for serial output but use a RECURSIVE mutex, so the low level I/O can claim the mutex, but if a task needs to piece together a message out of pieces, it can take the mutex itself to avoid the output being interrupted.

paulgcoleman wrote on Friday, March 23, 2012:

Thanks for the reply.
I have found my problem and it’s because I’m an idiot!! The messages which get interrupted are actually ones where there are multiple calls to the RS232WriteString and it’s getting in between the calls.
Somebody slap me…
Regards, Paul.

richard_damon wrote on Friday, March 23, 2012:

Which is why I was pointing out the possible change to a recursive mutex. That way you can guard even messages that need.use multiple calls (unless you WANT to be able to interrupt at break points to avoid holding up the higher priority task too long to get to the output routine.)

paulgcoleman wrote on Friday, March 23, 2012:

I’m not sure how that would help though as my RS232Write function has a take at the start and a give at the end. So if I had…

RS232WriteString(“string 1”);
RS232WriteString(“string 2”);
RS232WriteString(“string 3”);

A higher priority task would still be able to get in between the calls. It doesn’t cause me a problem though now that I understand why it happens.

Thanks, Paul.

richard_damon wrote on Friday, March 23, 2012:

If the mutex was a recursive mutex, and your code becomes

if(pdTRUE == xSemaphoreTakeRecursive(semRS232Tx, TIME_IN_MS(1000))){
  RS232WriteString(“string 1”);
  RS232WriteString(“string 2”);
  RS232WriteString(“string 3”);
  xSemaphoreGiveRecursive(semRS232Tx);
}

(and RS232WriteString and the init function are also changed for a RecursiveMutex)
then the Mutex isn’t given up until the give above (that is the difference with recursive muteness, the count the depth they are in use in a given task.

I often have a number of functions that write to the serial port (at the bottom a single char put, but above that writing strings, or numbers in various formats), and by using the recursive mutex, a task can take it if it wants to build a complex message, but if it just wants to send out a simple string or number, it can just call the appropriat output routine. Since the single character out routine also uses the mutex, it is impossible for to break into the middle of another tasks message. At worse, if a task is mis written then ITS messages might get broken up, but at least that points clearly to where the problem it.  The one exception is that if an ISR wants to put out a character, It might be able to break into the stream as it can’t be protected by a Mutex, but ISRs tend not to need to put much out, except maybe for some quick debugging (and then I need to realize that they might not get their data out if the serial port is full at the time).

paulgcoleman wrote on Friday, March 23, 2012:

Thanks again for the reply.

Yes I see what you mean but surely you could achieve the same thing as you’ve described by not having a mutex in the RS232Write function at all and just replace what you have above with a regular non-recursive mutex? Maybe I should have just done that in the first place!!

Regards, Paul.

richard_damon wrote on Friday, March 23, 2012:

Yes, you could have a single not recursive mutex, that you call outside the low level send. The reason I didn’t do that is for 2 reasons:

1) Using the recursive mutex, a task can’t forget to acquire the mutex and accidentally inject itself in another tasks output.
2) Often enough you are just sending a single item out, so that needing all the explicit calls is a bother.

By the time you include the fact that the tasks may well be limited by the speed of the serial port, the small extra overhead of the recursive mutex is minimal. If I am really worried about speed here, I shouldn’t be sending a message to a serial port.

paulgcoleman wrote on Friday, March 23, 2012:

Yes, fair point. Thanks for the info :o)

Regards, Paul.