Avoid losing Ctrl-C keystrokes in compilation mode on MS-Windows
[emacs.git] / src / systhread.c
blobaee12a9b482c9504b823d5c4f83189ec050aed72
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 #ifdef HAVE_NS
24 #include "nsterm.h"
25 #endif
27 #ifndef THREADS_ENABLED
29 void
30 sys_mutex_init (sys_mutex_t *m)
32 *m = 0;
35 void
36 sys_mutex_lock (sys_mutex_t *m)
40 void
41 sys_mutex_unlock (sys_mutex_t *m)
45 void
46 sys_cond_init (sys_cond_t *c)
48 *c = 0;
51 void
52 sys_cond_wait (sys_cond_t *c, sys_mutex_t *m)
56 void
57 sys_cond_signal (sys_cond_t *c)
61 void
62 sys_cond_broadcast (sys_cond_t *c)
66 void
67 sys_cond_destroy (sys_cond_t *c)
71 sys_thread_t
72 sys_thread_self (void)
74 return 0;
77 int
78 sys_thread_create (sys_thread_t *t, const char *name,
79 thread_creation_function *func, void *datum)
81 return 0;
84 void
85 sys_thread_yield (void)
89 #elif defined (HAVE_PTHREAD)
91 #include <sched.h>
93 #ifdef HAVE_SYS_PRCTL_H
94 #include <sys/prctl.h>
95 #endif
97 void
98 sys_mutex_init (sys_mutex_t *mutex)
100 pthread_mutex_init (mutex, NULL);
103 void
104 sys_mutex_lock (sys_mutex_t *mutex)
106 pthread_mutex_lock (mutex);
109 void
110 sys_mutex_unlock (sys_mutex_t *mutex)
112 pthread_mutex_unlock (mutex);
115 void
116 sys_cond_init (sys_cond_t *cond)
118 pthread_cond_init (cond, NULL);
121 void
122 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
124 pthread_cond_wait (cond, mutex);
127 void
128 sys_cond_signal (sys_cond_t *cond)
130 pthread_cond_signal (cond);
133 void
134 sys_cond_broadcast (sys_cond_t *cond)
136 pthread_cond_broadcast (cond);
137 #ifdef HAVE_NS
138 /* Send an app defined event to break out of the NS run loop.
139 It seems that if ns_select is running the NS run loop, this
140 broadcast has no effect until the loop is done, breaking a couple
141 of tests in thread-tests.el. */
142 ns_run_loop_break ();
143 #endif
146 void
147 sys_cond_destroy (sys_cond_t *cond)
149 pthread_cond_destroy (cond);
152 sys_thread_t
153 sys_thread_self (void)
155 return pthread_self ();
159 sys_thread_create (sys_thread_t *thread_ptr, const char *name,
160 thread_creation_function *func, void *arg)
162 pthread_attr_t attr;
163 int result = 0;
165 if (pthread_attr_init (&attr))
166 return 0;
168 if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))
170 result = pthread_create (thread_ptr, &attr, func, arg) == 0;
171 #if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME)
172 if (result && name != NULL)
173 prctl (PR_SET_NAME, name);
174 #endif
177 pthread_attr_destroy (&attr);
179 return result;
182 void
183 sys_thread_yield (void)
185 sched_yield ();
188 #elif defined (WINDOWSNT)
190 #include <windows.h>
192 /* Cannot include <process.h> because of the local header by the same
193 name, sigh. */
194 uintptr_t _beginthread (void (__cdecl *)(void *), unsigned, void *);
196 /* Mutexes are implemented as critical sections, because they are
197 faster than Windows mutex objects (implemented in userspace), and
198 satisfy the requirements, since we only need to synchronize within a
199 single process. */
200 void
201 sys_mutex_init (sys_mutex_t *mutex)
203 InitializeCriticalSection ((LPCRITICAL_SECTION)mutex);
206 void
207 sys_mutex_lock (sys_mutex_t *mutex)
209 /* FIXME: What happens if the owning thread exits without releasing
210 the mutex? According to MSDN, the result is undefined behavior. */
211 EnterCriticalSection ((LPCRITICAL_SECTION)mutex);
214 void
215 sys_mutex_unlock (sys_mutex_t *mutex)
217 LeaveCriticalSection ((LPCRITICAL_SECTION)mutex);
220 void
221 sys_cond_init (sys_cond_t *cond)
223 cond->initialized = false;
224 cond->wait_count = 0;
225 /* Auto-reset event for signal. */
226 cond->events[CONDV_SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
227 /* Manual-reset event for broadcast. */
228 cond->events[CONDV_BROADCAST] = CreateEvent (NULL, TRUE, FALSE, NULL);
229 if (!cond->events[CONDV_SIGNAL] || !cond->events[CONDV_BROADCAST])
230 return;
231 InitializeCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
232 cond->initialized = true;
235 void
236 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
238 DWORD wait_result;
239 bool last_thread_waiting;
241 if (!cond->initialized)
242 return;
244 /* Increment the wait count avoiding race conditions. */
245 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
246 cond->wait_count++;
247 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
249 /* Release the mutex and wait for either the signal or the broadcast
250 event. */
251 LeaveCriticalSection ((LPCRITICAL_SECTION)mutex);
252 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
254 /* Decrement the wait count and see if we are the last thread
255 waiting on the condition variable. */
256 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
257 cond->wait_count--;
258 last_thread_waiting =
259 wait_result == WAIT_OBJECT_0 + CONDV_BROADCAST
260 && cond->wait_count == 0;
261 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
263 /* Broadcast uses a manual-reset event, so when the last thread is
264 released, we must manually reset that event. */
265 if (last_thread_waiting)
266 ResetEvent (cond->events[CONDV_BROADCAST]);
268 /* Per the API, re-acquire the mutex. */
269 EnterCriticalSection ((LPCRITICAL_SECTION)mutex);
272 void
273 sys_cond_signal (sys_cond_t *cond)
275 bool threads_waiting;
277 if (!cond->initialized)
278 return;
280 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
281 threads_waiting = cond->wait_count > 0;
282 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
284 if (threads_waiting)
285 SetEvent (cond->events[CONDV_SIGNAL]);
288 void
289 sys_cond_broadcast (sys_cond_t *cond)
291 bool threads_waiting;
293 if (!cond->initialized)
294 return;
296 EnterCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
297 threads_waiting = cond->wait_count > 0;
298 LeaveCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
300 if (threads_waiting)
301 SetEvent (cond->events[CONDV_BROADCAST]);
304 void
305 sys_cond_destroy (sys_cond_t *cond)
307 if (cond->events[CONDV_SIGNAL])
308 CloseHandle (cond->events[CONDV_SIGNAL]);
309 if (cond->events[CONDV_BROADCAST])
310 CloseHandle (cond->events[CONDV_BROADCAST]);
312 if (!cond->initialized)
313 return;
315 /* FIXME: What if wait_count is non-zero, i.e. there are still
316 threads waiting on this condition variable? */
317 DeleteCriticalSection ((LPCRITICAL_SECTION)&cond->wait_count_lock);
320 sys_thread_t
321 sys_thread_self (void)
323 return (sys_thread_t) GetCurrentThreadId ();
326 static thread_creation_function *thread_start_address;
328 /* _beginthread wants a void function, while we are passed a function
329 that returns a pointer. So we use a wrapper. */
330 static void
331 w32_beginthread_wrapper (void *arg)
333 (void)thread_start_address (arg);
337 sys_thread_create (sys_thread_t *thread_ptr, const char *name,
338 thread_creation_function *func, void *arg)
340 /* FIXME: Do threads that run Lisp require some minimum amount of
341 stack? Zero here means each thread will get the same amount as
342 the main program. On GNU/Linux, it seems like the stack is 2MB
343 by default, overridden by RLIMIT_STACK at program start time.
344 Not sure what to do with this. See also the comment in
345 w32proc.c:new_child. */
346 const unsigned stack_size = 0;
347 uintptr_t thandle;
349 thread_start_address = func;
351 /* We use _beginthread rather than CreateThread because the former
352 arranges for the thread handle to be automatically closed when
353 the thread exits, thus preventing handle leaks and/or the need to
354 track all the threads and close their handles when they exit.
355 Also, MSDN seems to imply that code which uses CRT _must_ call
356 _beginthread, although if that is true, we already violate that
357 rule in many places... */
358 thandle = _beginthread (w32_beginthread_wrapper, stack_size, arg);
359 if (thandle == (uintptr_t)-1L)
360 return 0;
362 /* Kludge alert! We use the Windows thread ID, an unsigned 32-bit
363 number, as the sys_thread_t type, because that ID is the only
364 unique identifier of a thread on Windows. But _beginthread
365 returns a handle of the thread, and there's no easy way of
366 getting the thread ID given a handle (GetThreadId is available
367 only since Vista, so we cannot use it portably). Fortunately,
368 the value returned by sys_thread_create is not used by its
369 callers; instead, run_thread, which runs in the context of the
370 new thread, calls sys_thread_self and uses its return value;
371 sys_thread_self in this implementation calls GetCurrentThreadId.
372 Therefore, we return some more or less arbitrary value of the
373 thread ID from this function. */
374 *thread_ptr = thandle & 0xFFFFFFFF;
375 return 1;
378 void
379 sys_thread_yield (void)
381 Sleep (0);
384 #else
386 #error port me
388 #endif