[En-Nut-Discussion] Handling of condition variables broken?

Philipp Burch phip at hb9etc.ch
Thu Dec 27 22:56:45 CET 2012


Hi all,

today I needed to do some thread synchronization, which I'd liked to do 
using a condition variable. But there are a few problems with this. 
First, have a look at the implementation of the init function:

int NutConditionInit( CONDITION * cond)
{
     cond = NutHeapAlloc(sizeof(struct _CONDITION));
     if (cond == NULL) return 1;
     NutMutexInit(&cond->mutex);
     return 0;
}

Given the CONDITION declaration as

typedef struct _CONDITION CONDITION;

this will not work. NutHeapAlloc() may happily allocate memory and store 
it into cond, but there is no way to find out where the memory region is 
located. Either it should be returned as a pointer or the function must 
take a double pointer as argument.

Then there's NutConditionTimedWait() which advertises its return value 
as "0 on success, -1 on error or timeout". But:

int NutConditionTimedWait(CONDITION * cond, uint32_t abs_ms)
{
     uint32_t ms;

     if (cond == NULL) return -1;
     ms = abs_ms - NutGetMillis();
     if (ms > 0x7FFFFFFF) return -1;

     NutMutexUnlock(&cond->mutex);
     NutEventWait(&cond->event, ms);
     NutMutexLock(&cond->mutex);
     return 0;
}

A timeout will never cause the function to return -1. Additionally, this 
function is missing in the header file <sys/condition.h>. The example in 
the doxygen doc is also extremely outdated.

Could it be that there has been the idea to restructure some of this 
module but it was never finished?

Anyway, I'll change my code to use bare event queues instead. But this 
code should either be corrected or removed.

Thanks and best regards,
Philipp


More information about the En-Nut-Discussion mailing list