Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57882 - trunk/boost/thread/win32
From: anthony_at_[hidden]
Date: 2009-11-24 04:59:21


Author: anthonyw
Date: 2009-11-24 04:59:21 EST (Tue, 24 Nov 2009)
New Revision: 57882
URL: http://svn.boost.org/trac/boost/changeset/57882

Log:
Only allocate an event if there is contention in call_once
Text files modified:
   trunk/boost/thread/win32/once.hpp | 168 +++++++++++++++++++++------------------
   1 files changed, 92 insertions(+), 76 deletions(-)

Modified: trunk/boost/thread/win32/once.hpp
==============================================================================
--- trunk/boost/thread/win32/once.hpp (original)
+++ trunk/boost/thread/win32/once.hpp 2009-11-24 04:59:21 EST (Tue, 24 Nov 2009)
@@ -30,81 +30,33 @@
 
 namespace boost
 {
- typedef long once_flag;
+ struct once_flag
+ {
+ long status;
+ long count;
+ long throw_count;
+ void* event_handle;
+ };
 
-#define BOOST_ONCE_INIT 0
+#define BOOST_ONCE_INIT {0,0,0,0}
 
     namespace detail
     {
- struct win32_mutex_scoped_lock
- {
- void* const mutex_handle;
- explicit win32_mutex_scoped_lock(void* mutex_handle_):
- mutex_handle(mutex_handle_)
- {
- BOOST_VERIFY(!win32::WaitForSingleObject(mutex_handle,win32::infinite));
- }
- ~win32_mutex_scoped_lock()
- {
- BOOST_VERIFY(win32::ReleaseMutex(mutex_handle)!=0);
- }
- private:
- void operator=(win32_mutex_scoped_lock&);
- };
-
-#ifdef BOOST_NO_ANSI_APIS
- template <class I>
- void int_to_string(I p, wchar_t* buf)
- {
- for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
- {
- *buf = L'A' + static_cast<wchar_t>((p >> (i*4)) & 0x0f);
- }
- *buf = 0;
- }
-#else
- template <class I>
- void int_to_string(I p, char* buf)
+ inline void* allocate_event_handle(void*& handle)
         {
- for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
+ 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)
             {
- *buf = 'A' + static_cast<char>((p >> (i*4)) & 0x0f);
+ ::boost::detail::win32::CloseHandle(new_handle);
+ return event_handle;
             }
- *buf = 0;
+ return new_handle;
         }
-#endif
-
- // create a named mutex. It doesn't really matter what this name is
- // as long as it is unique both to this process, and to the address of "flag":
- inline void* create_once_mutex(void* flag_address)
- {
-
-#ifdef BOOST_NO_ANSI_APIS
- typedef wchar_t char_type;
- static const char_type fixed_mutex_name[]=L"{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
-#else
- typedef char char_type;
- static const char_type fixed_mutex_name[]="{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
-#endif
- unsigned const once_mutex_name_fixed_buffer_size=sizeof(fixed_mutex_name)/sizeof(char_type);
- unsigned const once_mutex_name_fixed_length=once_mutex_name_fixed_buffer_size-1;
- unsigned const once_mutex_name_length=once_mutex_name_fixed_buffer_size+sizeof(void*)*2+sizeof(unsigned long)*2;
- char_type mutex_name[once_mutex_name_length];
-
- std::memcpy(mutex_name,fixed_mutex_name,sizeof(fixed_mutex_name));
-
- BOOST_STATIC_ASSERT(sizeof(void*) == sizeof(std::ptrdiff_t));
- 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);
-
-#ifdef BOOST_NO_ANSI_APIS
- return win32::CreateMutexW(0, 0, mutex_name);
-#else
- return win32::CreateMutexA(0, 0, mutex_name);
-#endif
- }
-
-
     }
     
 
@@ -114,20 +66,84 @@
         // Try for a quick win: if the procedure has already been called
         // just skip through:
         long const function_complete_flag_value=0xc15730e2;
+ long const running_value=0x7f0725e3;
+ long status;
+ bool counted=false;
+ void* event_handle=0;
+ long throw_count=0;
 
- if(::boost::detail::interlocked_read_acquire(&flag)!=function_complete_flag_value)
+ while((status=::boost::detail::interlocked_read_acquire(&flag.status))
+ !=function_complete_flag_value)
         {
- void* const mutex_handle(::boost::detail::create_once_mutex(&flag));
- BOOST_ASSERT(mutex_handle);
- detail::win32::handle_manager const closer(mutex_handle);
- detail::win32_mutex_scoped_lock const lock(mutex_handle);
-
- if(flag!=function_complete_flag_value)
+ status=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&flag.status,running_value,0);
+ if(!status)
             {
- f();
- BOOST_INTERLOCKED_EXCHANGE(&flag,function_complete_flag_value);
+ try
+ {
+ f();
+ if(!counted)
+ {
+ BOOST_INTERLOCKED_INCREMENT(&flag.count);
+ counted=true;
+ }
+ BOOST_INTERLOCKED_EXCHANGE(&flag.status,function_complete_flag_value);
+ if(!event_handle &&
+ (::boost::detail::interlocked_read_acquire(&flag.count)>1))
+ {
+ event_handle=::boost::detail::allocate_event_handle(flag.event_handle);
+ }
+ if(event_handle)
+ {
+ ::boost::detail::win32::SetEvent(event_handle);
+ }
+ throw_count=::boost::detail::interlocked_read_acquire(&flag.throw_count);
+ break;
+ }
+ catch(...)
+ {
+ if(counted)
+ {
+ BOOST_INTERLOCKED_INCREMENT(&flag.throw_count);
+ }
+ BOOST_INTERLOCKED_EXCHANGE(&flag.status,0);
+ throw;
+ }
             }
+
+ if(!counted)
+ {
+ BOOST_INTERLOCKED_INCREMENT(&flag.count);
+ counted=true;
+ status=::boost::detail::interlocked_read_acquire(&flag.status);
+ if(status==function_complete_flag_value)
+ {
+ break;
+ }
+ event_handle=::boost::detail::interlocked_read_acquire(&flag.event_handle);
+ if(!event_handle)
+ {
+ event_handle=::boost::detail::allocate_event_handle(flag.event_handle);
+ continue;
+ }
+ }
+
         }
+ 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);
+ }
+ }
+ }
+
     }
 }
 


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