This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Using mutex.owner to detect re-entry
- From: Tom Schouten <tom at zwizwa dot be>
- To: ecos-discuss at ecos dot sourceware dot org
- Date: Tue, 10 Apr 2012 19:27:58 +0200
- Subject: [ECOS] Using mutex.owner to detect re-entry
Hi List,
Is it safe to use the following approach to use a mutex to detect re-entry?
( i.e. to avoid thread-local variables to keep an entry counter.)
static int re_entry(cyg_mutext_t *m) {
return (m->owner == (void*)cyg_thread_self());
}
As used in the following fragment:
cyg_mutex_t m;
int some_call() {
if (re_entry(&m)) {
/* Break off recursion. */
return error_re_entry;
}
else {
cyg_mutex_lock(&m);
int err = re_entry_protected_call(); /* Might call some_call(),
which should then return error_re_entry */
cyg_mutex_unlock(&);
return err;
}
}
Rationale: This is safe because (see mutex.cpp)
- If it's not self it's either NULL or the value of another thread, but
if this is the case it can never become self due to a race condition.
I.e. we're sure this is not a re-entrant call
- It's self if and only if it's a re-entrant call.
Cheers,
Tom
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss