1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 VLC authors and VideoLAN
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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
58 # include <unistd.h> // close(), write()
61 # include <winsock2.h>
62 # include <ws2tcpip.h>
64 # define read( a, b, c ) recv (a, b, c, 0)
66 # define write( a, b, c ) send (a, b, c, 0)
68 # define close( a ) closesocket (a)
77 #if defined (HAVE_SYS_EVENTFD_H)
78 # include <sys/eventfd.h>
80 # define EFD_CLOEXEC 0
81 # warning EFD_CLOEXEC missing. Consider updating libc.
86 /*****************************************************************************
88 *****************************************************************************/
89 static int DumpCommand( vlc_object_t *, char const *,
90 vlc_value_t, vlc_value_t, void * );
92 static vlc_object_t * FindName ( vlc_object_internals_t *, const char * );
93 static void PrintObject( vlc_object_internals_t *, const char * );
94 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
96 static vlc_list_t * NewList ( int );
98 static void vlc_object_destroy( vlc_object_t *p_this );
100 /*****************************************************************************
101 * Local structure lock
102 *****************************************************************************/
103 static void libvlc_lock (libvlc_int_t *p_libvlc)
105 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
108 static void libvlc_unlock (libvlc_int_t *p_libvlc)
110 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
113 #undef vlc_custom_create
114 void *vlc_custom_create (vlc_object_t *parent, size_t length,
115 const char *typename)
118 * VLC objects are laid out as follow:
119 * - first the LibVLC-private per-object data,
120 * - then VLC_COMMON members from vlc_object_t,
121 * - finally, the type-specific data (if any).
123 * This function initializes the LibVLC and common data,
124 * and zeroes the rest.
126 assert (length >= sizeof (vlc_object_t));
128 vlc_object_internals_t *priv = malloc (sizeof (*priv) + length);
129 if (unlikely(priv == NULL))
131 priv->psz_name = NULL;
132 priv->var_root = NULL;
133 vlc_mutex_init (&priv->var_lock);
134 vlc_cond_init (&priv->var_wait);
135 priv->pipes[0] = priv->pipes[1] = -1;
136 atomic_init (&priv->alive, true);
137 atomic_init (&priv->refs, 1);
138 priv->pf_destructor = NULL;
142 vlc_object_t *obj = (vlc_object_t *)(priv + 1);
143 obj->psz_object_type = typename;
144 obj->psz_header = NULL;
145 obj->b_force = false;
146 memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
148 if (likely(parent != NULL))
150 vlc_object_internals_t *papriv = vlc_internals (parent);
152 obj->i_flags = parent->i_flags;
153 obj->p_libvlc = parent->p_libvlc;
155 /* Attach the child to its parent (no lock needed) */
156 obj->p_parent = vlc_object_hold (parent);
158 /* Attach the parent to its child (structure lock needed) */
159 libvlc_lock (obj->p_libvlc);
160 priv->next = papriv->first;
161 if (priv->next != NULL)
162 priv->next->prev = priv;
163 papriv->first = priv;
164 libvlc_unlock (obj->p_libvlc);
168 libvlc_int_t *self = (libvlc_int_t *)obj;
171 obj->p_libvlc = self;
172 obj->p_parent = NULL;
174 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
176 /* TODO: should be in src/libvlc.c */
177 int canc = vlc_savecancel ();
178 var_Create (obj, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
179 var_AddCallback (obj, "tree", DumpCommand, obj);
180 var_Create (obj, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
181 var_AddCallback (obj, "vars", DumpCommand, obj);
182 vlc_restorecancel (canc);
188 #undef vlc_object_create
190 * Allocates and initializes a vlc object.
192 * @param i_size object byte size
194 * @return the new object, or NULL on error.
196 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
198 return vlc_custom_create( p_this, i_size, "generic" );
201 #undef vlc_object_set_destructor
203 ****************************************************************************
204 * Set the destructor of a vlc object
206 * This function sets the destructor of the vlc object. It will be called
207 * when the object is destroyed when the its refcount reaches 0.
208 * (It is called by the internal function vlc_object_destroy())
209 *****************************************************************************/
210 void vlc_object_set_destructor( vlc_object_t *p_this,
211 vlc_destructor_t pf_destructor )
213 vlc_object_internals_t *p_priv = vlc_internals(p_this );
215 p_priv->pf_destructor = pf_destructor;
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 if (unlikely(p_this == VLC_OBJECT(p_this->p_libvlc)))
264 /* TODO: should be in src/libvlc.c */
265 var_DelCallback (p_this, "tree", DumpCommand, p_this);
266 var_DelCallback (p_this, "vars", DumpCommand, p_this);
269 /* Destroy the associated variables. */
270 var_DestroyAll( p_this );
272 vlc_cond_destroy( &p_priv->var_wait );
273 vlc_mutex_destroy( &p_priv->var_lock );
275 free( p_this->psz_header );
277 free( p_priv->psz_name );
279 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
280 close( p_priv->pipes[1] );
281 if( p_priv->pipes[0] != -1 )
282 close( p_priv->pipes[0] );
283 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
284 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
290 #if defined(WIN32) || defined(__OS2__)
292 * select()-able pipes emulated using Winsock
294 # define vlc_pipe selectable_pipe
295 static int selectable_pipe (int fd[2])
297 struct sockaddr_in addr;
298 int addrlen = sizeof (addr);
300 int l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP),
301 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
302 if (l == -1 || c == -1)
305 memset (&addr, 0, sizeof (addr));
306 addr.sin_family = AF_INET;
307 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
308 if (bind (l, (struct sockaddr *)&addr, sizeof (addr))
309 || getsockname (l, (struct sockaddr *)&addr, &addrlen)
311 || connect (c, (struct sockaddr *)&addr, addrlen))
314 int a = accept (l, NULL, NULL);
332 #endif /* WIN32 || __OS2__ */
334 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
337 * Returns the readable end of a pipe that becomes readable once termination
338 * of the object is requested (vlc_object_kill()).
339 * This can be used to wake-up out of a select() or poll() event loop, such
340 * typically when doing network I/O.
342 * Note that the pipe will remain the same for the lifetime of the object.
343 * DO NOT read the pipe nor close it yourself. Ever.
345 * @param obj object that would be "killed"
346 * @return a readable pipe descriptor, or -1 on error.
348 int vlc_object_waitpipe( vlc_object_t *obj )
350 vlc_object_internals_t *internals = vlc_internals( obj );
352 vlc_mutex_lock (&pipe_lock);
353 if (internals->pipes[0] == -1)
355 /* This can only ever happen if someone killed us without locking: */
356 assert (internals->pipes[1] == -1);
358 /* pipe() is not a cancellation point, but write() is and eventfd() is
359 * unspecified (not in POSIX). */
360 int canc = vlc_savecancel ();
361 #if defined (HAVE_SYS_EVENTFD_H)
362 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
363 if (internals->pipes[0] == -1)
366 if (vlc_pipe (internals->pipes))
367 internals->pipes[0] = internals->pipes[1] = -1;
370 if (internals->pipes[0] != -1 && !atomic_load (&internals->alive))
371 { /* Race condition: vlc_object_kill() already invoked! */
372 msg_Dbg (obj, "waitpipe: object already dying");
373 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
375 vlc_restorecancel (canc);
377 vlc_mutex_unlock (&pipe_lock);
378 return internals->pipes[0];
381 #undef vlc_object_kill
383 * Requests termination of an object, cancels the object thread, and make the
384 * object wait pipe (if it exists) readable. Not a cancellation point.
386 void vlc_object_kill( vlc_object_t *p_this )
388 vlc_object_internals_t *priv = vlc_internals( p_this );
391 if (atomic_exchange (&priv->alive, false))
393 vlc_mutex_lock (&pipe_lock);
395 vlc_mutex_unlock (&pipe_lock);
400 int canc = vlc_savecancel ();
402 /* write _after_ setting b_die, so vlc_object_alive() returns false */
403 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
404 msg_Dbg (p_this, "waitpipe: object killed");
405 vlc_restorecancel (canc);
409 #undef vlc_object_find_name
411 * Finds a named object and increment its reference count.
412 * Beware that objects found in this manner can be "owned" by another thread,
413 * be of _any_ type, and be attached to any module (if any). With such an
414 * object reference, you can set or get object variables, emit log messages,
415 * and read write-once object parameters (psz_object_type, etc).
416 * You CANNOT cast the object to a more specific object type, and you
417 * definitely cannot invoke object type-specific callbacks with this.
419 * @param p_this object to search from
420 * @param psz_name name of the object to search for
422 * @return a matching object (must be released by the caller),
425 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
427 vlc_object_t *p_found;
429 /* Reading psz_object_name from a separate inhibits thread-safety.
430 * Use a libvlc address variable instead for that sort of things! */
431 msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
433 libvlc_lock (p_this->p_libvlc);
434 vlc_mutex_lock (&name_lock);
435 p_found = FindName (vlc_internals (p_this), psz_name);
436 vlc_mutex_unlock (&name_lock);
437 libvlc_unlock (p_this->p_libvlc);
441 #undef vlc_object_hold
443 * Increment an object reference counter.
445 void * vlc_object_hold( vlc_object_t *p_this )
447 vlc_object_internals_t *internals = vlc_internals( p_this );
448 unsigned refs = atomic_fetch_add (&internals->refs, 1);
450 assert (refs > 0); /* Avoid obvious freed object uses */ (void) refs;
454 #undef vlc_object_release
456 * Drops a reference to an object (decrements the reference count).
457 * If the count reaches zero, the object is destroyed.
459 void vlc_object_release( vlc_object_t *p_this )
461 vlc_object_internals_t *internals = vlc_internals( p_this );
462 vlc_object_t *parent = NULL;
463 unsigned refs = atomic_load (&internals->refs);
468 if (atomic_compare_exchange_weak (&internals->refs, &refs, refs - 1))
469 return; /* There are still other references to the object */
475 libvlc_lock (p_this->p_libvlc);
476 refs = atomic_fetch_sub (&internals->refs, 1);
479 if (likely(refs == 1))
481 /* Detach from parent to protect against vlc_object_find_name() */
482 parent = p_this->p_parent;
486 vlc_object_internals_t *prev = internals->prev;
487 vlc_object_internals_t *next = internals->next;
492 vlc_internals (parent)->first = next;
497 /* We have no children */
498 assert (internals->first == NULL);
500 libvlc_unlock (p_this->p_libvlc);
502 if (likely(refs == 1))
504 int canc = vlc_savecancel ();
505 vlc_object_destroy( p_this );
506 vlc_restorecancel (canc);
508 vlc_object_release (parent);
512 #undef vlc_object_alive
514 * This function returns true, except when it returns false.
515 * \warning Do not use this function. Ever. You were warned.
517 bool vlc_object_alive(vlc_object_t *obj)
519 vlc_object_internals_t *internals = vlc_internals (obj);
520 return atomic_load (&internals->alive);
523 #undef vlc_list_children
525 * Gets the list of children of an objects, and increment their reference
527 * @return a list (possibly empty) or NULL in case of error.
529 vlc_list_t *vlc_list_children( vlc_object_t *obj )
532 vlc_object_internals_t *priv;
535 libvlc_lock (obj->p_libvlc);
536 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
539 if (likely(l != NULL))
543 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
544 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
546 libvlc_unlock (obj->p_libvlc);
550 static void DumpVariable (const void *data, const VISIT which, const int depth)
552 if (which != postorder && which != leaf)
556 const variable_t *p_var = *(const variable_t **)data;
557 const char *psz_type = "unknown";
559 switch( p_var->i_type & VLC_VAR_TYPE )
561 #define MYCASE( type, nice ) \
562 case VLC_VAR_ ## type: \
565 MYCASE( VOID, "void" );
566 MYCASE( BOOL, "bool" );
567 MYCASE( INTEGER, "integer" );
568 MYCASE( HOTKEY, "hotkey" );
569 MYCASE( STRING, "string" );
570 MYCASE( VARIABLE, "variable" );
571 MYCASE( FLOAT, "float" );
572 MYCASE( TIME, "time" );
573 MYCASE( COORDS, "coords" );
574 MYCASE( ADDRESS, "address" );
577 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
578 if( p_var->psz_text )
579 printf( ", %s", p_var->psz_text );
580 fputc( ')', stdout );
581 if( p_var->i_type & VLC_VAR_HASCHOICE )
582 fputs( ", has choices", stdout );
583 if( p_var->i_type & VLC_VAR_ISCOMMAND )
584 fputs( ", command", stdout );
585 if( p_var->i_entries )
586 printf( ", %d callbacks", p_var->i_entries );
587 switch( p_var->i_type & VLC_VAR_CLASS )
592 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
594 case VLC_VAR_INTEGER:
595 printf( ": %"PRId64, p_var->val.i_int );
598 printf( ": \"%s\"", p_var->val.psz_string );
601 printf( ": %f", p_var->val.f_float );
604 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
607 printf( ": %"PRId32"x%"PRId32,
608 p_var->val.coords.x, p_var->val.coords.y );
610 case VLC_VAR_ADDRESS:
611 printf( ": %p", p_var->val.p_address );
614 fputc( '\n', stdout );
617 /*****************************************************************************
618 * DumpCommand: print the current vlc structure
619 *****************************************************************************
620 * This function prints either an ASCII tree showing the connections between
621 * vlc objects, and additional information such as their refcount, thread ID,
622 * etc. (command "tree"), or the same data as a simple list (command "list").
623 *****************************************************************************/
624 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
625 vlc_value_t oldval, vlc_value_t newval, void *p_data )
628 vlc_object_t *p_object = NULL;
630 if( *newval.psz_string )
632 /* try using the object's name to find it */
633 p_object = vlc_object_find_name( p_data, newval.psz_string );
638 libvlc_lock (p_this->p_libvlc);
639 if( *psz_cmd == 't' )
641 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
644 p_object = VLC_OBJECT(p_this->p_libvlc);
647 DumpStructure( vlc_internals(p_object), 0, psz_foo );
649 else if( *psz_cmd == 'v' )
652 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
654 PrintObject( vlc_internals(p_object), "" );
655 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
656 if( vlc_internals( p_object )->var_root == NULL )
657 puts( " `-o No variables" );
659 twalk( vlc_internals( p_object )->var_root, DumpVariable );
660 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
662 libvlc_unlock (p_this->p_libvlc);
664 if( *newval.psz_string )
666 vlc_object_release( p_object );
671 /*****************************************************************************
672 * vlc_list_release: free a list previously allocated by vlc_list_find
673 *****************************************************************************
674 * This function decreases the refcount of all objects in the list and
676 *****************************************************************************/
677 void vlc_list_release( vlc_list_t *p_list )
681 for( i_index = 0; i_index < p_list->i_count; i_index++ )
683 vlc_object_release( p_list->p_values[i_index].p_object );
686 free( p_list->p_values );
690 /* Following functions are local */
692 static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
694 if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
695 return vlc_object_hold (vlc_externals (priv));
697 for (priv = priv->first; priv != NULL; priv = priv->next)
699 vlc_object_t *found = FindName (priv, name);
706 static void PrintObject( vlc_object_internals_t *priv,
707 const char *psz_prefix )
709 char psz_refcount[20], psz_name[50], psz_parent[20];
711 int canc = vlc_savecancel ();
712 memset( &psz_name, 0, sizeof(psz_name) );
714 vlc_mutex_lock (&name_lock);
715 if (priv->psz_name != NULL)
717 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
721 vlc_mutex_unlock (&name_lock);
723 snprintf( psz_refcount, 19, ", %u refs", atomic_load( &priv->refs ) );
725 psz_parent[0] = '\0';
726 /* FIXME: need structure lock!!! */
727 if( vlc_externals(priv)->p_parent )
728 snprintf( psz_parent, 19, ", parent %p",
729 vlc_externals(priv)->p_parent );
731 printf( " %so %p %s%s%s%s\n", psz_prefix,
732 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
733 psz_name, psz_refcount, psz_parent );
734 vlc_restorecancel (canc);
737 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
740 char i_back = psz_foo[i_level];
741 psz_foo[i_level] = '\0';
743 PrintObject (priv, psz_foo);
745 psz_foo[i_level] = i_back;
747 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
749 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
753 for (priv = priv->first; priv != NULL; priv = priv->next)
757 psz_foo[i_level-1] = ' ';
759 if( psz_foo[i_level-2] == '`' )
761 psz_foo[i_level-2] = ' ';
765 psz_foo[i_level] = priv->next ? '|' : '`';
766 psz_foo[i_level+1] = '-';
767 psz_foo[i_level+2] = '\0';
769 DumpStructure (priv, i_level + 2, psz_foo);
773 static vlc_list_t * NewList( int i_count )
775 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
779 p_list->i_count = i_count;
783 p_list->p_values = NULL;
787 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
788 if( p_list->p_values == NULL )