1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2025-02-02 15:07:36 -05:00

Update: Migrated event_t, and event_registration_t to be a igloo ro object

This commit is contained in:
Philipp Schafft 2022-09-26 12:02:14 +00:00 committed by Philipp Schafft
parent 2728261b99
commit ee72096d99
4 changed files with 63 additions and 78 deletions

View File

@ -34,6 +34,9 @@
#include "common/thread/thread.h" #include "common/thread/thread.h"
#include "icecasttypes.h"
#include <igloo/ro.h>
#include "cfgfile.h" #include "cfgfile.h"
#include "global.h" #include "global.h"
#include "logging.h" #include "logging.h"
@ -728,7 +731,7 @@ static void __append_old_style_exec_event(event_registration_t **list,
er = event_new_from_xml_node(exec); er = event_new_from_xml_node(exec);
event_registration_push(list, er); event_registration_push(list, er);
event_registration_release(er); igloo_ro_unref(&er);
xmlFreeNode(exec); xmlFreeNode(exec);
} }
@ -755,7 +758,7 @@ static void __append_old_style_url_event(event_registration_t **list,
er = event_new_from_xml_node(exec); er = event_new_from_xml_node(exec);
event_registration_push(list, er); event_registration_push(list, er);
event_registration_release(er); igloo_ro_unref(&er);
xmlFreeNode(exec); xmlFreeNode(exec);
} }
@ -832,7 +835,7 @@ static void config_clear_mount(mount_proxy *mount)
if (mount->cluster_password) xmlFree(mount->cluster_password); if (mount->cluster_password) xmlFree(mount->cluster_password);
if (mount->authstack) auth_stack_release(mount->authstack); if (mount->authstack) auth_stack_release(mount->authstack);
event_registration_release(mount->event); igloo_ro_unref(&(mount->event));
config_clear_http_header(mount->http_headers); config_clear_http_header(mount->http_headers);
free(mount); free(mount);
} }
@ -932,7 +935,7 @@ void config_clear(ice_config_t *c)
if (c->tls_context.key_file) xmlFree(c->tls_context.key_file); if (c->tls_context.key_file) xmlFree(c->tls_context.key_file);
if (c->tls_context.cipher_list) xmlFree(c->tls_context.cipher_list); if (c->tls_context.cipher_list) xmlFree(c->tls_context.cipher_list);
event_registration_release(c->event); igloo_ro_unref(&(c->event));
while ((c->listen_sock = config_clear_listener(c->listen_sock))); while ((c->listen_sock = config_clear_listener(c->listen_sock)));
@ -2935,7 +2938,7 @@ static void _parse_events(event_registration_t **events, xmlNodePtr node)
if (xmlStrcmp(node->name, XMLSTR("event")) == 0) { if (xmlStrcmp(node->name, XMLSTR("event")) == 0) {
event_registration_t *reg = event_new_from_xml_node(node); event_registration_t *reg = event_new_from_xml_node(node);
event_registration_push(events, reg); event_registration_push(events, reg);
event_registration_release(reg); igloo_ro_unref(&reg);
} }
node = node->next; node = node->next;
} }

View File

