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/>. */
23 #ifndef THREADS_ENABLED
26 sys_mutex_init (sys_mutex_t
*m
)
32 sys_mutex_lock (sys_mutex_t
*m
)
37 sys_mutex_unlock (sys_mutex_t
*m
)
42 sys_cond_init (sys_cond_t
*c
)
48 sys_cond_wait (sys_cond_t
*c
, sys_mutex_t
*m
)
53 sys_cond_signal (sys_cond_t
*c
)
58 sys_cond_broadcast (sys_cond_t
*c
)
63 sys_cond_destroy (sys_cond_t
*c
)
68 sys_thread_self (void)
74 sys_thread_create (sys_thread_t
*t
, const char *name
,
75 thread_creation_function
*func
, void *datum
)
81 sys_thread_yield (void)
85 #elif defined (HAVE_PTHREAD)
89 #ifdef HAVE_SYS_PRCTL_H
90 #include <sys/prctl.h>
94 sys_mutex_init (sys_mutex_t
*mutex
)
96 pthread_mutex_init (mutex
, NULL
);
100 sys_mutex_lock (sys_mutex_t
*mutex
)
102 pthread_mutex_lock (mutex
);
106 sys_mutex_unlock (sys_mutex_t
*mutex
)
108 pthread_mutex_unlock (mutex
);
112 sys_cond_init (sys_cond_t
*cond
)
114 pthread_cond_init (cond
, NULL
);
118 sys_cond_wait (sys_cond_t
*cond
, sys_mutex_t
*mutex
)
120 pthread_cond_wait (cond
, mutex
);
124 sys_cond_signal (sys_cond_t
*cond
)
126 pthread_cond_signal (cond
);
130 sys_cond_broadcast (sys_cond_t
*cond
)
132 pthread_cond_broadcast (cond
);
136 sys_cond_destroy (sys_cond_t
*cond
)
138 pthread_cond_destroy (cond
);
142 sys_thread_self (void)
144 return pthread_self ();
148 sys_thread_create (sys_thread_t
*thread_ptr
, const char *name
,
149 thread_creation_function
*func
, void *arg
)
154 if (pthread_attr_init (&attr
))
157 if (!pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
))
159 result
= pthread_create (thread_ptr
, &attr
, func
, arg
) == 0;
160 #if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME)
161 if (result
&& name
!= NULL
)
162 prctl (PR_SET_NAME
, name
);
166 pthread_attr_destroy (&attr
);
172 sys_thread_yield (void)
177 #elif defined (WINDOWSNT)
181 /* Cannot include <process.h> because of the local header by the same
183 uintptr_t _beginthread (void (__cdecl
*)(void *), unsigned, void *);
185 /* Mutexes are implemented as critical sections, because they are
186 faster than Windows mutex objects (implemented in userspace), and
187 satisfy the requirements, since we only need to synchronize within a
190 sys_mutex_init (sys_mutex_t
*mutex
)
192 InitializeCriticalSection ((LPCRITICAL_SECTION
)mutex
);
196 sys_mutex_lock (sys_mutex_t
*mutex
)
198 /* FIXME: What happens if the owning thread exits without releasing
199 the mutex? According to MSDN, the result is undefined behavior. */
200 EnterCriticalSection ((LPCRITICAL_SECTION
)mutex
);
204 sys_mutex_unlock (sys_mutex_t
*mutex
)
206 LeaveCriticalSection ((LPCRITICAL_SECTION
)mutex
);
210 sys_cond_init (sys_cond_t
*cond
)
212 cond
->initialized
= false;
213 cond
->wait_count
= 0;
214 /* Auto-reset event for signal. */
215 cond
->events
[CONDV_SIGNAL
] = CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
216 /* Manual-reset event for broadcast. */
217 cond
->events
[CONDV_BROADCAST
] = CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
218 if (!cond
->events
[CONDV_SIGNAL
] || !cond
->events
[CONDV_BROADCAST
])
220 InitializeCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
221 cond
->initialized
= true;
225 sys_cond_wait (sys_cond_t
*cond
, sys_mutex_t
*mutex
)
228 bool last_thread_waiting
;
230 if (!cond
->initialized
)
233 /* Increment the wait count avoiding race conditions. */
234 EnterCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
236 LeaveCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
238 /* Release the mutex and wait for either the signal or the broadcast
240 LeaveCriticalSection ((LPCRITICAL_SECTION
)mutex
);
241 wait_result
= WaitForMultipleObjects (2, cond
->events
, FALSE
, INFINITE
);
243 /* Decrement the wait count and see if we are the last thread
244 waiting on the condition variable. */
245 EnterCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
247 last_thread_waiting
=
248 wait_result
== WAIT_OBJECT_0
+ CONDV_BROADCAST
249 && cond
->wait_count
== 0;
250 LeaveCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
252 /* Broadcast uses a manual-reset event, so when the last thread is
253 released, we must manually reset that event. */
254 if (last_thread_waiting
)
255 ResetEvent (cond
->events
[CONDV_BROADCAST
]);
257 /* Per the API, re-acquire the mutex. */
258 EnterCriticalSection ((LPCRITICAL_SECTION
)mutex
);
262 sys_cond_signal (sys_cond_t
*cond
)
264 bool threads_waiting
;
266 if (!cond
->initialized
)
269 EnterCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
270 threads_waiting
= cond
->wait_count
> 0;
271 LeaveCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
274 SetEvent (cond
->events
[CONDV_SIGNAL
]);
278 sys_cond_broadcast (sys_cond_t
*cond
)
280 bool threads_waiting
;
282 if (!cond
->initialized
)
285 EnterCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
286 threads_waiting
= cond
->wait_count
> 0;
287 LeaveCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
290 SetEvent (cond
->events
[CONDV_BROADCAST
]);
294 sys_cond_destroy (sys_cond_t
*cond
)
296 if (cond
->events
[CONDV_SIGNAL
])
297 CloseHandle (cond
->events
[CONDV_SIGNAL
]);
298 if (cond
->events
[CONDV_BROADCAST
])
299 CloseHandle (cond
->events
[CONDV_BROADCAST
]);
301 if (!cond
->initialized
)
304 /* FIXME: What if wait_count is non-zero, i.e. there are still
305 threads waiting on this condition variable? */
306 DeleteCriticalSection ((LPCRITICAL_SECTION
)&cond
->wait_count_lock
);
310 sys_thread_self (void)
312 return (sys_thread_t
) GetCurrentThreadId ();
315 static thread_creation_function
*thread_start_address
;
317 /* _beginthread wants a void function, while we are passed a function
318 that returns a pointer. So we use a wrapper. */
320 w32_beginthread_wrapper (void *arg
)
322 (void)thread_start_address (arg
);
326 sys_thread_create (sys_thread_t
*thread_ptr
, const char *name
,
327 thread_creation_function
*func
, void *arg
)
329 /* FIXME: Do threads that run Lisp require some minimum amount of
330 stack? Zero here means each thread will get the same amount as
331 the main program. On GNU/Linux, it seems like the stack is 2MB
332 by default, overridden by RLIMIT_STACK at program start time.
333 Not sure what to do with this. See also the comment in
334 w32proc.c:new_child. */
335 const unsigned stack_size
= 0;
338 thread_start_address
= func
;
340 /* We use _beginthread rather than CreateThread because the former
341 arranges for the thread handle to be automatically closed when
342 the thread exits, thus preventing handle leaks and/or the need to
343 track all the threads and close their handles when they exit.
344 Also, MSDN seems to imply that code which uses CRT _must_ call
345 _beginthread, although if that is true, we already violate that
346 rule in many places... */
347 thandle
= _beginthread (w32_beginthread_wrapper
, stack_size
, arg
);
348 if (thandle
== (uintptr_t)-1L)
351 /* Kludge alert! We use the Windows thread ID, an unsigned 32-bit
352 number, as the sys_thread_t type, because that ID is the only
353 unique identifier of a thread on Windows. But _beginthread
354 returns a handle of the thread, and there's no easy way of
355 getting the thread ID given a handle (GetThreadId is available
356 only since Vista, so we cannot use it portably). Fortunately,
357 the value returned by sys_thread_create is not used by its
358 callers; instead, run_thread, which runs in the context of the
359 new thread, calls sys_thread_self and uses its return value;
360 sys_thread_self in this implementation calls GetCurrentThreadId.
361 Therefore, we return some more or less arbitrary value of the
362 thread ID from this function. */
363 *thread_ptr
= thandle
& 0xFFFFFFFF;
368 sys_thread_yield (void)