File: Once.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (273 lines) | stat: -rw-r--r-- 6,816 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef ONCE_H
#define ONCE_H

// Adapted/ripped from Boost, original license below:

//  once.hpp
//
//  (C) Copyright 2005-7 Anthony Williams
//  (C) Copyright 2005 John Maddock
//  (C) Copyright 2011-2013 Vicente J. Botet Escriba
//
//  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 <cstring>
#include <cstddef>

namespace spring
{
	struct once_flag;
	namespace once
	{
		struct once_context;

		inline bool enter_once_region(once_flag& flag, once_context& ctx) noexcept;
		inline void commit_once_region(once_flag& flag, once_context& ctx) noexcept;
		inline void rollback_once_region(once_flag& flag, once_context& ctx) noexcept;
	}

	struct once_flag
	{
			constexpr once_flag() noexcept
				: status(0), count(0)
			{}
			long status;
			long count;
	};

#define SPRING_ONCE_INIT once_flag()


#define SPRING_THREAD_INVOKE_RET_VOID std::bind
#define SPRING_THREAD_INVOKE_RET_VOID_CALL ()

	namespace once
	{
		typedef char once_char_type;
		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;

		template <class I>
		void int_to_string(I p, once_char_type* buf)
		{
			for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
			{
				once_char_type const a='A';
				*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)
		{
			static const once_char_type fixed_mutex_name[]="Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";

			std::memcpy(mutex_name,fixed_mutex_name,sizeof(fixed_mutex_name));
			once::int_to_string(reinterpret_cast<std::ptrdiff_t>(flag_address),
														mutex_name + once_mutex_name_fixed_length);
			once::int_to_string(GetCurrentProcessId(),
														mutex_name + once_mutex_name_fixed_length + sizeof(void*)*2);
		}

		inline HANDLE open_once_event(once_char_type* mutex_name,void* flag_address)
		{
			if(!*mutex_name)
			{
				name_once_mutex(mutex_name,flag_address);
			}

			return OpenEventA(SYNCHRONIZE | EVENT_MODIFY_STATE,
				FALSE,
				mutex_name);
		}

		inline HANDLE create_once_event(once_char_type* mutex_name,void* flag_address)
		{
			if(!*mutex_name)
			{
					name_once_mutex(mutex_name,flag_address);
			}

			return CreateEventA(0, TRUE, FALSE, mutex_name);
		}

		struct once_context {
			long const function_complete_flag_value;
			long const running_value;
			bool counted;
			HANDLE event_handle;
			once::once_char_type mutex_name[once_mutex_name_length];
			once_context() :
				function_complete_flag_value(0xc15730e2),
				running_value(0x7f0725e3),
				counted(false),
				event_handle(NULL)
			{
				mutex_name[0]=0;
			}
			~once_context() {
				CloseHandle(event_handle);
			}
		};
		enum once_action {try_, break_, continue_};

		inline bool enter_once_region(once_flag& flag, once_context& ctx) noexcept
		{
			long status=InterlockedCompareExchange(&flag.status,ctx.running_value,0);
			if(!status)
			{
				if(!ctx.event_handle)
				{
					ctx.event_handle=once::open_once_event(ctx.mutex_name,&flag);
				}
				if(ctx.event_handle)
				{
					ResetEvent(ctx.event_handle);
				}
				return true;
			}
			return false;
		}
		inline void commit_once_region(once_flag& flag, once_context& ctx) noexcept
		{
			if(!ctx.counted)
			{
				InterlockedIncrement(&flag.count);
				ctx.counted=true;
			}
			InterlockedExchange(&flag.status,ctx.function_complete_flag_value);
			if(!ctx.event_handle &&
				(InterlockedCompareExchange(&flag.count,0,0)>1))
			{
				ctx.event_handle=once::create_once_event(ctx.mutex_name,&flag);
			}
			if(ctx.event_handle)
			{
				SetEvent(ctx.event_handle);
			}
		}
		inline void rollback_once_region(once_flag& flag, once_context& ctx) noexcept
		{
			InterlockedExchange(&flag.status,0);
			if(!ctx.event_handle)
			{
				ctx.event_handle=once::open_once_event(ctx.mutex_name,&flag);
			}
			if(ctx.event_handle)
			{
				SetEvent(ctx.event_handle);
			}
		}
	}

	inline void call_once(once_flag& flag, void (*f)())
	{
		// Try for a quick win: if the procedure has already been called
		// just skip through:
		once::once_context ctx;
		while(InterlockedCompareExchange(&flag.status,0,0)
				!=ctx.function_complete_flag_value)
		{
			if(once::enter_once_region(flag, ctx))
			{
				f();
				once::commit_once_region(flag, ctx);
				break;
			}
			if(!ctx.counted)
			{
				InterlockedIncrement(&flag.count);
				ctx.counted=true;
				long status=InterlockedCompareExchange(&flag.status,0,0);
				if(status==ctx.function_complete_flag_value)
				{
					break;
				}
				if(!ctx.event_handle)
				{
					ctx.event_handle=once::create_once_event(ctx.mutex_name,&flag);
					continue;
				}
			}
			WaitForSingleObjectEx(ctx.event_handle, INFINITE, 0);
		}
	}
//#endif
	template<typename Function>
	inline void call_once(once_flag& flag, Function&& f)
	{
		// Try for a quick win: if the procedure has already been called
		// just skip through:
		once::once_context ctx;
		while(InterlockedCompareExchange(&flag.status,0,0)
				!=ctx.function_complete_flag_value)
		{
			if(once::enter_once_region(flag, ctx))
			{
				f();
				once::commit_once_region(flag, ctx);
				break;
			}
			if(!ctx.counted)
			{
				InterlockedIncrement(&flag.count);
				ctx.counted=true;
				long status=InterlockedCompareExchange(&flag.status,0,0);
				if(status==ctx.function_complete_flag_value)
				{
					break;
				}
				if(!ctx.event_handle)
				{
					ctx.event_handle=once::create_once_event(ctx.mutex_name,&flag);
					continue;
				}
			}
			WaitForSingleObjectEx(ctx.event_handle,INFINITE,0);
		}
	}
	template<typename Function, class A, class ...ArgTypes>
	inline void call_once(once_flag& flag, Function&& f, A&& a, ArgTypes&&... args)
	{
		// Try for a quick win: if the procedure has already been called
		// just skip through:
		once::once_context ctx;
		while(InterlockedCompareExchange(&flag.status,0,0)
				!=ctx.function_complete_flag_value)
		{
			if(once::enter_once_region(flag, ctx))
			{
				std::bind(
					std::forward<Function>(f),
					std::forward<A>(a),
					std::forward<ArgTypes>(args)...
				)();
				once::commit_once_region(flag, ctx);
				break;
			}
			if(!ctx.counted)
			{
				InterlockedIncrement(&flag.count);
				ctx.counted=true;
				long status=InterlockedCompareExchange(&flag.status,0,0);
				if(status==ctx.function_complete_flag_value)
				{
					break;
				}
				if(!ctx.event_handle)
				{
					ctx.event_handle=once::create_once_event(ctx.mutex_name,&flag);
					continue;
				}
			}
			WaitForSingleObjectEx(ctx.event_handle,INFINITE,0);
		}
	}
}

#endif