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>
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 vlc_spin_init (&priv->ref_spin);
137 priv->i_refcount = 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;
146 obj->b_force = false;
147 memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
149 if (likely(parent != NULL))
151 vlc_object_internals_t *papriv = vlc_internals (parent);
153 obj->i_flags = parent->i_flags;
154 obj->p_libvlc = parent->p_libvlc;
156 /* Attach the child to its parent (no lock needed) */
157 obj->p_parent = vlc_object_hold (parent);
159 /* Attach the parent to its child (structure lock needed) */
160 libvlc_lock (obj->p_libvlc);
161 priv->next = papriv->first;
162 if (priv->next != NULL)
163 priv->next->prev = priv;
164 papriv->first = priv;
165 libvlc_unlock (obj->p_libvlc);
169 libvlc_int_t *self = (libvlc_int_t *)obj;
172 obj->p_libvlc = self;
173 obj->p_parent = NULL;
175 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
177 /* TODO: should be in src/libvlc.c */
178 int canc = vlc_savecancel ();
179 var_Create (obj, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
180 var_AddCallback (obj, "tree", DumpCommand, obj);
181 var_Create (obj, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND);
182 var_AddCallback (obj, "vars", DumpCommand, obj);
183 vlc_restorecancel (canc);
189 #undef vlc_object_create
191 * Allocates and initializes a vlc object.
193 * @param i_size object byte size
195 * @return the new object, or NULL on error.
197 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
199 return vlc_custom_create( p_this, i_size, "generic" );
202 #undef vlc_object_set_destructor
204 ****************************************************************************
205 * Set the destructor of a vlc object
207 * This function sets the destructor of the vlc object. It will be called
208 * when the object is destroyed when the its refcount reaches 0.
209 * (It is called by the internal function vlc_object_destroy())
210 *****************************************************************************/
211 void vlc_object_set_destructor( vlc_object_t *p_this,
212 vlc_destructor_t pf_destructor )
214 vlc_object_internals_t *p_priv = vlc_internals(p_this );
216 vlc_spin_lock( &p_priv->ref_spin );
217 p_priv->pf_destructor = pf_destructor;
218 vlc_spin_unlock( &p_priv->ref_spin );
221 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
223 #undef vlc_object_set_name
224 int vlc_object_set_name(vlc_object_t *obj, const char *name)
226 vlc_object_internals_t *priv = vlc_internals(obj);
227 char *newname = name ? strdup (name) : NULL;
230 vlc_mutex_lock (&name_lock);
231 oldname = priv->psz_name;
232 priv->psz_name = newname;
233 vlc_mutex_unlock (&name_lock);
236 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
239 #undef vlc_object_get_name
240 char *vlc_object_get_name(const vlc_object_t *obj)
242 vlc_object_internals_t *priv = vlc_internals(obj);
245 vlc_mutex_lock (&name_lock);
246 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
247 vlc_mutex_unlock (&name_lock);
253 * Destroys a VLC object once it has no more references.
255 * This function must be called with cancellation disabled (currently).
257 static void vlc_object_destroy( vlc_object_t *p_this )
259 vlc_object_internals_t *p_priv = vlc_internals( p_this );
261 /* Call the custom "subclass" destructor */
262 if( p_priv->pf_destructor )
263 p_priv->pf_destructor( p_this );
265 if (unlikely(p_this == VLC_OBJECT(p_this->p_libvlc)))
267 /* TODO: should be in src/libvlc.c */
268 var_DelCallback (p_this, "tree", DumpCommand, p_this);
269 var_DelCallback (p_this, "vars", DumpCommand, p_this);
272 /* Destroy the associated variables. */
273 var_DestroyAll( p_this );
275 vlc_cond_destroy( &p_priv->var_wait );
276 vlc_mutex_destroy( &p_priv->var_lock );
278 free( p_this->psz_header );
280 free( p_priv->psz_name );
282 vlc_spin_destroy( &p_priv->ref_spin );
283 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
284 close( p_priv->pipes[1] );
285 if( p_priv->pipes[0] != -1 )
286 close( p_priv->pipes[0] );
287 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
288 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
294 #if defined(WIN32) || defined(__OS2__)
296 * select()-able pipes emulated using Winsock
298 # define vlc_pipe selectable_pipe
299 static int selectable_pipe (int fd[2])
301 struct sockaddr_in addr;
302 int addrlen = sizeof (addr);
304 int l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP),
305 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
306 if (l == -1 || c == -1)
309 memset (&addr, 0, sizeof (addr));
310 addr.sin_family = AF_INET;
311 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
312 if (bind (l, (struct sockaddr *)&addr, sizeof (addr))
313 || getsockname (l, (struct sockaddr *)&addr, &addrlen)
315 || connect (c, (struct sockaddr *)&addr, addrlen))
318 int a = accept (l, NULL, NULL);
336 #endif /* WIN32 || __OS2__ */
338 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
341 * Returns the readable end of a pipe that becomes readable once termination
342 * of the object is requested (vlc_object_kill()).
343 * This can be used to wake-up out of a select() or poll() event loop, such
344 * typically when doing network I/O.
346 * Note that the pipe will remain the same for the lifetime of the object.
347 * DO NOT read the pipe nor close it yourself. Ever.
349 * @param obj object that would be "killed"
350 * @return a readable pipe descriptor, or -1 on error.
352 int vlc_object_waitpipe( vlc_object_t *obj )
354 vlc_object_internals_t *internals = vlc_internals( obj );
356 vlc_mutex_lock (&pipe_lock);
357 if (internals->pipes[0] == -1)
359 /* This can only ever happen if someone killed us without locking: */
360 assert (internals->pipes[1] == -1);
362 /* pipe() is not a cancellation point, but write() is and eventfd() is
363 * unspecified (not in POSIX). */
364 int canc = vlc_savecancel ();
365 #if defined (HAVE_SYS_EVENTFD_H)
366 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
367 if (internals->pipes[0] == -1)
370 if (vlc_pipe (internals->pipes))
371 internals->pipes[0] = internals->pipes[1] = -1;
374 if (internals->pipes[0] != -1 && obj->b_die)
375 { /* Race condition: vlc_object_kill() already invoked! */
376 msg_Dbg (obj, "waitpipe: object already dying");
377 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
379 vlc_restorecancel (canc);
381 vlc_mutex_unlock (&pipe_lock);
382 return internals->pipes[0];
385 #undef vlc_object_kill
387 * Requests termination of an object, cancels the object thread, and make the
388 * object wait pipe (if it exists) readable. Not a cancellation point.
390 void vlc_object_kill( vlc_object_t *p_this )
392 vlc_object_internals_t *priv = vlc_internals( p_this );
395 vlc_mutex_lock( &pipe_lock );
399 p_this->b_die = true;
402 /* This also serves as a memory barrier toward vlc_object_alive(): */
403 vlc_mutex_unlock( &pipe_lock );
407 int canc = vlc_savecancel ();
409 /* write _after_ setting b_die, so vlc_object_alive() returns false */
410 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
411 msg_Dbg (p_this, "waitpipe: object killed");
412 vlc_restorecancel (canc);
416 #undef vlc_object_find_name
418 * Finds a named object and increment its reference count.
419 * Beware that objects found in this manner can be "owned" by another thread,
420 * be of _any_ type, and be attached to any module (if any). With such an
421 * object reference, you can set or get object variables, emit log messages,
422 * and read write-once object parameters (psz_object_type, etc).
423 * You CANNOT cast the object to a more specific object type, and you
424 * definitely cannot invoke object type-specific callbacks with this.
426 * @param p_this object to search from
427 * @param psz_name name of the object to search for
429 * @return a matching object (must be released by the caller),
432 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
434 vlc_object_t *p_found;
436 /* Reading psz_object_name from a separate inhibits thread-safety.
437 * Use a libvlc address variable instead for that sort of things! */
438 msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
440 libvlc_lock (p_this->p_libvlc);
441 vlc_mutex_lock (&name_lock);
442 p_found = FindName (vlc_internals (p_this), psz_name);
443 vlc_mutex_unlock (&name_lock);
444 libvlc_unlock (p_this->p_libvlc);
448 #undef vlc_object_hold
450 * Increment an object reference counter.
452 void * vlc_object_hold( vlc_object_t *p_this )
454 vlc_object_internals_t *internals = vlc_internals( p_this );
456 vlc_spin_lock( &internals->ref_spin );
457 /* Avoid obvious freed object uses */
458 assert( internals->i_refcount > 0 );
459 /* Increment the counter */
460 internals->i_refcount++;
461 vlc_spin_unlock( &internals->ref_spin );
465 #undef vlc_object_release
467 * Drops a reference to an object (decrements the reference count).
468 * If the count reaches zero, the object is destroyed.
470 void vlc_object_release( vlc_object_t *p_this )
472 vlc_object_internals_t *internals = vlc_internals( p_this );
473 vlc_object_t *parent = NULL;
474 bool b_should_destroy;
476 vlc_spin_lock( &internals->ref_spin );
477 assert( internals->i_refcount > 0 );
479 if( internals->i_refcount > 1 )
482 /* There are still other references to the object */
483 internals->i_refcount--;
484 vlc_spin_unlock( &internals->ref_spin );
487 vlc_spin_unlock( &internals->ref_spin );
490 /* Remember that we cannot hold the spin while waiting on the mutex */
491 libvlc_lock (p_this->p_libvlc);
492 /* Take the spin again. Note that another thread may have held the
493 * object in the (very short) mean time. */
494 vlc_spin_lock( &internals->ref_spin );
495 b_should_destroy = --internals->i_refcount == 0;
496 vlc_spin_unlock( &internals->ref_spin );
498 if( b_should_destroy )
500 /* Detach from parent to protect against vlc_object_find_name() */
501 parent = p_this->p_parent;
505 if (internals->prev != NULL)
506 internals->prev->next = internals->next;
508 vlc_internals(parent)->first = internals->next;
509 if (internals->next != NULL)
510 internals->next->prev = internals->prev;
513 /* We have no children */
514 assert (internals->first == NULL);
516 libvlc_unlock (p_this->p_libvlc);
518 if( b_should_destroy )
522 canc = vlc_savecancel ();
523 vlc_object_destroy( p_this );
524 vlc_restorecancel (canc);
526 vlc_object_release (parent);
530 #undef vlc_list_children
532 * Gets the list of children of an objects, and increment their reference
534 * @return a list (possibly empty) or NULL in case of error.
536 vlc_list_t *vlc_list_children( vlc_object_t *obj )
539 vlc_object_internals_t *priv;
542 libvlc_lock (obj->p_libvlc);
543 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
546 if (likely(l != NULL))
550 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
551 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
553 libvlc_unlock (obj->p_libvlc);
557 static void DumpVariable (const void *data, const VISIT which, const int depth)
559 if (which != postorder && which != leaf)
563 const variable_t *p_var = *(const variable_t **)data;
564 const char *psz_type = "unknown";
566 switch( p_var->i_type & VLC_VAR_TYPE )
568 #define MYCASE( type, nice ) \
569 case VLC_VAR_ ## type: \
572 MYCASE( VOID, "void" );
573 MYCASE( BOOL, "bool" );
574 MYCASE( INTEGER, "integer" );
575 MYCASE( HOTKEY, "hotkey" );
576 MYCASE( STRING, "string" );
577 MYCASE( VARIABLE, "variable" );
578 MYCASE( FLOAT, "float" );
579 MYCASE( TIME, "time" );
580 MYCASE( COORDS, "coords" );
581 MYCASE( ADDRESS, "address" );
584 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
585 if( p_var->psz_text )
586 printf( ", %s", p_var->psz_text );
587 fputc( ')', stdout );
588 if( p_var->i_type & VLC_VAR_HASCHOICE )
589 fputs( ", has choices", stdout );
590 if( p_var->i_type & VLC_VAR_ISCOMMAND )
591 fputs( ", command", stdout );
592 if( p_var->i_entries )
593 printf( ", %d callbacks", p_var->i_entries );
594 switch( p_var->i_type & VLC_VAR_CLASS )
599 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
601 case VLC_VAR_INTEGER:
602 printf( ": %"PRId64, p_var->val.i_int );
605 printf( ": \"%s\"", p_var->val.psz_string );
608 printf( ": %f", p_var->val.f_float );
611 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
614 printf( ": %"PRId32"x%"PRId32,
615 p_var->val.coords.x, p_var->val.coords.y );
617 case VLC_VAR_ADDRESS:
618 printf( ": %p", p_var->val.p_address );
621 fputc( '\n', stdout );
624 /*****************************************************************************
625 * DumpCommand: print the current vlc structure
626 *****************************************************************************
627 * This function prints either an ASCII tree showing the connections between
628 * vlc objects, and additional information such as their refcount, thread ID,
629 * etc. (command "tree"), or the same data as a simple list (command "list").
630 *****************************************************************************/
631 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
632 vlc_value_t oldval, vlc_value_t newval, void *p_data )
635 vlc_object_t *p_object = NULL;
637 if( *newval.psz_string )
639 /* try using the object's name to find it */
640 p_object = vlc_object_find_name( p_data, newval.psz_string );
645 libvlc_lock (p_this->p_libvlc);
646 if( *psz_cmd == 't' )
648 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
651 p_object = VLC_OBJECT(p_this->p_libvlc);
654 DumpStructure( vlc_internals(p_object), 0, psz_foo );
656 else if( *psz_cmd == 'v' )
659 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
661 PrintObject( vlc_internals(p_object), "" );
662 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
663 if( vlc_internals( p_object )->var_root == NULL )
664 puts( " `-o No variables" );
666 twalk( vlc_internals( p_object )->var_root, DumpVariable );
667 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
669 libvlc_unlock (p_this->p_libvlc);
671 if( *newval.psz_string )
673 vlc_object_release( p_object );
678 /*****************************************************************************
679 * vlc_list_release: free a list previously allocated by vlc_list_find
680 *****************************************************************************
681 * This function decreases the refcount of all objects in the list and
683 *****************************************************************************/
684 void vlc_list_release( vlc_list_t *p_list )
688 for( i_index = 0; i_index < p_list->i_count; i_index++ )
690 vlc_object_release( p_list->p_values[i_index].p_object );
693 free( p_list->p_values );
697 /* Following functions are local */
699 static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
701 if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
702 return vlc_object_hold (vlc_externals (priv));
704 for (priv = priv->first; priv != NULL; priv = priv->next)
706 vlc_object_t *found = FindName (priv, name);
713 static void PrintObject( vlc_object_internals_t *priv,
714 const char *psz_prefix )
716 char psz_refcount[20], psz_name[50], psz_parent[20];
718 int canc = vlc_savecancel ();
719 memset( &psz_name, 0, sizeof(psz_name) );
721 vlc_mutex_lock (&name_lock);
722 if (priv->psz_name != NULL)
724 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
728 vlc_mutex_unlock (&name_lock);
730 psz_refcount[0] = '\0';
731 if( priv->i_refcount > 0 )
732 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
734 psz_parent[0] = '\0';
735 /* FIXME: need structure lock!!! */
736 if( vlc_externals(priv)->p_parent )
737 snprintf( psz_parent, 19, ", parent %p",
738 vlc_externals(priv)->p_parent );
740 printf( " %so %p %s%s%s%s\n", psz_prefix,
741 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
742 psz_name, psz_refcount, psz_parent );
743 vlc_restorecancel (canc);
746 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
749 char i_back = psz_foo[i_level];
750 psz_foo[i_level] = '\0';
752 PrintObject (priv, psz_foo);
754 psz_foo[i_level] = i_back;
756 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
758 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
762 for (priv = priv->first; priv != NULL; priv = priv->next)
766 psz_foo[i_level-1] = ' ';
768 if( psz_foo[i_level-2] == '`' )
770 psz_foo[i_level-2] = ' ';
774 psz_foo[i_level] = priv->next ? '|' : '`';
775 psz_foo[i_level+1] = '-';
776 psz_foo[i_level+2] = '\0';
778 DumpStructure (priv, i_level + 2, psz_foo);
782 static vlc_list_t * NewList( int i_count )
784 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
788 p_list->i_count = i_count;
792 p_list->p_values = NULL;
796 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
797 if( p_list->p_values == NULL )