Really fix bug 131, for real this time
[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         return;
104 }
105
106 void
107 eq_worker_work_started(Lisp_Object job)
108 {
109         Lisp_Object wsev = Qnil;
110         struct gcpro gcpro1;
111
112         GCPRO1(wsev);
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);
117         UNGCPRO;
118         return;
119 }
120
121 void
122 eq_worker_work_finished(Lisp_Object job)
123 {
124         Lisp_Object wfev = Qnil;
125         struct gcpro gcpro1;
126
127         GCPRO1(wfev);
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);
132         UNGCPRO;
133         asyneq_handle_event(asyneq);
134         return;
135 }
136
137 void
138 eq_delegate_work(event_queue_t eq)
139 {
140         int cur = eq_queue_size(eq);
141         while (cur--) { 
142                 eq_queue_trigger(eq);
143         }
144         return;
145 }
146
147 \f
148 static void
149 eq_worker_th_blksig(void)
150 {
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 */
157         
158 #if defined SIGHUP
159         EMACS_BLOCK_SIGNAL(SIGHUP);     /* POSIX */
160 #endif
161 #if defined SIGQUIT
162         EMACS_BLOCK_SIGNAL(SIGQUIT);    /* POSIX */
163 #endif
164 #if defined SIGTRAP
165         EMACS_BLOCK_SIGNAL(SIGTRAP);    /* POSIX */
166 #endif
167 #if defined SIGUSR1
168         EMACS_BLOCK_SIGNAL(SIGUSR1);    /* POSIX */
169 #endif
170 #if defined SIGUSR2
171         EMACS_BLOCK_SIGNAL(SIGUSR2);    /* POSIX */
172 #endif
173 #if defined SIGPIPE
174         EMACS_BLOCK_SIGNAL(SIGPIPE);    /* POSIX */
175 #endif
176 #if defined SIGALRM
177         EMACS_BLOCK_SIGNAL(SIGALRM);    /* POSIX */
178 #endif
179 #if defined SIGCHLD
180         EMACS_BLOCK_SIGNAL(SIGCHLD);    /* POSIX */
181 #endif
182 #if defined SIGCONT
183         EMACS_BLOCK_SIGNAL(SIGCONT);    /* POSIX */
184 #endif
185 #if defined SIGSTOP
186         EMACS_BLOCK_SIGNAL(SIGSTOP);    /* POSIX */
187 #endif
188 #if defined SIGTSTP
189         EMACS_BLOCK_SIGNAL(SIGTSTP);    /* POSIX */
190 #endif
191 #if defined SIGTTIN
192         EMACS_BLOCK_SIGNAL(SIGTTIN);    /* POSIX */
193 #endif
194 #if defined SIGTTOU
195         EMACS_BLOCK_SIGNAL(SIGTTOU);    /* POSIX */
196 #endif
197
198 #if defined SIGBUS
199         EMACS_BLOCK_SIGNAL(SIGBUS);     /* XPG5 */
200 #endif
201 #if defined SIGPOLL
202         EMACS_BLOCK_SIGNAL(SIGPOLL);    /* XPG5 */
203 #endif
204 #if defined SIGPROF
205         EMACS_BLOCK_SIGNAL(SIGPROF);    /* XPG5 */
206 #endif
207 #if defined SIGSYS
208         EMACS_BLOCK_SIGNAL(SIGSYS);     /* XPG5 */
209 #endif
210 #if defined SIGURG
211         EMACS_BLOCK_SIGNAL(SIGURG);     /* XPG5 */
212 #endif
213 #if defined SIGXCPU
214         EMACS_BLOCK_SIGNAL(SIGXCPU);    /* XPG5 */
215 #endif
216 #if defined SIGXFSZ
217         EMACS_BLOCK_SIGNAL(SIGXFSZ);    /* XPG5 */
218 #endif
219 #if defined SIGVTALRM
220         EMACS_BLOCK_SIGNAL(SIGVTALRM);  /* XPG5 */
221 #endif
222
223 #if defined SIGIO
224         EMACS_BLOCK_SIGNAL(SIGIO);      /* BSD 4.2 */
225 #endif
226 #if defined SIGWINCH
227         EMACS_BLOCK_SIGNAL(SIGWINCH);   /* BSD 4.3 */
228 #endif
229
230 #if defined SIGEMT
231         EMACS_BLOCK_SIGNAL(SIGEMT);
232 #endif
233 #if defined SIGINFO
234         EMACS_BLOCK_SIGNAL(SIGINFO);
235 #endif
236 #if defined SIGHWE
237         EMACS_BLOCK_SIGNAL(SIGHWE);
238 #endif
239 #if defined SIGPRE
240         EMACS_BLOCK_SIGNAL(SIGPRE);
241 #endif
242 #if defined SIGUME
243         EMACS_BLOCK_SIGNAL(SIGUME);
244 #endif
245 #if defined SIGDLK
246         EMACS_BLOCK_SIGNAL(SIGDLK);
247 #endif
248 #if defined SIGCPULIM
249         EMACS_BLOCK_SIGNAL(SIGCPULIM);
250 #endif
251 #if defined SIGIOT
252         EMACS_BLOCK_SIGNAL(SIGIOT);
253 #endif
254 #if defined SIGLOST
255 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
256         EMACS_BLOCK_SIGNAL(SIGLOST);
257 # endif  /* BDWGC case */
258 #endif
259 #if defined SIGSTKFLT
260         EMACS_BLOCK_SIGNAL(SIGSTKFLT);
261 #endif
262 #if defined SIGUNUSED
263         EMACS_BLOCK_SIGNAL(SIGUNUSED);
264 #endif
265 #if defined SIGDANGER
266         EMACS_BLOCK_SIGNAL(SIGDANGER);  /* AIX */
267 #endif
268 #if defined SIGMSG
269         EMACS_BLOCK_SIGNAL(SIGMSG);
270 #endif
271 #if defined SIGSOUND
272         EMACS_BLOCK_SIGNAL(SIGSOUND);
273 #endif
274 #if defined SIGRETRACT
275         EMACS_BLOCK_SIGNAL(SIGRETRACT);
276 #endif
277 #if defined SIGGRANT
278         EMACS_BLOCK_SIGNAL(SIGGRANT);
279 #endif
280 #if defined SIGPWR
281 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
282         EMACS_BLOCK_SIGNAL(SIGPWR);
283 # endif  /* BDWGC case */
284 #endif
285 }
286
287 static void *
288 eq_worker_th(void *eqwptr)
289 {
290         Lisp_Object ljob = Qnil;
291         worker_job_t job = NULL;
292         eq_worker_t eqw = eqwptr;
293         work_handler_t hdl;
294         struct gcpro gcpro1;
295
296         eq_worker_th_blksig();
297
298         GCPRO1(ljob);
299 listen:
300         eq_queue_synch(delegate_eq);
301
302         EQUEUE_DEBUG_WORKER("dequeuing thread: 0x%lx\n",
303                             (long unsigned int)pthread_self());
304
305         /* fetch one event now */
306 refetch:
307         ljob = Qnil;
308         eq_dequeue_pro(&ljob, delegate_eq);
309         if (NILP(ljob)) {
310                 EQUEUE_DEBUG_WORKER("No event on the queue. "
311                                     "Who dared to wake me up?! >8(\n");
312                 goto listen;
313         }
314
315         eq_lock_meself(eqw);
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);
321
322         /* maybe it's a eat-yourself ticket? */
323         if (hdl == &eat_yerself) {
324                 /* awww ... we gotta exit :( */
325                 EQUEUE_DEBUG_WORKER(
326                         "Worker 0x%lx commits suicide...\n",
327                         (long unsigned int)eqw);
328                 eq_unlock_meself(eqw);
329                 eq_worker_eaten_myself(eqw);
330                 UNGCPRO;
331                 pthread_exit(NULL);
332                 return NULL;
333         }
334
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);
341
342         /* generate a started event and update job state */
343         worker_job_state(job) = WORKER_JOB_RUNNING;
344         eq_worker_work_started(ljob);
345
346         /* ... otherwise handle the event */
347         work_handler(hdl)(job);
348
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);
354
355         eq_unlock_meself(eqw);
356
357         EQUEUE_DEBUG_WORKER("enqueuing thread: 0x%lx\n",
358                             (long unsigned int)pthread_self());
359         goto refetch;
360         /* not reached */
361         return NULL;
362 }
363
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.
368 */
369       (number_of_workers))
370 {
371         CHECK_NATNUM(number_of_workers);
372         if (dllist_get_size(workers) <= 1) {
373                 init_workers(XINT(number_of_workers), eq_worker_th);
374         }
375         return Qt;
376 }
377
378
379 DEFUN("add-workers", Fadd_workers, 1, 1, 0, /*
380 Add NUMBER-OF-WORKERS worker threads.
381 */
382       (number_of_workers))
383 {
384         CHECK_NATNUM(number_of_workers);
385         init_workers(XINT(number_of_workers), eq_worker_th);
386         return Qt;
387 }
388
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.
393 */
394       (number_of_workers))
395 {
396         Lisp_Object job = Qnil;
397         int i, noev = 0;        /* how many eat_yerself events to send? */
398         struct gcpro gcpro1;
399
400         if (NILP(number_of_workers)) {
401                 noev = dllist_get_size(workers)-1;
402         } else {
403                 CHECK_NATNUM(number_of_workers);
404                 noev = XINT(number_of_workers);
405         }
406
407         GCPRO1(job);
408         for (i = 0; i < noev; i++) {
409                 job = wrap_object(make_worker_job(&eat_yerself));
410                 eq_enqueue(delegate_eq, job);
411         }
412         eq_queue_trigger_all(delegate_eq);
413         UNGCPRO;
414         return job;
415 }
416
417 DEFUN("trigger-workers", Ftrigger_workers, 0, 0, 0, /*
418 Trigger all worker threads.
419 */
420       ())
421 {
422         eq_queue_trigger_all(delegate_eq);
423         return Qt;
424 }
425
426 DEFUN("running-workers", Frunning_workers, 0, 0, 0, /*
427 Return the number of currently running worker threads,
428 the main thread excluded.
429 */
430       ())
431 {
432         return make_int(dllist_get_size(workers)-1);
433 }
434
435 \f
436 void syms_of_worker_asyneq(void)
437 {
438         DEFSUBR(Finit_workers);
439         DEFSUBR(Fadd_workers);
440         DEFSUBR(Fremove_workers);
441         DEFSUBR(Ftrigger_workers);
442         DEFSUBR(Frunning_workers);
443 }
444
445 void reinit_vars_of_worker_asyneq(void)
446 {
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);
451 }
452
453 void vars_of_worker_asyneq(void)
454 {
455         Fprovide(intern("asyneq"));
456 }
457
458 /* workers.c ends here */