Minor fixes to comments.
[AROS.git] / rom / exec / allocsignal.c
blob1d622edf405daf30d16f1b83c9cbadec4c64bf57
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Allocate a signal
6 Lang: english
7 */
9 #include <exec/execbase.h>
10 #include <exec/tasks.h>
11 #include <aros/libcall.h>
12 #include <proto/exec.h>
14 #include "exec_util.h"
16 /*****************************************************************************
18 NAME */
20 AROS_LH1(BYTE, AllocSignal,
22 /* SYNOPSIS */
23 AROS_LHA(LONG, signalNum, D0),
25 /* LOCATION */
26 struct ExecBase *, SysBase, 55, Exec)
28 /* FUNCTION
29 Allocate a given signal out of the current task's pool of signals.
30 Every task has a set of signals to communicate with other tasks.
31 Half of them are reserved for the system and half of them are
32 free for general use. Some of the reserved signals (e.g.
33 SIGBREAKF_CTRL_C) have a defined behaviour and may be used by user
34 code, however.
36 You must not allocate or free signals from exception handlers.
38 INPUTS
39 signalNum - Number of the signal to allocate or -1 if any signal
40 will do.
42 RESULT
43 Number of the signal or -1 if the signal couldn't be allocated.
45 NOTES
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 FreeSignal(), Signal(), Wait()
54 INTERNALS
56 ******************************************************************************/
58 AROS_LIBFUNC_INIT
60 /* Cast signalNum to BYTE for AOS/68k compatibility. Apps may set up only D0.b */
61 return AllocTaskSignal(FindTask(NULL), (BYTE)signalNum, SysBase);
63 AROS_LIBFUNC_EXIT
64 } /* AllocSignal() */
66 LONG AllocTaskSignal(struct Task *ThisTask, LONG signalNum, struct ExecBase *SysBase)
68 ULONG mask;
69 ULONG mask1;
71 mask = ThisTask->tc_SigAlloc;
73 /* Will any signal do? */
74 if(signalNum < 0)
77 * To get the last nonzero bit in a number I use a&~a+1:
78 * Given a number that ends with a row of zeros xxxx1000
79 * I first toggle all bits in that number XXXX0111
80 * then add 1 to toggle all but the last 0 again XXXX1000
81 * and AND this with the original number 00001000
83 * And since ~a+1=-a I can use a&-a instead.
85 * And to get the last zero bit I finally use ~a&-~a.
87 mask1 = ~mask & - ~mask;
89 /* Is the bit already allocated? */
90 if(mask1 == 0)
91 return -1;
93 /* And get the bit number */
94 signalNum=(mask1&0xffff0000?16:0)+(mask1&0xff00ff00?8:0)+
95 (mask1&0xf0f0f0f0? 4:0)+(mask1&0xcccccccc?2:0)+
96 (mask1&0xaaaaaaaa? 1:0);
98 else
100 mask1 = 1L << signalNum;
102 /* If signal bit is already allocated, return. */
103 if(ThisTask->tc_SigAlloc & mask1)
104 return -1;
108 * I shouldn't need to disable around changing the signal masks
109 * because the only thing allowed to change the mask of allocated,
110 * excepting and waiting signals is the task itself. On the other
111 * hand, I need to use Disable around the received signals because it
112 * can be modified by interrupts, and I cannot rely upon the below
113 * being atomic.
116 ThisTask->tc_SigAlloc |= mask1;
117 ThisTask->tc_SigExcept &= ~mask1;
118 ThisTask->tc_SigWait &= ~mask1;
120 Disable();
121 ThisTask->tc_SigRecvd &= ~mask1;
122 Enable();
124 return signalNum;