lumiera_/src/lib/sync.cpp
Ichthyostega 08cae2617d fix insideous problem with mutex initialisation
explanation: we use pthread_once to define a mutex type descriptor,
used to define some of our mutexes as recursive mutexes. Now,
pthread_once relies on a counter stored in a given location;
we used a non-exported global var for this counter.

Unfortunately this ties the mutex initialisation to the static
initialisation of the compilation unit holding this counter variable.
Theoretically it would be possible (we never observed such an incident)
that, during static initialisation, a singleton was brought up,
which requires a class-scoped lock, implemented as recursive mutex.
And it would be possible for this singleton locking to happen prior
to initialisation of the mentioned counter variable.

As a fix, I've moved the counter varialbe into a function scoped
static variable, since that is guaranteed by the C++ runtime system
to be initialised at first usage of the function, irrespective of the
initialisation order of the enclosing compilation units
2013-10-13 01:48:27 +02:00

72 lines
1.9 KiB
C++

/*
Sync - generic helper for object based locking and synchronisation
Copyright (C) Lumiera.org
2011, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
/** @file sync.cpp
** This compilation unit holds the static attribute struct
** for initialising pthread's recursive mutex.
**
*/
#include "lib/sync.hpp"
namespace lib {
namespace sync {
namespace { // private pthread attributes
pthread_mutexattr_t attribute_;
void
initAttribute()
{
pthread_mutexattr_init (&attribute_);
pthread_mutexattr_settype (&attribute_, PTHREAD_MUTEX_RECURSIVE);
}
inline pthread_mutexattr_t*
recursive_flag()
{
static pthread_once_t _is_init_sync_mutex_attribute_(PTHREAD_ONCE_INIT);
pthread_once (&_is_init_sync_mutex_attribute_, initAttribute);
return &attribute_;
}
}
/**
* @internal creating a recursive mutex.
* Defined here in a separate compilation unit,
* so it can refer to a single mutex attribute flag.
*/
Wrapped_RecursiveMutex::Wrapped_RecursiveMutex()
{
pthread_mutex_init (&mutex_, recursive_flag());
}
}}// lib::sync