[vlc-commits] lua/sd: use queue
Rémi Denis-Courmont
git at videolan.org
Mon Apr 13 13:05:24 CEST 2020
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Apr 11 23:45:54 2020 +0300| [65b83f162abd5ea981ace52fe1bcb34f16902db8] | committer: Rémi Denis-Courmont
lua/sd: use queue
This adds missing memory allocation error handling and removes one use of
CV cancellation.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=65b83f162abd5ea981ace52fe1bcb34f16902db8
---
modules/lua/services_discovery.c | 76 +++++++++++++++-------------------------
1 file changed, 29 insertions(+), 47 deletions(-)
diff --git a/modules/lua/services_discovery.c b/modules/lua/services_discovery.c
index 3984bf3de8..2dc3621c3b 100644
--- a/modules/lua/services_discovery.c
+++ b/modules/lua/services_discovery.c
@@ -28,6 +28,7 @@
#include <assert.h>
#include <vlc_common.h>
#include <vlc_services_discovery.h>
+#include <vlc_queue.h>
#include "vlc.h"
#include "libs.h"
@@ -141,14 +142,17 @@ typedef struct
char *psz_filename;
vlc_thread_t thread;
- vlc_mutex_t lock;
- vlc_cond_t cond;
-
- char **ppsz_query;
- int i_query;
+ vlc_queue_t queue;
+ bool dead;
} services_discovery_sys_t;
static const luaL_Reg p_reg[] = { { NULL, NULL } };
+struct sd_query
+{
+ struct sd_query *next;
+ char query[];
+};
+
/*****************************************************************************
* Open: initialize and create stuff
*****************************************************************************/
@@ -232,13 +236,11 @@ int Open_LuaSD( vlc_object_t *p_this )
p_sd->description = p_sd->psz_name;
p_sys->L = L;
- vlc_mutex_init( &p_sys->lock );
- vlc_cond_init( &p_sys->cond );
- TAB_INIT( p_sys->i_query, p_sys->ppsz_query );
+ p_sys->dead = false;
+ vlc_queue_Init( &p_sys->queue, offsetof (struct sd_query, next) );
if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
{
- TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
goto error;
}
return VLC_SUCCESS;
@@ -259,13 +261,8 @@ void Close_LuaSD( vlc_object_t *p_this )
services_discovery_t *p_sd = ( services_discovery_t * )p_this;
services_discovery_sys_t *p_sys = p_sd->p_sys;
- vlc_cancel( p_sys->thread );
+ vlc_queue_Kill( &p_sys->queue, &p_sys->dead );
vlc_join( p_sys->thread, NULL );
-
- for( int i = 0; i < p_sys->i_query; i++ )
- free( p_sys->ppsz_query[i] );
- TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
-
free( p_sys->psz_filename );
lua_close( p_sys->L );
free( p_sys );
@@ -280,8 +277,6 @@ static void* Run( void *data )
services_discovery_sys_t *p_sys = p_sd->p_sys;
lua_State *L = p_sys->L;
- int cancel = vlc_savecancel();
-
lua_getglobal( L, "main" );
if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
{
@@ -289,7 +284,6 @@ static void* Run( void *data )
"function main(): %s", p_sys->psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
- vlc_restorecancel( cancel );
return NULL;
}
msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
@@ -298,37 +292,20 @@ static void* Run( void *data )
* open, but lua will never gc until lua_close(). */
lua_gc( L, LUA_GCCOLLECT, 0 );
- vlc_restorecancel( cancel );
-
/* Main loop to handle search requests */
- vlc_mutex_lock( &p_sys->lock );
- mutex_cleanup_push( &p_sys->lock );
- for( ;; )
- {
- /* Wait for a request */
- if( !p_sys->i_query )
- {
- vlc_cond_wait( &p_sys->cond, &p_sys->lock );
- continue;
- }
-
- /* Execute one query (protected against cancellation) */
- char *psz_query = p_sys->ppsz_query[p_sys->i_query - 1];
- TAB_ERASE(p_sys->i_query, p_sys->ppsz_query, p_sys->i_query - 1);
- vlc_mutex_unlock( &p_sys->lock );
+ struct sd_query *query;
- cancel = vlc_savecancel();
- DoSearch( p_sd, psz_query );
- free( psz_query );
+ while( (query = vlc_queue_DequeueKillable( &p_sys->queue,
+ &p_sys->dead )) != NULL )
+ {
+ /* Execute one query */
+ DoSearch( p_sd, query->query );
+ free( query );
/* Force garbage collection, because the core will keep the SD
* open, but lua will never gc until lua_close(). */
lua_gc( L, LUA_GCCOLLECT, 0 );
- vlc_restorecancel( cancel );
-
- vlc_mutex_lock( &p_sys->lock );
}
- vlc_cleanup_pop();
- vlc_assert_unreachable();
+ return NULL;
}
/*****************************************************************************
@@ -343,10 +320,15 @@ static int Control( services_discovery_t *p_sd, int i_command, va_list args )
case SD_CMD_SEARCH:
{
const char *psz_query = va_arg( args, const char * );
- vlc_mutex_lock( &p_sys->lock );
- TAB_APPEND( p_sys->i_query, p_sys->ppsz_query, strdup( psz_query ) );
- vlc_cond_signal( &p_sys->cond );
- vlc_mutex_unlock( &p_sys->lock );
+ size_t len = strlen(psz_query) + 1;
+ struct sd_query *query = malloc(sizeof (*query) + len);
+
+ if (unlikely(query == NULL))
+ return VLC_ENOMEM;
+
+ query->next = NULL;
+ memcpy(query->query, psz_query, len);
+ vlc_queue_Enqueue(&p_sys->queue, query);
break;
}
More information about the vlc-commits
mailing list