2013-12-25 21:40:33 -08:00
|
|
|
/*
|
2014-03-01 04:54:55 -08:00
|
|
|
* Copyright (c) 2013-2014 Hugh Bailey <obs.jim@gmail.com>
|
2013-12-25 21:40:33 -08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
#include "../util/darray.h"
|
|
|
|
#include "../util/threading.h"
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
#include "decl.h"
|
2013-12-26 03:26:17 -08:00
|
|
|
#include "signal.h"
|
|
|
|
|
|
|
|
struct signal_callback {
|
2013-12-31 18:33:16 -08:00
|
|
|
signal_callback_t callback;
|
2013-12-26 03:26:17 -08:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct signal_info {
|
2014-03-01 04:54:55 -08:00
|
|
|
struct decl_info func;
|
2013-12-26 03:26:17 -08:00
|
|
|
DARRAY(struct signal_callback) callbacks;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
|
|
|
struct signal_info *next;
|
|
|
|
};
|
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
static inline struct signal_info *signal_info_create(struct decl_info *info)
|
2013-12-26 03:26:17 -08:00
|
|
|
{
|
|
|
|
struct signal_info *si = bmalloc(sizeof(struct signal_info));
|
2014-03-01 04:54:55 -08:00
|
|
|
|
|
|
|
si->func = *info;
|
2013-12-26 03:26:17 -08:00
|
|
|
si->next = NULL;
|
|
|
|
da_init(si->callbacks);
|
|
|
|
|
2013-12-27 04:08:58 -08:00
|
|
|
if (pthread_mutex_init(&si->mutex, NULL) != 0) {
|
2014-03-01 04:54:55 -08:00
|
|
|
blog(LOG_ERROR, "Could not create signal");
|
|
|
|
|
|
|
|
decl_info_free(&si->func);
|
2013-12-26 03:26:17 -08:00
|
|
|
bfree(si);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void signal_info_destroy(struct signal_info *si)
|
|
|
|
{
|
|
|
|
if (si) {
|
|
|
|
pthread_mutex_destroy(&si->mutex);
|
2014-03-01 04:54:55 -08:00
|
|
|
decl_info_free(&si->func);
|
2013-12-26 03:26:17 -08:00
|
|
|
da_free(si->callbacks);
|
|
|
|
bfree(si);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t signal_get_callback_idx(struct signal_info *si,
|
2013-12-31 18:33:16 -08:00
|
|
|
signal_callback_t callback, void *data)
|
2013-12-26 03:26:17 -08:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < si->callbacks.num; i++) {
|
|
|
|
struct signal_callback *sc = si->callbacks.array+i;
|
|
|
|
|
|
|
|
if (sc->callback == callback && sc->data == data)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DARRAY_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct signal_handler {
|
|
|
|
struct signal_info *first;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct signal_info *getsignal(signal_handler_t handler,
|
|
|
|
const char *name, struct signal_info **p_last)
|
|
|
|
{
|
|
|
|
struct signal_info *signal, *last= NULL;
|
|
|
|
|
|
|
|
signal = handler->first;
|
|
|
|
while (signal != NULL) {
|
2014-03-01 04:54:55 -08:00
|
|
|
if (strcmp(signal->func.name, name) == 0)
|
2013-12-26 03:26:17 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
last = signal;
|
|
|
|
signal = signal->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_last)
|
|
|
|
*p_last = last;
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
signal_handler_t signal_handler_create(void)
|
|
|
|
{
|
|
|
|
struct signal_handler *handler = bmalloc(sizeof(struct signal_handler));
|
|
|
|
handler->first = NULL;
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&handler->mutex, NULL) != 0) {
|
2013-12-26 03:28:09 -08:00
|
|
|
blog(LOG_ERROR, "Couldn't create signal handler!");
|
2013-12-26 03:26:17 -08:00
|
|
|
bfree(handler);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void signal_handler_destroy(signal_handler_t handler)
|
|
|
|
{
|
|
|
|
if (handler) {
|
|
|
|
struct signal_info *sig = handler->first;
|
|
|
|
while (sig != NULL) {
|
|
|
|
struct signal_info *next = sig->next;
|
|
|
|
signal_info_destroy(sig);
|
|
|
|
sig = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&handler->mutex);
|
|
|
|
bfree(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
bool signal_handler_add(signal_handler_t handler, const char *signal_decl)
|
|
|
|
{
|
|
|
|
struct decl_info func = {0};
|
|
|
|
struct signal_info *sig, *last;
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
if (!parse_decl_string(&func, signal_decl)) {
|
|
|
|
blog(LOG_ERROR, "Signal declaration invalid: %s", signal_decl);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&handler->mutex);
|
|
|
|
|
|
|
|
sig = getsignal(handler, func.name, &last);
|
|
|
|
if (sig) {
|
|
|
|
blog(LOG_WARNING, "Signal declaration '%s' exists", func.name);
|
|
|
|
decl_info_free(&func);
|
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
sig = signal_info_create(&func);
|
|
|
|
if (!last)
|
|
|
|
handler->first = sig;
|
|
|
|
else
|
|
|
|
last->next = sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&handler->mutex);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
void signal_handler_connect(signal_handler_t handler, const char *signal,
|
2013-12-31 18:33:16 -08:00
|
|
|
signal_callback_t callback, void *data)
|
2013-12-26 03:26:17 -08:00
|
|
|
{
|
|
|
|
struct signal_info *sig, *last;
|
|
|
|
struct signal_callback cb_data = {callback, data};
|
|
|
|
size_t idx;
|
|
|
|
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!handler)
|
|
|
|
return;
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
pthread_mutex_lock(&handler->mutex);
|
|
|
|
sig = getsignal(handler, signal, &last);
|
|
|
|
pthread_mutex_unlock(&handler->mutex);
|
|
|
|
|
2014-06-03 04:41:12 -07:00
|
|
|
if (!sig) {
|
|
|
|
blog(LOG_WARNING, "signal_handler_connect: "
|
|
|
|
"signal '%s' not found", signal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
/* -------------- */
|
|
|
|
|
|
|
|
pthread_mutex_lock(&sig->mutex);
|
|
|
|
|
|
|
|
idx = signal_get_callback_idx(sig, callback, data);
|
|
|
|
if (idx == DARRAY_INVALID)
|
|
|
|
da_push_back(sig->callbacks, &cb_data);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&sig->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct signal_info *getsignal_locked(signal_handler_t handler,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct signal_info *sig;
|
|
|
|
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!handler)
|
|
|
|
return NULL;
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
pthread_mutex_lock(&handler->mutex);
|
|
|
|
sig = getsignal(handler, name, NULL);
|
|
|
|
pthread_mutex_unlock(&handler->mutex);
|
|
|
|
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void signal_handler_disconnect(signal_handler_t handler, const char *signal,
|
2013-12-31 18:33:16 -08:00
|
|
|
signal_callback_t callback, void *data)
|
2013-12-26 03:26:17 -08:00
|
|
|
{
|
|
|
|
struct signal_info *sig = getsignal_locked(handler, signal);
|
|
|
|
size_t idx;
|
|
|
|
|
2013-12-27 04:08:58 -08:00
|
|
|
if (!sig)
|
|
|
|
return;
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
pthread_mutex_lock(&sig->mutex);
|
|
|
|
|
|
|
|
idx = signal_get_callback_idx(sig, callback, data);
|
2013-12-26 03:33:16 -08:00
|
|
|
if (idx != DARRAY_INVALID)
|
2013-12-26 03:26:17 -08:00
|
|
|
da_erase(sig->callbacks, idx);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&sig->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void signal_handler_signal(signal_handler_t handler, const char *signal,
|
|
|
|
calldata_t params)
|
|
|
|
{
|
|
|
|
struct signal_info *sig = getsignal_locked(handler, signal);
|
|
|
|
|
2013-12-27 04:08:58 -08:00
|
|
|
if (!sig)
|
|
|
|
return;
|
|
|
|
|
2013-12-26 03:26:17 -08:00
|
|
|
pthread_mutex_lock(&sig->mutex);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sig->callbacks.num; i++) {
|
|
|
|
struct signal_callback *cb = sig->callbacks.array+i;
|
2013-12-28 04:33:16 -08:00
|
|
|
cb->callback(cb->data, params);
|
2013-12-26 03:26:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&sig->mutex);
|
|
|
|
}
|