Only define static locks in multithreaded mode

Newlib build system defines __SINGLE_THREAD__ to allow concurrency code
to be only compiled when newlib is configured for multithread. One such
example are locks which become useless in single thread mode. Although
most static locks are indeed guarded by !defined(__SINGLE_THREAD__),
some are not.

This commit adds these missing guards to __dd_hash_mutex,
__atexit_recursive_mutex, __at_quick_exit_mutex and __arc4random_mutex.
It also makes sure locking macros in lock.h are noop in single thread
mode.
This commit is contained in:
Thomas Preud'homme 2017-01-30 11:23:00 +00:00 committed by Jeff Johnston
parent af272aca59
commit fa55c610fa
5 changed files with 28 additions and 6 deletions

View File

@ -70,7 +70,7 @@ struct ddloc {
static long dd_loccnt = 1; /* Index of entry for sequential readdir's */
static struct ddloc *dd_hash[NDIRHASH]; /* Hash list heads for ddlocs */
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__LOCK_INIT(static, __dd_hash_mutex);
#endif
@ -92,7 +92,9 @@ _DEFUN(telldir, (dirp),
#ifdef HAVE_DD_LOCK
__lock_acquire_recursive(dirp->dd_lock);
#ifndef __SINGLE_THREAD__
__lock_acquire(__dd_hash_mutex);
#endif
#endif
index = dd_loccnt++;
lp->loc_index = index;
@ -102,7 +104,9 @@ _DEFUN(telldir, (dirp),
lp->loc_next = dd_hash[LOCHASH(index)];
dd_hash[LOCHASH(index)] = lp;
#ifdef HAVE_DD_LOCK
#ifndef __SINGLE_THREAD__
__lock_release(__dd_hash_mutex);
#endif
__lock_release_recursive(dirp->dd_lock);
#endif
return (index);
@ -123,7 +127,7 @@ _DEFUN(_seekdir, (dirp, loc),
register struct ddloc **prevlp;
struct dirent *dp;
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__lock_acquire(__dd_hash_mutex);
#endif
if (loc != 0) {
@ -136,7 +140,7 @@ _DEFUN(_seekdir, (dirp, loc),
lp = lp->loc_next;
}
if (lp == NULL) {
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__lock_release(__dd_hash_mutex);
#endif
return;
@ -162,7 +166,7 @@ found:
dirp->dd_seek = 0;
dirp->dd_loc = 0;
}
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__lock_release(__dd_hash_mutex);
#endif
}
@ -174,7 +178,7 @@ _DEFUN(_cleanupdir, (dirp),
{
int i;
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__lock_acquire(__dd_hash_mutex);
#endif
for (i = 0; i < NDIRHASH; ++i) {
@ -199,7 +203,7 @@ _DEFUN(_cleanupdir, (dirp),
}
dd_hash[i] = head.loc_next;
}
#ifdef HAVE_DD_LOCK
#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK)
__lock_release(__dd_hash_mutex);
#endif

View File

@ -11,7 +11,9 @@
/* Make this a weak reference to avoid pulling in free. */
void free(void *) _ATTRIBUTE((__weak__));
#ifndef __SINGLE_THREAD__
__LOCK_INIT_RECURSIVE(, __atexit_recursive_mutex);
#endif
#ifdef _REENT_GLOBAL_ATEXIT
struct _atexit *_global_atexit = _NULL;

View File

@ -180,16 +180,24 @@ arc4random(void)
{
uint32_t val;
#ifndef __SINGLE_THREAD__
_ARC4_LOCK();
#endif
_rs_random_u32(&val);
#ifndef __SINGLE_THREAD__
_ARC4_UNLOCK();
#endif
return val;
}
void
arc4random_buf(void *buf, size_t n)
{
#ifndef __SINGLE_THREAD__
_ARC4_LOCK();
#endif
_rs_random_buf(buf, n);
#ifndef __SINGLE_THREAD__
_ARC4_UNLOCK();
#endif
}

View File

@ -47,7 +47,9 @@
#endif /* _ARC4_LOCK_INIT */
#ifndef __SINGLE_THREAD__
_ARC4_LOCK_INIT
#endif
#ifdef _ARC4RANDOM_DATA
_ARC4RANDOM_DATA

View File

@ -44,7 +44,9 @@ struct quick_exit_handler {
/**
* Lock protecting the handlers list.
*/
#ifndef __SINGLE_THREAD__
__LOCK_INIT(static, __at_quick_exit_mutex);
#endif
/**
* Stack of cleanup handlers. These will be invoked in reverse order when
*/
@ -60,10 +62,14 @@ at_quick_exit(void (*func)(void))
if (NULL == h)
return (1);
h->cleanup = func;
#ifndef __SINGLE_THREAD__
__lock_acquire(__at_quick_exit_mutex);
#endif
h->next = handlers;
handlers = h;
#ifndef __SINGLE_THREAD__
__lock_release(__at_quick_exit_mutex);
#endif
return (0);
}