Merge branch 'bug/131' into next
[sxemacs] / src / events / worker-asyneq.c
1 /*** worker-asyneq.c -- worker threads for asyneq feature
2  *
3  * Copyright (C) 2006-2008  Sebastian Freundt
4  *
5  * Author:  Sebastian Freundt <hroptatyr@sxemacs.org>
6  *
7  * This file is part of SXEmacs.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
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.
19  *
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.
23  *
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.
35  *
36  ***/
37
38 /* Synched up with: Not in FSF. */
39
40 #include <config.h>
41 #include "lisp.h"
42 #include "syssignal.h"
43 #include "worker-asyneq.h"
44 #define INCLUDE_EVENTS_H_PRIVATE_SPHERE
45 #include "events.h"
46
47 event_queue_t delegate_eq = Qnull_pointer;
48 static Lisp_Object Vdelegate_eq;
49 static void eq_worker_th_blksig(void);
50
51 static struct work_handler_s eat_yerself = {
52         NULL,                   /* markfun */
53         NULL,                   /* printer */
54         NULL                    /* finaliser */
55 };
56
57 \f
58 void
59 init_workers(int nthreads, sxe_thread_f handler)
60 {
61         pthread_attr_t attr;
62         int i;
63
64         pthread_attr_init(&attr);
65         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
66
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));
71                 xthread_create(
72                         &eq_worker_thread(eqw), &attr, handler, eqw);
73                 dllist_append(workers, eqw);
74         }
75
76         pthread_attr_destroy(&attr);
77         return;
78 }
79
80 void
81 fini_worker(eq_worker_t eqw)
82 {
83         xthread_join(eq_worker_thread(eqw), NULL);
84         SXE_MUTEX_UNLOCK(&eq_worker_mtx(eqw));
85         eq_free_worker(eqw);
86 }
87
88 \f
89 extern event_queue_t asyneq;
90
91 void
92 eq_worker_eaten_myself(eq_worker_t eqw)
93 {
94         Lisp_Object emev = Qnil;
95         struct gcpro gcpro1;
96
97         GCPRO1(emev);
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);
102         UNGCPRO;
103         asyneq_handle_event(asyneq);
104         return;
105 }
106
107 void
108 eq_worker_work_started(Lisp_Object job)
109 {
110         Lisp_Object wsev = Qnil;
111         struct gcpro gcpro1;
112
113         GCPRO1(wsev);
114         wsev = make_empty_event();
115         XEVENT(wsev)->event_type = work_started_event;
116         XEVENT(wsev)->event.work_started.job = job;
117         eq_enqueue(asyneq, wsev);
118         UNGCPRO;
119         asyneq_handle_event(asyneq);
120         return;
121 }
122
123 void
124 eq_worker_work_finished(Lisp_Object job)
125 {
126         Lisp_Object wfev = Qnil;
127         struct gcpro gcpro1;
128
129         GCPRO1(wfev);
130         wfev = make_empty_event();
131         XEVENT(wfev)->event_type = work_finished_event;
132         XEVENT(wfev)->event.work_finished.job = job;
133         eq_enqueue(asyneq, wfev);
134         UNGCPRO;
135         asyneq_handle_event(asyneq);
136         return;
137 }
138
139 void
140 eq_delegate_work(event_queue_t eq)
141 {
142         int cur = eq_queue_size(eq);
143         while (cur--) { 
144                 eq_queue_trigger(eq);
145         }
146         return;
147 }
148
149 \f
150 static void
151 eq_worker_th_blksig(void)
152 {
153         EMACS_BLOCK_SIGNAL(SIGINT);     /* ANSI */
154         EMACS_BLOCK_SIGNAL(SIGILL);     /* ANSI */
155         EMACS_BLOCK_SIGNAL(SIGABRT);    /* ANSI */
156         EMACS_BLOCK_SIGNAL(SIGFPE);     /* ANSI */
157         EMACS_BLOCK_SIGNAL(SIGSEGV);    /* ANSI */
158         EMACS_BLOCK_SIGNAL(SIGTERM);    /* ANSI */
159         
160 #if defined SIGHUP
161         EMACS_BLOCK_SIGNAL(SIGHUP);     /* POSIX */
162 #endif
163 #if defined SIGQUIT
164         EMACS_BLOCK_SIGNAL(SIGQUIT);    /* POSIX */
165 #endif
166 #if defined SIGTRAP
167         EMACS_BLOCK_SIGNAL(SIGTRAP);    /* POSIX */
168 #endif
169 #if defined SIGUSR1
170         EMACS_BLOCK_SIGNAL(SIGUSR1);    /* POSIX */
171 #endif
172 #if defined SIGUSR2
173         EMACS_BLOCK_SIGNAL(SIGUSR2);    /* POSIX */
174 #endif
175 #if defined SIGPIPE
176         EMACS_BLOCK_SIGNAL(SIGPIPE);    /* POSIX */
177 #endif
178 #if defined SIGALRM
179         EMACS_BLOCK_SIGNAL(SIGALRM);    /* POSIX */
180 #endif
181 #if defined SIGCHLD
182         EMACS_BLOCK_SIGNAL(SIGCHLD);    /* POSIX */
183 #endif
184 #if defined SIGCONT
185         EMACS_BLOCK_SIGNAL(SIGCONT);    /* POSIX */
186 #endif
187 #if defined SIGSTOP
188         EMACS_BLOCK_SIGNAL(SIGSTOP);    /* POSIX */
189 #endif
190 #if defined SIGTSTP
191         EMACS_BLOCK_SIGNAL(SIGTSTP);    /* POSIX */
192 #endif
193 #if defined SIGTTIN
194         EMACS_BLOCK_SIGNAL(SIGTTIN);    /* POSIX */
195 #endif
196 #if defined SIGTTOU
197         EMACS_BLOCK_SIGNAL(SIGTTOU);    /* POSIX */
198 #endif
199
200 #if defined SIGBUS
201         EMACS_BLOCK_SIGNAL(SIGBUS);     /* XPG5 */
202 #endif
203 #if defined SIGPOLL
204         EMACS_BLOCK_SIGNAL(SIGPOLL);    /* XPG5 */
205 #endif
206 #if defined SIGPROF
207         EMACS_BLOCK_SIGNAL(SIGPROF);    /* XPG5 */
208 #endif
209 #if defined SIGSYS
210         EMACS_BLOCK_SIGNAL(SIGSYS);     /* XPG5 */
211 #endif
212 #if defined SIGURG
213         EMACS_BLOCK_SIGNAL(SIGURG);     /* XPG5 */
214 #endif
215 #if defined SIGXCPU
216         EMACS_BLOCK_SIGNAL(SIGXCPU);    /* XPG5 */
217 #endif
218 #if defined SIGXFSZ
219         EMACS_BLOCK_SIGNAL(SIGXFSZ);    /* XPG5 */
220 #endif
221 #if defined SIGVTALRM
222         EMACS_BLOCK_SIGNAL(SIGVTALRM);  /* XPG5 */
223 #endif
224
225 #if defined SIGIO
226         EMACS_BLOCK_SIGNAL(SIGIO);      /* BSD 4.2 */
227 #endif
228 #if defined SIGWINCH
229         EMACS_BLOCK_SIGNAL(SIGWINCH);   /* BSD 4.3 */
230 #endif
231
232 #if defined SIGEMT
233         EMACS_BLOCK_SIGNAL(SIGEMT);
234 #endif
235 #if defined SIGINFO
236         EMACS_BLOCK_SIGNAL(SIGINFO);
237 #endif
238 #if defined SIGHWE
239         EMACS_BLOCK_SIGNAL(SIGHWE);
240 #endif
241 #if defined SIGPRE
242         EMACS_BLOCK_SIGNAL(SIGPRE);
243 #endif
244 #if defined SIGUME
245         EMACS_BLOCK_SIGNAL(SIGUME);
246 #endif
247 #if defined SIGDLK
248         EMACS_BLOCK_SIGNAL(SIGDLK);
249 #endif
250 #if defined SIGCPULIM
251         EMACS_BLOCK_SIGNAL(SIGCPULIM);
252 #endif
253 #if defined SIGIOT
254         EMACS_BLOCK_SIGNAL(SIGIOT);
255 #endif
256 #if defined SIGLOST
257 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
258         EMACS_BLOCK_SIGNAL(SIGLOST);
259 # endif  /* BDWGC case */
260 #endif
261 #if defined SIGSTKFLT
262         EMACS_BLOCK_SIGNAL(SIGSTKFLT);
263 #endif
264 #if defined SIGUNUSED
265         EMACS_BLOCK_SIGNAL(SIGUNUSED);
266 #endif
267 #if defined SIGDANGER
268         EMACS_BLOCK_SIGNAL(SIGDANGER);  /* AIX */
269 #endif
270 #if defined SIGMSG
271         EMACS_BLOCK_SIGNAL(SIGMSG);
272 #endif
273 #if defined SIGSOUND
274         EMACS_BLOCK_SIGNAL(SIGSOUND);
275 #endif
276 #if defined SIGRETRACT
277         EMACS_BLOCK_SIGNAL(SIGRETRACT);
278 #endif
279 #if defined SIGGRANT
280         EMACS_BLOCK_SIGNAL(SIGGRANT);
281 #endif
282 #if defined SIGPWR
283 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
284         EMACS_BLOCK_SIGNAL(SIGPWR);
285 # endif  /* BDWGC case */
286 #endif
287 }
288
289 static void *
290 eq_worker_th(void *eqwptr)
291 {
292         Lisp_Object ljob = Qnil;
293         worker_job_t job = NULL;
294         eq_worker_t eqw = eqwptr;
295         work_handler_t hdl;
296         struct gcpro gcpro1;
297
298         eq_worker_th_blksig();
299
300         GCPRO1(ljob);
301 listen:
302         eq_queue_synch(delegate_eq);
303
304         EQUEUE_DEBUG_WORKER("dequeuing thread: 0x%lx\n",
305                             (long unsigned int)pthread_self());
306
307         /* fetch one event now */
308 refetch:
309         ljob = Qnil;
310         eq_dequeue_pro(&ljob, delegate_eq);
311         if (NILP(ljob)) {
312                 EQUEUE_DEBUG_WORKER("No event on the queue. "
313                                     "Who dared to wake me up?! >8(\n");
314                 goto listen;
315         }
316
317         eq_lock_meself(eqw);
318         job = XWORKER_JOB(ljob);
319         hdl = XWORKER_JOB_HANDLER(ljob);
320         EQUEUE_DEBUG_WORKER("escrowing event 0x%lx in worker 0x%lx.\n",
321                             (long unsigned int)job,
322                             (long unsigned int)eqw);
323
324         /* maybe it's a eat-yourself ticket? */
325         if (hdl == &eat_yerself) {
326                 /* awww ... we gotta exit :( */
327                 EQUEUE_DEBUG_WORKER(
328                         "Worker 0x%lx commits suicide...\n",
329                         (long unsigned int)eqw);
330                 eq_unlock_meself(eqw);
331                 eq_worker_eaten_myself(eqw);
332                 UNGCPRO;
333                 pthread_exit(NULL);
334                 return NULL;
335         }
336
337         /* help the job a bit with local resources */
338         EQUEUE_DEBUG_SCRATCH("inherit scratch buffer 0x%lx of size %ld\n",
339                              (long unsigned int)eq_worker_scratch(eqw),
340                              eq_worker_scratch_alloc_size(eqw));
341         worker_job_buffer(job) = eq_worker_scratch(eqw);
342         worker_job_buffer_alloc_size(job) = eq_worker_scratch_alloc_size(eqw);
343
344         /* generate a started event and update job state */
345         worker_job_state(job) = WORKER_JOB_RUNNING;
346         eq_worker_work_started(ljob);
347
348         /* ... otherwise handle the event */
349         work_handler(hdl)(job);
350
351         /* generate a `finished' event,
352          * sentinel code shall be injected in the routine
353          * called by eq_worker_handle_event() */
354         worker_job_state(job) = WORKER_JOB_FINISHED;
355         eq_worker_work_finished(ljob);
356
357         eq_unlock_meself(eqw);
358
359         EQUEUE_DEBUG_WORKER("enqueuing thread: 0x%lx\n",
360                             (long unsigned int)pthread_self());
361         goto refetch;
362         /* not reached */
363         return NULL;
364 }
365
366 DEFUN("init-workers", Finit_workers, 1, 1, 0, /*
367 Initialise NUMBER-OF-WORKERS worker threads.
368 If called repeatedly this function does NOT add more workers
369 use `add-workers' instead.
370 */
371       (number_of_workers))
372 {
373         CHECK_NATNUM(number_of_workers);
374         if (dllist_get_size(workers) <= 1) {
375                 init_workers(XINT(number_of_workers), eq_worker_th);
376         }
377         return Qt;
378 }
379
380
381 DEFUN("add-workers", Fadd_workers, 1, 1, 0, /*
382 Add NUMBER-OF-WORKERS worker threads.
383 */
384       (number_of_workers))
385 {
386         CHECK_NATNUM(number_of_workers);
387         init_workers(XINT(number_of_workers), eq_worker_th);
388         return Qt;
389 }
390
391 DEFUN("remove-workers", Fremove_workers, 0, 1, 0, /*
392 Stop NUMBER-OF-WORKERS worker threads.  By default stop all.
393 Depending on whether there are busy this operation may block the
394 main execution loop until all worker threads are non-busy.
395 */
396       (number_of_workers))
397 {
398         Lisp_Object job = Qnil;
399         int i, noev = 0;        /* how many eat_yerself events to send? */
400         struct gcpro gcpro1;
401
402         if (NILP(number_of_workers)) {
403                 noev = dllist_get_size(workers)-1;
404         } else {
405                 CHECK_NATNUM(number_of_workers);
406                 noev = XINT(number_of_workers);
407         }
408
409         GCPRO1(job);
410         for (i = 0; i < noev; i++) {
411                 job = wrap_object(make_worker_job(&eat_yerself));
412                 eq_enqueue(delegate_eq, job);
413         }
414         eq_queue_trigger_all(delegate_eq);
415         UNGCPRO;
416         return job;
417 }
418
419 DEFUN("trigger-workers", Ftrigger_workers, 0, 0, 0, /*
420 Trigger all worker threads.
421 */
422       ())
423 {
424         eq_queue_trigger_all(delegate_eq);
425         return Qt;
426 }
427
428 DEFUN("running-workers", Frunning_workers, 0, 0, 0, /*
429 Return the number of currently running worker threads,
430 the main thread excluded.
431 */
432       ())
433 {
434         return make_int(dllist_get_size(workers)-1);
435 }
436
437 \f
438 void syms_of_worker_asyneq(void)
439 {
440         DEFSUBR(Finit_workers);
441         DEFSUBR(Fadd_workers);
442         DEFSUBR(Fremove_workers);
443         DEFSUBR(Ftrigger_workers);
444         DEFSUBR(Frunning_workers);
445 }
446
447 void reinit_vars_of_worker_asyneq(void)
448 {
449         /* the delegate queue in case of multiple threads */
450         delegate_eq = make_event_queue();
451         XSETEVENT_QUEUE(Vdelegate_eq, delegate_eq);
452         staticpro_nodump(&Vdelegate_eq);
453 }
454
455 void vars_of_worker_asyneq(void)
456 {
457         Fprovide(intern("asyneq"));
458 }
459
460 /* workers.c ends here */