X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fvcl_event.c;h=e72b864e9440d0e9ab19506f6b39514405372283;hb=7e607a73898b81c21a24038a2350fdda3b1c67ff;hp=d8bd8c74e369cfb124ab96fc63c2ad34e14df524;hpb=49c13c71939fafa2d7d113a6a6d2062df5825a89;p=vpp.git diff --git a/src/vcl/vcl_event.c b/src/vcl/vcl_event.c index d8bd8c74e36..e72b864e944 100644 --- a/src/vcl/vcl_event.c +++ b/src/vcl/vcl_event.c @@ -18,7 +18,9 @@ #include #include +#include #include + /** * @file * @brief VPP Communications Library (VCL) event handler. @@ -36,7 +38,7 @@ vce_generate_event (vce_event_thread_t *evt, u32 ev_idx) /* Check there is event data for this event */ - clib_spinlock_lock (&(evt->events_lockp)); + VCE_EVENTS_LOCK(); p = pool_elt_at_index (evt->vce_events, ev_idx); ASSERT(p); @@ -52,29 +54,28 @@ vce_generate_event (vce_event_thread_t *evt, u32 ev_idx) rv = VNET_API_ERROR_QUEUE_FULL; } - clib_spinlock_unlock (&(evt->events_lockp)); + VCE_EVENTS_UNLOCK (); pthread_mutex_unlock (&(evt->generator_lock)); return rv; } void -vce_clear_event (vce_event_thread_t *evt, vce_event_t *ev) +vce_clear_event (vce_event_thread_t *evt, u32 ev_idx) { - clib_spinlock_lock (&(evt->events_lockp)); - pool_put (evt->vce_events, ev); - clib_spinlock_unlock (&(evt->events_lockp)); + VCE_EVENTS_LOCK (); + pool_put_index (evt->vce_events, ev_idx); + VCE_EVENTS_UNLOCK (); } vce_event_t * vce_get_event_from_index(vce_event_thread_t *evt, u32 ev_idx) { vce_event_t *ev = 0; + /* Assumes caller has obtained the spinlock (evt->events_lockp) */ - clib_spinlock_lock (&(evt->events_lockp)); if ( ! pool_is_free_index (evt->vce_events, ev_idx)) ev = pool_elt_at_index (evt->vce_events, ev_idx); - clib_spinlock_unlock (&(evt->events_lockp)); return ev; } @@ -85,18 +86,18 @@ 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); + VCE_HANDLERS_LOCK (); 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); + VCE_HANDLERS_UNLOCK (); 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, void *cb_args) + vce_event_callback_t cb, void *cb_args) { vce_event_handler_reg_t *handler; vce_event_handler_reg_t *old_handler = 0; @@ -106,7 +107,7 @@ vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk, /* TODO - multiple handler support. For now we can replace * and re-instate, which is useful for event recycling */ - clib_spinlock_lock (&evt->handlers_lockp); + VCE_HANDLERS_LOCK (); p = hash_get (evt->handlers_index_by_event_key, evk->as_u64); if (p) @@ -115,25 +116,25 @@ vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk, /* If we are just re-registering, ignore and move on * else store the old handler_fn for unregister to re-instate */ if (old_handler->handler_fn == cb) - { + { - clib_spinlock_unlock (&evt->handlers_lockp); + VCE_HANDLERS_UNLOCK (); - /* Signal event thread that a handler exists in case any - * recycled events requiring this handler are pending */ - pthread_mutex_lock (&(evt->generator_lock)); - pthread_cond_signal (&(evt->generator_cond)); - pthread_mutex_unlock (&(evt->generator_lock)); - return old_handler; - } + /* Signal event thread that a handler exists in case any + * recycled events requiring this handler are pending */ + pthread_mutex_lock (&(evt->generator_lock)); + pthread_cond_signal (&(evt->generator_cond)); + pthread_mutex_unlock (&(evt->generator_lock)); + return old_handler; + } } pool_get (evt->vce_event_handlers, handler); handler_index = (u32) (handler - evt->vce_event_handlers); handler->handler_fn = cb; - handler->replaced_handler_idx = (p) ? p[0] : ~0; - handler->ev_idx = ~0; //This will be set by the event thread if event happens + handler->replaced_handler_idx = (u32) ((p) ? p[0] : ~0); + handler->ev_idx = (u32) ~0; //This will be set by the event thread if event happens handler->evk = evk->as_u64; handler->handler_fn_args = cb_args; @@ -142,7 +143,7 @@ vce_register_handler (vce_event_thread_t *evt, vce_event_key_t *evk, pthread_cond_init (&(handler->handler_cond), NULL); pthread_mutex_init (&(handler->handler_lock), NULL); - clib_spinlock_unlock (&evt->handlers_lockp); + VCE_HANDLERS_UNLOCK (); /* Signal event thread that a new handler exists in case any * recycled events requiring this handler are pending */ @@ -161,12 +162,12 @@ vce_unregister_handler (vce_event_thread_t *evt, u64 evk = handler->evk; u8 generate_signal = 0; - clib_spinlock_lock (&evt->handlers_lockp); + VCE_HANDLERS_LOCK (); p = hash_get (evt->handlers_index_by_event_key, evk); if (!p) { - clib_spinlock_unlock (&evt->handlers_lockp); + VCE_HANDLERS_UNLOCK (); return VNET_API_ERROR_NO_SUCH_ENTRY; } @@ -188,7 +189,7 @@ vce_unregister_handler (vce_event_thread_t *evt, pthread_cond_destroy (&(handler->handler_cond)); pool_put (evt->vce_event_handlers, handler); - clib_spinlock_unlock (&evt->handlers_lockp); + VCE_HANDLERS_UNLOCK (); if (generate_signal) { @@ -210,61 +211,64 @@ vce_event_thread_fn (void *arg) u32 ev_idx; vce_event_handler_reg_t *handler; uword *p; + u32 recycle_count = 0; 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 + while (1) { - 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)); - } + uword fifo_depth = clib_fifo_elts (evt->event_index_fifo); + while ((fifo_depth == 0) || (recycle_count == fifo_depth)) + { + recycle_count = 0; + pthread_cond_wait (&(evt->generator_cond), &(evt->generator_lock)); + fifo_depth = clib_fifo_elts (evt->event_index_fifo); + } /* Remove event */ - clib_spinlock_lock (&(evt->events_lockp)); - + VCE_EVENTS_LOCK (); clib_fifo_sub1 (evt->event_index_fifo, ev_idx); - ev = pool_elt_at_index (evt->vce_events, ev_idx); - + ev = vce_get_event_from_index (evt, ev_idx); ASSERT(ev); - - clib_spinlock_lock (&evt->handlers_lockp); + if (recycle_count && ev->recycle) + { + clib_fifo_add1 (evt->event_index_fifo, ev_idx); + VCE_EVENTS_UNLOCK (); + continue; + } + VCE_HANDLERS_LOCK (); p = hash_get (evt->handlers_index_by_event_key, ev->evk.as_u64); if (!p) - { - /* If an event falls in the woods, and there is no handler to hear it, - * does it make any sound? - * 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); + { + /* If an event falls in the woods, and there is no handler to hear it, + * does it make any sound? + * I don't know either, so lets biff the event */ + pool_put(evt->vce_events, ev); + VCE_EVENTS_UNLOCK (); + VCE_HANDLERS_UNLOCK (); pthread_mutex_unlock (&(evt->generator_lock)); - } + } else { + u32 evt_recycle = ev->recycle; handler = pool_elt_at_index (evt->vce_event_handlers, p[0]); handler->ev_idx = ev_idx; + ev->recycle = 0; - clib_spinlock_unlock (&(evt->events_lockp)); - clib_spinlock_unlock (&evt->handlers_lockp); + VCE_EVENTS_UNLOCK (); + VCE_HANDLERS_UNLOCK (); pthread_mutex_unlock (&(evt->generator_lock)); (handler->handler_fn)(handler); + + VCE_EVENTS_LOCK (); + ev = vce_get_event_from_index (evt, ev_idx); + recycle_count += (!evt_recycle && ev && ev->recycle) ? 1 : 0; + VCE_EVENTS_UNLOCK (); } pthread_mutex_lock (&(evt->generator_lock)); } - while (1); return NULL; } @@ -281,5 +285,6 @@ vce_start_event_thread (vce_event_thread_t *evt, u8 max_events) clib_spinlock_init (&(evt->handlers_lockp)); return pthread_create (&(evt->thread), NULL /* attr */ , - vce_event_thread_fn, evt); + vce_event_thread_fn, evt); } +