mirror of https://code.videolan.org/videolan/vlc
lib: remove no lonegr used asynchronous event queue
This fixes excessive (and dumb) usage of thread local variables, and invalid use of thread cancellation.
This commit is contained in:
parent
e978f924cb
commit
062bd6c22a
|
@ -44,7 +44,6 @@ libvlc_la_SOURCES = \
|
|||
video.c \
|
||||
audio.c \
|
||||
event.c \
|
||||
event_async.c \
|
||||
media.c \
|
||||
media_player.c \
|
||||
media_list.c \
|
||||
|
|
68
lib/event.c
68
lib/event.c
|
@ -80,7 +80,6 @@ libvlc_event_manager_new( void * p_obj )
|
|||
}
|
||||
|
||||
p_em->p_obj = p_obj;
|
||||
p_em->async_event_queue = NULL;
|
||||
|
||||
vlc_array_init( &p_em->listeners_groups );
|
||||
vlc_mutex_init( &p_em->object_lock );
|
||||
|
@ -98,8 +97,6 @@ void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
|
|||
libvlc_event_listeners_group_t * p_lg;
|
||||
int i,j ;
|
||||
|
||||
libvlc_event_async_fini(p_em);
|
||||
|
||||
vlc_mutex_destroy( &p_em->event_sending_lock );
|
||||
vlc_mutex_destroy( &p_em->object_lock );
|
||||
|
||||
|
@ -204,30 +201,21 @@ void libvlc_event_send( libvlc_event_manager_t * p_em,
|
|||
listener_cached = array_listeners_cached;
|
||||
for( i = 0; i < i_cached_listeners; i++ )
|
||||
{
|
||||
if(listener_cached->is_asynchronous)
|
||||
{
|
||||
/* The listener wants not to block the emitter during event callback */
|
||||
libvlc_event_async_dispatch(p_em, listener_cached, p_event);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The listener wants to block the emitter during event callback */
|
||||
/* The listener wants to block the emitter during event callback */
|
||||
listener_cached->pf_callback( p_event, listener_cached->p_user_data );
|
||||
listener_cached++;
|
||||
|
||||
listener_cached->pf_callback( p_event, listener_cached->p_user_data );
|
||||
listener_cached++;
|
||||
|
||||
if( listeners_group->b_sublistener_removed )
|
||||
if( listeners_group->b_sublistener_removed )
|
||||
{
|
||||
/* If a callback was removed, this gets called */
|
||||
bool valid_listener;
|
||||
vlc_mutex_lock( &p_em->object_lock );
|
||||
valid_listener = group_contains_listener( listeners_group, listener_cached );
|
||||
vlc_mutex_unlock( &p_em->object_lock );
|
||||
if( !valid_listener )
|
||||
{
|
||||
/* If a callback was removed, this gets called */
|
||||
bool valid_listener;
|
||||
vlc_mutex_lock( &p_em->object_lock );
|
||||
valid_listener = group_contains_listener( listeners_group, listener_cached );
|
||||
vlc_mutex_unlock( &p_em->object_lock );
|
||||
if( !valid_listener )
|
||||
{
|
||||
listener_cached++;
|
||||
continue;
|
||||
}
|
||||
listener_cached++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -343,8 +331,7 @@ const char * libvlc_event_type_name( int event_type )
|
|||
static
|
||||
int event_attach( libvlc_event_manager_t * p_event_manager,
|
||||
libvlc_event_type_t event_type,
|
||||
libvlc_callback_t pf_callback, void *p_user_data,
|
||||
bool is_asynchronous )
|
||||
libvlc_callback_t pf_callback, void *p_user_data )
|
||||
{
|
||||
libvlc_event_listeners_group_t * listeners_group;
|
||||
libvlc_event_listener_t * listener;
|
||||
|
@ -357,7 +344,6 @@ int event_attach( libvlc_event_manager_t * p_event_manager,
|
|||
listener->event_type = event_type;
|
||||
listener->p_user_data = p_user_data;
|
||||
listener->pf_callback = pf_callback;
|
||||
listener->is_asynchronous = is_asynchronous;
|
||||
|
||||
vlc_mutex_lock( &p_event_manager->object_lock );
|
||||
for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
|
||||
|
@ -389,22 +375,7 @@ int libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
|
|||
libvlc_callback_t pf_callback,
|
||||
void *p_user_data )
|
||||
{
|
||||
return event_attach(p_event_manager, event_type, pf_callback, p_user_data,
|
||||
false /* synchronous */);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* libvlc_event_attach (public) :
|
||||
*
|
||||
* Add a callback for an event.
|
||||
**************************************************************************/
|
||||
void libvlc_event_attach_async( libvlc_event_manager_t * p_event_manager,
|
||||
libvlc_event_type_t event_type,
|
||||
libvlc_callback_t pf_callback,
|
||||
void *p_user_data )
|
||||
{
|
||||
event_attach(p_event_manager, event_type, pf_callback, p_user_data,
|
||||
true /* asynchronous */);
|
||||
return event_attach(p_event_manager, event_type, pf_callback, p_user_data);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
|
@ -453,14 +424,5 @@ 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->event_sending_lock );
|
||||
|
||||
/* Now make sure any pending async event won't get fired after that point */
|
||||
libvlc_event_listener_t listener_to_remove;
|
||||
listener_to_remove.event_type = event_type;
|
||||
listener_to_remove.pf_callback = pf_callback;
|
||||
listener_to_remove.p_user_data = p_user_data;
|
||||
listener_to_remove.is_asynchronous = true;
|
||||
|
||||
libvlc_event_async_ensure_listener_removal(p_event_manager, &listener_to_remove);
|
||||
|
||||
assert(found);
|
||||
}
|
||||
|
|
|
@ -1,287 +0,0 @@
|
|||
/*****************************************************************************
|
||||
* event_async.c: New libvlc event control API
|
||||
*****************************************************************************
|
||||
* Copyright (C) 2007 VLC authors and VideoLAN
|
||||
* $Id $
|
||||
*
|
||||
* Authors: Filippo Carone <filippo@carone.org>
|
||||
* Pierre d'Herbemont <pdherbemont # videolan.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <vlc/libvlc.h>
|
||||
|
||||
#include "libvlc_internal.h"
|
||||
#include "event_internal.h"
|
||||
|
||||
struct queue_elmt {
|
||||
libvlc_event_listener_t listener;
|
||||
libvlc_event_t event;
|
||||
struct queue_elmt * next;
|
||||
};
|
||||
|
||||
struct libvlc_event_async_queue {
|
||||
struct queue_elmt *first_elmt, *last_elmt;
|
||||
vlc_mutex_t lock;
|
||||
vlc_cond_t signal;
|
||||
vlc_thread_t thread;
|
||||
bool is_idle;
|
||||
vlc_cond_t signal_idle;
|
||||
vlc_threadvar_t is_asynch_dispatch_thread_var;
|
||||
};
|
||||
|
||||
/*
|
||||
* Utilities
|
||||
*/
|
||||
|
||||
static void* event_async_loop(void * arg);
|
||||
|
||||
static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
return p_em->async_event_queue;
|
||||
}
|
||||
|
||||
static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
return queue(p_em) != NULL;
|
||||
}
|
||||
|
||||
static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var)
|
||||
!= NULL;
|
||||
}
|
||||
|
||||
/* Lock must be held */
|
||||
static void push(libvlc_event_manager_t * p_em,
|
||||
libvlc_event_listener_t * listener, libvlc_event_t * event)
|
||||
{
|
||||
struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
|
||||
elmt->listener = *listener;
|
||||
elmt->event = *event;
|
||||
elmt->next = NULL;
|
||||
|
||||
/* Append to the end of the queue */
|
||||
if(!queue(p_em)->first_elmt)
|
||||
queue(p_em)->first_elmt = elmt;
|
||||
else
|
||||
queue(p_em)->last_elmt->next = elmt;
|
||||
queue(p_em)->last_elmt = elmt;
|
||||
}
|
||||
|
||||
static inline void queue_lock(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
vlc_mutex_lock(&queue(p_em)->lock);
|
||||
}
|
||||
|
||||
static inline void queue_unlock(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
vlc_mutex_unlock(&queue(p_em)->lock);
|
||||
}
|
||||
|
||||
/* Lock must be held */
|
||||
static bool pop(libvlc_event_manager_t * p_em,
|
||||
libvlc_event_listener_t * listener, libvlc_event_t * event)
|
||||
{
|
||||
if(!queue(p_em)->first_elmt)
|
||||
return false; /* No first_elmt */
|
||||
|
||||
struct queue_elmt * elmt = queue(p_em)->first_elmt;
|
||||
*listener = elmt->listener;
|
||||
*event = elmt->event;
|
||||
|
||||
queue(p_em)->first_elmt = elmt->next;
|
||||
if( !elmt->next ) queue(p_em)->last_elmt=NULL;
|
||||
|
||||
free(elmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Lock must be held */
|
||||
static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
|
||||
{
|
||||
struct queue_elmt * iter = queue(p_em)->first_elmt;
|
||||
struct queue_elmt * prev = NULL;
|
||||
while (iter) {
|
||||
if(listeners_are_equal(&iter->listener, listener))
|
||||
{
|
||||
struct queue_elmt * to_delete = iter;
|
||||
if(!prev)
|
||||
queue(p_em)->first_elmt = to_delete->next;
|
||||
else
|
||||
prev->next = to_delete->next;
|
||||
iter = to_delete->next;
|
||||
free(to_delete);
|
||||
}
|
||||
else {
|
||||
prev = iter;
|
||||
iter = iter->next;
|
||||
}
|
||||
}
|
||||
queue(p_em)->last_elmt=prev;
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* libvlc_event_async_fini (internal) :
|
||||
*
|
||||
* Destroy what might have been created by.
|
||||
**************************************************************************/
|
||||
void
|
||||
libvlc_event_async_fini(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
if(!is_queue_initialized(p_em)) return;
|
||||
|
||||
if(current_thread_is_asynch_thread(p_em))
|
||||
{
|
||||
fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
vlc_thread_t thread = queue(p_em)->thread;
|
||||
if(thread)
|
||||
{
|
||||
vlc_cancel(thread);
|
||||
vlc_join(thread, NULL);
|
||||
}
|
||||
|
||||
vlc_mutex_destroy(&queue(p_em)->lock);
|
||||
vlc_cond_destroy(&queue(p_em)->signal);
|
||||
vlc_cond_destroy(&queue(p_em)->signal_idle);
|
||||
vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
|
||||
|
||||
struct queue_elmt * iter = queue(p_em)->first_elmt;
|
||||
while (iter) {
|
||||
struct queue_elmt * elemt_to_delete = iter;
|
||||
iter = iter->next;
|
||||
free(elemt_to_delete);
|
||||
}
|
||||
|
||||
free(queue(p_em));
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* libvlc_event_async_init (private) :
|
||||
*
|
||||
* Destroy what might have been created by.
|
||||
**************************************************************************/
|
||||
static void
|
||||
libvlc_event_async_init(libvlc_event_manager_t * p_em)
|
||||
{
|
||||
p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
|
||||
|
||||
int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
|
||||
assert(!error);
|
||||
|
||||
vlc_mutex_init(&queue(p_em)->lock);
|
||||
vlc_cond_init(&queue(p_em)->signal);
|
||||
vlc_cond_init(&queue(p_em)->signal_idle);
|
||||
|
||||
error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
|
||||
if(error)
|
||||
{
|
||||
free(p_em->async_event_queue);
|
||||
p_em->async_event_queue = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* libvlc_event_async_ensure_listener_removal (internal) :
|
||||
*
|
||||
* Make sure no more message will be issued to the listener.
|
||||
**************************************************************************/
|
||||
void
|
||||
libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
|
||||
{
|
||||
if(!is_queue_initialized(p_em)) return;
|
||||
|
||||
queue_lock(p_em);
|
||||
pop_listener(p_em, listener);
|
||||
|
||||
// Wait for the asynch_loop to have processed all events.
|
||||
if(!current_thread_is_asynch_thread(p_em))
|
||||
{
|
||||
while(!queue(p_em)->is_idle)
|
||||
vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
|
||||
}
|
||||
queue_unlock(p_em);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* libvlc_event_async_dispatch (internal) :
|
||||
*
|
||||
* Send an event in an asynchronous way.
|
||||
**************************************************************************/
|
||||
void
|
||||
libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
|
||||
{
|
||||
// We do a lazy init here, to prevent constructing the thread when not needed.
|
||||
vlc_mutex_lock(&p_em->object_lock);
|
||||
if(!queue(p_em))
|
||||
libvlc_event_async_init(p_em);
|
||||
vlc_mutex_unlock(&p_em->object_lock);
|
||||
|
||||
queue_lock(p_em);
|
||||
push(p_em, listener, event);
|
||||
vlc_cond_signal(&queue(p_em)->signal);
|
||||
queue_unlock(p_em);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* event_async_loop (private) :
|
||||
*
|
||||
* Send queued events.
|
||||
**************************************************************************/
|
||||
static void * event_async_loop(void * arg)
|
||||
{
|
||||
libvlc_event_manager_t * p_em = arg;
|
||||
libvlc_event_listener_t listener;
|
||||
libvlc_event_t event;
|
||||
|
||||
vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
|
||||
|
||||
queue_lock(p_em);
|
||||
while (true) {
|
||||
int has_listener = pop(p_em, &listener, &event);
|
||||
|
||||
if (has_listener)
|
||||
{
|
||||
queue_unlock(p_em);
|
||||
listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
|
||||
queue_lock(p_em);
|
||||
}
|
||||
else
|
||||
{
|
||||
queue(p_em)->is_idle = true;
|
||||
|
||||
mutex_cleanup_push(&queue(p_em)->lock);
|
||||
vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
|
||||
vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
|
||||
vlc_cleanup_pop();
|
||||
|
||||
queue(p_em)->is_idle = false;
|
||||
}
|
||||
}
|
||||
queue_unlock(p_em);
|
||||
return NULL;
|
||||
}
|
|
@ -80,7 +80,6 @@ typedef struct libvlc_event_listener_t
|
|||
libvlc_event_type_t event_type;
|
||||
void * p_user_data;
|
||||
libvlc_callback_t pf_callback;
|
||||
bool is_asynchronous;
|
||||
} libvlc_event_listener_t;
|
||||
|
||||
typedef struct libvlc_event_manager_t
|
||||
|
@ -89,7 +88,6 @@ typedef struct libvlc_event_manager_t
|
|||
vlc_array_t listeners_groups;
|
||||
vlc_mutex_t object_lock;
|
||||
vlc_mutex_t event_sending_lock;
|
||||
struct libvlc_event_async_queue * async_event_queue;
|
||||
} libvlc_event_sender_t;
|
||||
|
||||
|
||||
|
@ -99,13 +97,7 @@ listeners_are_equal( libvlc_event_listener_t * listener1,
|
|||
{
|
||||
return listener1->event_type == listener2->event_type &&
|
||||
listener1->pf_callback == listener2->pf_callback &&
|
||||
listener1->p_user_data == listener2->p_user_data &&
|
||||
listener1->is_asynchronous == listener2->is_asynchronous;
|
||||
listener1->p_user_data == listener2->p_user_data;
|
||||
}
|
||||
|
||||
/* event_async.c */
|
||||
void libvlc_event_async_fini(libvlc_event_manager_t * p_em);
|
||||
void libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event);
|
||||
void libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -103,11 +103,6 @@ void libvlc_event_send(
|
|||
libvlc_event_manager_t * p_em,
|
||||
libvlc_event_t * p_event );
|
||||
|
||||
void libvlc_event_attach_async( libvlc_event_manager_t * p_event_manager,
|
||||
libvlc_event_type_t event_type,
|
||||
libvlc_callback_t pf_callback,
|
||||
void *p_user_data );
|
||||
|
||||
static inline libvlc_time_t from_mtime(mtime_t time)
|
||||
{
|
||||
return (time + 500ULL)/ 1000ULL;
|
||||
|
|
Loading…
Reference in New Issue