once.hpp
Go to the documentation of this file.
00001 /* 00002 Copyright 2005-2007 Adobe Systems Incorporated 00003 Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt 00004 or a copy at http://stlab.adobe.com/licenses.html) 00005 */ 00006 00007 /*************************************************************************************************/ 00008 00009 #ifndef ADOBE_ONCE_HPP 00010 #define ADOBE_ONCE_HPP 00011 00012 /*************************************************************************************************/ 00013 00014 #include <adobe/config.hpp> 00015 00016 #if defined(BOOST_HAS_THREADS) 00017 #include <boost/thread.hpp> 00018 #endif 00019 00020 /*************************************************************************************************/ 00021 00022 namespace adobe { 00023 00024 /*************************************************************************************************/ 00025 00026 #if defined(BOOST_HAS_THREADS) 00027 00028 /*************************************************************************************************/ 00029 00030 typedef boost::once_flag once_flag; 00031 #define ADOBE_ONCE_INIT BOOST_ONCE_INIT 00032 00033 inline void call_once(void (*func)(), adobe::once_flag& flag) 00034 { 00035 boost::call_once(func, flag); 00036 } 00037 00038 /*************************************************************************************************/ 00039 00040 #else 00041 00042 /*************************************************************************************************/ 00043 00044 typedef bool once_flag; 00045 #define ADOBE_ONCE_INIT false 00046 00047 inline void call_once(void (*func)(), adobe::once_flag& flag) 00048 { 00049 if (!flag) 00050 { 00051 (*func)(); 00052 flag = true; 00053 } 00054 } 00055 00056 /*************************************************************************************************/ 00057 00058 #endif 00059 00060 /*************************************************************************************************/ 00061 00062 } // namespace adobe 00063 00064 /*************************************************************************************************/ 00065 00066 #define ADOBE_ONCE_DECLARATION(signature) \ 00067 struct adobe_initialize_constants_##signature##_t \ 00068 { \ 00069 adobe_initialize_constants_##signature##_t(); \ 00070 }; 00071 00072 #define ADOBE_ONCE_DEFINITION(signature, func) \ 00073 namespace { \ 00074 adobe::once_flag adobe_once_flag_##signature##_s = ADOBE_ONCE_INIT; \ 00075 } \ 00076 adobe_initialize_constants_##signature##_t::adobe_initialize_constants_##signature##_t() \ 00077 { \ 00078 adobe::call_once(&func, adobe_once_flag_##signature##_s); \ 00079 } 00080 00081 #define ADOBE_ONCE_INSTANCE(signature) \ 00082 adobe_initialize_constants_##signature##_t adobe_initialize_constants_##signature##_s 00083 00084 #define ADOBE_ONCE_STATIC_INSTANCE(signature) \ 00085 namespace { ADOBE_ONCE_INSTANCE(signature); } 00086 00087 #if defined(BOOST_HAS_THREADS) 00088 00089 #define ADOBE_GLOBAL_MUTEX_DEFINITION(signature) \ 00090 namespace { \ 00091 adobe::once_flag adobe_once_flag_##signature##_s = ADOBE_ONCE_INIT; \ 00092 boost::mutex* adobe_mutex_ptr_##signature##_s = 0; \ 00093 void adobe_init_once_##signature() \ 00094 { \ 00095 static boost::mutex mutex_s; \ 00096 adobe_mutex_ptr_##signature##_s = &mutex_s; \ 00097 } \ 00098 } 00099 00100 #define ADOBE_GLOBAL_MUTEX_INSTANCE(signature) \ 00101 boost::call_once(&adobe_init_once_##signature, adobe_once_flag_##signature##_s); \ 00102 boost::mutex::scoped_lock lock(*adobe_mutex_ptr_##signature##_s) 00103 00104 #else 00105 00106 #define ADOBE_GLOBAL_MUTEX_DEFINITION(signature) 00107 #define ADOBE_GLOBAL_MUTEX_INSTANCE(signature) 00108 00109 #endif 00110 00111 /*************************************************************************************************/ 00112 00113 #if defined(BOOST_HAS_THREADS) 00114 00115 #define ADOBE_THREAD_LOCAL_STORAGE_1(type, signature, ctor_p1) \ 00116 namespace { \ 00117 typedef boost::thread_specific_ptr< type > adobe_thread_local_storage_##signature##_t; \ 00118 adobe_thread_local_storage_##signature##_t* adobe_thread_local_storage_##signature##_g = 0;\ 00119 type& adobe_thread_local_storage_##signature##_access(); \ 00120 type& adobe_thread_local_storage_##signature##_access() \ 00121 { \ 00122 type* result = adobe_thread_local_storage_##signature##_g->get(); \ 00123 if (result) return *result; \ 00124 result = new type(ctor_p1); \ 00125 adobe_thread_local_storage_##signature##_g->reset(result); \ 00126 return *result; \ 00127 } } 00128 00129 #define ADOBE_THREAD_LOCAL_STORAGE(type, signature) \ 00130 namespace { \ 00131 typedef boost::thread_specific_ptr< type > adobe_thread_local_storage_##signature##_t; \ 00132 adobe_thread_local_storage_##signature##_t* adobe_thread_local_storage_##signature##_g = 0;\ 00133 type& adobe_thread_local_storage_##signature##_access(); \ 00134 type& adobe_thread_local_storage_##signature##_access() \ 00135 { \ 00136 type* result = adobe_thread_local_storage_##signature##_g->get(); \ 00137 if (result) return *result; \ 00138 result = new type(); \ 00139 adobe_thread_local_storage_##signature##_g->reset(result); \ 00140 return *result; \ 00141 } } 00142 00143 #define ADOBE_THREAD_LOCAL_STORAGE_INITIALIZE(signature) \ 00144 static adobe_thread_local_storage_##signature##_t adobe_thread_local_storage_##signature##_s; \ 00145 adobe_thread_local_storage_##signature##_g = &adobe_thread_local_storage_##signature##_s 00146 00147 #else 00148 00149 #define ADOBE_THREAD_LOCAL_STORAGE_1(type, signature, ctor_p1) \ 00150 type& adobe_thread_local_storage_##signature##_access(); \ 00151 type& adobe_thread_local_storage_##signature##_access() \ 00152 { \ 00153 static type adobe_thread_local_storage_##signature##_s(ctor_p1); \ 00154 return adobe_thread_local_storage_##signature##_s; \ 00155 } 00156 00157 #define ADOBE_THREAD_LOCAL_STORAGE(type, signature) \ 00158 type& adobe_thread_local_storage_##signature##_access(); \ 00159 type& adobe_thread_local_storage_##signature##_access() \ 00160 { \ 00161 static type adobe_thread_local_storage_##signature##_s; \ 00162 return adobe_thread_local_storage_##signature##_s; \ 00163 } 00164 00165 #define ADOBE_THREAD_LOCAL_STORAGE_INITIALIZE(signature) 00166 00167 #endif 00168 00169 #define ADOBE_THREAD_LOCAL_STORAGE_ACCESS(signature) \ 00170 adobe_thread_local_storage_##signature##_access() 00171 00172 /*************************************************************************************************/ 00173 00174 #endif // ADOBE_ONCE_HPP 00175 00176 /*************************************************************************************************/ |