#ifndef BOOST_WIN32_ONCE_HPP #define BOOST_WIN32_ONCE_HPP // once.hpp // // (C) Copyright 2005 Anthony Williams // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include namespace boost { typedef LONG once_flag; #define BOOST_ONCE_INIT 0 namespace detail { std::string create_unique_name(void* p,const char* tag) { std::ostringstream res; res<<"unique-"< void call_once(Function f,once_flag& flag) { LONG const function_complete_flag_value=0xc15730e2; if(!::InterlockedCompareExchange(&flag,function_complete_flag_value, function_complete_flag_value)) { std::string const once_mutex_name=detail::create_unique_name(&flag,"once_init"); HANDLE const mutex_handle(::CreateMutex(NULL,0,once_mutex_name.c_str())); if(!mutex_handle) { abort(); } detail::handle_closer const closer(mutex_handle); ::WaitForSingleObject(mutex_handle,INFINITE); detail::mutex_releaser const releaser(mutex_handle); if(!::InterlockedCompareExchange(&flag,function_complete_flag_value, function_complete_flag_value)) { f(); ::InterlockedExchange(&flag,function_complete_flag_value); } } } } #endif