Commit 0a222373 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

lib: merge both event manager locks

The distinction was rather vain, as the inner lock was almost never
held without the outer lock. The only case was adding an event callback
(but _not_ removing one). On the other hand, the distinction required
extra heap manipulations and memory copying for each event.
parent b80a4563
...@@ -84,8 +84,7 @@ typedef struct libvlc_event_manager_t ...@@ -84,8 +84,7 @@ typedef struct libvlc_event_manager_t
{ {
void * p_obj; void * p_obj;
vlc_array_t listeners_groups; vlc_array_t listeners_groups;
vlc_mutex_t object_lock; vlc_mutex_t lock;
vlc_mutex_t event_sending_lock;
} libvlc_event_sender_t; } libvlc_event_sender_t;
...@@ -147,8 +146,7 @@ libvlc_event_manager_new( void * p_obj ) ...@@ -147,8 +146,7 @@ libvlc_event_manager_new( void * p_obj )
p_em->p_obj = p_obj; p_em->p_obj = p_obj;
vlc_array_init( &p_em->listeners_groups ); vlc_array_init( &p_em->listeners_groups );
vlc_mutex_init( &p_em->object_lock ); vlc_mutex_init_recursive(&p_em->lock);
vlc_mutex_init_recursive( &p_em->event_sending_lock );
return p_em; return p_em;
} }
...@@ -162,8 +160,7 @@ void libvlc_event_manager_release( libvlc_event_manager_t * p_em ) ...@@ -162,8 +160,7 @@ void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
libvlc_event_listeners_group_t * p_lg; libvlc_event_listeners_group_t * p_lg;
int i,j ; int i,j ;
vlc_mutex_destroy( &p_em->event_sending_lock ); vlc_mutex_destroy(&p_em->lock);
vlc_mutex_destroy( &p_em->object_lock );
for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++) for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
{ {
...@@ -193,9 +190,9 @@ void libvlc_event_manager_register_event_type( ...@@ -193,9 +190,9 @@ void libvlc_event_manager_register_event_type(
listeners_group->event_type = event_type; listeners_group->event_type = event_type;
vlc_array_init( &listeners_group->listeners ); vlc_array_init( &listeners_group->listeners );
vlc_mutex_lock( &p_em->object_lock ); vlc_mutex_lock(&p_em->lock);
vlc_array_append( &p_em->listeners_groups, listeners_group ); vlc_array_append( &p_em->listeners_groups, listeners_group );
vlc_mutex_unlock( &p_em->object_lock ); vlc_mutex_unlock(&p_em->lock);
} }
/************************************************************************** /**************************************************************************
...@@ -215,8 +212,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -215,8 +212,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
/* Fill event with the sending object now */ /* Fill event with the sending object now */
p_event->p_obj = p_em->p_obj; p_event->p_obj = p_em->p_obj;
vlc_mutex_lock( &p_em->event_sending_lock ); vlc_mutex_lock(&p_em->lock);
vlc_mutex_lock( &p_em->object_lock );
for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++) for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
{ {
listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i); listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
...@@ -231,8 +227,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -231,8 +227,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners)); array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
if( !array_listeners_cached ) if( !array_listeners_cached )
{ {
vlc_mutex_unlock( &p_em->object_lock ); vlc_mutex_unlock(&p_em->lock);
vlc_mutex_unlock( &p_em->event_sending_lock );
fprintf(stderr, "Can't alloc memory in libvlc_event_send" ); fprintf(stderr, "Can't alloc memory in libvlc_event_send" );
return; return;
} }
...@@ -250,9 +245,8 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -250,9 +245,8 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
if( !listeners_group ) if( !listeners_group )
{ {
vlc_mutex_unlock(&p_em->lock);
free( array_listeners_cached ); free( array_listeners_cached );
vlc_mutex_unlock( &p_em->object_lock );
vlc_mutex_unlock( &p_em->event_sending_lock );
return; return;
} }
...@@ -261,8 +255,6 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -261,8 +255,6 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
* allowing to remove an event listener from within a callback */ * allowing to remove an event listener from within a callback */
listeners_group->b_sublistener_removed = false; listeners_group->b_sublistener_removed = false;
vlc_mutex_unlock( &p_em->object_lock );
listener_cached = array_listeners_cached; listener_cached = array_listeners_cached;
for( i = 0; i < i_cached_listeners; i++ ) for( i = 0; i < i_cached_listeners; i++ )
{ {
...@@ -274,9 +266,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -274,9 +266,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
{ {
/* If a callback was removed, this gets called */ /* If a callback was removed, this gets called */
bool valid_listener; bool valid_listener;
vlc_mutex_lock( &p_em->object_lock );
valid_listener = group_contains_listener( listeners_group, listener_cached ); valid_listener = group_contains_listener( listeners_group, listener_cached );
vlc_mutex_unlock( &p_em->object_lock );
if( !valid_listener ) if( !valid_listener )
{ {
listener_cached++; listener_cached++;
...@@ -284,7 +274,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em, ...@@ -284,7 +274,7 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
} }
} }
} }
vlc_mutex_unlock( &p_em->event_sending_lock ); vlc_mutex_unlock(&p_em->lock);
free( array_listeners_cached ); free( array_listeners_cached );
} }
...@@ -410,18 +400,18 @@ int event_attach( libvlc_event_manager_t * p_event_manager, ...@@ -410,18 +400,18 @@ int event_attach( libvlc_event_manager_t * p_event_manager,
listener->p_user_data = p_user_data; listener->p_user_data = p_user_data;
listener->pf_callback = pf_callback; listener->pf_callback = pf_callback;
vlc_mutex_lock( &p_event_manager->object_lock ); vlc_mutex_lock(&p_event_manager->lock);
for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ ) for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
{ {
listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i); listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
if( listeners_group->event_type == listener->event_type ) if( listeners_group->event_type == listener->event_type )
{ {
vlc_array_append( &listeners_group->listeners, listener ); vlc_array_append( &listeners_group->listeners, listener );
vlc_mutex_unlock( &p_event_manager->object_lock ); vlc_mutex_unlock(&p_event_manager->lock);
return 0; return 0;
} }
} }
vlc_mutex_unlock( &p_event_manager->object_lock ); vlc_mutex_unlock(&p_event_manager->lock);
free(listener); free(listener);
fprintf( stderr, "This object event manager doesn't know about '%s' events", fprintf( stderr, "This object event manager doesn't know about '%s' events",
...@@ -458,8 +448,7 @@ void libvlc_event_detach( libvlc_event_manager_t *p_event_manager, ...@@ -458,8 +448,7 @@ void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
int i, j; int i, j;
bool found = false; bool found = false;
vlc_mutex_lock( &p_event_manager->event_sending_lock ); vlc_mutex_lock(&p_event_manager->lock);
vlc_mutex_lock( &p_event_manager->object_lock );
for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++) for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
{ {
listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i); listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
...@@ -486,8 +475,7 @@ void libvlc_event_detach( libvlc_event_manager_t *p_event_manager, ...@@ -486,8 +475,7 @@ void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
} }
} }
} }
vlc_mutex_unlock( &p_event_manager->object_lock ); vlc_mutex_unlock(&p_event_manager->lock);
vlc_mutex_unlock( &p_event_manager->event_sending_lock );
assert(found); assert(found);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment