Comment (not an answer):
Is that what you mean?
Global:
// protected by m:
pthread_mutex_t m;
pthread_cond_t c;
bool about_to_pthread_cond_wait = false;
bool condition_waited_on = false;
Topic A:
pthread_mutex_lock (& ββm);
{// locked region
about_to_pthread_cond_wait = true;
while (condition_waited_on) {
// pthread_cond_wait (& m, & c) is decomposed here:
__pthread_mutex_cond_wait_then_unlock (& ββm, & c);
// unlocked region
pthread_mutex_lock (& ββm);
}
}
pthread_mutex_unlock (& ββm);
Theme B:
for (bool break_loop = false;! break_loop;) {
pthread_mutex_lock (& ββm);
{// locked region
condition_waited_on = true;
if (about_to_pthread_cond_wait) {
pthread_cond_signal (& c);
pthread_cond_destroy (& c);
break_loop = true;
}
}
pthread_mutex_unlock (& ββm);
pthread_yield ();
}
EDIT:
I assume pthread_cond_wait (&m, &c);
does:
__pthread_mutex_cond_wait_then_unlock (& ββm, & c);
pthread_mutex_lock (& ββm);
__pthread_mutex_cond_wait_then_unlock
will track the signals in the CV, then unlock the mutexes, then go into sleep mode.
If the CV has an internal mutex that __pthread_mutex_cond_wait_then_unlock
and pthread_cond_signal
should block internally, then I assume that __pthread_mutex_cond_wait_then_unlock
does:
pthread_mutex_lock (& ββc.int_mutex);
{// locked region for c.int_state
__register_wakeup_cond (& c.int_state);
pthread_mutex_unlock (& ββm);
}
pthread_mutex_unlock (& ββc.int_mutex);
// will not touch c.int_state any more
__sleep_until_registered_wakeups ();
and pthread_cond_signal
does:
pthread_mutex_lock (& ββc.int_mutex);
{// locked region for CV internal state
__wakeup_registered (& c.int_state);
}
pthread_mutex_unlock (& ββc.int_mutex);
Then pthread_cond_destroy
is called only after __pthread_mutex_cond_wait_then_unlock
completed using c.int_state
.
source share