1 /**
2  * WinPR: Windows Portable Runtime
3  * Synchronization Functions
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  * Copyright 2016 Norbert Federa <norbert.federa@thincast.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <winpr/synch.h>
26 
27 #include "synch.h"
28 
29 #include <winpr/crt.h>
30 
31 #ifdef WINPR_SYNCHRONIZATION_BARRIER
32 
33 #include <assert.h>
34 #include <winpr/sysinfo.h>
35 #include <winpr/library.h>
36 #include <winpr/interlocked.h>
37 #include <winpr/thread.h>
38 
39 /**
40  * WinPR uses the internal RTL_BARRIER struct members exactly like Windows:
41  *
42  * DWORD Reserved1:          number of threads that have not yet entered the barrier
43  * DWORD Reserved2:          number of threads required to enter the barrier
44  * ULONG_PTR Reserved3[2];   two synchronization events (manual reset events)
45  * DWORD Reserved4;          number of processors
46  * DWORD Reserved5;          spincount
47  */
48 
49 #ifdef _WIN32
50 
51 static HMODULE g_Kernel32 = NULL;
52 static BOOL g_NativeBarrier = FALSE;
53 static INIT_ONCE g_InitOnce = INIT_ONCE_STATIC_INIT;
54 
55 typedef BOOL(WINAPI* fnInitializeSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier,
56                                                          LONG lTotalThreads, LONG lSpinCount);
57 typedef BOOL(WINAPI* fnEnterSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier,
58                                                     DWORD dwFlags);
59 typedef BOOL(WINAPI* fnDeleteSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier);
60 
61 static fnInitializeSynchronizationBarrier pfnInitializeSynchronizationBarrier = NULL;
62 static fnEnterSynchronizationBarrier pfnEnterSynchronizationBarrier = NULL;
63 static fnDeleteSynchronizationBarrier pfnDeleteSynchronizationBarrier = NULL;
64 
InitOnce_Barrier(PINIT_ONCE once,PVOID param,PVOID * context)65 static BOOL CALLBACK InitOnce_Barrier(PINIT_ONCE once, PVOID param, PVOID* context)
66 {
67 	g_Kernel32 = LoadLibraryA("kernel32.dll");
68 
69 	if (!g_Kernel32)
70 		return TRUE;
71 
72 	pfnInitializeSynchronizationBarrier = (fnInitializeSynchronizationBarrier)GetProcAddress(
73 	    g_Kernel32, "InitializeSynchronizationBarrier");
74 
75 	pfnEnterSynchronizationBarrier =
76 	    (fnEnterSynchronizationBarrier)GetProcAddress(g_Kernel32, "EnterSynchronizationBarrier");
77 
78 	pfnDeleteSynchronizationBarrier =
79 	    (fnDeleteSynchronizationBarrier)GetProcAddress(g_Kernel32, "DeleteSynchronizationBarrier");
80 
81 	if (pfnInitializeSynchronizationBarrier && pfnEnterSynchronizationBarrier &&
82 	    pfnDeleteSynchronizationBarrier)
83 	{
84 		g_NativeBarrier = TRUE;
85 	}
86 
87 	return TRUE;
88 }
89 
90 #endif
91 
winpr_InitializeSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier,LONG lTotalThreads,LONG lSpinCount)92 BOOL WINAPI winpr_InitializeSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier,
93                                                    LONG lTotalThreads, LONG lSpinCount)
94 {
95 	SYSTEM_INFO sysinfo;
96 	HANDLE hEvent0;
97 	HANDLE hEvent1;
98 
99 #ifdef _WIN32
100 	InitOnceExecuteOnce(&g_InitOnce, InitOnce_Barrier, NULL, NULL);
101 
102 	if (g_NativeBarrier)
103 		return pfnInitializeSynchronizationBarrier(lpBarrier, lTotalThreads, lSpinCount);
104 #endif
105 
106 	if (!lpBarrier || lTotalThreads < 1 || lSpinCount < -1)
107 	{
108 		SetLastError(ERROR_INVALID_PARAMETER);
109 		return FALSE;
110 	}
111 
112 	ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
113 
114 	if (lSpinCount == -1)
115 		lSpinCount = 2000;
116 
117 	if (!(hEvent0 = CreateEvent(NULL, TRUE, FALSE, NULL)))
118 		return FALSE;
119 
120 	if (!(hEvent1 = CreateEvent(NULL, TRUE, FALSE, NULL)))
121 	{
122 		CloseHandle(hEvent0);
123 		return FALSE;
124 	}
125 
126 	GetNativeSystemInfo(&sysinfo);
127 
128 	lpBarrier->Reserved1 = lTotalThreads;
129 	lpBarrier->Reserved2 = lTotalThreads;
130 	lpBarrier->Reserved3[0] = (ULONG_PTR)hEvent0;
131 	lpBarrier->Reserved3[1] = (ULONG_PTR)hEvent1;
132 	lpBarrier->Reserved4 = sysinfo.dwNumberOfProcessors;
133 	lpBarrier->Reserved5 = lSpinCount;
134 
135 	return TRUE;
136 }
137 
winpr_EnterSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier,DWORD dwFlags)138 BOOL WINAPI winpr_EnterSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier, DWORD dwFlags)
139 {
140 	LONG remainingThreads;
141 	HANDLE hCurrentEvent;
142 	HANDLE hDormantEvent;
143 
144 #ifdef _WIN32
145 	if (g_NativeBarrier)
146 		return pfnEnterSynchronizationBarrier(lpBarrier, dwFlags);
147 #endif
148 
149 	if (!lpBarrier)
150 		return FALSE;
151 
152 	/**
153 	 * dwFlags according to
154 	 * https://msdn.microsoft.com/en-us/library/windows/desktop/hh706889(v=vs.85).aspx
155 	 *
156 	 * SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY (0x01)
157 	 * Specifies that the thread entering the barrier should block
158 	 * immediately until the last thread enters the barrier.
159 	 *
160 	 * SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY (0x02)
161 	 * Specifies that the thread entering the barrier should spin until the
162 	 * last thread enters the barrier, even if the spinning thread exceeds
163 	 * the barrier's maximum spin count.
164 	 *
165 	 * SYNCHRONIZATION_BARRIER_FLAGS_NO_DELETE (0x04)
166 	 * Specifies that the function can skip the work required to ensure
167 	 * that it is safe to delete the barrier, which can improve
168 	 * performance. All threads that enter this barrier must specify the
169 	 * flag; otherwise, the flag is ignored. This flag should be used only
170 	 * if the barrier will never be deleted.
171 	 */
172 
173 	hCurrentEvent = (HANDLE)lpBarrier->Reserved3[0];
174 	hDormantEvent = (HANDLE)lpBarrier->Reserved3[1];
175 
176 	remainingThreads = InterlockedDecrement((LONG*)&lpBarrier->Reserved1);
177 
178 	assert(remainingThreads >= 0);
179 
180 	if (remainingThreads > 0)
181 	{
182 		DWORD dwProcessors = lpBarrier->Reserved4;
183 		BOOL spinOnly = dwFlags & SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY;
184 		BOOL blockOnly = dwFlags & SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY;
185 		BOOL block = TRUE;
186 
187 		/**
188 		 * If SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY is set we will
189 		 * always spin and trust that the user knows what he/she/it
190 		 * is doing. Otherwise we'll only spin if the flag
191 		 * SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY is not set and
192 		 * the number of remaining threads is less than the number
193 		 * of processors.
194 		 */
195 
196 		if (spinOnly || (((ULONG)remainingThreads < dwProcessors) && !blockOnly))
197 		{
198 			DWORD dwSpinCount = lpBarrier->Reserved5;
199 			DWORD sp = 0;
200 			/**
201 			 * nb: we must let the compiler know that our comparand
202 			 * can change between the iterations in the loop below
203 			 */
204 			volatile ULONG_PTR* cmp = &lpBarrier->Reserved3[0];
205 			/* we spin until the last thread _completed_ the event switch */
206 			while ((block = (*cmp == (ULONG_PTR)hCurrentEvent)))
207 				if (!spinOnly && ++sp > dwSpinCount)
208 					break;
209 		}
210 
211 		if (block)
212 			WaitForSingleObject(hCurrentEvent, INFINITE);
213 
214 		return FALSE;
215 	}
216 
217 	/* reset the dormant event first */
218 	ResetEvent(hDormantEvent);
219 
220 	/* reset the remaining counter */
221 	lpBarrier->Reserved1 = lpBarrier->Reserved2;
222 
223 	/* switch events - this will also unblock the spinning threads */
224 	lpBarrier->Reserved3[1] = (ULONG_PTR)hCurrentEvent;
225 	lpBarrier->Reserved3[0] = (ULONG_PTR)hDormantEvent;
226 
227 	/* signal the blocked threads */
228 	SetEvent(hCurrentEvent);
229 
230 	return TRUE;
231 }
232 
winpr_DeleteSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier)233 BOOL WINAPI winpr_DeleteSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier)
234 {
235 #ifdef _WIN32
236 	if (g_NativeBarrier)
237 		return pfnDeleteSynchronizationBarrier(lpBarrier);
238 #endif
239 
240 	/**
241 	 * According to https://msdn.microsoft.com/en-us/library/windows/desktop/hh706887(v=vs.85).aspx
242 	 * Return value:
243 	 * The DeleteSynchronizationBarrier function always returns TRUE.
244 	 */
245 
246 	if (!lpBarrier)
247 		return TRUE;
248 
249 	while (lpBarrier->Reserved1 != lpBarrier->Reserved2)
250 		SwitchToThread();
251 
252 	if (lpBarrier->Reserved3[0])
253 		CloseHandle((HANDLE)lpBarrier->Reserved3[0]);
254 
255 	if (lpBarrier->Reserved3[1])
256 		CloseHandle((HANDLE)lpBarrier->Reserved3[1]);
257 
258 	ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
259 
260 	return TRUE;
261 }
262 
263 #endif
264