acl-plugin: improvement on 'show acl-plugin' CLI
[vpp.git] / src / vcl / vcl_event.c
index 65d2870..d8bd8c7 100644 (file)
@@ -79,9 +79,24 @@ vce_get_event_from_index(vce_event_thread_t *evt, u32 ev_idx)
   return ev;
 }
 
+vce_event_handler_reg_t *
+vce_get_event_handler (vce_event_thread_t *evt, vce_event_key_t *evk)
+{
+  vce_event_handler_reg_t *handler = 0;
+  uword *p;
+
+  clib_spinlock_lock (&evt->handlers_lockp);
+  p = hash_get (evt->handlers_index_by_event_key, evk->as_u64);
+  if (p)
+    handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
+  clib_spinlock_unlock (&evt->handlers_lockp);
+
+  return handler;
+}
+
 vce_event_handler_reg_t *
 vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk,
-                     vce_event_callback_t cb)
+                     vce_event_callback_t cb, void *cb_args)
 {
   vce_event_handler_reg_t *handler;
   vce_event_handler_reg_t *old_handler = 0;
@@ -120,6 +135,7 @@ vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk,
   handler->replaced_handler_idx = (p) ? p[0] : ~0;
   handler->ev_idx = ~0; //This will be set by the event thread if event happens
   handler->evk = evk->as_u64;
+  handler->handler_fn_args = cb_args;
 
   hash_set (evt->handlers_index_by_event_key, evk->as_u64, handler_index);
 
@@ -195,16 +211,19 @@ vce_event_thread_fn (void *arg)
   vce_event_handler_reg_t *handler;
   uword *p;
 
+  pthread_mutex_lock (&(evt->generator_lock));
+  clib_spinlock_lock (&(evt->events_lockp));
   evt->recycle_event = 1; // Used for recycling events with no handlers
-
+  clib_spinlock_unlock (&(evt->events_lockp));
 
   do
     {
-      pthread_mutex_lock (&(evt->generator_lock));
       while ( (clib_fifo_elts (evt->event_index_fifo) == 0) ||
              evt->recycle_event)
        {
+          clib_spinlock_lock (&(evt->events_lockp));
          evt->recycle_event = 0;
+          clib_spinlock_unlock (&(evt->events_lockp));
          pthread_cond_wait (&(evt->generator_cond),
                             &(evt->generator_lock));
        }
@@ -215,8 +234,6 @@ vce_event_thread_fn (void *arg)
       clib_fifo_sub1 (evt->event_index_fifo, ev_idx);
       ev = pool_elt_at_index (evt->vce_events, ev_idx);
 
-      clib_spinlock_unlock (&(evt->events_lockp));
-
       ASSERT(ev);
 
       clib_spinlock_lock (&evt->handlers_lockp);
@@ -229,18 +246,23 @@ vce_event_thread_fn (void *arg)
           * I don't know either, so lets try recycling the event */
          clib_fifo_add1 (evt->event_index_fifo, ev_idx);
          evt->recycle_event = 1;
+          clib_spinlock_unlock (&(evt->events_lockp));
          clib_spinlock_unlock (&evt->handlers_lockp);
-         goto unlock;
+          pthread_mutex_unlock (&(evt->generator_lock));
        }
-      handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
-      handler->ev_idx = ev_idx;
+      else
+        {
+          handler = pool_elt_at_index (evt->vce_event_handlers, p[0]);
+          handler->ev_idx = ev_idx;
 
-      clib_spinlock_unlock (&evt->handlers_lockp);
+          clib_spinlock_unlock (&(evt->events_lockp));
+          clib_spinlock_unlock (&evt->handlers_lockp);
+          pthread_mutex_unlock (&(evt->generator_lock));
 
-      (handler->handler_fn)(handler);
+          (handler->handler_fn)(handler);
+        }
 
-    unlock:
-      pthread_mutex_unlock (&(evt->generator_lock));
+      pthread_mutex_lock (&(evt->generator_lock));
     }
   while (1);
   return NULL;
@@ -260,4 +282,4 @@ vce_start_event_thread (vce_event_thread_t *evt, u8 max_events)
 
   return pthread_create (&(evt->thread), NULL /* attr */ ,
                         vce_event_thread_fn, evt);
-}
\ No newline at end of file
+}