1 /*** worker-asyneq.c -- worker threads for asyneq feature
3 * Copyright (C) 2006-2008 Sebastian Freundt
5 * Author: Sebastian Freundt <hroptatyr@sxemacs.org>
7 * This file is part of SXEmacs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the author nor the names of any contributors
21 * may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /* Synched up with: Not in FSF. */
42 #include "syssignal.h"
43 #include "worker-asyneq.h"
44 #define INCLUDE_EVENTS_H_PRIVATE_SPHERE
47 event_queue_t delegate_eq = Qnull_pointer;
48 static Lisp_Object Vdelegate_eq;
49 static void eq_worker_th_blksig(void);
51 static struct work_handler_s eat_yerself = {
59 init_workers(int nthreads, sxe_thread_f handler)
64 pthread_attr_init(&attr);
65 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
67 for (i=0; i < nthreads; i++) {
68 eq_worker_t eqw = eq_make_worker();
69 /* value taken from SOUND_MAX_AUDIO_FRAME_SIZE */
70 resize_worker_scratch(eqw, 48000*6*sizeof(uint32_t));
72 &eq_worker_thread(eqw), &attr, handler, eqw);
73 dllist_append(workers, eqw);
76 pthread_attr_destroy(&attr);
81 fini_worker(eq_worker_t eqw)
83 xthread_join(eq_worker_thread(eqw), NULL);
84 SXE_MUTEX_UNLOCK(&eq_worker_mtx(eqw));
89 extern event_queue_t asyneq;
92 eq_worker_eaten_myself(eq_worker_t eqw)
94 Lisp_Object emev = Qnil;
98 emev = make_empty_event();
99 XEVENT(emev)->event_type = eaten_myself_event;
100 XEVENT(emev)->event.eaten_myself.worker = eqw;
101 eq_enqueue(asyneq, emev);
107 eq_worker_work_started(Lisp_Object job)
109 Lisp_Object wsev = Qnil;
113 wsev = make_empty_event();
114 XEVENT(wsev)->event_type = work_started_event;
115 XEVENT(wsev)->event.work_started.job = job;
116 eq_enqueue(asyneq, wsev);
122 eq_worker_work_finished(Lisp_Object job)
124 Lisp_Object wfev = Qnil;
128 wfev = make_empty_event();
129 XEVENT(wfev)->event_type = work_finished_event;
130 XEVENT(wfev)->event.work_finished.job = job;
131 eq_enqueue(asyneq, wfev);
133 asyneq_handle_event(asyneq);
138 eq_delegate_work(event_queue_t eq)
140 int cur = eq_queue_size(eq);
142 eq_queue_trigger(eq);
149 eq_worker_th_blksig(void)
151 EMACS_BLOCK_SIGNAL(SIGINT); /* ANSI */
152 EMACS_BLOCK_SIGNAL(SIGILL); /* ANSI */
153 EMACS_BLOCK_SIGNAL(SIGABRT); /* ANSI */
154 EMACS_BLOCK_SIGNAL(SIGFPE); /* ANSI */
155 EMACS_BLOCK_SIGNAL(SIGSEGV); /* ANSI */
156 EMACS_BLOCK_SIGNAL(SIGTERM); /* ANSI */
159 EMACS_BLOCK_SIGNAL(SIGHUP); /* POSIX */
162 EMACS_BLOCK_SIGNAL(SIGQUIT); /* POSIX */
165 EMACS_BLOCK_SIGNAL(SIGTRAP); /* POSIX */
168 EMACS_BLOCK_SIGNAL(SIGUSR1); /* POSIX */
171 EMACS_BLOCK_SIGNAL(SIGUSR2); /* POSIX */
174 EMACS_BLOCK_SIGNAL(SIGPIPE); /* POSIX */
177 EMACS_BLOCK_SIGNAL(SIGALRM); /* POSIX */
180 EMACS_BLOCK_SIGNAL(SIGCHLD); /* POSIX */
183 EMACS_BLOCK_SIGNAL(SIGCONT); /* POSIX */
186 EMACS_BLOCK_SIGNAL(SIGSTOP); /* POSIX */
189 EMACS_BLOCK_SIGNAL(SIGTSTP); /* POSIX */
192 EMACS_BLOCK_SIGNAL(SIGTTIN); /* POSIX */
195 EMACS_BLOCK_SIGNAL(SIGTTOU); /* POSIX */
199 EMACS_BLOCK_SIGNAL(SIGBUS); /* XPG5 */
202 EMACS_BLOCK_SIGNAL(SIGPOLL); /* XPG5 */
205 EMACS_BLOCK_SIGNAL(SIGPROF); /* XPG5 */
208 EMACS_BLOCK_SIGNAL(SIGSYS); /* XPG5 */
211 EMACS_BLOCK_SIGNAL(SIGURG); /* XPG5 */
214 EMACS_BLOCK_SIGNAL(SIGXCPU); /* XPG5 */
217 EMACS_BLOCK_SIGNAL(SIGXFSZ); /* XPG5 */
219 #if defined SIGVTALRM
220 EMACS_BLOCK_SIGNAL(SIGVTALRM); /* XPG5 */
224 EMACS_BLOCK_SIGNAL(SIGIO); /* BSD 4.2 */
227 EMACS_BLOCK_SIGNAL(SIGWINCH); /* BSD 4.3 */
231 EMACS_BLOCK_SIGNAL(SIGEMT);
234 EMACS_BLOCK_SIGNAL(SIGINFO);
237 EMACS_BLOCK_SIGNAL(SIGHWE);
240 EMACS_BLOCK_SIGNAL(SIGPRE);
243 EMACS_BLOCK_SIGNAL(SIGUME);
246 EMACS_BLOCK_SIGNAL(SIGDLK);
248 #if defined SIGCPULIM
249 EMACS_BLOCK_SIGNAL(SIGCPULIM);
252 EMACS_BLOCK_SIGNAL(SIGIOT);
255 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
256 EMACS_BLOCK_SIGNAL(SIGLOST);
257 # endif /* BDWGC case */
259 #if defined SIGSTKFLT
260 EMACS_BLOCK_SIGNAL(SIGSTKFLT);
262 #if defined SIGUNUSED
263 EMACS_BLOCK_SIGNAL(SIGUNUSED);
265 #if defined SIGDANGER
266 EMACS_BLOCK_SIGNAL(SIGDANGER); /* AIX */
269 EMACS_BLOCK_SIGNAL(SIGMSG);
272 EMACS_BLOCK_SIGNAL(SIGSOUND);
274 #if defined SIGRETRACT
275 EMACS_BLOCK_SIGNAL(SIGRETRACT);
278 EMACS_BLOCK_SIGNAL(SIGGRANT);
281 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
282 EMACS_BLOCK_SIGNAL(SIGPWR);
283 # endif /* BDWGC case */
288 eq_worker_th(void *eqwptr)
290 Lisp_Object ljob = Qnil;
291 worker_job_t job = NULL;
292 eq_worker_t eqw = eqwptr;
296 eq_worker_th_blksig();
300 eq_queue_synch(delegate_eq);
302 EQUEUE_DEBUG_WORKER("dequeuing thread: 0x%lx\n",
303 (long unsigned int)pthread_self());
305 /* fetch one event now */
308 eq_dequeue_pro(&ljob, delegate_eq);
310 EQUEUE_DEBUG_WORKER("No event on the queue. "
311 "Who dared to wake me up?! >8(\n");
316 job = XWORKER_JOB(ljob);
317 hdl = XWORKER_JOB_HANDLER(ljob);
318 EQUEUE_DEBUG_WORKER("escrowing event 0x%lx in worker 0x%lx.\n",
319 (long unsigned int)job,
320 (long unsigned int)eqw);
322 /* maybe it's a eat-yourself ticket? */
323 if (hdl == &eat_yerself) {
324 /* awww ... we gotta exit :( */
326 "Worker 0x%lx commits suicide...\n",
327 (long unsigned int)eqw);
328 eq_unlock_meself(eqw);
329 eq_worker_eaten_myself(eqw);
335 /* help the job a bit with local resources */
336 EQUEUE_DEBUG_SCRATCH("inherit scratch buffer 0x%lx of size %ld\n",
337 (long unsigned int)eq_worker_scratch(eqw),
338 eq_worker_scratch_alloc_size(eqw));
339 worker_job_buffer(job) = eq_worker_scratch(eqw);
340 worker_job_buffer_alloc_size(job) = eq_worker_scratch_alloc_size(eqw);
342 /* generate a started event and update job state */
343 worker_job_state(job) = WORKER_JOB_RUNNING;
344 eq_worker_work_started(ljob);
346 /* ... otherwise handle the event */
347 work_handler(hdl)(job);
349 /* generate a `finished' event,
350 * sentinel code shall be injected in the routine
351 * called by eq_worker_handle_event() */
352 worker_job_state(job) = WORKER_JOB_FINISHED;
353 eq_worker_work_finished(ljob);
355 eq_unlock_meself(eqw);
357 EQUEUE_DEBUG_WORKER("enqueuing thread: 0x%lx\n",
358 (long unsigned int)pthread_self());
364 DEFUN("init-workers", Finit_workers, 1, 1, 0, /*
365 Initialise NUMBER-OF-WORKERS worker threads.
366 If called repeatedly this function does NOT add more workers
367 use `add-workers' instead.
371 CHECK_NATNUM(number_of_workers);
372 if (dllist_get_size(workers) <= 1) {
373 init_workers(XINT(number_of_workers), eq_worker_th);
379 DEFUN("add-workers", Fadd_workers, 1, 1, 0, /*
380 Add NUMBER-OF-WORKERS worker threads.
384 CHECK_NATNUM(number_of_workers);
385 init_workers(XINT(number_of_workers), eq_worker_th);
389 DEFUN("remove-workers", Fremove_workers, 0, 1, 0, /*
390 Stop NUMBER-OF-WORKERS worker threads. By default stop all.
391 Depending on whether there are busy this operation may block the
392 main execution loop until all worker threads are non-busy.
396 Lisp_Object job = Qnil;
397 int i, noev = 0; /* how many eat_yerself events to send? */
400 if (NILP(number_of_workers)) {
401 noev = dllist_get_size(workers)-1;
403 CHECK_NATNUM(number_of_workers);
404 noev = XINT(number_of_workers);
408 for (i = 0; i < noev; i++) {
409 job = wrap_object(make_worker_job(&eat_yerself));
410 eq_enqueue(delegate_eq, job);
412 eq_queue_trigger_all(delegate_eq);
417 DEFUN("trigger-workers", Ftrigger_workers, 0, 0, 0, /*
418 Trigger all worker threads.
422 eq_queue_trigger_all(delegate_eq);
426 DEFUN("running-workers", Frunning_workers, 0, 0, 0, /*
427 Return the number of currently running worker threads,
428 the main thread excluded.
432 return make_int(dllist_get_size(workers)-1);
436 void syms_of_worker_asyneq(void)
438 DEFSUBR(Finit_workers);
439 DEFSUBR(Fadd_workers);
440 DEFSUBR(Fremove_workers);
441 DEFSUBR(Ftrigger_workers);
442 DEFSUBR(Frunning_workers);
445 void reinit_vars_of_worker_asyneq(void)
447 /* the delegate queue in case of multiple threads */
448 delegate_eq = make_event_queue();
449 XSETEVENT_QUEUE(Vdelegate_eq, delegate_eq);
450 staticpro_nodump(&Vdelegate_eq);
453 void vars_of_worker_asyneq(void)
455 Fprovide(intern("asyneq"));
458 /* workers.c ends here */