Skip to content

Commit

Permalink
For thread-debug builds, use error-checking mutexes for
Browse files Browse the repository at this point in the history
proc_mutex_pthread:

* locks/unix/proc_mutex.c (proc_mutex_pthread_create):
  [APR_THREAD_PROC]: Set the mutex type to ERRORCHECK.

* test/testprocmutex.c (test_exclusive): Skip the trylock/timedlock
  tests for thread-debug builds since it triggers undefined behaviour
  with proc_pthread mutexes.
  • Loading branch information
notroj committed Jun 11, 2024
1 parent eecbe1f commit 38678e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions locks/unix/proc_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,18 @@ static apr_status_t proc_mutex_pthread_create(apr_proc_mutex_t *new_mutex,
}
#endif /* HAVE_PTHREAD_MUTEX_ROBUST[_NP] */

#if defined(APR_THREAD_DEBUG)
/* ignore errors. */
if ((rv = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK))) {
#ifdef HAVE_ZOS_PTHREADS
rv = errno;
#endif
proc_mutex_pthread_cleanup(new_mutex);
pthread_mutexattr_destroy(&mattr);
return rv;
}
#endif

if ((rv = pthread_mutex_init(&proc_pthread_mutex(new_mutex), &mattr))) {
#ifdef HAVE_ZOS_PTHREADS
rv = errno;
Expand Down
14 changes: 14 additions & 0 deletions test/testprocmutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ static void test_exclusive(abts_case *tc, const char *lockname,

ABTS_ASSERT(tc, "Locks don't appear to work", *x == MAX_COUNTER);

#if defined(APR_THREAD_DEBUG)
/* The following tests attempt to (try-)lock a mutex which is
* already held by the process. This is undefined behaviour for a
* pthreads mutex, and with the ERRORCHECK mutex type enabled for
* APR_THREAD_DEBUG, fails with EDEADLOCK. (These tests may fail
* for other pthreads implementations as well) */
if (mech->num == APR_LOCK_PROC_PTHREAD
|| ((mech->num == APR_LOCK_DEFAULT || mech->num == APR_LOCK_DEFAULT_TIMED
&& APR_USE_PROC_PTHREAD_SERIALIZE))) {
fprintf(stderr, "skipping trylock tests for %s\n", mech->name);
return;
}
#endif

rv = apr_proc_mutex_trylock(proc_lock);
if (rv == APR_ENOTIMPL) {
fprintf(stderr, "%s_trylock() not implemented, ", mech->name);
Expand Down

0 comments on commit 38678e9

Please sign in to comment.