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

Reply via email to