This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why to signal condvar with mutex held?


Hi,

Browsing eCos sources and documentation, I've observed that condvars are
usually being signalled with associated mutex locked. However, as far as
I understand, it is sub-optimal from the point of view of execution time
as it could lead to additional context switches. Consider the following
scenario:

1. thread1 signals condvar.
2. thread2 wakes up and preempts thread1 (context switch 1) only to see
   that the mutex is locked.
3. thread2 goes to wait on the locked mutex, thread1 is resumed (context
   switch 2).
4. thread1 unlocks mutex.
5. thread2 wakes up and preempts thread1 (context switch 3).

Thus, we end up with 3 context switches instead of 1.

So there are the questions:

1. Does this problem actually exist, or am I missing something [1]?

2. Is it allowed in eCos to signal/broadcast condvar with mutex
   unlocked? I almost sure it is, for signalling from DSR would be
   impossible if it weren't.

3. If both of the above are true, isn't it better to signal/broadcast
   with mutex unlocked?

Thanks in advance.

[1] E.g., it could be the case that optimization called "wait morphing"[2]
    is implemented in eCos, but I fail to see it.

[2] At least it's how David R. Butenhof calls it in its "Programming
    with POSIX Threads" book.

-- 
Sergei.


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]