]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add high and low priority event queues
authorAnthony Minessale <anthony.minessale@gmail.com>
Sun, 26 Feb 2006 00:12:17 +0000 (00:12 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Sun, 26 Feb 2006 00:12:17 +0000 (00:12 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@674 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_event.h
src/include/switch_types.h
src/include/switch_utils.h
src/switch_event.c
src/switch_utils.c

index 30cc9ba7be666c1b1b017bb4660744072bb1b7e0..6635413081779c05f414e951386f21daa5ac1182 100644 (file)
@@ -85,6 +85,8 @@ struct switch_event_subclass {
 struct switch_event {
        /*! the event id (descriptor) */
        switch_event_t event_id;
+       /*! the priority of the event */
+       switch_priority_t priority;
        /*! the owner of the event */
        char *owner;
        /*! the subclass of the event */
@@ -139,6 +141,14 @@ SWITCH_DECLARE(switch_status) switch_event_shutdown(void);
 */
 SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event, switch_event_t event_id, char *subclass_name);
 
+/*!
+  \brief Set the priority of an event
+  \param event the event to set the priority on
+  \param priority the event priority
+  \return SWITCH_STATUS_SUCCESS
+*/
+SWITCH_DECLARE(switch_status) switch_event_set_priority(switch_event *event, switch_priority_t priority);
+
 /*!
   \brief Retrieve a header value from an event
   \param event the event to read the header from
@@ -253,6 +263,13 @@ SWITCH_DECLARE(switch_status) switch_event_add_body(switch_event *event, char *f
 */
 #define switch_event_create(event, id) switch_event_create_subclass(event, id, SWITCH_EVENT_SUBCLASS_ANY)
 
+/*!
+  \brief Deliver an event to all of the registered event listeners
+  \param event the event to send (will be nulled)
+  \note normaly use switch_event_fire for delivering events (only use this when you wish to deliver the event blocking on your thread)
+*/
+SWITCH_DECLARE(void) switch_event_deliver(switch_event **event);
+
 /*!
   \brief Fire an event filling in most of the arguements with obvious values
   \param event the event to send (will be nulled on success)
index b89c4a88d8d8768dfee2eb273f1c0ac7a5bd2501..8808b4b82295cbaf8ef6289b96c2805a5dd6aa3e 100644 (file)
@@ -47,6 +47,22 @@ extern "C" {
 #define SWITCH_TRUE 1
 #define SWITCH_FALSE 0
 
+
+/*!
+  \enum switch_priority_t
+  \brief Priority Indication
+<pre>
+    SWITCH_PRIORITY_NORMAL  - Normal Priority
+    SWITCH_PRIORITY_LOW     - Low Priority
+    SWITCH_PRIORITY_HIGH    - High Priority
+</pre>
+ */
+typedef enum {
+       SWITCH_PRIORITY_NORMAL,
+       SWITCH_PRIORITY_LOW,
+       SWITCH_PRIORITY_HIGH,
+} switch_priority_t;
+
 /*!
   \enum switch_ivr_option_t
   \brief Possible options related to ivr functions
index 0ea0e199d2c9ccf9429ff7d51f5e9ec5a3e97f38..f1eb714076ec43f81860032584866216fd910289 100644 (file)
@@ -65,7 +65,12 @@ extern "C" {
 !strcasecmp(expr, "true") ||\
 atoi(expr))) ? SWITCH_TRUE : SWITCH_FALSE
 
-
+/*!
+  \brief Return a printable name of a switch_priority_t
+  \param priority the priority to get the name of
+  \return the printable form of the priority
+*/
+SWITCH_DECLARE(char *) switch_priority_name(switch_priority_t priority);
 
 /*!
   \brief Return the RFC2833 character based on an event id
index 817088e3090235436fe87f97ad32157a62927c5e..689e2b0fe6d6197e816edd4714bc4281770cfaf6 100644 (file)
@@ -38,22 +38,12 @@ static switch_memory_pool *RUNTIME_POOL = NULL;
 static switch_memory_pool *APOOL = NULL;
 static switch_memory_pool *BPOOL = NULL;
 static switch_memory_pool *THRUNTIME_POOL = NULL;
-static switch_queue_t *EVENT_QUEUE = NULL;
-//#define MALLOC_EVENTS
-
-#ifdef MALLOC_EVENTS
-static int POOL_COUNT = 0;
-#endif
-
+static switch_queue_t *EVENT_QUEUE[3] = {0,0,0};
 static int POOL_COUNT_MAX = 100;
 
 static switch_hash *CUSTOM_HASH = NULL;
 static int THREAD_RUNNING = 0;
 
-#ifdef MALLOC_EVENTS
-#define ALLOC(size) malloc(size)
-#define DUP(str) strdup(str)
-#else
 static void *locked_alloc(size_t len)
 {
        void *mem;
@@ -82,7 +72,7 @@ static void *locked_dup(char *str)
 
 #define ALLOC(size) locked_alloc(size)
 #define DUP(str) locked_dup(str)
-#endif
+
 
 /* make sure this is synced with the switch_event_t enum in switch_types.h
 also never put any new ones before EVENT_ALL
@@ -145,57 +135,61 @@ static int switch_events_match(switch_event *event, switch_event_node *node)
 
 static void *SWITCH_THREAD_FUNC switch_event_thread(switch_thread *thread, void *obj)
 {
-       switch_event_node *node;
        switch_event *out_event = NULL;
-       switch_event_t e;
+       switch_queue_t *queue = NULL;
+       switch_queue_t *queues[3] = {0,0,0};
        void *pop;
+       int i, len[3] = {0,0,0};
 
        assert(POOL_LOCK != NULL);
        assert(RUNTIME_POOL != NULL);
        THREAD_RUNNING = 1;
-       while (THREAD_RUNNING == 1 || switch_queue_size(EVENT_QUEUE)) {
-
-#ifdef MALLOC_EVENTS
-               switch_mutex_lock(POOL_LOCK);
-               /* <LOCKED> ----------------------------------------------- */
-               if (POOL_COUNT >= POOL_COUNT_MAX) {
-                       if (THRUNTIME_POOL == APOOL) {
-                               THRUNTIME_POOL = BPOOL;
-                       } else {
-                               THRUNTIME_POOL = APOOL;
+       
+       queues[0] = EVENT_QUEUE[SWITCH_PRIORITY_HIGH];
+       queues[1] = EVENT_QUEUE[SWITCH_PRIORITY_NORMAL];
+       queues[2] = EVENT_QUEUE[SWITCH_PRIORITY_LOW];
+
+       while (THREAD_RUNNING == 1 || 
+                  (len[1] = switch_queue_size(EVENT_QUEUE[SWITCH_PRIORITY_NORMAL])) ||
+                  (len[2] = switch_queue_size(EVENT_QUEUE[SWITCH_PRIORITY_LOW])) ||
+                  (len[0] = switch_queue_size(EVENT_QUEUE[SWITCH_PRIORITY_HIGH]))
+                  ) {
+
+               for(i = 0; i < 3; i++) {
+                       queue = queues[i];
+                       while (queue && switch_queue_trypop(queue, &pop) == SWITCH_STATUS_SUCCESS) {
+                               out_event = pop;
+                               switch_event_deliver(&out_event);
                        }
-                       switch_pool_clear(THRUNTIME_POOL);
-                       POOL_COUNT = 0;
                }
-               switch_mutex_unlock(POOL_LOCK);
-               /* </LOCKED> ----------------------------------------------- */
-#endif
-
-               while (switch_queue_trypop(EVENT_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
-                       out_event = pop;
+               switch_yield(1000);
+       }
+       THREAD_RUNNING = 0;
+       return NULL;
+}
 
-                       for (e = out_event->event_id;; e = SWITCH_EVENT_ALL) {
-                               for (node = EVENT_NODES[e]; node; node = node->next) {
-                                       if (switch_events_match(out_event, node)) {
-                                               out_event->bind_user_data = node->user_data;
-                                               node->callback(out_event);
-                                       }
-                               }
+SWITCH_DECLARE(void) switch_event_deliver(switch_event **event)
+{
+       switch_event_t e;
+       switch_event_node *node;
 
-                               if (e == SWITCH_EVENT_ALL) {
-                                       break;
-                               }
+       for (e = (*event)->event_id;; e = SWITCH_EVENT_ALL) {
+               for (node = EVENT_NODES[e]; node; node = node->next) {
+                       if (switch_events_match(*event, node)) {
+                               (*event)->bind_user_data = node->user_data;
+                               node->callback(*event);
                        }
-
-                       switch_event_destroy(&out_event);
                }
 
-               switch_yield(1000);
+               if (e == SWITCH_EVENT_ALL) {
+                       break;
+               }
        }
-       THREAD_RUNNING = 0;
-       return NULL;
+
+       switch_event_destroy(event);
 }
 
+
 SWITCH_DECLARE(switch_status) switch_event_running(void)
 {
        return THREAD_RUNNING ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
@@ -265,7 +259,9 @@ SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool)
        }
 
        THRUNTIME_POOL = APOOL;
-       switch_queue_create(&EVENT_QUEUE, POOL_COUNT_MAX + 10, THRUNTIME_POOL);
+       switch_queue_create(&EVENT_QUEUE[0], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
+       switch_queue_create(&EVENT_QUEUE[1], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
+       switch_queue_create(&EVENT_QUEUE[2], POOL_COUNT_MAX + 10, THRUNTIME_POOL);
 
        switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Eventing Engine.\n");
        switch_mutex_init(&BLOCK, SWITCH_MUTEX_NESTED, RUNTIME_POOL);
@@ -280,7 +276,8 @@ SWITCH_DECLARE(switch_status) switch_event_init(switch_memory_pool *pool)
 
 }
 
-SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event, switch_event_t event_id,
+SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event,
+                                                                                                                  switch_event_t event_id,
                                                                                                                   char *subclass_name)
 {
 
@@ -291,9 +288,6 @@ SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event,
        if ((*event = ALLOC(sizeof(switch_event))) == 0) {
                return SWITCH_STATUS_MEMERR;
        }
-#ifdef MALLOC_EVENTS
-       memset(*event, 0, sizeof(switch_event));
-#endif
 
        (*event)->event_id = event_id;
 
@@ -304,6 +298,13 @@ SWITCH_DECLARE(switch_status) switch_event_create_subclass(switch_event **event,
        return SWITCH_STATUS_SUCCESS;
 }
 
+SWITCH_DECLARE(switch_status) switch_event_set_priority(switch_event *event, switch_priority_t priority)
+{
+       event->priority = priority;
+       switch_event_add_header(event, SWITCH_STACK_TOP, "priority", switch_priority_name(priority));
+       return SWITCH_STATUS_SUCCESS;
+}
+
 SWITCH_DECLARE(char *) switch_event_get_header(switch_event *event, char *header_name)
 {
        switch_event_header *hp;
@@ -336,9 +337,6 @@ SWITCH_DECLARE(switch_status) switch_event_add_header(switch_event *event, switc
                if ((header = ALLOC(sizeof(*header))) == 0) {
                        return SWITCH_STATUS_MEMERR;
                }
-#ifdef MALLOC_EVENTS
-               memset(header, 0, sizeof(*header));
-#endif
 
                header->name = DUP(header_name);
                header->value = DUP(data);
@@ -380,19 +378,6 @@ SWITCH_DECLARE(switch_status) switch_event_add_body(switch_event *event, char *f
 
 SWITCH_DECLARE(void) switch_event_destroy(switch_event **event)
 {
-#ifdef MALLOC_EVENTS
-       switch_event_header *hp, *tofree;
-
-       for (hp = (*event)->headers; hp && hp->next;) {
-               tofree = hp;
-               hp = hp->next;
-               free(tofree->name);
-               free(tofree->value);
-               free(tofree);
-       }
-
-       free((*event));
-#endif
        *event = NULL;
 }
 
@@ -412,9 +397,6 @@ SWITCH_DECLARE(switch_status) switch_event_dup(switch_event **event, switch_even
                if ((header = ALLOC(sizeof(*header))) == 0) {
                        return SWITCH_STATUS_MEMERR;
                }
-#ifdef MALLOC_EVENTS
-               memset(header, 0, sizeof(*header));
-#endif
                header->name = DUP(hp->name);
                header->value = DUP(hp->value);
 
@@ -520,13 +502,9 @@ SWITCH_DECLARE(switch_status) switch_event_fire_detailed(char *file, char *func,
                (*event)->event_user_data = user_data;
        }
 
-       switch_queue_push(EVENT_QUEUE, *event);
+       switch_queue_push(EVENT_QUEUE[(*event)->priority], *event);
        *event = NULL;
 
-#ifdef MALLOC_EVENTS
-       POOL_COUNT++;
-#endif
-
        return SWITCH_STATUS_SUCCESS;
 }
 
index 9085309db9e06ef71fe68efd695381133a4daff6..b34665c4c2b907cbbf01ad58344f59f548013e08 100644 (file)
  * switch_utils.c -- Compatability and Helper Code
  *
  */
-#include <switch_utils.h>
+#include <switch.h>
+
+SWITCH_DECLARE(char *) switch_priority_name(switch_priority_t priority)
+{
+       switch(priority) { /*lol*/
+       case SWITCH_PRIORITY_NORMAL:
+               return "NORMAL";
+               break;
+       case SWITCH_PRIORITY_LOW:
+               return "LOW";
+               break;
+       case SWITCH_PRIORITY_HIGH:
+               return "HIGH";
+               break;
+       default:
+               return "INVALID";
+               break;
+       }
+}
 
 static char RFC2833_CHARS[] = "0123456789*#ABCDF";