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"
56 # include <sys/socket.h>
57 # include <netinet/in.h>
59 typedef struct sockaddr_in SOCKADDR_IN;
60 typedef struct sockaddr *PSOCKADDR;
62 # define closesocket soclose
65 # include <winsock2.h>
66 # include <ws2tcpip.h>
68 # define read( a, b, c ) recv (a, b, c, 0)
70 # define write( a, b, c ) send (a, b, c, 0)
72 # define close( a ) closesocket (a)
81 #if defined (HAVE_SYS_EVENTFD_H)
82 # include <sys/eventfd.h>
84 # define EFD_CLOEXEC 0
85 # warning EFD_CLOEXEC missing. Consider updating libc.
90 /*****************************************************************************
92 *****************************************************************************/
93 static int DumpCommand( vlc_object_t *, char const *,
94 vlc_value_t, vlc_value_t, void * );
96 static vlc_object_t * FindName ( vlc_object_internals_t *, const char * );
97 static void PrintObject( vlc_object_internals_t *, const char * );
98 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
100 static vlc_list_t * NewList ( int );
102 static void vlc_object_destroy( vlc_object_t *p_this );
104 /*****************************************************************************
105 * Local structure lock
106 *****************************************************************************/
107 static void libvlc_lock (libvlc_int_t *p_libvlc)
109 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
112 static void libvlc_unlock (libvlc_int_t *p_libvlc)
114 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
117 #undef vlc_custom_create
118 void *vlc_custom_create (vlc_object_t *parent, size_t length,
119 const char *typename)
122 * VLC objects are laid out as follow:
123 * - first the LibVLC-private per-object data,
124 * - then VLC_COMMON members from vlc_object_t,
125 * - finally, the type-specific data (if any).
127 * This function initializes the LibVLC and common data,
128 * and zeroes the rest.
130 assert (length >= sizeof (vlc_object_t));
132 vlc_object_internals_t *priv = malloc (sizeof (*priv) + length);
133 if (unlikely(priv == NULL))
135 priv->psz_name = NULL;
136 priv->var_root = NULL;
137 vlc_mutex_init (&priv->var_lock);
138 vlc_cond_init (&priv->var_wait);
139 priv->pipes[0] = priv->pipes[1] = -1;
140 vlc_spin_init (&priv->ref_spin);
141 priv->i_refcount = 1;
142 priv->pf_destructor = NULL;
146 vlc_object_t *obj = (vlc_object_t *)(priv + 1);
147 obj->psz_object_type = typename;
148 obj->psz_header = NULL;
150 obj->b_force = false;
151 memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
153 if (likely(parent != NULL))
155 vlc_object_internals_t *papriv = vlc_internals (parent);
157 obj->i_flags = parent->i_flags;
158 obj->p_libvlc = parent->p_libvlc;
160 /* Attach the child to its parent (no lock needed) */
161 obj->p_parent = vlc_object_hold (parent);
163 /* Attach the parent to its child (structure lock needed) */
164 libvlc_lock (obj->p_libvlc);
165 priv->next = papriv->first;
166 if (priv->next != NULL)
167 priv->next->prev = priv;
168 papriv->first = priv;
169 libvlc_unlock (obj->p_libvlc);
173 libvlc_int_t *self = (libvlc_int_t *)obj;
176 obj->p_libvlc = self;
177 obj->p_parent = NULL;
179 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
181 /* TODO: should be in src/libvlc.c */
182 int canc = vlc_savecancel ();
183 var_Create (obj, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
184 var_AddCallback (obj, "tree", DumpCommand, obj);
185 var_Create (obj, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
186 var_AddCallback (obj, "vars", DumpCommand, obj);
187 vlc_restorecancel (canc);
193 #undef vlc_object_create
195 * Allocates and initializes a vlc object.
197 * @param i_size object byte size
199 * @return the new object, or NULL on error.
201 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
203 return vlc_custom_create( p_this, i_size, "generic" );
206 #undef vlc_object_set_destructor
208 ****************************************************************************
209 * Set the destructor of a vlc object
211 * This function sets the destructor of the vlc object. It will be called
212 * when the object is destroyed when the its refcount reaches 0.
213 * (It is called by the internal function vlc_object_destroy())
214 *****************************************************************************/
215 void vlc_object_set_destructor( vlc_object_t *p_this,
216 vlc_destructor_t pf_destructor )
218 vlc_object_internals_t *p_priv = vlc_internals(p_this );
220 vlc_spin_lock( &p_priv->ref_spin );
221 p_priv->pf_destructor = pf_destructor;
222 vlc_spin_unlock( &p_priv->ref_spin );
225 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
227 #undef vlc_object_set_name
228 int vlc_object_set_name(vlc_object_t *obj, const char *name)
230 vlc_object_internals_t *priv = vlc_internals(obj);
231 char *newname = name ? strdup (name) : NULL;
234 vlc_mutex_lock (&name_lock);
235 oldname = priv->psz_name;
236 priv->psz_name = newname;
237 vlc_mutex_unlock (&name_lock);
240 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
243 #undef vlc_object_get_name
244 char *vlc_object_get_name(const vlc_object_t *obj)
246 vlc_object_internals_t *priv = vlc_internals(obj);
249 vlc_mutex_lock (&name_lock);
250 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
251 vlc_mutex_unlock (&name_lock);
257 * Destroys a VLC object once it has no more references.
259 * This function must be called with cancellation disabled (currently).
261 static void vlc_object_destroy( vlc_object_t *p_this )
263 vlc_object_internals_t *p_priv = vlc_internals( p_this );
265 /* Call the custom "subclass" destructor */
266 if( p_priv->pf_destructor )
267 p_priv->pf_destructor( p_this );
269 if (unlikely(p_this == VLC_OBJECT(p_this->p_libvlc)))
271 /* TODO: should be in src/libvlc.c */
272 var_DelCallback (p_this, "tree", DumpCommand, p_this);
273 var_DelCallback (p_this, "vars", DumpCommand, p_this);
276 /* Destroy the associated variables. */
277 var_DestroyAll( p_this );
279 vlc_cond_destroy( &p_priv->var_wait );
280 vlc_mutex_destroy( &p_priv->var_lock );
282 free( p_this->psz_header );
284 free( p_priv->psz_name );
286 vlc_spin_destroy( &p_priv->ref_spin );
287 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
288 close( p_priv->pipes[1] );
289 if( p_priv->pipes[0] != -1 )
290 close( p_priv->pipes[0] );
291 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
292 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
298 #if defined(WIN32) || defined(__OS2__)
300 * select()-able pipes emulated using Winsock
302 # define vlc_pipe selectable_pipe
303 static int selectable_pipe (int fd[2])
306 int addrlen = sizeof (addr);
308 int l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
309 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
310 if ((l == -1) || (c == -1))
313 memset (&addr, 0, sizeof (addr));
314 addr.sin_family = AF_INET;
315 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
316 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
317 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
319 || connect (c, (PSOCKADDR)&addr, addrlen))
322 a = accept (l, NULL, NULL);
340 #endif /* WIN32 || __OS2__ */
342 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
345 * Returns the readable end of a pipe that becomes readable once termination
346 * of the object is requested (vlc_object_kill()).
347 * This can be used to wake-up out of a select() or poll() event loop, such
348 * typically when doing network I/O.
350 * Note that the pipe will remain the same for the lifetime of the object.
351 * DO NOT read the pipe nor close it yourself. Ever.
353 * @param obj object that would be "killed"
354 * @return a readable pipe descriptor, or -1 on error.
356 int vlc_object_waitpipe( vlc_object_t *obj )
358 vlc_object_internals_t *internals = vlc_internals( obj );
360 vlc_mutex_lock (&pipe_lock);
361 if (internals->pipes[0] == -1)
363 /* This can only ever happen if someone killed us without locking: */
364 assert (internals->pipes[1] == -1);
366 /* pipe() is not a cancellation point, but write() is and eventfd() is
367 * unspecified (not in POSIX). */
368 int canc = vlc_savecancel ();
369 #if defined (HAVE_SYS_EVENTFD_H)
370 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
371 if (internals->pipes[0] == -1)
374 if (vlc_pipe (internals->pipes))
375 internals->pipes[0] = internals->pipes[1] = -1;
378 if (internals->pipes[0] != -1 && obj->b_die)
379 { /* Race condition: vlc_object_kill() already invoked! */
380 msg_Dbg (obj, "waitpipe: object already dying");
381 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
383 vlc_restorecancel (canc);
385 vlc_mutex_unlock (&pipe_lock);
386 return internals->pipes[0];
389 #undef vlc_object_kill
391 * Requests termination of an object, cancels the object thread, and make the
392 * object wait pipe (if it exists) readable. Not a cancellation point.
394 void vlc_object_kill( vlc_object_t *p_this )
396 vlc_object_internals_t *priv = vlc_internals( p_this );
399 vlc_mutex_lock( &pipe_lock );
403 p_this->b_die = true;
406 /* This also serves as a memory barrier toward vlc_object_alive(): */
407 vlc_mutex_unlock( &pipe_lock );
411 int canc = vlc_savecancel ();
413 /* write _after_ setting b_die, so vlc_object_alive() returns false */
414 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
415 msg_Dbg (p_this, "waitpipe: object killed");
416 vlc_restorecancel (canc);
420 #undef vlc_object_find_name
422 * Finds a named object and increment its reference count.
423 * Beware that objects found in this manner can be "owned" by another thread,
424 * be of _any_ type, and be attached to any module (if any). With such an
425 * object reference, you can set or get object variables, emit log messages,
426 * and read write-once object parameters (psz_object_type, etc).
427 * You CANNOT cast the object to a more specific object type, and you
428 * definitely cannot invoke object type-specific callbacks with this.
430 * @param p_this object to search from
431 * @param psz_name name of the object to search for
433 * @return a matching object (must be released by the caller),
436 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
438 vlc_object_t *p_found;
440 /* Reading psz_object_name from a separate inhibits thread-safety.
441 * Use a libvlc address variable instead for that sort of things! */
442 msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
444 libvlc_lock (p_this->p_libvlc);
445 vlc_mutex_lock (&name_lock);
446 p_found = FindName (vlc_internals (p_this), psz_name);
447 vlc_mutex_unlock (&name_lock);
448 libvlc_unlock (p_this->p_libvlc);
452 #undef vlc_object_hold
454 * Increment an object reference counter.
456 void * vlc_object_hold( vlc_object_t *p_this )
458 vlc_object_internals_t *internals = vlc_internals( p_this );
460 vlc_spin_lock( &internals->ref_spin );
461 /* Avoid obvious freed object uses */
462 assert( internals->i_refcount > 0 );
463 /* Increment the counter */
464 internals->i_refcount++;
465 vlc_spin_unlock( &internals->ref_spin );
469 #undef vlc_object_release
471 * Drops a reference to an object (decrements the reference count).
472 * If the count reaches zero, the object is destroyed.
474 void vlc_object_release( vlc_object_t *p_this )
476 vlc_object_internals_t *internals = vlc_internals( p_this );
477 vlc_object_t *parent = NULL;
478 bool b_should_destroy;
480 vlc_spin_lock( &internals->ref_spin );
481 assert( internals->i_refcount > 0 );
483 if( internals->i_refcount > 1 )
486 /* There are still other references to the object */
487 internals->i_refcount--;
488 vlc_spin_unlock( &internals->ref_spin );
491 vlc_spin_unlock( &internals->ref_spin );
494 /* Remember that we cannot hold the spin while waiting on the mutex */
495 libvlc_lock (p_this->p_libvlc);
496 /* Take the spin again. Note that another thread may have held the
497 * object in the (very short) mean time. */
498 vlc_spin_lock( &internals->ref_spin );
499 b_should_destroy = --internals->i_refcount == 0;
500 vlc_spin_unlock( &internals->ref_spin );
502 if( b_should_destroy )
504 /* Detach from parent to protect against vlc_object_find_name() */
505 parent = p_this->p_parent;
509 if (internals->prev != NULL)
510 internals->prev->next = internals->next;
512 vlc_internals(parent)->first = internals->next;
513 if (internals->next != NULL)
514 internals->next->prev = internals->prev;
517 /* We have no children */
518 assert (internals->first == NULL);
520 libvlc_unlock (p_this->p_libvlc);
522 if( b_should_destroy )
526 canc = vlc_savecancel ();
527 vlc_object_destroy( p_this );
528 vlc_restorecancel (canc);
530 vlc_object_release (parent);
534 #undef vlc_list_children
536 * Gets the list of children of an objects, and increment their reference
538 * @return a list (possibly empty) or NULL in case of error.
540 vlc_list_t *vlc_list_children( vlc_object_t *obj )
543 vlc_object_internals_t *priv;
546 libvlc_lock (obj->p_libvlc);
547 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
550 if (likely(l != NULL))
554 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
555 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
557 libvlc_unlock (obj->p_libvlc);
561 static void DumpVariable (const void *data, const VISIT which, const int depth)
563 if (which != postorder && which != leaf)
567 const variable_t *p_var = *(const variable_t **)data;
568 const char *psz_type = "unknown";
570 switch( p_var->i_type & VLC_VAR_TYPE )
572 #define MYCASE( type, nice ) \
573 case VLC_VAR_ ## type: \
576 MYCASE( VOID, "void" );
577 MYCASE( BOOL, "bool" );
578 MYCASE( INTEGER, "integer" );
579 MYCASE( HOTKEY, "hotkey" );
580 MYCASE( STRING, "string" );
581 MYCASE( VARIABLE, "variable" );
582 MYCASE( FLOAT, "float" );
583 MYCASE( TIME, "time" );
584 MYCASE( COORDS, "coords" );
585 MYCASE( ADDRESS, "address" );
588 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
589 if( p_var->psz_text )
590 printf( ", %s", p_var->psz_text );
591 fputc( ')', stdout );
592 if( p_var->i_type & VLC_VAR_HASCHOICE )
593 fputs( ", has choices", stdout );
594 if( p_var->i_type & VLC_VAR_ISCOMMAND )
595 fputs( ", command", stdout );
596 if( p_var->i_entries )
597 printf( ", %d callbacks", p_var->i_entries );
598 switch( p_var->i_type & VLC_VAR_CLASS )
603 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
605 case VLC_VAR_INTEGER:
606 printf( ": %"PRId64, p_var->val.i_int );
609 printf( ": \"%s\"", p_var->val.psz_string );
612 printf( ": %f", p_var->val.f_float );
615 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
618 printf( ": %"PRId32"x%"PRId32,
619 p_var->val.coords.x, p_var->val.coords.y );
621 case VLC_VAR_ADDRESS:
622 printf( ": %p", p_var->val.p_address );
625 fputc( '\n', stdout );
628 /*****************************************************************************
629 * DumpCommand: print the current vlc structure
630 *****************************************************************************
631 * This function prints either an ASCII tree showing the connections between
632 * vlc objects, and additional information such as their refcount, thread ID,
633 * etc. (command "tree"), or the same data as a simple list (command "list").
634 *****************************************************************************/
635 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
636 vlc_value_t oldval, vlc_value_t newval, void *p_data )
639 vlc_object_t *p_object = NULL;
641 if( *newval.psz_string )
643 /* try using the object's name to find it */
644 p_object = vlc_object_find_name( p_data, newval.psz_string );
649 libvlc_lock (p_this->p_libvlc);
650 if( *psz_cmd == 't' )
652 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
655 p_object = VLC_OBJECT(p_this->p_libvlc);
658 DumpStructure( vlc_internals(p_object), 0, psz_foo );
660 else if( *psz_cmd == 'v' )
663 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
665 PrintObject( vlc_internals(p_object), "" );
666 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
667 if( vlc_internals( p_object )->var_root == NULL )
668 puts( " `-o No variables" );
670 twalk( vlc_internals( p_object )->var_root, DumpVariable );
671 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
673 libvlc_unlock (p_this->p_libvlc);
675 if( *newval.psz_string )
677 vlc_object_release( p_object );
682 /*****************************************************************************
683 * vlc_list_release: free a list previously allocated by vlc_list_find
684 *****************************************************************************
685 * This function decreases the refcount of all objects in the list and
687 *****************************************************************************/
688 void vlc_list_release( vlc_list_t *p_list )
692 for( i_index = 0; i_index < p_list->i_count; i_index++ )
694 vlc_object_release( p_list->p_values[i_index].p_object );
697 free( p_list->p_values );
701 /* Following functions are local */
703 static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
705 if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
706 return vlc_object_hold (vlc_externals (priv));
708 for (priv = priv->first; priv != NULL; priv = priv->next)
710 vlc_object_t *found = FindName (priv, name);
717 static void PrintObject( vlc_object_internals_t *priv,
718 const char *psz_prefix )
720 char psz_refcount[20], psz_name[50], psz_parent[20];
722 int canc = vlc_savecancel ();
723 memset( &psz_name, 0, sizeof(psz_name) );
725 vlc_mutex_lock (&name_lock);
726 if (priv->psz_name != NULL)
728 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
732 vlc_mutex_unlock (&name_lock);
734 psz_refcount[0] = '\0';
735 if( priv->i_refcount > 0 )
736 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
738 psz_parent[0] = '\0';
739 /* FIXME: need structure lock!!! */
740 if( vlc_externals(priv)->p_parent )
741 snprintf( psz_parent, 19, ", parent %p",
742 vlc_externals(priv)->p_parent );
744 printf( " %so %p %s%s%s%s\n", psz_prefix,
745 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
746 psz_name, psz_refcount, psz_parent );
747 vlc_restorecancel (canc);
750 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
753 char i_back = psz_foo[i_level];
754 psz_foo[i_level] = '\0';
756 PrintObject (priv, psz_foo);
758 psz_foo[i_level] = i_back;
760 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
762 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
766 for (priv = priv->first; priv != NULL; priv = priv->next)
770 psz_foo[i_level-1] = ' ';
772 if( psz_foo[i_level-2] == '`' )
774 psz_foo[i_level-2] = ' ';
778 psz_foo[i_level] = priv->next ? '|' : '`';
779 psz_foo[i_level+1] = '-';
780 psz_foo[i_level+2] = '\0';
782 DumpStructure (priv, i_level + 2, psz_foo);
786 static vlc_list_t * NewList( int i_count )
788 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
792 p_list->i_count = i_count;
796 p_list->p_values = NULL;
800 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
801 if( p_list->p_values == NULL )