Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63795 - trunk/boost/thread/win32
From: anthony_at_[hidden]
Date: 2010-07-09 17:15:58


Author: anthonyw
Date: 2010-07-09 17:15:57 EDT (Fri, 09 Jul 2010)
New Revision: 63795
URL: http://svn.boost.org/trac/boost/changeset/63795

Log:
Fix for issue #4225 to allow static initialization of boost::once_flag
Text files modified:
   trunk/boost/thread/win32/once.hpp | 118 +++++++++++++++++++++++++--------------
   trunk/boost/thread/win32/thread_primitives.hpp | 8 ++
   2 files changed, 82 insertions(+), 44 deletions(-)

Modified: trunk/boost/thread/win32/once.hpp
==============================================================================
--- trunk/boost/thread/win32/once.hpp (original)
+++ trunk/boost/thread/win32/once.hpp 2010-07-09 17:15:57 EDT (Fri, 09 Jul 2010)
@@ -35,41 +35,85 @@
         long status;
         long count;
         long throw_count;
- void* event_handle;
+ };
+
+#define BOOST_ONCE_INIT {0,0,0}
+
+ namespace detail
+ {
+#ifdef BOOST_NO_ANSI_APIS
+ typedef wchar_t once_char_type;
+#else
+ typedef char once_char_type;
+#endif
+ unsigned const once_mutex_name_fixed_length=54;
+ unsigned const once_mutex_name_length=once_mutex_name_fixed_length+
+ sizeof(void*)*2+sizeof(unsigned long)*2+1;
 
- ~once_flag()
+ template <class I>
+ void int_to_string(I p, once_char_type* buf)
         {
- if(count)
+ for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
             {
- BOOST_ASSERT(count==throw_count);
+#ifdef BOOST_NO_ANSI_APIS
+ once_char_type const a=L'A';
+#else
+ once_char_type const a='A';
+#endif
+ *buf = a + static_cast<once_char_type>((p >> (i*4)) & 0x0f);
             }
+ *buf = 0;
+ }
+
+ inline void name_once_mutex(once_char_type* mutex_name,void* flag_address)
+ {
+#ifdef BOOST_NO_ANSI_APIS
+ static const once_char_type fixed_mutex_name[]=L"Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
+#else
+ static const once_char_type fixed_mutex_name[]="Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
+#endif
+ BOOST_STATIC_ASSERT(sizeof(fixed_mutex_name) ==
+ (sizeof(once_char_type)*(once_mutex_name_fixed_length+1)));
             
- void* const old_event=BOOST_INTERLOCKED_EXCHANGE_POINTER(&event_handle,0);
- if(old_event)
+ std::memcpy(mutex_name,fixed_mutex_name,sizeof(fixed_mutex_name));
+ detail::int_to_string(reinterpret_cast<std::ptrdiff_t>(flag_address),
+ mutex_name + once_mutex_name_fixed_length);
+ detail::int_to_string(win32::GetCurrentProcessId(),
+ mutex_name + once_mutex_name_fixed_length + sizeof(void*)*2);
+ }
+
+ inline void* open_once_event(once_char_type* mutex_name,void* flag_address)
+ {
+ if(!*mutex_name)
             {
- ::boost::detail::win32::CloseHandle(old_event);
+ name_once_mutex(mutex_name,flag_address);
             }
+
+#ifdef BOOST_NO_ANSI_APIS
+ return ::boost::detail::win32::OpenEventW(
+#else
+ return ::boost::detail::win32::OpenEventA(
+#endif
+ ::boost::detail::win32::synchronize |
+ ::boost::detail::win32::event_modify_state,
+ false,
+ mutex_name);
         }
- };
-
-#define BOOST_ONCE_INIT {0,0,0,0}
 
- namespace detail
- {
- inline void* allocate_event_handle(void*& handle)
+ inline void* create_once_event(once_char_type* mutex_name,void* flag_address)
         {
- void* const new_handle=::boost::detail::win32::create_anonymous_event(
- ::boost::detail::win32::manual_reset_event,
- ::boost::detail::win32::event_initially_reset);
-
- void* event_handle=BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&handle,
- new_handle,0);
- if(event_handle)
+ if(!*mutex_name)
             {
- ::boost::detail::win32::CloseHandle(new_handle);
- return event_handle;
+ name_once_mutex(mutex_name,flag_address);
             }
- return new_handle;
+#ifdef BOOST_NO_ANSI_APIS
+ return ::boost::detail::win32::CreateEventW(
+#else
+ return ::boost::detail::win32::CreateEventA(
+#endif
+ 0,::boost::detail::win32::manual_reset_event,
+ ::boost::detail::win32::event_initially_reset,
+ mutex_name);
         }
     }
     
@@ -83,8 +127,10 @@
         long const running_value=0x7f0725e3;
         long status;
         bool counted=false;
- void* event_handle=0;
+ detail::win32::handle_manager event_handle;
         long throw_count=0;
+ detail::once_char_type mutex_name[detail::once_mutex_name_length];
+ mutex_name[0]=0;
 
         while((status=::boost::detail::interlocked_read_acquire(&flag.status))
               !=function_complete_flag_value)
@@ -96,7 +142,7 @@
                 {
                     if(!event_handle)
                     {
- event_handle=::boost::detail::interlocked_read_acquire(&flag.event_handle);
+ event_handle=detail::open_once_event(mutex_name,&flag);
                     }
                     if(event_handle)
                     {
@@ -112,7 +158,7 @@
                     if(!event_handle &&
                        (::boost::detail::interlocked_read_acquire(&flag.count)>1))
                     {
- event_handle=::boost::detail::allocate_event_handle(flag.event_handle);
+ event_handle=detail::create_once_event(mutex_name,&flag);
                     }
                     if(event_handle)
                     {
@@ -130,7 +176,7 @@
                     BOOST_INTERLOCKED_EXCHANGE(&flag.status,0);
                     if(!event_handle)
                     {
- event_handle=::boost::detail::interlocked_read_acquire(&flag.event_handle);
+ event_handle=detail::open_once_event(mutex_name,&flag);
                     }
                     if(event_handle)
                     {
@@ -149,31 +195,15 @@
                 {
                     break;
                 }
- event_handle=::boost::detail::interlocked_read_acquire(&flag.event_handle);
                 if(!event_handle)
                 {
- event_handle=::boost::detail::allocate_event_handle(flag.event_handle);
+ event_handle=detail::create_once_event(mutex_name,&flag);
                     continue;
                 }
             }
             BOOST_VERIFY(!::boost::detail::win32::WaitForSingleObject(
                              event_handle,::boost::detail::win32::infinite));
         }
- if(counted || throw_count)
- {
- if(!BOOST_INTERLOCKED_EXCHANGE_ADD(&flag.count,(counted?-1:0)-throw_count))
- {
- if(!event_handle)
- {
- event_handle=::boost::detail::interlocked_read_acquire(&flag.event_handle);
- }
- if(event_handle)
- {
- BOOST_INTERLOCKED_EXCHANGE_POINTER(&flag.event_handle,0);
- ::boost::detail::win32::CloseHandle(event_handle);
- }
- }
- }
     }
 }
 

Modified: trunk/boost/thread/win32/thread_primitives.hpp
==============================================================================
--- trunk/boost/thread/win32/thread_primitives.hpp (original)
+++ trunk/boost/thread/win32/thread_primitives.hpp 2010-07-09 17:15:57 EDT (Fri, 09 Jul 2010)
@@ -31,14 +31,18 @@
             unsigned const infinite=INFINITE;
             unsigned const timeout=WAIT_TIMEOUT;
             handle const invalid_handle_value=INVALID_HANDLE_VALUE;
+ unsigned const event_modify_state=EVENT_MODIFY_STATE;
+ unsigned const synchronize=SYNCHRONIZE;
 
 # ifdef BOOST_NO_ANSI_APIS
             using ::CreateMutexW;
             using ::CreateEventW;
+ using ::OpenEventW;
             using ::CreateSemaphoreW;
 # else
             using ::CreateMutexA;
             using ::CreateEventA;
+ using ::OpenEventA;
             using ::CreateSemaphoreA;
 # endif
             using ::CloseHandle;
@@ -100,6 +104,8 @@
             unsigned const infinite=~0U;
             unsigned const timeout=258U;
             handle const invalid_handle_value=(handle)(-1);
+ unsigned const event_modify_state=2;
+ unsigned const synchronize=0x100000u;
 
             extern "C"
             {
@@ -108,10 +114,12 @@
                 __declspec(dllimport) void* __stdcall CreateMutexW(_SECURITY_ATTRIBUTES*,int,wchar_t const*);
                 __declspec(dllimport) void* __stdcall CreateSemaphoreW(_SECURITY_ATTRIBUTES*,long,long,wchar_t const*);
                 __declspec(dllimport) void* __stdcall CreateEventW(_SECURITY_ATTRIBUTES*,int,int,wchar_t const*);
+ __declspec(dllimport) void* __stdcall OpenEventW(unsigned long,int,wchar_t const*);
 # else
                 __declspec(dllimport) void* __stdcall CreateMutexA(_SECURITY_ATTRIBUTES*,int,char const*);
                 __declspec(dllimport) void* __stdcall CreateSemaphoreA(_SECURITY_ATTRIBUTES*,long,long,char const*);
                 __declspec(dllimport) void* __stdcall CreateEventA(_SECURITY_ATTRIBUTES*,int,int,char const*);
+ __declspec(dllimport) void* __stdcall OpenEventA(unsigned long,int,char const*);
 # endif
                 __declspec(dllimport) int __stdcall CloseHandle(void*);
                 __declspec(dllimport) int __stdcall ReleaseMutex(void*);


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk