Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is boost's interprocess mutex not robust on posix?

I'm very new to boost, and just trying to understand a small part of it - the interprocess mutex.

AFAIU, there's a robust mutex emulation in the cross platform implementation using file lock: http://www.boost.org/doc/libs/1_62_0/boost/interprocess/detail/robust_emulation.hpp

This is for emulating the robust mutex that's available in posix standard, but the generic approach will only be used when platform specific versions are not available.

#if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
   #include <boost/interprocess/sync/posix/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_POSIX
//Experimental...
#elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
   #include <boost/interprocess/sync/windows/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_WINDOWS
#elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
   #include <boost/interprocess/sync/spin/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION

But on posix system that actually supports robust mutexes (I guess it depends on the kernel version), robust mutex is actually not enabled. http://www.boost.org/doc/libs/1_62_0/boost/interprocess/sync/posix/pthread_helpers.hpp

So I don't really understand the logic here, is there any serious performance penalty with posix robust mutex implementation so that we don't want to use it? Even so, there should be at least an option to optionally enable this feature.

And to solve this problem, as I'm noob to boost, is there any way to make the generic implementation available over the posix implementation so that I can make use of the robustness?

like image 656
Shiva Wu Avatar asked Nov 09 '22 06:11

Shiva Wu


1 Answers

I just looked into boost implementation for wrapper class for intercrosses mutex in file boost/interprocess/sync/posix/pthread_helpers.hpp. I saw PTHREAD_MUTEX_ROBUST is missing. Please add ROBUST attributes as below and try.

mutexattr_wrapper(bool recursive = false)
 {
         if(pthread_mutexattr_init(&m_attr)!=0 ||
            pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
             (recursive &&
              pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ) ||
              pthread_mutexattr_setrobust(&m_attr, PTHREAD_MUTEX_ROBUST)!= 0)
            throw interprocess_exception("pthread_mutexattr_xxxx failed");
} 
like image 200
Aditya Kumar Avatar answered Nov 14 '22 21:11

Aditya Kumar