Avoid leaving garbage on screen when using 'raise' display property
[emacs.git] / src / systhread.c
bloba1b3eae64a6a14fe3694060388d3592c6aa0e7e9
1 /* System thread definitions
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
20 #include <setjmp.h>
21 #include "lisp.h"
23 #ifndef THREADS_ENABLED
25 void
26 sys_mutex_init (sys_mutex_t *m)
28 *m = 0;
31 void
32 sys_mutex_lock (sys_mutex_t *m)
36 void
37 sys_mutex_unlock (sys_mutex_t *m)
41 void
42 sys_mutex_destroy (sys_mutex_t *m)
46 void
47 sys_cond_init (sys_cond_t *c)
49 *c = 0;
52 void
53 sys_cond_wait (sys_cond_t *c, sys_mutex_t *m)
57 void
58 sys_cond_signal (sys_cond_t *c)
62 void
63 sys_cond_broadcast (sys_cond_t *c)
67 void
68 sys_cond_destroy (sys_cond_t *c)
72 sys_thread_t
73 sys_thread_self (void)
75 return 0;
78 int
79 sys_thread_equal (sys_thread_t x, sys_thread_t y)
81 return x == y;
84 int
85 sys_thread_create (sys_thread_t *t, const char *name,
86 thread_creation_function *func, void *datum)
88 return 0;
91 void
92 sys_thread_yield (void)
96 #elif defined (HAVE_PTHREAD)
98 #include <sched.h>
100 #ifdef HAVE_SYS_PRCTL_H
101 #include <sys/prctl.h>
102 #endif
104 void
105 sys_mutex_init (sys_mutex_t *mutex)
107 pthread_mutex_init (mutex, NULL);
110 void
111 sys_mutex_lock (sys_mutex_t *mutex)
113 pthread_mutex_lock (mutex);
116 void
117 sys_mutex_unlock (sys_mutex_t *mutex)
119 pthread_mutex_unlock (mutex);
122 void
123 sys_mutex_destroy (sys_mutex_t *mutex)
125 pthread_mutex_destroy (mutex);
128 void
129 sys_cond_init (sys_cond_t *cond)
131 pthread_cond_init (cond, NULL);
134 void
135 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
137 pthread_cond_wait (cond, mutex);
140 void
141 sys_cond_signal (sys_cond_t *cond)
143 pthread_cond_signal (cond);
146 void
147 sys_cond_broadcast (sys_cond_t *cond)
149 pthread_cond_broadcast (cond);
152 void
153 sys_cond_destroy (sys_cond_t *cond)
155 pthread_cond_destroy (cond);
158 sys_thread_t
159 sys_thread_self (void)
161 return pthread_self ();
165 sys_thread_equal (sys_thread_t one, sys_thread_t two)
167 return pthread_equal (one, two);
171 sys_thread_create (sys_thread_t *thread_ptr, const char *name,
172 thread_creation_function *func, void *arg)
174 pthread_attr_t attr;
175 int result = 0;
177 if (pthread_attr_init (&attr))
178 return 0;
180 if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))
182 result = pthread_create (thread_ptr, &attr, func, arg) == 0;
183 #if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME)
184 if (result && name != NULL)
185 prctl (PR_SET_NAME, name);
186 #endif
189 pthread_attr_destroy (&attr);
191 return result;
194 void
195 sys_thread_yield (void)
197 sched_yield ();
200 #elif defined (WINDOWSNT)
202 #include <windows.h>
204 /* Cannot include <process.h> because of the local header by the same
205 name, sigh. */
206 uintptr_t _beginthread (void (__cdecl *)(void *), unsigned, void *);
208 /* Mutexes are implemented as critical sections, because they are
209 faster than Windows mutex objects (implemented in userspace), and
210 satisfy the requirements, since we only need to synchronize within a
211 single process. */
212 void
213 sys_mutex_init (sys_mutex_t *mutex)
215 InitializeCriticalSection ((LPCRITICAL_SECTION)mutex);
218 void
219 sys_mutex_lock (sys_mutex_t *mutex)
221 /* FIXME: What happens if the owning thread exits without releasing
222 the mutex? According to MSDN, the result is undefined behavior. */
223 EnterCriticalSection ((LPCRITICAL_SECTION)mutex);
226 void
227 sys_mutex_unlock (sys_mutex_t *mutex)
229 LeaveCriticalSection ((LPCRITICAL_SECTION)mutex);
232 void
233 sys_mutex_destroy (sys_mutex_t *mutex)
235 /* FIXME: According to MSDN, deleting a critical session that is
236 owned by a thread leaves the other threads waiting for the
237 critical session in an undefined state. Posix docs seem to say
238 the same about pthread_mutex_destroy. Do we need to protect
239 against such calamities? */
240 DeleteCriticalSection ((LPCRITICAL_SECTION)mutex);
243 void
244 sys_cond_init (sys_cond_t *cond)
246 cond->initialized = false;
247 cond->wait_count = 0;
248 /* Auto-reset event for signal. */
249 cond->events[CONDV_SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
250 /* Manual-reset event for broadcast. */
251 cond->events[CONDV_BROADCAST] = CreateEvent (NULL, TRUE, FALSE, NULL);
252 if (!cond->events[CONDV_SIGNAL] || !cond->events[CONDV_BROADCAST])
253 return;
254 InitializeCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
255 cond->initialized = true;
258 void
259 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
261 DWORD wait_result;
262 bool last_thread_waiting;
264 if (!cond->initialized)
265 return;
267 /* Increment the wait count avoiding race conditions. */
268 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
269 cond->wait_count++;
270 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
272 /* Release the mutex and wait for either the signal or the broadcast
273 event. */
274 LeaveCriticalSection ((LPCRITICAL_SECTION)mutex);
275 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
277 /* Decrement the wait count and see if we are the last thread
278 waiting on the condition variable. */
279 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
280 cond->wait_count--;
281 last_thread_waiting =
282 wait_result == WAIT_OBJECT_0 + CONDV_BROADCAST
283 && cond->wait_count == 0;
284 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
286 /* Broadcast uses a manual-reset event, so when the last thread is
287 released, we must manually reset that event. */
288 if (last_thread_waiting)
289 ResetEvent (cond->events[CONDV_BROADCAST]);
291 /* Per the API, re-acquire the mutex. */
292 EnterCriticalSection ((LPCRITICAL_SECTION)mutex);
295 void
296 sys_cond_signal (sys_cond_t *cond)
298 bool threads_waiting;
300 if (!cond->initialized)
301 return;
303 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
304 threads_waiting = cond->wait_count > 0;
305 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
307 if (threads_waiting)
308 SetEvent (cond->events[CONDV_SIGNAL]);
311 void
312 sys_cond_broadcast (sys_cond_t *cond)
314 bool threads_waiting;
316 if (!cond->initialized)
317 return;
319 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
320 threads_waiting = cond->wait_count > 0;
321 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
323 if (threads_waiting)
324 SetEvent (cond->events[CONDV_BROADCAST]);
327 void
328 sys_cond_destroy (sys_cond_t *cond)
330 if (cond->events[CONDV_SIGNAL])
331 CloseHandle (cond->events[CONDV_SIGNAL]);
332 if (cond->events[CONDV_BROADCAST])
333 CloseHandle (cond->events[CONDV_BROADCAST]);
335 if (!cond->initialized)
336 return;
338 /* FIXME: What if wait_count is non-zero, i.e. there are still
339 threads waiting on this condition variable? */
340 DeleteCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
343 sys_thread_t
344 sys_thread_self (void)
346 return (sys_thread_t) GetCurrentThreadId ();
350 sys_thread_equal (sys_thread_t one, sys_thread_t two)
352 return one == two;
355 static thread_creation_function *thread_start_address;
357 /* _beginthread wants a void function, while we are passed a function
358 that returns a pointer. So we use a wrapper. */
359 static void
360 w32_beginthread_wrapper (void *arg)
362 (void)thread_start_address (arg);
366 sys_thread_create (sys_thread_t *thread_ptr, const char *name,
367 thread_creation_function *func, void *arg)
369 /* FIXME: Do threads that run Lisp require some minimum amount of
370 stack? Zero here means each thread will get the same amount as
371 the main program. On GNU/Linux, it seems like the stack is 2MB
372 by default, overridden by RLIMIT_STACK at program start time.
373 Not sure what to do with this. See also the comment in
374 w32proc.c:new_child. */
375 const unsigned stack_size = 0;
376 uintptr_t thandle;
378 thread_start_address = func;
380 /* We use _beginthread rather than CreateThread because the former
381 arranges for the thread handle to be automatically closed when
382 the thread exits, thus preventing handle leaks and/or the need to
383 track all the threads and close their handles when they exit.
384 Also, MSDN seems to imply that code which uses CRT _must_ call
385 _beginthread, although if that is true, we already violate that
386 rule in many places... */
387 thandle = _beginthread (w32_beginthread_wrapper, stack_size, arg);
388 if (thandle == (uintptr_t)-1L)
389 return 0;
391 /* Kludge alert! We use the Windows thread ID, an unsigned 32-bit
392 number, as the sys_thread_t type, because that ID is the only
393 unique identifier of a thread on Windows. But _beginthread
394 returns a handle of the thread, and there's no easy way of
395 getting the thread ID given a handle (GetThreadId is available
396 only since Vista, so we cannot use it portably). Fortunately,
397 the value returned by sys_thread_create is not used by its
398 callers; instead, run_thread, which runs in the context of the
399 new thread, calls sys_thread_self and uses its return value;
400 sys_thread_self in this implementation calls GetCurrentThreadId.
401 Therefore, we return some more or less arbitrary value of the
402 thread ID from this function. */
403 *thread_ptr = thandle & 0xFFFFFFFF;
404 return 1;
407 void
408 sys_thread_yield (void)
410 Sleep (0);
413 #else
415 #error port me
417 #endif