1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
5 * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
28 * Unless otherwise stated, functions in this file are not cancellation point.
29 * All functions in this file are safe w.r.t. deferred cancellation.
33 /*****************************************************************************
35 *****************************************************************************/
40 #include <vlc_common.h>
42 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
49 #include "variables.h"
60 # include <winsock2.h>
61 # include <ws2tcpip.h>
63 # define read( a, b, c ) recv (a, b, c, 0)
65 # define write( a, b, c ) send (a, b, c, 0)
67 # define close( a ) closesocket (a)
73 #if defined (HAVE_SYS_EVENTFD_H)
74 # include <sys/eventfd.h>
76 # define EFD_CLOEXEC 0
77 # warning EFD_CLOEXEC missing. Consider updating libc.
82 /*****************************************************************************
84 *****************************************************************************/
85 static int DumpCommand( vlc_object_t *, char const *,
86 vlc_value_t, vlc_value_t, void * );
88 static vlc_object_t * FindName ( vlc_object_internals_t *, const char * );
89 static void PrintObject( vlc_object_internals_t *, const char * );
90 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
92 static vlc_list_t * NewList ( int );
94 static void vlc_object_destroy( vlc_object_t *p_this );
96 /*****************************************************************************
97 * Local structure lock
98 *****************************************************************************/
99 static void libvlc_lock (libvlc_int_t *p_libvlc)
101 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
104 static void libvlc_unlock (libvlc_int_t *p_libvlc)
106 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
109 #undef vlc_custom_create
110 void *vlc_custom_create (vlc_object_t *parent, size_t length,
111 int type, const char *typename)
114 * VLC objects are laid out as follow:
115 * - first the LibVLC-private per-object data,
116 * - then VLC_COMMON members from vlc_object_t,
117 * - finally, the type-specific data (if any).
119 * This function initializes the LibVLC and common data,
120 * and zeroes the rest.
122 assert (length >= sizeof (vlc_object_t));
124 vlc_object_internals_t *priv = malloc (sizeof (*priv) + length);
125 if (unlikely(priv == NULL))
127 priv->i_object_type = type;
128 priv->psz_name = NULL;
129 priv->var_root = NULL;
130 vlc_mutex_init (&priv->var_lock);
131 vlc_cond_init (&priv->var_wait);
132 priv->pipes[0] = priv->pipes[1] = -1;
133 vlc_spin_init (&priv->ref_spin);
134 priv->i_refcount = 1;
135 priv->pf_destructor = NULL;
139 vlc_object_t *obj = (vlc_object_t *)(priv + 1);
140 obj->psz_object_type = typename;
141 obj->psz_header = NULL;
143 obj->b_force = false;
144 memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
146 if (likely(parent != NULL))
148 vlc_object_internals_t *papriv = vlc_internals (parent);
150 obj->i_flags = parent->i_flags;
151 obj->p_libvlc = parent->p_libvlc;
153 /* Attach the child to its parent (no lock needed) */
154 obj->p_parent = vlc_object_hold (parent);
156 /* Attach the parent to its child (structure lock needed) */
157 libvlc_lock (obj->p_libvlc);
158 priv->next = papriv->first;
159 if (priv->next != NULL)
160 priv->next->prev = priv;
161 papriv->first = priv;
162 libvlc_unlock (obj->p_libvlc);
166 libvlc_int_t *self = (libvlc_int_t *)obj;
169 obj->p_libvlc = self;
170 obj->p_parent = NULL;
172 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
174 /* TODO: should be in src/libvlc.c */
175 int canc = vlc_savecancel ();
176 var_Create (obj, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
177 var_AddCallback (obj, "tree", DumpCommand, obj);
178 var_Create (obj, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
179 var_AddCallback (obj, "vars", DumpCommand, obj);
180 vlc_restorecancel (canc);
186 #undef vlc_object_create
188 * Allocates and initializes a vlc object.
190 * @param i_size object byte size
192 * @return the new object, or NULL on error.
194 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
196 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
199 #undef vlc_object_set_destructor
201 ****************************************************************************
202 * Set the destructor of a vlc object
204 * This function sets the destructor of the vlc object. It will be called
205 * when the object is destroyed when the its refcount reaches 0.
206 * (It is called by the internal function vlc_object_destroy())
207 *****************************************************************************/
208 void vlc_object_set_destructor( vlc_object_t *p_this,
209 vlc_destructor_t pf_destructor )
211 vlc_object_internals_t *p_priv = vlc_internals(p_this );
213 vlc_spin_lock( &p_priv->ref_spin );
214 p_priv->pf_destructor = pf_destructor;
215 vlc_spin_unlock( &p_priv->ref_spin );
218 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
220 #undef vlc_object_set_name
221 int vlc_object_set_name(vlc_object_t *obj, const char *name)
223 vlc_object_internals_t *priv = vlc_internals(obj);
224 char *newname = name ? strdup (name) : NULL;
227 vlc_mutex_lock (&name_lock);
228 oldname = priv->psz_name;
229 priv->psz_name = newname;
230 vlc_mutex_unlock (&name_lock);
233 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
236 #undef vlc_object_get_name
237 char *vlc_object_get_name(const vlc_object_t *obj)
239 vlc_object_internals_t *priv = vlc_internals(obj);
242 vlc_mutex_lock (&name_lock);
243 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
244 vlc_mutex_unlock (&name_lock);
250 * Destroys a VLC object once it has no more references.
252 * This function must be called with cancellation disabled (currently).
254 static void vlc_object_destroy( vlc_object_t *p_this )
256 vlc_object_internals_t *p_priv = vlc_internals( p_this );
258 /* Call the custom "subclass" destructor */
259 if( p_priv->pf_destructor )
260 p_priv->pf_destructor( p_this );
262 /* Destroy the associated variables. */
263 var_DestroyAll( p_this );
265 vlc_cond_destroy( &p_priv->var_wait );
266 vlc_mutex_destroy( &p_priv->var_lock );
268 free( p_this->psz_header );
270 free( p_priv->psz_name );
272 vlc_spin_destroy( &p_priv->ref_spin );
273 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
274 close( p_priv->pipes[1] );
275 if( p_priv->pipes[0] != -1 )
276 close( p_priv->pipes[0] );
277 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
278 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
286 * select()-able pipes emulated using Winsock
288 # define vlc_pipe selectable_pipe
289 static int selectable_pipe (int fd[2])
292 int addrlen = sizeof (addr);
294 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
295 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
296 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
299 memset (&addr, 0, sizeof (addr));
300 addr.sin_family = AF_INET;
301 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
302 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
303 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
305 || connect (c, (PSOCKADDR)&addr, addrlen))
308 a = accept (l, NULL, NULL);
309 if (a == INVALID_SOCKET)
320 if (l != INVALID_SOCKET)
322 if (c != INVALID_SOCKET)
328 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
331 * Returns the readable end of a pipe that becomes readable once termination
332 * of the object is requested (vlc_object_kill()).
333 * This can be used to wake-up out of a select() or poll() event loop, such
334 * typically when doing network I/O.
336 * Note that the pipe will remain the same for the lifetime of the object.
337 * DO NOT read the pipe nor close it yourself. Ever.
339 * @param obj object that would be "killed"
340 * @return a readable pipe descriptor, or -1 on error.
342 int vlc_object_waitpipe( vlc_object_t *obj )
344 vlc_object_internals_t *internals = vlc_internals( obj );
346 vlc_mutex_lock (&pipe_lock);
347 if (internals->pipes[0] == -1)
349 /* This can only ever happen if someone killed us without locking: */
350 assert (internals->pipes[1] == -1);
352 /* pipe() is not a cancellation point, but write() is and eventfd() is
353 * unspecified (not in POSIX). */
354 int canc = vlc_savecancel ();
355 #if defined (HAVE_SYS_EVENTFD_H)
356 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
357 if (internals->pipes[0] == -1)
360 if (vlc_pipe (internals->pipes))
361 internals->pipes[0] = internals->pipes[1] = -1;
364 if (internals->pipes[0] != -1 && obj->b_die)
365 { /* Race condition: vlc_object_kill() already invoked! */
366 msg_Dbg (obj, "waitpipe: object already dying");
367 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
369 vlc_restorecancel (canc);
371 vlc_mutex_unlock (&pipe_lock);
372 return internals->pipes[0];
375 #undef vlc_object_kill
377 * Requests termination of an object, cancels the object thread, and make the
378 * object wait pipe (if it exists) readable. Not a cancellation point.
380 void vlc_object_kill( vlc_object_t *p_this )
382 vlc_object_internals_t *priv = vlc_internals( p_this );
385 vlc_mutex_lock( &pipe_lock );
389 p_this->b_die = true;
392 /* This also serves as a memory barrier toward vlc_object_alive(): */
393 vlc_mutex_unlock( &pipe_lock );
397 int canc = vlc_savecancel ();
399 /* write _after_ setting b_die, so vlc_object_alive() returns false */
400 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
401 msg_Dbg (p_this, "waitpipe: object killed");
402 vlc_restorecancel (canc);
406 static int objnamecmp(const vlc_object_t *obj, const char *name)
408 char *objname = vlc_object_get_name(obj);
412 int ret = strcmp (objname, name);
417 #undef vlc_object_find_name
419 * Finds a named object and increment its reference count.
420 * Beware that objects found in this manner can be "owned" by another thread,
421 * be of _any_ type, and be attached to any module (if any). With such an
422 * object reference, you can set or get object variables, emit log messages,
423 * and read write-once object parameters (psz_object_type, etc).
424 * You CANNOT cast the object to a more specific object type, and you
425 * definitely cannot invoke object type-specific callbacks with this.
427 * @param p_this object to search from
428 * @param psz_name name of the object to search for
430 * @return a matching object (must be released by the caller),
433 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
435 vlc_object_t *p_found;
437 /* Reading psz_object_name from a separate inhibits thread-safety.
438 * Use a libvlc address variable instead for that sort of things! */
439 msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
441 libvlc_lock (p_this->p_libvlc);
442 vlc_mutex_lock (&name_lock);
443 p_found = FindName (vlc_internals (p_this), psz_name);
444 vlc_mutex_unlock (&name_lock);
445 libvlc_unlock (p_this->p_libvlc);
449 #undef vlc_object_hold
451 * Increment an object reference counter.
453 void * vlc_object_hold( vlc_object_t *p_this )
455 vlc_object_internals_t *internals = vlc_internals( p_this );
457 vlc_spin_lock( &internals->ref_spin );
458 /* Avoid obvious freed object uses */
459 assert( internals->i_refcount > 0 );
460 /* Increment the counter */
461 internals->i_refcount++;
462 vlc_spin_unlock( &internals->ref_spin );
466 #undef vlc_object_release
468 * Drops a reference to an object (decrements the reference count).
469 * If the count reaches zero, the object is destroyed.
471 void vlc_object_release( vlc_object_t *p_this )
473 vlc_object_internals_t *internals = vlc_internals( p_this );
474 vlc_object_t *parent = NULL;
475 bool b_should_destroy;
477 vlc_spin_lock( &internals->ref_spin );
478 assert( internals->i_refcount > 0 );
480 if( internals->i_refcount > 1 )
483 /* There are still other references to the object */
484 internals->i_refcount--;
485 vlc_spin_unlock( &internals->ref_spin );
488 vlc_spin_unlock( &internals->ref_spin );
491 /* Remember that we cannot hold the spin while waiting on the mutex */
492 libvlc_lock (p_this->p_libvlc);
493 /* Take the spin again. Note that another thread may have held the
494 * object in the (very short) mean time. */
495 vlc_spin_lock( &internals->ref_spin );
496 b_should_destroy = --internals->i_refcount == 0;
497 vlc_spin_unlock( &internals->ref_spin );
499 if( b_should_destroy )
501 /* Detach from parent to protect against vlc_object_find_name() */
502 parent = p_this->p_parent;
506 if (internals->prev != NULL)
507 internals->prev->next = internals->next;
509 vlc_internals(parent)->first = internals->next;
510 if (internals->next != NULL)
511 internals->next->prev = internals->prev;
514 /* We have no children */
515 assert (internals->first == NULL);
517 libvlc_unlock (p_this->p_libvlc);
519 if( b_should_destroy )
523 canc = vlc_savecancel ();
524 vlc_object_destroy( p_this );
525 vlc_restorecancel (canc);
527 vlc_object_release (parent);
531 #undef vlc_list_children
533 * Gets the list of children of an objects, and increment their reference
535 * @return a list (possibly empty) or NULL in case of error.
537 vlc_list_t *vlc_list_children( vlc_object_t *obj )
540 vlc_object_internals_t *priv;
543 libvlc_lock (obj->p_libvlc);
544 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
547 if (likely(l != NULL))
551 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
552 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
554 libvlc_unlock (obj->p_libvlc);
558 static void DumpVariable (const void *data, const VISIT which, const int depth)
560 if (which != postorder && which != leaf)
564 const variable_t *p_var = *(const variable_t **)data;
565 const char *psz_type = "unknown";
567 switch( p_var->i_type & VLC_VAR_TYPE )
569 #define MYCASE( type, nice ) \
570 case VLC_VAR_ ## type: \
573 MYCASE( VOID, "void" );
574 MYCASE( BOOL, "bool" );
575 MYCASE( INTEGER, "integer" );
576 MYCASE( HOTKEY, "hotkey" );
577 MYCASE( STRING, "string" );
578 MYCASE( VARIABLE, "variable" );
579 MYCASE( FLOAT, "float" );
580 MYCASE( TIME, "time" );
581 MYCASE( COORDS, "coords" );
582 MYCASE( ADDRESS, "address" );
583 MYCASE( MUTEX, "mutex" );
586 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
587 if( p_var->psz_text )
588 printf( ", %s", p_var->psz_text );
589 fputc( ')', stdout );
590 if( p_var->i_type & VLC_VAR_HASCHOICE )
591 fputs( ", has choices", stdout );
592 if( p_var->i_type & VLC_VAR_ISCOMMAND )
593 fputs( ", command", stdout );
594 if( p_var->i_entries )
595 printf( ", %d callbacks", p_var->i_entries );
596 switch( p_var->i_type & VLC_VAR_CLASS )
602 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
604 case VLC_VAR_INTEGER:
605 printf( ": %"PRId64, p_var->val.i_int );
608 printf( ": \"%s\"", p_var->val.psz_string );
611 printf( ": %f", p_var->val.f_float );
614 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
617 printf( ": %"PRId32"x%"PRId32,
618 p_var->val.coords.x, p_var->val.coords.y );
620 case VLC_VAR_ADDRESS:
621 printf( ": %p", p_var->val.p_address );
624 fputc( '\n', stdout );
627 /*****************************************************************************
628 * DumpCommand: print the current vlc structure
629 *****************************************************************************
630 * This function prints either an ASCII tree showing the connections between
631 * vlc objects, and additional information such as their refcount, thread ID,
632 * etc. (command "tree"), or the same data as a simple list (command "list").
633 *****************************************************************************/
634 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
635 vlc_value_t oldval, vlc_value_t newval, void *p_data )
638 vlc_object_t *p_object = NULL;
640 if( *newval.psz_string )
642 /* try using the object's name to find it */
643 p_object = vlc_object_find_name( p_data, newval.psz_string );
648 libvlc_lock (p_this->p_libvlc);
649 if( *psz_cmd == 't' )
651 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
654 p_object = VLC_OBJECT(p_this->p_libvlc);
657 DumpStructure( vlc_internals(p_object), 0, psz_foo );
659 else if( *psz_cmd == 'v' )
662 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
664 PrintObject( vlc_internals(p_object), "" );
665 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
666 if( vlc_internals( p_object )->var_root == NULL )
667 puts( " `-o No variables" );
669 twalk( vlc_internals( p_object )->var_root, DumpVariable );
670 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
672 libvlc_unlock (p_this->p_libvlc);
674 if( *newval.psz_string )
676 vlc_object_release( p_object );
681 /*****************************************************************************
682 * vlc_list_release: free a list previously allocated by vlc_list_find
683 *****************************************************************************
684 * This function decreases the refcount of all objects in the list and
686 *****************************************************************************/
687 void vlc_list_release( vlc_list_t *p_list )
691 for( i_index = 0; i_index < p_list->i_count; i_index++ )
693 vlc_object_release( p_list->p_values[i_index].p_object );
696 free( p_list->p_values );
700 /* Following functions are local */
702 static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
704 if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
705 return vlc_object_hold (vlc_externals (priv));
707 for (priv = priv->first; priv != NULL; priv = priv->next)
709 vlc_object_t *found = FindName (priv, name);
716 static void PrintObject( vlc_object_internals_t *priv,
717 const char *psz_prefix )
719 char psz_refcount[20], psz_name[50], psz_parent[20];
721 int canc = vlc_savecancel ();
722 memset( &psz_name, 0, sizeof(psz_name) );
724 vlc_mutex_lock (&name_lock);
725 if (priv->psz_name != NULL)
727 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
731 vlc_mutex_unlock (&name_lock);
733 psz_refcount[0] = '\0';
734 if( priv->i_refcount > 0 )
735 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
737 psz_parent[0] = '\0';
738 /* FIXME: need structure lock!!! */
739 if( vlc_externals(priv)->p_parent )
740 snprintf( psz_parent, 19, ", parent %p",
741 vlc_externals(priv)->p_parent );
743 printf( " %so %p %s%s%s%s\n", psz_prefix,
744 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
745 psz_name, psz_refcount, psz_parent );
746 vlc_restorecancel (canc);
749 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
752 char i_back = psz_foo[i_level];
753 psz_foo[i_level] = '\0';
755 PrintObject (priv, psz_foo);
757 psz_foo[i_level] = i_back;
759 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
761 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
765 for (priv = priv->first; priv != NULL; priv = priv->next)
769 psz_foo[i_level-1] = ' ';
771 if( psz_foo[i_level-2] == '`' )
773 psz_foo[i_level-2] = ' ';
777 psz_foo[i_level] = priv->next ? '|' : '`';
778 psz_foo[i_level+1] = '-';
779 psz_foo[i_level+2] = '\0';
781 DumpStructure (priv, i_level + 2, psz_foo);
785 static vlc_list_t * NewList( int i_count )
787 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
791 p_list->i_count = i_count;
795 p_list->p_values = NULL;
799 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
800 if( p_list->p_values == NULL )