@ -16,6 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include "icecasttypes.h"
#include <igloo/error.h> #include <igloo/error.h>
#include <igloo/sp.h> #include <igloo/sp.h>
@ -38,6 +39,16 @@ static bool event_running = false;
static thread_type *event_thread = NULL; static thread_type *event_thread = NULL;
static cond_t cond; static cond_t cond;
static void event_registration_free(igloo_ro_t self);
igloo_RO_PUBLIC_TYPE(event_registration_t, igloo_ro_full_t,
igloo_RO_TYPEDECL_FREE(event_registration_free)
);
static void event_free(igloo_ro_t self);
igloo_RO_PUBLIC_TYPE(event_t, igloo_ro_full_t,
igloo_RO_TYPEDECL_FREE(event_free)
);
/* ignores errors */ /* ignores errors */
static void extra_add(event_t *event, event_extra_key_t key, const char *value) static void extra_add(event_t *event, event_extra_key_t key, const char *value)
{ {
@ -120,15 +131,9 @@ igloo_error_t event_to_string_renderer(const event_t *event, string_renderer_t *
} }
/* work with event_t* */ /* work with event_t* */
static void event_addref(event_t *event) { static void event_free(igloo_ro_t self)
if (!event) {
return; event_t *event = igloo_ro_to_type(self, event_t);
thread_mutex_lock(&event_lock);
event->refcount++;
thread_mutex_unlock(&event_lock);
}
static void event_release(event_t *event) {
size_t i; size_t i;
event_t *to_free; event_t *to_free;
@ -136,14 +141,8 @@ static void event_release(event_t *event) {
return; return;
thread_mutex_lock(&event_lock); thread_mutex_lock(&event_lock);
event->refcount--;
if (event->refcount) {
thread_mutex_unlock(&event_lock);
return;
}
for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++) for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++)
event_registration_release(event->reglist[i]); igloo_ro_unref(&(event->reglist[i]));
free(event->trigger); free(event->trigger);
@ -152,11 +151,10 @@ static void event_release(event_t *event) {
free(event->extra_entries); free(event->extra_entries);
to_free = event->next; to_free = event->next;
free(event);
thread_mutex_unlock(&event_lock); thread_mutex_unlock(&event_lock);
if (to_free) if (to_free)
event_release(to_free); igloo_ro_unref(&to_free);
} }
static void event_push(event_t **event, event_t *next) { static void event_push(event_t **event, event_t *next) {
@ -175,7 +173,7 @@ static void event_push(event_t **event, event_t *next) {
return; return;
} }
*event = next; igloo_ro_ref(next, event, event_t);
thread_cond_broadcast(&cond); thread_cond_broadcast(&cond);
} }
@ -188,7 +186,7 @@ static void event_push_reglist(event_t *event, event_registration_t *reglist) {
for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++) { for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++) {
if (!event->reglist[i]) { if (!event->reglist[i]) {
event_registration_addref(event->reglist[i] = reglist); igloo_ro_ref(reglist, &(event->reglist[i]), event_registration_t);
return; return;
} }
} }
@ -202,17 +200,14 @@ static event_t *event_new(const char *trigger) {
if (!trigger) if (!trigger)
return NULL; return NULL;
ret = calloc(1, sizeof(event_t)); if (igloo_ro_new_raw(&ret, event_t, igloo_instance) != igloo_ERROR_NONE)
if (!ret)
return NULL; return NULL;
ret->refcount = 1;
ret->trigger = strdup(trigger); ret->trigger = strdup(trigger);
ret->client_admin_command = ADMIN_COMMAND_ERROR; ret->client_admin_command = ADMIN_COMMAND_ERROR;
if (!ret->trigger) { if (!ret->trigger) {
event_release(ret); igloo_ro_unref(&ret);
return NULL; return NULL;
} }
@ -283,7 +278,7 @@ static void *event_run_thread (void *arg) {
for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++) for (i = 0; i < (sizeof(event->reglist)/sizeof(*event->reglist)); i++)
_try_registrations(event->reglist[i], event); _try_registrations(event->reglist[i], event);
event_release(event); igloo_ro_unref(&event);
} while (running); } while (running);
return NULL; return NULL;
@ -325,7 +320,7 @@ void event_shutdown(void) {
event_queue = NULL; event_queue = NULL;
thread_mutex_unlock(&event_lock); thread_mutex_unlock(&event_lock);
event_release(event_queue_to_free); igloo_ro_unref(&event_queue_to_free);
thread_cond_destroy(&cond); thread_cond_destroy(&cond);
/* destry mutex */ /* destry mutex */
@ -335,22 +330,20 @@ void event_shutdown(void) {
/* basic functions to work with event registrations */ /* basic functions to work with event registrations */
event_registration_t * event_new_from_xml_node(xmlNodePtr node) { event_registration_t * event_new_from_xml_node(xmlNodePtr node) {
event_registration_t *ret = calloc(1, sizeof(event_registration_t)); event_registration_t *ret;
config_options_t *options; config_options_t *options;
int rv; int rv;
if(!ret) if (igloo_ro_new_raw(&ret, event_registration_t, igloo_instance) != igloo_ERROR_NONE)
return NULL; return NULL;
ret->refcount = 1;
/* BEFORE RELEASE 2.5.0 DOCUMENT: Document <event type="..." trigger="..."> */ /* BEFORE RELEASE 2.5.0 DOCUMENT: Document <event type="..." trigger="..."> */
ret->type = (char*)xmlGetProp(node, XMLSTR("type")); ret->type = (char*)xmlGetProp(node, XMLSTR("type"));
ret->trigger = (char*)xmlGetProp(node, XMLSTR("trigger")); ret->trigger = (char*)xmlGetProp(node, XMLSTR("trigger"));
if (!ret->type || !ret->trigger) { if (!ret->type || !ret->trigger) {
ICECAST_LOG_ERROR("Event node isn't complete. Type or Trigger missing."); ICECAST_LOG_ERROR("Event node isn't complete. Type or Trigger missing.");
event_registration_release(ret); igloo_ro_unref(&ret);
return NULL; return NULL;
} }
@ -371,34 +364,21 @@ event_registration_t * event_new_from_xml_node(xmlNodePtr node) {
if (rv != 0) { if (rv != 0) {
ICECAST_LOG_ERROR("Can not set up event backend %s for trigger %s", ret->type, ret->trigger); ICECAST_LOG_ERROR("Can not set up event backend %s for trigger %s", ret->type, ret->trigger);
event_registration_release(ret); igloo_ro_unref(&ret);
return NULL; return NULL;
} }
return ret; return ret;
} }
void event_registration_addref(event_registration_t * er) { static void event_registration_free(igloo_ro_t self)
if(!er) {
return; event_registration_t *er = igloo_ro_to_type(self, event_registration_t);
thread_mutex_lock(&er->lock);
er->refcount++;
thread_mutex_unlock(&er->lock);
}
void event_registration_release(event_registration_t *er) {
if(!er)
return;
thread_mutex_lock(&er->lock); thread_mutex_lock(&er->lock);
er->refcount--;
if (er->refcount) {
thread_mutex_unlock(&er->lock);
return;
}
if (er->next) if (er->next)
event_registration_release(er->next); igloo_ro_unref(&(er->next));
xmlFree(er->type); xmlFree(er->type);
xmlFree(er->trigger); xmlFree(er->trigger);
@ -408,7 +388,6 @@ void event_registration_release(event_registration_t *er) {
thread_mutex_unlock(&er->lock); thread_mutex_unlock(&er->lock);
thread_mutex_destroy(&er->lock); thread_mutex_destroy(&er->lock);
free(er);
} }
void event_registration_push(event_registration_t **er, event_registration_t *tail) { void event_registration_push(event_registration_t **er, event_registration_t *tail) {
@ -418,33 +397,33 @@ void event_registration_push(event_registration_t **er, event_registration_t *ta
return; return;
if (!*er) { if (!*er) {
event_registration_addref(*er = tail); igloo_ro_ref(tail, er, event_registration_t);
return; return;
} }
event_registration_addref(cur = *er); igloo_ro_ref(*er, &cur, event_registration_t);
thread_mutex_lock(&cur->lock); thread_mutex_lock(&cur->lock);
while (1) { while (1) {
next = cur->next;
if (!cur->next) if (!cur->next)
break; break;
event_registration_addref(next); if (igloo_ro_ref(cur->next, &next, event_registration_t) != igloo_ERROR_NONE)
break;
thread_mutex_unlock(&cur->lock); thread_mutex_unlock(&cur->lock);
event_registration_release(cur); igloo_ro_unref(&cur);
cur = next; cur = next;
thread_mutex_lock(&cur->lock); thread_mutex_lock(&cur->lock);
} }
event_registration_addref(cur->next = tail); igloo_ro_ref(tail, &(cur->next), event_registration_t);
thread_mutex_unlock(&cur->lock); thread_mutex_unlock(&cur->lock);
event_registration_release(cur); igloo_ro_unref(&cur);
} }
/* event signaling */ /* event signaling */
void event_emit(event_t *event) { void event_emit(event_t *event) {
fastevent_emit(FASTEVENT_TYPE_SLOWEVENT, FASTEVENT_FLAG_NONE, FASTEVENT_DATATYPE_EVENT, event); fastevent_emit(FASTEVENT_TYPE_SLOWEVENT, FASTEVENT_FLAG_NONE, FASTEVENT_DATATYPE_EVENT, event);
event_addref(event);
thread_mutex_lock(&event_lock); thread_mutex_lock(&event_lock);
event_push(&event_queue, event); event_push(&event_queue, event);
thread_mutex_unlock(&event_lock); thread_mutex_unlock(&event_lock);
@ -505,7 +484,7 @@ void event_emit_va(const char *trigger, ...) {
#ifndef FASTEVENT_ENABLED #ifndef FASTEVENT_ENABLED
if (event->reglist[0] == NULL) { if (event->reglist[0] == NULL) {
/* we have no registrations, drop this event. */ /* we have no registrations, drop this event. */
event_release(event); igloo_ro_unref(&event);
return; return;
} }
#endif #endif
@ -548,5 +527,5 @@ void event_emit_va(const char *trigger, ...) {
} }
event_emit(event); event_emit(event);
event_release(event); igloo_ro_unref(&event);
} }

View File

@ -15,12 +15,13 @@
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/tree.h> #include <libxml/tree.h>
#include "icecasttypes.h"
#include <igloo/error.h> #include <igloo/error.h>
#include <igloo/typedef.h>
#include <igloo/ro.h>
#include "common/thread/thread.h" #include "common/thread/thread.h"
#include "icecasttypes.h"
/* implemented */ /* implemented */
#define EVENT_TYPE_LOG "log" #define EVENT_TYPE_LOG "log"
#define EVENT_TYPE_EXEC "exec" #define EVENT_TYPE_EXEC "exec"
@ -49,17 +50,14 @@ typedef struct {
const char *value; const char *value;
} event_extra_entry_t; } event_extra_entry_t;
struct event_registration_tag; igloo_RO_FORWARD_TYPE(event_t);
typedef struct event_registration_tag event_registration_t; igloo_RO_FORWARD_TYPE(event_registration_t);
struct event_tag;
typedef struct event_tag event_t;
/* this has no lock member to protect multiple accesses as every non-readonly access is within event.c /* this has no lock member to protect multiple accesses as every non-readonly access is within event.c
* and is protected by global lock or on the same thread anyway. * and is protected by global lock or on the same thread anyway.
*/ */
struct event_tag { struct event_tag {
/* refernece counter */ igloo_ro_full_t __parent;
size_t refcount;
/* reference to next element in chain */ /* reference to next element in chain */
event_t *next; event_t *next;
@ -84,8 +82,8 @@ struct event_tag {
}; };
struct event_registration_tag { struct event_registration_tag {
/* refernece counter */ igloo_ro_full_t __parent;
size_t refcount;
/* reference to next element in chain */ /* reference to next element in chain */
event_registration_t *next; event_registration_t *next;
@ -116,8 +114,6 @@ void event_shutdown(void);
/* basic functions to work with event registrations */ /* basic functions to work with event registrations */
event_registration_t * event_new_from_xml_node(xmlNodePtr node); event_registration_t * event_new_from_xml_node(xmlNodePtr node);
void event_registration_addref(event_registration_t *er);
void event_registration_release(event_registration_t *er);
void event_registration_push(event_registration_t **er, event_registration_t *tail); void event_registration_push(event_registration_t **er, event_registration_t *tail);
/* event signaling */ /* event signaling */

View File

@ -139,6 +139,11 @@ typedef struct mount_identifier_tag mount_identifier_t;
typedef struct string_renderer_tag string_renderer_t; typedef struct string_renderer_tag string_renderer_t;
/* ---[ event.[ch] ]--- */
typedef struct event_tag event_t;
typedef struct event_registration_tag event_registration_t;
/* ---[ refobject.[ch] ]--- */ /* ---[ refobject.[ch] ]--- */
typedef struct refobject_base_tag refobject_base_t; typedef struct refobject_base_tag refobject_base_t;
@ -162,6 +167,8 @@ typedef void * refobject_t;
/* --- [ For libigloo ]--- */ /* --- [ For libigloo ]--- */
#define igloo_RO_APPTYPES \ #define igloo_RO_APPTYPES \
igloo_RO_TYPE(string_renderer_t) \ igloo_RO_TYPE(string_renderer_t) \
igloo_RO_TYPE(event_t) \
igloo_RO_TYPE(event_registration_t) \
igloo_RO_TYPE(module_t) \ igloo_RO_TYPE(module_t) \
igloo_RO_TYPE(module_container_t) igloo_RO_TYPE(module_container_t)