Index: src/libbu/CMakeLists.txt =================================================================== --- src/libbu/CMakeLists.txt (revision 70487) +++ src/libbu/CMakeLists.txt (working copy) @@ -105,6 +105,7 @@ redblack.c realpath.c semaphore.c + semaphore_cpp11mutex.cpp sha1.c simd.c sort.c Index: src/libbu/semaphore.c =================================================================== --- src/libbu/semaphore.c (revision 70487) +++ src/libbu/semaphore.c (working copy) @@ -84,6 +84,11 @@ static struct bu_semaphores bu_semaphores[SEMAPHORE_MAX] = {{0, SEMAPHORE_INIT}}; #endif +/* C++11 mutex based semaphores. */ +void semaphore_cpp11mutex_init(unsigned int nsemaphores); +void semaphore_cpp11mutex_free(); +void semaphore_cpp11mutex_acquire(unsigned int semaphore); +void semaphore_cpp11mutex_release(unsigned int semaphore); void bu_semaphore_init(unsigned int nsemaphores) @@ -92,6 +97,8 @@ if (nsemaphores) /* quellage */ return; return; /* No support on this hardware */ +#elif HAVE_THREAD_LOCAL + semaphore_cpp11mutex_init(nsemaphores); #else unsigned int i; @@ -172,6 +179,8 @@ #if !defined(PARALLEL) && !defined(DEFINED_BU_SEMAPHORES) return; /* No support on this hardware */ +#elif HAVE_THREAD_LOCAL + semaphore_cpp11mutex_free(); #else unsigned int i; @@ -209,6 +218,8 @@ if (i) /* quellage */ return; return; /* No support on this hardware */ +#elif HAVE_THREAD_LOCAL + semaphore_cpp11mutex_acquire(i); #else /* ensure we have this semaphore */ @@ -249,6 +260,8 @@ if (i) /* quellage */ return; return; /* No support on this hardware */ +#elif HAVE_THREAD_LOCAL + semaphore_cpp11mutex_release(i); #else /* ensure we have this semaphore */ Index: src/libbu/semaphore_cpp11mutex.cpp =================================================================== --- src/libbu/semaphore_cpp11mutex.cpp (nonexistent) +++ src/libbu/semaphore_cpp11mutex.cpp (working copy) @@ -0,0 +1,91 @@ +/* S E M A P H O R E _ C P P 1 1 M U T E X . C P P + * BRL-CAD + * + * Copyright (c) 2013-2016 United States Government as represented by + * the U.S. Army Research Laboratory. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; see the file named COPYING for more + * information. + */ + +#include "common.h" + +#include "bu/exit.h" + +#include +#include +#include +#include + + +static std::deque semaphores; +static std::vector lockedMutex; +static std::mutex initMutex; + + +extern "C" { + + +void +semaphore_cpp11mutex_init(unsigned int nsemaphores) +{ + std::lock_guard guard(initMutex); + if (nsemaphores > semaphores.size()) { + semaphores.resize(nsemaphores); + lockedMutex.resize(nsemaphores); + } +} + + +void +semaphore_cpp11mutex_free() +{ + semaphores.clear(); + lockedMutex.clear(); +} + + +void +semaphore_cpp11mutex_acquire(unsigned int semaphore) +{ + semaphore_cpp11mutex_init(semaphore + 1); + semaphores[semaphore].lock(); + lockedMutex[semaphore] = true; +} + + +void +semaphore_cpp11mutex_release(unsigned int semaphore) +{ + if (semaphore >= semaphores.size() || !lockedMutex[semaphore]) { + std::fprintf(stderr, "bu_semaphore_release(): semaphore_cpp11mutex_release() failed on [%d]\n", semaphore); + bu_bomb("fatal semaphore acquisition failure"); + } + + lockedMutex[semaphore] = false; + semaphores[semaphore].unlock(); +} + + +} + + +/* + * Local Variables: + * mode: C + * tab-width: 8 + * indent-tabs-mode: t + * c-file-style: "stroustrup" + * End: + * ex: shiftwidth=4 tabstop=8 + */