Another go at fixing 131
[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         return;
134 }
135
136 void
137 eq_delegate_work(event_queue_t eq)
138 {
139         int cur = eq_queue_size(eq);
140         while (cur--) { 
141                 eq_queue_trigger(eq);
142         }
143         return;
144 }
145
146 \f
147 static void
148 eq_worker_th_blksig(void)
149 {
150         EMACS_BLOCK_SIGNAL(SIGINT);     /* ANSI */
151         EMACS_BLOCK_SIGNAL(SIGILL);     /* ANSI */
152         EMACS_BLOCK_SIGNAL(SIGABRT);    /* ANSI */
153         EMACS_BLOCK_SIGNAL(SIGFPE);     /* ANSI */
154         EMACS_BLOCK_SIGNAL(SIGSEGV);    /* ANSI */
155         EMACS_BLOCK_SIGNAL(SIGTERM);    /* ANSI */
156         
157 #if defined SIGHUP
158         EMACS_BLOCK_SIGNAL(SIGHUP);     /* POSIX */
159 #endif
160 #if defined SIGQUIT
161         EMACS_BLOCK_SIGNAL(SIGQUIT);    /* POSIX */
162 #endif
163 #if defined SIGTRAP
164         EMACS_BLOCK_SIGNAL(SIGTRAP);    /* POSIX */
165 #endif
166 #if defined SIGUSR1
167         EMACS_BLOCK_SIGNAL(SIGUSR1);    /* POSIX */
168 #endif
169 #if defined SIGUSR2
170         EMACS_BLOCK_SIGNAL(SIGUSR2);    /* POSIX */
171 #endif
172 #if defined SIGPIPE
173         EMACS_BLOCK_SIGNAL(SIGPIPE);    /* POSIX */
174 #endif
175 #if defined SIGALRM
176         EMACS_BLOCK_SIGNAL(SIGALRM);    /* POSIX */
177 #endif
178 #if defined SIGCHLD
179         EMACS_BLOCK_SIGNAL(SIGCHLD);    /* POSIX */
180 #endif
181 #if defined SIGCONT
182         EMACS_BLOCK_SIGNAL(SIGCONT);    /* POSIX */
183 #endif
184 #if defined SIGSTOP
185         EMACS_BLOCK_SIGNAL(SIGSTOP);    /* POSIX */
186 #endif
187 #if defined SIGTSTP
188         EMACS_BLOCK_SIGNAL(SIGTSTP);    /* POSIX */
189 #endif
190 #if defined SIGTTIN
191         EMACS_BLOCK_SIGNAL(SIGTTIN);    /* POSIX */
192 #endif
193 #if defined SIGTTOU
194         EMACS_BLOCK_SIGNAL(SIGTTOU);    /* POSIX */
195 #endif
196
197 #if defined SIGBUS
198         EMACS_BLOCK_SIGNAL(SIGBUS);     /* XPG5 */
199 #endif
200 #if defined SIGPOLL
201         EMACS_BLOCK_SIGNAL(SIGPOLL);    /* XPG5 */
202 #endif
203 #if defined SIGPROF
204         EMACS_BLOCK_SIGNAL(SIGPROF);    /* XPG5 */
205 #endif
206 #if defined SIGSYS
207         EMACS_BLOCK_SIGNAL(SIGSYS);     /* XPG5 */
208 #endif
209 #if defined SIGURG
210         EMACS_BLOCK_SIGNAL(SIGURG);     /* XPG5 */
211 #endif
212 #if defined SIGXCPU
213         EMACS_BLOCK_SIGNAL(SIGXCPU);    /* XPG5 */
214 #endif
215 #if defined SIGXFSZ
216         EMACS_BLOCK_SIGNAL(SIGXFSZ);    /* XPG5 */
217 #endif
218 #if defined SIGVTALRM
219         EMACS_BLOCK_SIGNAL(SIGVTALRM);  /* XPG5 */
220 #endif
221
222 #if defined SIGIO
223         EMACS_BLOCK_SIGNAL(SIGIO);      /* BSD 4.2 */
224 #endif
225 #if defined SIGWINCH
226         EMACS_BLOCK_SIGNAL(SIGWINCH);   /* BSD 4.3 */
227 #endif
228
229 #if defined SIGEMT
230         EMACS_BLOCK_SIGNAL(SIGEMT);
231 #endif
232 #if defined SIGINFO
233         EMACS_BLOCK_SIGNAL(SIGINFO);
234 #endif
235 #if defined SIGHWE
236         EMACS_BLOCK_SIGNAL(SIGHWE);
237 #endif
238 #if defined SIGPRE
239         EMACS_BLOCK_SIGNAL(SIGPRE);
240 #endif
241 #if defined SIGUME
242         EMACS_BLOCK_SIGNAL(SIGUME);
243 #endif
244 #if defined SIGDLK
245         EMACS_BLOCK_SIGNAL(SIGDLK);
246 #endif
247 #if defined SIGCPULIM
248         EMACS_BLOCK_SIGNAL(SIGCPULIM);
249 #endif
250 #if defined SIGIOT
251         EMACS_BLOCK_SIGNAL(SIGIOT);
252 #endif
253 #if defined SIGLOST
254 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
255         EMACS_BLOCK_SIGNAL(SIGLOST);
256 # endif  /* BDWGC case */
257 #endif
258 #if defined SIGSTKFLT
259         EMACS_BLOCK_SIGNAL(SIGSTKFLT);
260 #endif
261 #if defined SIGUNUSED
262         EMACS_BLOCK_SIGNAL(SIGUNUSED);
263 #endif
264 #if defined SIGDANGER
265         EMACS_BLOCK_SIGNAL(SIGDANGER);  /* AIX */
266 #endif
267 #if defined SIGMSG
268         EMACS_BLOCK_SIGNAL(SIGMSG);
269 #endif
270 #if defined SIGSOUND
271         EMACS_BLOCK_SIGNAL(SIGSOUND);
272 #endif
273 #if defined SIGRETRACT
274         EMACS_BLOCK_SIGNAL(SIGRETRACT);
275 #endif
276 #if defined SIGGRANT
277         EMACS_BLOCK_SIGNAL(SIGGRANT);
278 #endif
279 #if defined SIGPWR
280 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
281         EMACS_BLOCK_SIGNAL(SIGPWR);
282 # endif  /* BDWGC case */
283 #endif
284 }
285
286 static void *
287 eq_worker_th(void *eqwptr)
288 {
289         Lisp_Object ljob = Qnil;
290         worker_job_t job = NULL;
291         eq_worker_t eqw = eqwptr;
292         work_handler_t hdl;
293         struct gcpro gcpro1;
294
295         eq_worker_th_blksig();
296
297         GCPRO1(ljob);
298 listen:
299         eq_queue_synch(delegate_eq);
300
301         EQUEUE_DEBUG_WORKER("dequeuing thread: 0x%lx\n",
302                             (long unsigned int)pthread_self());
303
304         /* fetch one event now */
305 refetch:
306         ljob = Qnil;
307         eq_dequeue_pro(&ljob, delegate_eq);
308         if (NILP(ljob)) {
309                 EQUEUE_DEBUG_WORKER("No event on the queue. "
310                                     "Who dared to wake me up?! >8(\n");
311                 goto listen;
312         }
313
314         eq_lock_meself(eqw);
315         job = XWORKER_JOB(ljob);
316         hdl = XWORKER_JOB_HANDLER(ljob);
317         EQUEUE_DEBUG_WORKER("escrowing event 0x%lx in worker 0x%lx.\n",
318                             (long unsigned int)job,
319                             (long unsigned int)eqw);
320
321         /* maybe it's a eat-yourself ticket? */
322         if (hdl == &eat_yerself) {
323                 /* awww ... we gotta exit :( */
324                 EQUEUE_DEBUG_WORKER(
325                         "Worker 0x%lx commits suicide...\n",
326                         (long unsigned int)eqw);
327                 eq_unlock_meself(eqw);
328                 eq_worker_eaten_myself(eqw);
329                 UNGCPRO;
330                 pthread_exit(NULL);
331                 return NULL;
332         }
333
334         /* help the job a bit with local resources */
335         EQUEUE_DEBUG_SCRATCH("inherit scratch buffer 0x%lx of size %ld\n",
336                              (long unsigned int)eq_worker_scratch(eqw),
337                              eq_worker_scratch_alloc_size(eqw));
338         worker_job_buffer(job) = eq_worker_scratch(eqw);
339         worker_job_buffer_alloc_size(job) = eq_worker_scratch_alloc_size(eqw);
340
341         /* generate a started event and update job state */
342         worker_job_state(job) = WORKER_JOB_RUNNING;
343         eq_worker_work_started(ljob);
344
345         /* ... otherwise handle the event */
346         work_handler(hdl)(job);
347
348         /* generate a `finished' event,
349          * sentinel code shall be injected in the routine
350          * called by eq_worker_handle_event() */
351         worker_job_state(job) = WORKER_JOB_FINISHED;
352         eq_worker_work_finished(ljob);
353
354         eq_unlock_meself(eqw);
355
356         EQUEUE_DEBUG_WORKER("enqueuing thread: 0x%lx\n",
357                             (long unsigned int)pthread_self());
358         goto refetch;
359         /* not reached */
360         return NULL;
361 }
362
363 DEFUN("init-workers", Finit_workers, 1, 1, 0, /*
364 Initialise NUMBER-OF-WORKERS worker threads.
365 If called repeatedly this function does NOT add more workers
366 use `add-workers' instead.
367 */
368       (number_of_workers))
369 {
370         CHECK_NATNUM(number_of_workers);
371         if (dllist_get_size(workers) <= 1) {
372                 init_workers(XINT(number_of_workers), eq_worker_th);
373         }
374         return Qt;
375 }
376
377
378 DEFUN("add-workers", Fadd_workers, 1, 1, 0, /*
379 Add NUMBER-OF-WORKERS worker threads.
380 */
381       (number_of_workers))
382 {
383         CHECK_NATNUM(number_of_workers);
384         init_workers(XINT(number_of_workers), eq_worker_th);
385         return Qt;
386 }
387
388 DEFUN("remove-workers", Fremove_workers, 0, 1, 0, /*
389 Stop NUMBER-OF-WORKERS worker threads.  By default stop all.
390 Depending on whether there are busy this operation may block the
391 main execution loop until all worker threads are non-busy.
392 */
393       (number_of_workers))
394 {
395         Lisp_Object job = Qnil;
396         int i, noev = 0;        /* how many eat_yerself events to send? */
397         struct gcpro gcpro1;
398
399         if (NILP(number_of_workers)) {
400                 noev = dllist_get_size(workers)-1;
401         } else {
402                 CHECK_NATNUM(number_of_workers);
403                 noev = XINT(number_of_workers);
404         }
405
406         GCPRO1(job);
407         for (i = 0; i < noev; i++) {
408                 job = wrap_object(make_worker_job(&eat_yerself));
409                 eq_enqueue(delegate_eq, job);
410         }
411         eq_queue_trigger_all(delegate_eq);
412         UNGCPRO;
413         return job;
414 }
415
416 DEFUN("trigger-workers", Ftrigger_workers, 0, 0, 0, /*
417 Trigger all worker threads.
418 */
419       ())
420 {
421         eq_queue_trigger_all(delegate_eq);
422         return Qt;
423 }
424
425 DEFUN("running-workers", Frunning_workers, 0, 0, 0, /*
426 Return the number of currently running worker threads,
427 the main thread excluded.
428 */
429       ())
430 {
431         return make_int(dllist_get_size(workers)-1);
432 }
433
434 \f
435 void syms_of_worker_asyneq(void)
436 {
437         DEFSUBR(Finit_workers);
438         DEFSUBR(Fadd_workers);
439         DEFSUBR(Fremove_workers);
440         DEFSUBR(Ftrigger_workers);
441         DEFSUBR(Frunning_workers);
442 }
443
444 void reinit_vars_of_worker_asyneq(void)
445 {
446         /* the delegate queue in case of multiple threads */
447         delegate_eq = make_event_queue();
448         XSETEVENT_QUEUE(Vdelegate_eq, delegate_eq);
449         staticpro_nodump(&Vdelegate_eq);
450 }
451
452 void vars_of_worker_asyneq(void)
453 {
454         Fprovide(intern("asyneq"));
455 }
456
457 /* workers.c ends here */