This introduces the low-level system threading support. It also adds
[emacs.git] / src / systhread.c
blobb7147c4fc950f18c0d7c8876f548be8c93d19cc9
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 void
28 sys_mutex_init (sys_mutex_t *mutex)
30 pthread_mutex_init (mutex, NULL);
33 void
34 sys_mutex_lock (sys_mutex_t *mutex)
36 pthread_mutex_lock (mutex);
39 void
40 sys_mutex_unlock (sys_mutex_t *mutex)
42 pthread_mutex_unlock (mutex);
45 void
46 sys_mutex_destroy (sys_mutex_t *mutex)
48 pthread_mutex_destroy (mutex);
51 void
52 sys_cond_init (sys_cond_t *cond)
54 pthread_cond_init (cond, NULL);
57 void
58 sys_cond_wait (sys_cond_t *cond, sys_mutex_t *mutex)
60 pthread_cond_wait (cond, mutex);
63 void
64 sys_cond_signal (sys_cond_t *cond)
66 pthread_cond_signal (cond);
69 void
70 sys_cond_broadcast (sys_cond_t *cond)
72 pthread_cond_broadcast (cond);
75 void
76 sys_cond_destroy (sys_cond_t *cond)
78 pthread_cond_destroy (cond);
81 void
82 lisp_mutex_init (lisp_mutex_t *mutex)
84 mutex->owner = NULL;
85 mutex->count = 0;
86 /* A lisp "mutex" is really a condition variable. */
87 pthread_cond_init (&mutex->condition, NULL);
90 void
91 lisp_mutex_lock (lisp_mutex_t *mutex)
93 struct thread_state *self;
95 if (mutex->owner == NULL)
97 mutex->owner = current_thread;
98 mutex->count = 1;
99 return;
101 if (mutex->owner == current_thread)
103 ++mutex->count;
104 return;
107 self = current_thread;
108 while (mutex->owner != NULL /* && EQ (self->error_symbol, Qnil) */)
109 pthread_cond_wait (&mutex->condition, &global_lock);
111 #if 0
112 if (!EQ (self->error_symbol, Qnil))
114 Lisp_Object error_symbol = self->error_symbol;
115 Lisp_Object data = self->error_data;
116 self->error_symbol = Qnil;
117 self->error_data = Qnil;
118 Fsignal (error_symbol, error_data);
120 #endif
122 mutex->owner = self;
123 mutex->count = 1;
126 void
127 lisp_mutex_unlock (lisp_mutex_t *mutex)
129 struct thread_state *self = current_thread;
131 if (mutex->owner != current_thread)
132 error ("blah");
134 if (--mutex->count > 0)
135 return;
137 mutex->owner = NULL;
138 pthread_cond_broadcast (&mutex->condition);
140 post_acquire_global_lock (self);
143 void
144 lisp_mutex_destroy (lisp_mutex_t *mutex)
146 sys_cond_destroy (&mutex->condition);
149 sys_thread_t
150 sys_thread_self (void)
152 return pthread_self ();
156 sys_thread_equal (sys_thread_t one, sys_thread_t two)
158 return pthread_equal (one, two);
162 sys_thread_create (sys_thread_t *thread_ptr, thread_creation_function *func,
163 void *arg)
165 pthread_attr_t attr;
166 int result = 0;
168 if (pthread_attr_init (&attr))
169 return 0;
171 if (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))
172 result = pthread_create (thread_ptr, &attr, func, arg) == 0;
174 pthread_attr_destroy (&attr);
176 return result;
179 void
180 sys_thread_yield (void)
182 sched_yield ();
185 #else
187 #error port me
189 #endif