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/>. */
28 sys_mutex_init (sys_mutex_t
*mutex
)
30 pthread_mutex_init (mutex
, NULL
);
34 sys_mutex_lock (sys_mutex_t
*mutex
)
36 pthread_mutex_lock (mutex
);
40 sys_mutex_unlock (sys_mutex_t
*mutex
)
42 pthread_mutex_unlock (mutex
);
46 sys_mutex_destroy (sys_mutex_t
*mutex
)
48 pthread_mutex_destroy (mutex
);
52 sys_cond_init (sys_cond_t
*cond
)
54 pthread_cond_init (cond
, NULL
);
58 sys_cond_wait (sys_cond_t
*cond
, sys_mutex_t
*mutex
)
60 pthread_cond_wait (cond
, mutex
);
64 sys_cond_signal (sys_cond_t
*cond
)
66 pthread_cond_signal (cond
);
70 sys_cond_broadcast (sys_cond_t
*cond
)
72 pthread_cond_broadcast (cond
);
76 sys_cond_destroy (sys_cond_t
*cond
)
78 pthread_cond_destroy (cond
);
82 lisp_mutex_init (lisp_mutex_t
*mutex
)
86 /* A lisp "mutex" is really a condition variable. */
87 pthread_cond_init (&mutex
->condition
, NULL
);
91 lisp_mutex_lock (lisp_mutex_t
*mutex
)
93 struct thread_state
*self
;
95 if (mutex
->owner
== NULL
)
97 mutex
->owner
= current_thread
;
101 if (mutex
->owner
== current_thread
)
107 self
= current_thread
;
108 while (mutex
->owner
!= NULL
/* && EQ (self->error_symbol, Qnil) */)
109 pthread_cond_wait (&mutex
->condition
, &global_lock
);
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
);
127 lisp_mutex_unlock (lisp_mutex_t
*mutex
)
129 struct thread_state
*self
= current_thread
;
131 if (mutex
->owner
!= current_thread
)
134 if (--mutex
->count
> 0)
138 pthread_cond_broadcast (&mutex
->condition
);
140 post_acquire_global_lock (self
);
144 lisp_mutex_destroy (lisp_mutex_t
*mutex
)
146 sys_cond_destroy (&mutex
->condition
);
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
,
168 if (pthread_attr_init (&attr
))
171 if (!pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
))
172 result
= pthread_create (thread_ptr
, &attr
, func
, arg
) == 0;
174 pthread_attr_destroy (&attr
);
180 sys_thread_yield (void)