merge from trunk
[emacs.git] / src / systhread.c
blobab6475284527e139398402ca3ed7efe7ee2f322c
1 /* System thread definitions
2 Copyright (C) 2012 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_PTHREAD
25 #include <sched.h>
27 #ifdef HAVE_SYS_PRCTL_H
28 #include <sys/prctl.h>
29 #endif
31 void
32 sys_mutex_init (sys_mutex_t *mutex)
34 pthread_mutex_init (mutex, NULL);
37 void
38 sys_mutex_lock (sys_mutex_t *mutex)
40 pthread_mutex_lock (mutex);
43 void
44 sys_mutex_unlock (sys_mutex_t *mutex)
46 pthread_mutex_unlock (mutex);
49 void
50 sys_mutex_destroy (sys_mutex_t *mutex)
52 pthread_mutex_destroy (mutex);
55 void
56 sys_cond_init (sys_cond_t *cond)
58 pthread_cond_init (cond, NULL);
61 void
62 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
64 pthread_cond_wait (cond, mutex);
67 void
68 sys_cond_signal (sys_cond_t *cond)
70 pthread_cond_signal (cond);
73 void
74 sys_cond_broadcast (sys_cond_t *cond)
76 pthread_cond_broadcast (cond);
79 void
80 sys_cond_destroy (sys_cond_t *cond)
82 pthread_cond_destroy (cond);
85 sys_thread_t
86 sys_thread_self (void)
88 return pthread_self ();
91 int
92 sys_thread_equal (sys_thread_t one, sys_thread_t two)
94 return pthread_equal (one, two);
97 int
98 sys_thread_create (sys_thread_t *thread_ptr, const char *name,
99 thread_creation_function *func, void *arg)
101 pthread_attr_t attr;
102 int result = 0;
104 if (pthread_attr_init (&attr))
105 return 0;
107 if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))
109 result = pthread_create (thread_ptr, &attr, func, arg) == 0;
110 #if defined (HAVE_SYS_PRCTL_H) && defined (HAVE_PRCTL) && defined (PR_SET_NAME)
111 if (result && name != NULL)
112 prctl (PR_SET_NAME, name);
113 #endif
116 pthread_attr_destroy (&attr);
118 return result;
121 void
122 sys_thread_yield (void)
124 sched_yield ();
127 #else
129 #error port me
131 #endif