stlab.adobe.com Adobe Systems Incorporated
once.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_ONCE_HPP
10 #define ADOBE_ONCE_HPP
11 
12 /*************************************************************************************************/
13 
14 #if defined(BOOST_HAS_THREADS)
15  #include <boost/thread.hpp>
16 #endif
17 
18 #include <adobe/config.hpp>
19 
20 /*************************************************************************************************/
21 
22 namespace adobe {
23 
24 /*************************************************************************************************/
25 
26 #if defined(BOOST_HAS_THREADS)
27 
28 /*************************************************************************************************/
29 
31 #define ADOBE_ONCE_INIT BOOST_ONCE_INIT
32 
33 inline void call_once(void (*func)(), adobe::once_flag& flag)
34 {
35  boost::call_once(func, flag);
36 }
37 
38 /*************************************************************************************************/
39 
40 #else
41 
42 /*************************************************************************************************/
43 
44 typedef bool once_flag;
45 #define ADOBE_ONCE_INIT false
46 
47 inline void call_once(void (*func)(), adobe::once_flag& flag)
48 {
49  if (!flag)
50  {
51  (*func)();
52  flag = true;
53  }
54 }
55 
56 /*************************************************************************************************/
57 
58 #endif
59 
60 /*************************************************************************************************/
61 
62 } // namespace adobe
63 
64 /*************************************************************************************************/
65 
66 #define ADOBE_ONCE_DECLARATION(signature) \
67  struct adobe_initialize_constants_##signature##_t \
68  { \
69  adobe_initialize_constants_##signature##_t(); \
70  };
71 
72 #define ADOBE_ONCE_DEFINITION(signature, func) \
73  namespace { \
74  adobe::once_flag adobe_once_flag_##signature##_s = ADOBE_ONCE_INIT; \
75  } \
76  adobe_initialize_constants_##signature##_t::adobe_initialize_constants_##signature##_t() \
77  { \
78  adobe::call_once(&func, adobe_once_flag_##signature##_s); \
79  }
80 
81 #define ADOBE_ONCE_INSTANCE(signature) \
82  adobe_initialize_constants_##signature##_t adobe_initialize_constants_##signature##_s
83 
84 #define ADOBE_ONCE_STATIC_INSTANCE(signature) \
85  namespace { ADOBE_ONCE_INSTANCE(signature); }
86 
87 #if defined(BOOST_HAS_THREADS)
88 
89 #define ADOBE_GLOBAL_MUTEX_DEFINITION(signature) \
90  namespace { \
91  adobe::once_flag adobe_once_flag_##signature##_s = ADOBE_ONCE_INIT; \
92  boost::mutex* adobe_mutex_ptr_##signature##_s = 0; \
93  void adobe_init_once_##signature() \
94  { \
95  static boost::mutex mutex_s; \
96  adobe_mutex_ptr_##signature##_s = &mutex_s; \
97  } \
98  }
99 
100 #define ADOBE_GLOBAL_MUTEX_INSTANCE(signature) \
101  boost::call_once(&adobe_init_once_##signature, adobe_once_flag_##signature##_s); \
102  boost::mutex::scoped_lock lock(*adobe_mutex_ptr_##signature##_s)
103 
104 #else
105 
106 #define ADOBE_GLOBAL_MUTEX_DEFINITION(signature)
107 #define ADOBE_GLOBAL_MUTEX_INSTANCE(signature)
108 
109 #endif
110 
111 /*************************************************************************************************/
112 
113 #if defined(BOOST_HAS_THREADS)
114 
115 #define ADOBE_THREAD_LOCAL_STORAGE_1(type, signature, ctor_p1) \
116  namespace { \
117  typedef boost::thread_specific_ptr< type > adobe_thread_local_storage_##signature##_t; \
118  adobe_thread_local_storage_##signature##_t* adobe_thread_local_storage_##signature##_g = 0;\
119  type& adobe_thread_local_storage_##signature##_access(); \
120  type& adobe_thread_local_storage_##signature##_access() \
121  { \
122  type* result = adobe_thread_local_storage_##signature##_g->get(); \
123  if (result) return *result; \
124  result = new type(ctor_p1); \
125  adobe_thread_local_storage_##signature##_g->reset(result); \
126  return *result; \
127  } }
128 
129 #define ADOBE_THREAD_LOCAL_STORAGE(type, signature) \
130  namespace { \
131  typedef boost::thread_specific_ptr< type > adobe_thread_local_storage_##signature##_t; \
132  adobe_thread_local_storage_##signature##_t* adobe_thread_local_storage_##signature##_g = 0;\
133  type& adobe_thread_local_storage_##signature##_access(); \
134  type& adobe_thread_local_storage_##signature##_access() \
135  { \
136  type* result = adobe_thread_local_storage_##signature##_g->get(); \
137  if (result) return *result; \
138  result = new type(); \
139  adobe_thread_local_storage_##signature##_g->reset(result); \
140  return *result; \
141  } }
142 
143 #define ADOBE_THREAD_LOCAL_STORAGE_INITIALIZE(signature) \
144  static adobe_thread_local_storage_##signature##_t adobe_thread_local_storage_##signature##_s; \
145  adobe_thread_local_storage_##signature##_g = &adobe_thread_local_storage_##signature##_s
146 
147 #else
148 
149 #define ADOBE_THREAD_LOCAL_STORAGE_1(type, signature, ctor_p1) \
150  type& adobe_thread_local_storage_##signature##_access(); \
151  type& adobe_thread_local_storage_##signature##_access() \
152  { \
153  static type adobe_thread_local_storage_##signature##_s(ctor_p1); \
154  return adobe_thread_local_storage_##signature##_s; \
155  }
156 
157 #define ADOBE_THREAD_LOCAL_STORAGE(type, signature) \
158  type& adobe_thread_local_storage_##signature##_access(); \
159  type& adobe_thread_local_storage_##signature##_access() \
160  { \
161  static type adobe_thread_local_storage_##signature##_s; \
162  return adobe_thread_local_storage_##signature##_s; \
163  }
164 
165 #define ADOBE_THREAD_LOCAL_STORAGE_INITIALIZE(signature)
166 
167 #endif
168 
169 #define ADOBE_THREAD_LOCAL_STORAGE_ACCESS(signature) \
170  adobe_thread_local_storage_##signature##_access()
171 
172 /*************************************************************************************************/
173 
174 #endif // ADOBE_ONCE_HPP
175 
176 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google