2 * common/signal.h - Signal handling functions
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef AWESOME_COMMON_SIGNAL
23 #define AWESOME_COMMON_SIGNAL
25 #include "common/lualib.h"
26 #include "common/array.h"
28 DO_ARRAY(const void *, cptr
, DO_NOTHING
)
33 cptr_array_t sigfuncs
;
37 signal_cmp(const void *a
, const void *b
)
39 const signal_t
*x
= a
, *y
= b
;
40 return x
->id
> y
->id
? 1 : (x
->id
< y
->id
? -1 : 0);
44 signal_wipe(signal_t
*sig
)
46 cptr_array_wipe(&sig
->sigfuncs
);
49 DO_BARRAY(signal_t
, signal
, signal_wipe
, signal_cmp
)
51 static inline signal_t
*
52 signal_array_getbyid(signal_array_t
*arr
, unsigned long id
)
54 signal_t sig
= { .id
= id
};
55 return signal_array_lookup(arr
, &sig
);
58 /** Add a signal inside a signal array.
59 * You are in charge of reference counting.
60 * \param arr The signal array.
61 * \param name The signal name.
62 * \param ref The reference to add.
65 signal_add(signal_array_t
*arr
, const char *name
, const void *ref
)
67 unsigned long tok
= a_strhash((const unsigned char *) name
);
68 signal_t
*sigfound
= signal_array_getbyid(arr
, tok
);
70 cptr_array_append(&sigfound
->sigfuncs
, ref
);
73 signal_t sig
= { .id
= tok
};
74 cptr_array_append(&sig
.sigfuncs
, ref
);
75 signal_array_insert(arr
, sig
);
79 /** Remove a signal inside a signal array.
80 * You are in charge of reference counting.
81 * \param arr The signal array.
82 * \param name The signal name.
83 * \param ref The reference to remove.
86 signal_remove(signal_array_t
*arr
, const char *name
, const void *ref
)
88 signal_t
*sigfound
= signal_array_getbyid(arr
,
89 a_strhash((const unsigned char *) name
));
91 foreach(func
, sigfound
->sigfuncs
)
94 cptr_array_remove(&sigfound
->sigfuncs
, func
);
101 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80