Linux "complete()" and "wait_for_completion_t

Hi Folks,

I’m porting a Linux driver to QNX. The driver uses “complete(struct completion *x)” and “wait_for_completion_timeout(struct completion *x, unsigned long timeout)” kernel call in Linux. What is the best way to implement these functions in QNX? Can I just use semaphore to implement block waiting?

Thanks!
Yicheng

See:
lwn.net/Articles/23993/
lwn.net/Articles/120614/

Looks like sem_timedwait() might be a good fit.

Nope. Semaphores should never be used in a real-time system (they are not priority conveying).

Use pthread_sleepon_timedwait().

So if a higher priority thread waits for a semaphore which has already been locked by a lower priority thread, the lower one won’t be promoted to the same priority level as the higher one, is it the meaning of priority conveying? Can I use semaphore if the two threads have the same priority?

Yes.

Not unless you have a really good reason; and absolutely never in any program that ever makes a blocking call which could result in an elevated priority (since the presence of the semaphore would break the “promise” made by your thread when it “accepted” that priority, that it would complete all work at that elevated priority - a promise it can’t keep if it uses a semaphore).

Unless you enjoy arduous debugging sessions, I’d stay as far away from semaphores as I would from a leaky flask full of ebola virus…

Then what synchronization primitive should be used to replace the “counting semaphore”, which has positive value more than 1?

None. There is nothing in Posix threads that replaces the counting semaphore, precisely because priority inheritance can’t operate with a model that has no identifiable owner.

Any architecture that uses a counting semaphore structure, can be implemented with a struct that contains a mutex, count and condvar.