1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
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
30 /*****************************************************************************
32 *****************************************************************************/
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_interface.h"
52 #include "vlc_codec.h"
53 #include "vlc_filter.h"
55 #include "variables.h"
61 # include <errno.h> /* ENOSYS */
65 /*****************************************************************************
67 *****************************************************************************/
68 static int DumpCommand( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void PrintObject ( vlc_object_t *, const char * );
74 static void DumpStructure ( vlc_object_t *, int, char * );
76 static vlc_list_t * NewList ( int );
77 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
78 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
79 static int CountChildren ( vlc_object_t *, int );
80 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
82 static void vlc_object_destroy( vlc_object_t *p_this );
83 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
85 /*****************************************************************************
86 * Local structure lock
87 *****************************************************************************/
88 static vlc_mutex_t structure_lock;
89 static vlc_threadvar_t thread_object;
91 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
92 int i_type, const char *psz_type )
95 vlc_object_internals_t *p_priv;
98 * VLC objects are laid out as follow:
99 * - first the LibVLC-private per-object data,
100 * - then VLC_COMMON members from vlc_object_t,
101 * - finally, the type-specific data (if any).
103 * This function initializes the LibVLC and common data,
104 * and zeroes the rest.
106 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
110 assert (i_size >= sizeof (vlc_object_t));
111 p_new = (vlc_object_t *)(p_priv + 1);
113 p_new->i_object_type = i_type;
114 p_new->psz_object_type = psz_type;
115 p_new->psz_object_name = NULL;
117 p_new->b_die = false;
118 p_new->b_error = false;
119 p_new->b_dead = false;
120 p_new->b_force = false;
122 p_new->psz_header = NULL;
125 p_new->i_flags = p_this->i_flags
126 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
128 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
130 if( !p_priv->p_vars )
136 libvlc_global_data_t *p_libvlc_global;
139 /* Only the global root object is created out of the blue */
140 p_libvlc_global = (libvlc_global_data_t *)p_new;
141 p_new->p_libvlc = NULL;
143 p_libvlc_global->i_counter = 0;
144 p_priv->next = p_priv->prev = p_new;
145 vlc_mutex_init( &structure_lock );
149 p_libvlc_global = vlc_global();
150 if( i_type == VLC_OBJECT_LIBVLC )
151 p_new->p_libvlc = (libvlc_int_t*)p_new;
153 p_new->p_libvlc = p_this->p_libvlc;
156 vlc_spin_init( &p_priv->ref_spin );
157 p_priv->i_refcount = 1;
158 p_priv->pf_destructor = NULL;
159 p_priv->b_thread = false;
160 p_new->p_parent = NULL;
161 p_priv->pp_children = NULL;
162 p_priv->i_children = 0;
164 p_new->p_private = NULL;
166 /* Initialize mutexes and condvars */
167 vlc_mutex_init( &p_new->object_lock );
168 vlc_cond_init( p_new, &p_new->object_wait );
169 vlc_mutex_init( &p_priv->var_lock );
170 vlc_spin_init( &p_priv->spin );
171 p_priv->pipes[0] = p_priv->pipes[1] = -1;
173 p_priv->next = VLC_OBJECT (p_libvlc_global);
176 #elif defined (LIBVLC_USE_PTHREAD)
177 p_priv->creator_id = pthread_self ();
178 #elif defined (WIN32)
179 p_priv->creator_id = GetCurrentThreadId ();
181 vlc_mutex_lock( &structure_lock );
182 p_priv->prev = vlc_internals (p_libvlc_global)->prev;
183 vlc_internals (p_libvlc_global)->prev = p_new;
184 vlc_internals (p_priv->prev)->next = p_new;
185 p_new->i_object_id = p_libvlc_global->i_counter++;
186 vlc_mutex_unlock( &structure_lock );
188 if( i_type == VLC_OBJECT_LIBVLC )
190 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "list", DumpCommand, NULL );
192 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193 var_AddCallback( p_new, "tree", DumpCommand, NULL );
194 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
195 var_AddCallback( p_new, "vars", DumpCommand, NULL );
203 * Allocates and initializes a vlc object.
205 * @param i_type known object type (all of them are negative integer values),
206 * or object byte size (always positive).
208 * @return the new object, or NULL on error.
210 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
212 const char * psz_type;
217 case VLC_OBJECT_LIBVLC:
218 i_size = sizeof(libvlc_int_t);
221 case VLC_OBJECT_INTF:
222 i_size = sizeof(intf_thread_t);
223 psz_type = "interface";
225 case VLC_OBJECT_DIALOGS:
226 i_size = sizeof(intf_thread_t);
227 psz_type = "dialogs";
229 case VLC_OBJECT_DEMUX:
230 i_size = sizeof(demux_t);
233 case VLC_OBJECT_ACCESS:
234 i_size = sizeof(access_t);
237 case VLC_OBJECT_DECODER:
238 i_size = sizeof(decoder_t);
239 psz_type = "decoder";
241 case VLC_OBJECT_PACKETIZER:
242 i_size = sizeof(decoder_t);
243 psz_type = "packetizer";
245 case VLC_OBJECT_ENCODER:
246 i_size = sizeof(encoder_t);
247 psz_type = "encoder";
249 case VLC_OBJECT_FILTER:
250 i_size = sizeof(filter_t);
253 case VLC_OBJECT_VOUT:
254 i_size = sizeof(vout_thread_t);
255 psz_type = "video output";
257 case VLC_OBJECT_AOUT:
258 i_size = sizeof(aout_instance_t);
259 psz_type = "audio output";
261 case VLC_OBJECT_SOUT:
262 i_size = sizeof(sout_instance_t);
263 psz_type = "stream output";
265 case VLC_OBJECT_OPENGL:
266 i_size = sizeof( vout_thread_t );
269 case VLC_OBJECT_ANNOUNCE:
270 i_size = sizeof( announce_handler_t );
271 psz_type = "announce";
273 case VLC_OBJECT_INTERACTION:
274 i_size = sizeof( interaction_t );
275 psz_type = "interaction";
278 i_size = i_type > (int)sizeof(vlc_object_t)
279 ? i_type : (int)sizeof(vlc_object_t);
280 i_type = VLC_OBJECT_GENERIC;
281 psz_type = "generic";
285 return vlc_custom_create( p_this, i_size, i_type, psz_type );
290 ****************************************************************************
291 * Set the destructor of a vlc object
293 * This function sets the destructor of the vlc object. It will be called
294 * when the object is destroyed when the its refcount reaches 0.
295 * (It is called by the internal function vlc_object_destroy())
296 *****************************************************************************/
297 void __vlc_object_set_destructor( vlc_object_t *p_this,
298 vlc_destructor_t pf_destructor )
300 vlc_object_internals_t *p_priv = vlc_internals(p_this );
301 p_priv->pf_destructor = pf_destructor;
305 ****************************************************************************
306 * Destroy a vlc object (Internal)
308 * This function destroys an object that has been previously allocated with
309 * vlc_object_create. The object's refcount must be zero and it must not be
310 * attached to other objects in any way.
311 *****************************************************************************/
312 static void vlc_object_destroy( vlc_object_t *p_this )
314 vlc_object_internals_t *p_priv = vlc_internals( p_this );
316 /* Objects are always detached beforehand */
317 assert( !p_this->p_parent );
319 /* Send a kill to the object's thread if applicable */
320 vlc_object_kill( p_this );
322 /* If we are running on a thread, wait until it ends */
323 if( p_priv->b_thread )
324 vlc_thread_join( p_this );
326 /* Call the custom "subclass" destructor */
327 if( p_priv->pf_destructor )
328 p_priv->pf_destructor( p_this );
330 /* Destroy the associated variables, starting from the end so that
331 * no memmove calls have to be done. */
332 while( p_priv->i_vars )
334 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
337 free( p_priv->p_vars );
338 vlc_mutex_destroy( &p_priv->var_lock );
340 free( p_this->psz_header );
342 if( p_this->p_libvlc == NULL )
344 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
347 assert( p_global == vlc_global() );
349 if (p_priv->next != p_this)
351 vlc_object_t *leaked = p_priv->next, *first = leaked;
354 /* We are leaking this object */
356 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
357 leaked->i_object_id, leaked->psz_object_type,
358 leaked->psz_object_name );
359 /* Dump libvlc object to ease debugging */
360 vlc_object_dump( leaked );
362 leaked = vlc_internals (leaked)->next;
364 while (leaked != first);
366 /* Dump global object to ease debugging */
367 vlc_object_dump( p_this );
368 /* Strongly abort, cause we want these to be fixed */
373 /* We are the global object ... no need to lock. */
374 vlc_mutex_destroy( &structure_lock );
377 FREENULL( p_this->psz_object_name );
379 #if defined(WIN32) || defined(UNDER_CE)
380 /* if object has an associated thread, close it now */
381 if( p_priv->thread_id )
382 CloseHandle(p_priv->thread_id);
385 vlc_spin_destroy( &p_priv->ref_spin );
386 vlc_mutex_destroy( &p_this->object_lock );
387 vlc_cond_destroy( &p_this->object_wait );
388 vlc_spin_destroy( &p_priv->spin );
389 if( p_priv->pipes[1] != -1 )
390 close( p_priv->pipes[1] );
391 if( p_priv->pipes[0] != -1 )
392 close( p_priv->pipes[0] );
398 /** Inter-object signaling */
400 void __vlc_object_lock( vlc_object_t *obj )
402 vlc_mutex_lock( &obj->object_lock );
405 void __vlc_object_unlock( vlc_object_t *obj )
407 vlc_assert_locked( &obj->object_lock );
408 vlc_mutex_unlock( &obj->object_lock );
412 # include <winsock2.h>
413 # include <ws2tcpip.h>
416 * select()-able pipes emulated using Winsock
418 static int pipe (int fd[2])
421 int addrlen = sizeof (addr);
423 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
424 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
425 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
428 memset (&addr, 0, sizeof (addr));
429 addr.sin_family = AF_INET;
430 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
431 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
432 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
434 || connect (c, (PSOCKADDR)&addr, addrlen))
437 a = accept (l, NULL, NULL);
438 if (a == INVALID_SOCKET)
449 if (l != INVALID_SOCKET)
451 if (c != INVALID_SOCKET)
457 #define read( a, b, c ) recv (a, b, c, 0)
459 #define write( a, b, c ) send (a, b, c, 0)
461 #define close( a ) closesocket (a)
465 * Returns the readable end of a pipe that becomes readable once termination
466 * of the object is requested (vlc_object_kill()).
467 * This can be used to wake-up out of a select() or poll() event loop, such
468 * typically when doing network I/O.
470 * Note that the pipe will remain the same for the lifetime of the object.
471 * DO NOT read the pipe nor close it yourself. Ever.
473 * @param obj object that would be "killed"
474 * @return a readable pipe descriptor, or -1 on error.
476 int __vlc_object_waitpipe( vlc_object_t *obj )
478 int pfd[2] = { -1, -1 };
479 vlc_object_internals_t *internals = vlc_internals( obj );
482 vlc_spin_lock (&internals->spin);
483 if (internals->pipes[0] == -1)
485 /* This can only ever happen if someone killed us without locking: */
486 assert (internals->pipes[1] == -1);
487 vlc_spin_unlock (&internals->spin);
492 vlc_spin_lock (&internals->spin);
493 if (internals->pipes[0] == -1)
495 internals->pipes[0] = pfd[0];
496 internals->pipes[1] = pfd[1];
497 pfd[0] = pfd[1] = -1;
501 vlc_spin_unlock (&internals->spin);
505 /* Race condition: vlc_object_kill() already invoked! */
508 vlc_spin_lock (&internals->spin);
509 fd = internals->pipes[1];
510 internals->pipes[1] = -1;
511 vlc_spin_unlock (&internals->spin);
513 msg_Dbg (obj, "waitpipe: object already dying");
518 /* Race condition: two threads call pipe() - unlikely */
524 return internals->pipes[0];
529 * Waits for the object to be signaled (using vlc_object_signal()).
530 * It is assumed that the caller has locked the object. This function will
531 * unlock the object, and lock it again before returning.
532 * If the object was signaled before the caller locked the object, it is
533 * undefined whether the signal will be lost or will wake the process.
535 * @return true if the object is dying and should terminate.
537 void __vlc_object_wait( vlc_object_t *obj )
539 vlc_assert_locked( &obj->object_lock );
540 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
545 * Waits for the object to be signaled (using vlc_object_signal()), or for
546 * a timer to expire. It is asserted that the caller holds the object lock.
548 * @return negative if the object is dying and should terminate,
549 * positive if the the object has been signaled but is not dying,
550 * 0 if timeout has been reached.
552 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
556 vlc_assert_locked( &obj->object_lock );
557 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
558 if( v == 0 ) /* signaled */
559 return obj->b_die ? -1 : 1;
565 * Checks whether an object has been "killed".
566 * The object lock must be held.
568 * Typical code for an object thread could be:
570 vlc_object_lock (self);
572 while (vlc_object_alive (self))
576 vlc_object_wait (self);
580 ...deinitialization...
581 vlc_object_unlock (self);
584 * @return true iff the object has not been killed yet
586 bool __vlc_object_alive( vlc_object_t *obj )
588 vlc_assert_locked( &obj->object_lock );
594 * Signals an object for which the lock is held.
595 * At least one thread currently sleeping in vlc_object_wait() or
596 * vlc_object_timedwait() will wake up, assuming that there is at least one
597 * such thread in the first place. Otherwise, it is undefined whether the
598 * signal will be lost or will wake up one or more thread later.
600 void __vlc_object_signal_unlocked( vlc_object_t *obj )
602 vlc_assert_locked (&obj->object_lock);
603 vlc_cond_signal( &obj->object_wait );
608 * Requests termination of an object.
609 * If the object is LibVLC, also request to terminate all its children.
611 void __vlc_object_kill( vlc_object_t *p_this )
613 vlc_object_internals_t *internals = vlc_internals( p_this );
616 vlc_mutex_lock( &p_this->object_lock );
617 p_this->b_die = true;
619 vlc_spin_lock (&internals->spin);
620 fd = internals->pipes[1];
621 internals->pipes[1] = -1;
622 vlc_spin_unlock (&internals->spin);
626 msg_Dbg (p_this, "waitpipe: object killed");
630 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
631 for( int i = 0; i < internals->i_children ; i++ )
632 vlc_object_kill( internals->pp_children[i] );
634 vlc_object_signal_unlocked( p_this );
635 vlc_mutex_unlock( &p_this->object_lock );
640 * Find an object given its ID.
642 * This function looks for the object whose i_object_id field is i_id.
643 * This function is slow, and often used to hide bugs. Do not use it.
644 * If you need to retain reference to an object, yield the object pointer with
645 * vlc_object_yield(), use the pointer as your reference, and call
646 * vlc_object_release() when you're done.
648 void * vlc_object_get( int i_id )
650 libvlc_global_data_t *p_libvlc_global = vlc_global();
651 vlc_object_t *obj = NULL;
652 vlc_object_t *caller = vlc_threadobj ();
655 msg_Dbg (caller, "uses deprecated vlc_object_get(%d)", i_id);
657 fprintf (stderr, "main thread uses deprecated vlc_object_get(%d)\n",
660 vlc_mutex_lock( &structure_lock );
662 for( obj = vlc_internals (p_libvlc_global)->next;
663 obj != VLC_OBJECT (p_libvlc_global);
664 obj = vlc_internals (obj)->next )
666 if( obj->i_object_id == i_id )
668 vlc_object_yield( obj );
674 msg_Warn (caller, "wants non-existing object %d", i_id);
676 fprintf (stderr, "main thread wants non-existing object %d\n", i_id);
678 vlc_mutex_unlock( &structure_lock );
683 ****************************************************************************
684 * find a typed object and increment its refcount
685 *****************************************************************************
686 * This function recursively looks for a given object type. i_mode can be one
687 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
688 *****************************************************************************/
689 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
691 vlc_object_t *p_found;
693 /* If we are of the requested type ourselves, don't look further */
694 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
696 vlc_object_yield( p_this );
700 /* Otherwise, recursively look for the object */
701 if ((i_mode & 0x000f) == FIND_ANYWHERE)
704 if (i_type == VLC_OBJECT_PLAYLIST)
705 msg_Warn (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
706 "instead of pl_Yield()");
708 return vlc_object_find (p_this->p_libvlc, i_type,
709 (i_mode & ~0x000f)|FIND_CHILD);
712 vlc_mutex_lock( &structure_lock );
713 p_found = FindObject( p_this, i_type, i_mode );
714 vlc_mutex_unlock( &structure_lock );
719 ****************************************************************************
720 * find a named object and increment its refcount
721 *****************************************************************************
722 * This function recursively looks for a given object name. i_mode can be one
723 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
724 *****************************************************************************/
725 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
728 vlc_object_t *p_found;
730 /* If have the requested name ourselves, don't look further */
731 if( !(i_mode & FIND_STRICT)
732 && p_this->psz_object_name
733 && !strcmp( p_this->psz_object_name, psz_name ) )
735 vlc_object_yield( p_this );
739 vlc_mutex_lock( &structure_lock );
741 /* Otherwise, recursively look for the object */
742 if( (i_mode & 0x000f) == FIND_ANYWHERE )
744 vlc_object_t *p_root = p_this;
747 while( p_root->p_parent != NULL &&
748 p_root != VLC_OBJECT( p_this->p_libvlc ) )
750 p_root = p_root->p_parent;
753 p_found = FindObjectName( p_root, psz_name,
754 (i_mode & ~0x000f)|FIND_CHILD );
755 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
757 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
758 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
763 p_found = FindObjectName( p_this, psz_name, i_mode );
766 vlc_mutex_unlock( &structure_lock );
772 * Increment an object reference counter.
774 void __vlc_object_yield( vlc_object_t *p_this )
776 vlc_object_internals_t *internals = vlc_internals( p_this );
778 vlc_spin_lock( &internals->ref_spin );
779 /* Avoid obvious freed object uses */
780 assert( internals->i_refcount > 0 );
781 /* Increment the counter */
782 internals->i_refcount++;
783 vlc_spin_unlock( &internals->ref_spin );
786 /*****************************************************************************
787 * decrement an object refcount
788 * And destroy the object if its refcount reach zero.
789 *****************************************************************************/
790 void __vlc_object_release( vlc_object_t *p_this )
792 vlc_object_internals_t *internals = vlc_internals( p_this );
793 bool b_should_destroy;
795 vlc_spin_lock( &internals->ref_spin );
796 assert( internals->i_refcount > 0 );
798 if( internals->i_refcount > 1 )
801 /* There are still other references to the object */
802 internals->i_refcount--;
803 vlc_spin_unlock( &internals->ref_spin );
806 vlc_spin_unlock( &internals->ref_spin );
809 /* Remember that we cannot hold the spin while waiting on the mutex */
810 vlc_mutex_lock( &structure_lock );
811 /* Take the spin again. Note that another thread may have yielded the
812 * object in the (very short) mean time. */
813 vlc_spin_lock( &internals->ref_spin );
814 b_should_destroy = --internals->i_refcount == 0;
815 vlc_spin_unlock( &internals->ref_spin );
817 if( b_should_destroy )
819 /* Remove the object from object list
820 * so that it cannot be encountered by vlc_object_get() */
821 vlc_internals (internals->next)->prev = internals->prev;
822 vlc_internals (internals->prev)->next = internals->next;
824 /* Detach from parent to protect against FIND_CHILDREN */
825 vlc_object_detach_unlocked (p_this);
826 /* Detach from children to protect against FIND_PARENT */
827 for (int i = 0; i < internals->i_children; i++)
828 internals->pp_children[i]->p_parent = NULL;
831 vlc_mutex_unlock( &structure_lock );
833 if( b_should_destroy )
835 free( internals->pp_children );
836 internals->pp_children = NULL;
837 internals->i_children = 0;
838 vlc_object_destroy( p_this );
843 ****************************************************************************
844 * attach object to a parent object
845 *****************************************************************************
846 * This function sets p_this as a child of p_parent, and p_parent as a parent
847 * of p_this. This link can be undone using vlc_object_detach.
848 *****************************************************************************/
849 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
851 if( !p_this ) return;
853 vlc_mutex_lock( &structure_lock );
855 /* Attach the parent to its child */
856 assert (!p_this->p_parent);
857 p_this->p_parent = p_parent;
859 /* Attach the child to its parent */
860 vlc_object_internals_t *priv = vlc_internals( p_parent );
861 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
864 /* Kill the object if parent is already dead.
865 * Note: We should surely lock parent here, but that would
866 * create quite a few dead lock case. Hopefully, it
867 * is perfectly safe to do it that way. We only risk
868 * receiving kill event twice. But given current API
870 if( p_this->p_parent->b_die )
871 vlc_object_kill( p_this );
873 vlc_mutex_unlock( &structure_lock );
877 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
879 vlc_assert_locked (&structure_lock);
881 if (p_this->p_parent == NULL)
884 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
888 /* Remove p_this's parent */
889 p_this->p_parent = NULL;
891 /* Remove all of p_parent's children which are p_this */
892 for( i_index = priv->i_children ; i_index-- ; )
894 if( priv->pp_children[i_index] == p_this )
897 for( i = i_index ; i < priv->i_children ; i++ )
898 priv->pp_children[i] = priv->pp_children[i+1];
902 if( priv->i_children )
904 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
905 priv->i_children * sizeof(vlc_object_t *) );
909 /* Special case - don't realloc() to zero to avoid leaking */
910 free( priv->pp_children );
911 priv->pp_children = NULL;
917 ****************************************************************************
918 * detach object from its parent
919 *****************************************************************************
920 * This function removes all links between an object and its parent.
921 *****************************************************************************/
922 void __vlc_object_detach( vlc_object_t *p_this )
924 if( !p_this ) return;
926 vlc_mutex_lock( &structure_lock );
927 if( !p_this->p_parent )
928 msg_Err( p_this, "object is not attached" );
930 vlc_object_detach_unlocked( p_this );
931 vlc_mutex_unlock( &structure_lock );
936 ****************************************************************************
937 * find a list typed objects and increment their refcount
938 *****************************************************************************
939 * This function recursively looks for a given object type. i_mode can be one
940 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
941 *****************************************************************************/
942 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
947 /* Look for the objects */
948 switch( i_mode & 0x000f )
951 /* Modules should probably not be object, and the module should perhaps
952 * not be shared across LibVLC instances. In the mean time, this ugly
953 * hack is brought to you by Courmisch. */
954 if (i_type == VLC_OBJECT_MODULE)
955 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
957 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
960 vlc_mutex_lock( &structure_lock );
961 i_count = CountChildren( p_this, i_type );
962 p_list = NewList( i_count );
964 /* Check allocation was successful */
965 if( p_list->i_count != i_count )
967 vlc_mutex_unlock( &structure_lock );
968 msg_Err( p_this, "list allocation failed!" );
974 ListChildren( p_list, p_this, i_type );
975 vlc_mutex_unlock( &structure_lock );
979 msg_Err( p_this, "unimplemented!" );
980 p_list = NewList( 0 );
988 * Gets the list of children of an objects, and increment their reference
990 * @return a list (possibly empty) or NULL in case of error.
992 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
995 vlc_object_internals_t *priv = vlc_internals( obj );
997 vlc_mutex_lock( &structure_lock );
998 l = NewList( priv->i_children );
999 for (int i = 0; i < l->i_count; i++)
1001 vlc_object_yield( priv->pp_children[i] );
1002 l->p_values[i].p_object = priv->pp_children[i];
1004 vlc_mutex_unlock( &structure_lock );
1008 /*****************************************************************************
1009 * DumpCommand: print the current vlc structure
1010 *****************************************************************************
1011 * This function prints either an ASCII tree showing the connections between
1012 * vlc objects, and additional information such as their refcount, thread ID,
1013 * etc. (command "tree"), or the same data as a simple list (command "list").
1014 *****************************************************************************/
1015 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1016 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1018 (void)oldval; (void)p_data;
1019 if( *psz_cmd == 'l' )
1021 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
1023 vlc_mutex_lock( &structure_lock );
1026 PrintObject (cur, "");
1027 cur = vlc_internals (cur)->next;
1029 while (cur != root);
1030 vlc_mutex_unlock( &structure_lock );
1034 vlc_object_t *p_object = NULL;
1036 if( *newval.psz_string )
1039 int i_id = strtol( newval.psz_string, &end, 0 );
1040 if( end != newval.psz_string )
1041 p_object = vlc_object_get( i_id );
1043 /* try using the object's name to find it */
1044 p_object = vlc_object_find_name( p_this, newval.psz_string,
1053 vlc_mutex_lock( &structure_lock );
1055 if( *psz_cmd == 't' )
1057 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1060 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1063 DumpStructure( p_object, 0, psz_foo );
1065 else if( *psz_cmd == 'v' )
1070 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1072 PrintObject( p_object, "" );
1074 if( !vlc_internals( p_object )->i_vars )
1075 printf( " `-o No variables\n" );
1076 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1078 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1080 const char *psz_type = "unknown";
1081 switch( p_var->i_type & VLC_VAR_TYPE )
1083 #define MYCASE( type, nice ) \
1084 case VLC_VAR_ ## type: \
1087 MYCASE( VOID, "void" );
1088 MYCASE( BOOL, "bool" );
1089 MYCASE( INTEGER, "integer" );
1090 MYCASE( HOTKEY, "hotkey" );
1091 MYCASE( STRING, "string" );
1092 MYCASE( MODULE, "module" );
1093 MYCASE( FILE, "file" );
1094 MYCASE( DIRECTORY, "directory" );
1095 MYCASE( VARIABLE, "variable" );
1096 MYCASE( FLOAT, "float" );
1097 MYCASE( TIME, "time" );
1098 MYCASE( ADDRESS, "address" );
1099 MYCASE( MUTEX, "mutex" );
1100 MYCASE( LIST, "list" );
1103 printf( " %c-o \"%s\" (%s",
1104 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1105 p_var->psz_name, psz_type );
1106 if( p_var->psz_text )
1107 printf( ", %s", p_var->psz_text );
1109 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1110 printf( ", command" );
1111 if( p_var->i_entries )
1112 printf( ", %d callbacks", p_var->i_entries );
1113 switch( p_var->i_type & 0x00f0 )
1119 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1121 case VLC_VAR_INTEGER:
1122 printf( ": %d", p_var->val.i_int );
1124 case VLC_VAR_STRING:
1125 printf( ": \"%s\"", p_var->val.psz_string );
1128 printf( ": %f", p_var->val.f_float );
1131 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1133 case VLC_VAR_ADDRESS:
1134 printf( ": %p", p_var->val.p_address );
1144 vlc_mutex_unlock( &structure_lock );
1146 if( *newval.psz_string )
1148 vlc_object_release( p_object );
1155 /*****************************************************************************
1156 * vlc_list_release: free a list previously allocated by vlc_list_find
1157 *****************************************************************************
1158 * This function decreases the refcount of all objects in the list and
1160 *****************************************************************************/
1161 void vlc_list_release( vlc_list_t *p_list )
1165 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1167 vlc_object_release( p_list->p_values[i_index].p_object );
1170 free( p_list->p_values );
1174 /*****************************************************************************
1175 * dump an object. (Debug function)
1176 *****************************************************************************/
1177 void __vlc_object_dump( vlc_object_t *p_this )
1179 vlc_mutex_lock( &structure_lock );
1180 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1182 DumpStructure( p_this, 0, psz_foo );
1183 vlc_mutex_unlock( &structure_lock );
1186 /* Following functions are local */
1188 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1191 vlc_object_t *p_tmp;
1193 switch( i_mode & 0x000f )
1196 p_tmp = p_this->p_parent;
1199 if( p_tmp->i_object_type == i_type )
1201 vlc_object_yield( p_tmp );
1206 return FindObject( p_tmp, i_type, i_mode );
1212 for( i = vlc_internals( p_this )->i_children; i--; )
1214 p_tmp = vlc_internals( p_this )->pp_children[i];
1215 if( p_tmp->i_object_type == i_type )
1217 vlc_object_yield( p_tmp );
1220 else if( vlc_internals( p_tmp )->i_children )
1222 p_tmp = FindObject( p_tmp, i_type, i_mode );
1232 /* Handled in vlc_object_find */
1239 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1240 const char *psz_name,
1244 vlc_object_t *p_tmp;
1246 switch( i_mode & 0x000f )
1249 p_tmp = p_this->p_parent;
1252 if( p_tmp->psz_object_name
1253 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1255 vlc_object_yield( p_tmp );
1260 return FindObjectName( p_tmp, psz_name, i_mode );
1266 for( i = vlc_internals( p_this )->i_children; i--; )
1268 p_tmp = vlc_internals( p_this )->pp_children[i];
1269 if( p_tmp->psz_object_name
1270 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1272 vlc_object_yield( p_tmp );
1275 else if( vlc_internals( p_tmp )->i_children )
1277 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1287 /* Handled in vlc_object_find */
1295 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1297 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1300 memset( &psz_name, 0, sizeof(psz_name) );
1301 if( p_this->psz_object_name )
1303 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1305 psz_name[48] = '\"';
1308 psz_children[0] = '\0';
1309 switch( vlc_internals( p_this )->i_children )
1314 strcpy( psz_children, ", 1 child" );
1317 snprintf( psz_children, 19, ", %i children",
1318 vlc_internals( p_this )->i_children );
1322 psz_refcount[0] = '\0';
1323 if( vlc_internals( p_this )->i_refcount > 0 )
1324 snprintf( psz_refcount, 19, ", refcount %u",
1325 vlc_internals( p_this )->i_refcount );
1327 psz_thread[0] = '\0';
1328 if( vlc_internals( p_this )->b_thread )
1329 snprintf( psz_thread, 29, " (thread %lu)",
1330 (unsigned long)vlc_internals( p_this )->thread_id );
1332 psz_parent[0] = '\0';
1333 if( p_this->p_parent )
1334 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1336 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1337 p_this->i_object_id, p_this->psz_object_type,
1338 psz_name, psz_thread, psz_refcount, psz_children,
1342 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1345 char i_back = psz_foo[i_level];
1346 psz_foo[i_level] = '\0';
1348 PrintObject( p_this, psz_foo );
1350 psz_foo[i_level] = i_back;
1352 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1354 msg_Warn( p_this, "structure tree is too deep" );
1358 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1362 psz_foo[i_level-1] = ' ';
1364 if( psz_foo[i_level-2] == '`' )
1366 psz_foo[i_level-2] = ' ';
1370 if( i == vlc_internals( p_this )->i_children - 1 )
1372 psz_foo[i_level] = '`';
1376 psz_foo[i_level] = '|';
1379 psz_foo[i_level+1] = '-';
1380 psz_foo[i_level+2] = '\0';
1382 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1387 static vlc_list_t * NewList( int i_count )
1389 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1390 if( p_list == NULL )
1395 p_list->i_count = i_count;
1399 p_list->p_values = NULL;
1403 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1404 if( p_list->p_values == NULL )
1406 p_list->i_count = 0;
1413 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1416 if( p_list == NULL || i_index >= p_list->i_count )
1421 vlc_object_yield( p_object );
1423 p_list->p_values[i_index].p_object = p_object;
1428 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1430 if( p_list == NULL )
1435 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1436 * sizeof( vlc_value_t ) );
1437 if( p_list->p_values == NULL )
1439 p_list->i_count = 0;
1443 vlc_object_yield( p_object );
1445 p_list->p_values[p_list->i_count].p_object = p_object;
1451 static int CountChildren( vlc_object_t *p_this, int i_type )
1453 vlc_object_t *p_tmp;
1456 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1458 p_tmp = vlc_internals( p_this )->pp_children[i];
1460 if( p_tmp->i_object_type == i_type )
1464 i_count += CountChildren( p_tmp, i_type );
1470 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1472 vlc_object_t *p_tmp;
1475 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1477 p_tmp = vlc_internals( p_this )->pp_children[i];
1479 if( p_tmp->i_object_type == i_type )
1480 ListReplace( p_list, p_tmp, p_list->i_count++ );
1482 ListChildren( p_list, p_tmp, i_type );
1487 # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
1488 # include <execinfo.h>
1491 void vlc_refcheck (vlc_object_t *obj)
1493 static unsigned errors = 0;
1497 /* Anyone can use the root object (though it should not exist) */
1498 if (obj == VLC_OBJECT (vlc_global ()))
1501 /* Anyone can use its libvlc instance object */
1502 if (obj == VLC_OBJECT (obj->p_libvlc))
1505 /* The thread that created the object holds the initial reference */
1506 vlc_object_internals_t *priv = vlc_internals (obj);
1507 #if defined (LIBVLC_USE_PTHREAD)
1508 if (pthread_equal (priv->creator_id, pthread_self ()))
1510 if (priv->creator_id == GetCurrentThreadId ())
1516 /* A thread can use its own object without reference! */
1517 vlc_object_t *caller = vlc_threadobj ();
1521 /* The calling thread is younger than the object.
1522 * Access could be valid through cross-thread synchronization;
1523 * we would need better accounting. */
1524 if (caller && (caller->i_object_id > obj->i_object_id))
1528 vlc_spin_lock (&priv->ref_spin);
1529 refs = priv->i_refcount;
1530 vlc_spin_unlock (&priv->ref_spin);
1532 /* Object has more than one reference.
1533 * The current thread could be holding a valid reference. */
1537 fprintf (stderr, "The %s %s thread object is accessing...\n"
1538 "the %s %s object in a suspicous manner.\n",
1539 caller && caller->psz_object_name
1540 ? caller->psz_object_name : "unnamed",
1541 caller ? caller->psz_object_type : "main",
1542 obj->psz_object_name ? obj->psz_object_name : "unnamed",
1543 obj->psz_object_type);
1546 #ifdef HAVE_BACKTRACE
1548 int stackdepth = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
1549 backtrace_symbols_fd (stack, stackdepth, 2);
1552 if (++errors == 100)
1553 fprintf (stderr, "Too many reference errors!\n");