]> git.sesse.net Git - vlc/blobdiff - modules/misc/testsuite/test4.c
Remove useless vlc_object_detach() before vlc_object_release()
[vlc] / modules / misc / testsuite / test4.c
index addf437df00027f71fb775bec41ee9a8024ab795..06a0b0ecc1105c5317e657044ce4e9a2abf1d022 100644 (file)
@@ -10,7 +10,7 @@
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
-#include <stdlib.h>
 #include <signal.h>
 
 /*****************************************************************************
@@ -59,21 +63,21 @@ static int    Signal    ( vlc_object_t *, char const *,
 /*****************************************************************************
  * Module descriptor.
  *****************************************************************************/
-vlc_module_begin();
-    set_description( _("Miscellaneous stress tests") );
-    var_Create( p_module->p_libvlc_global, "foo-test",
+vlc_module_begin ()
+    set_description( N_("Miscellaneous stress tests") )
+    var_Create( p_module->p_libvlc, "foo-test",
                 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
-    var_AddCallback( p_module->p_libvlc_global, "foo-test", Foo, NULL );
-    var_Create( p_module->p_libvlc_global, "callback-test",
+    var_AddCallback( p_module->p_libvlc, "foo-test", Foo, NULL );
+    var_Create( p_module->p_libvlc, "callback-test",
                 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
-    var_AddCallback( p_module->p_libvlc_global, "callback-test", Callback, NULL );
-    var_Create( p_module->p_libvlc_global, "stress-test",
+    var_AddCallback( p_module->p_libvlc, "callback-test", Callback, NULL );
+    var_Create( p_module->p_libvlc, "stress-test",
                 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
-    var_AddCallback( p_module->p_libvlc_global, "stress-test", Stress, NULL );
-    var_Create( p_module->p_libvlc_global, "signal",
+    var_AddCallback( p_module->p_libvlc, "stress-test", Stress, NULL );
+    var_Create( p_module->p_libvlc, "signal",
                 VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
-    var_AddCallback( p_module->p_libvlc_global, "signal", Signal, NULL );
-vlc_module_end();
+    var_AddCallback( p_module->p_libvlc, "signal", Signal, NULL );
+vlc_module_end ()
 
 /*****************************************************************************
  * Foo: put anything here
@@ -95,16 +99,19 @@ static int Foo( vlc_object_t *p_this, char const *psz_cmd,
     var_Change( p_this, "honk", VLC_VAR_SETDEFAULT, &val, NULL );
 
     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
+    free( val.psz_string );
 
     val.psz_string = "foo";
     var_Set( p_this, "honk", val );
 
     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
+    free( val.psz_string );
 
     val.psz_string = "blork";
     var_Set( p_this, "honk", val );
 
     var_Get( p_this, "honk", &val ); printf( "value: %s\n", val.psz_string );
+    free( val.psz_string );
 
     val.psz_string = "baz";
     var_Change( p_this, "honk", VLC_VAR_DELCHOICE, &val, NULL );
@@ -116,7 +123,7 @@ static int Foo( vlc_object_t *p_this, char const *psz_cmd,
     {
         printf( "value %i: %s\n", i, val.p_list->p_values[i].psz_string );
     }
-    var_Change( p_this, "honk", VLC_VAR_FREELIST, &val, NULL );
+    var_FreeList( &val, NULL );
 
     var_Destroy( p_this, "honk" );
 
@@ -164,9 +171,9 @@ static int Callback( vlc_object_t *p_this, char const *psz_cmd,
 
     for( i = 0; i < 10; i++ )
     {
-        pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
+        pp_objects[i] = vlc_object_create( p_this, sizeof( vlc_object_t ) );
         vlc_object_attach( pp_objects[i], p_this );
-        vlc_thread_create( pp_objects[i], "foo", MyThread, 0, VLC_TRUE );
+        vlc_thread_create( pp_objects[i], "foo", MyThread, 0, true );
     }
 
     msleep( 3000000 );
@@ -177,8 +184,7 @@ static int Callback( vlc_object_t *p_this, char const *psz_cmd,
     {
         vlc_object_kill( pp_objects[i] );
         vlc_thread_join( pp_objects[i] );
-        vlc_object_detach( pp_objects[i] );
-        vlc_object_destroy( pp_objects[i] );
+        vlc_object_release( pp_objects[i] );
     }
 
     /* Clean our mess */
@@ -216,7 +222,7 @@ static int MyCallback( vlc_object_t *p_this, char const *psz_var,
     sprintf( psz_newvar, "blork-%i", i_var );
     var_Set( p_this, psz_newvar, newval );
 
-    return VLC_SUCCESS;   
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -232,13 +238,16 @@ static void * MyThread( vlc_object_t *p_this )
 
     val.i_int = 42;
 
-    while( !p_this->b_die )
+    while( vlc_object_alive (p_this) )
     {
         int i = (int) (100.0 * rand() / (RAND_MAX));
+        /* FIXME: not thread-safe */
 
         sprintf( psz_var, "blork-%i", i );
         val.i_int = i + 200;
+        int canc = vlc_savecancel ();
         var_Set( p_parent, psz_var, val );
+        vlc_restorecancel (canc);
 
         /* This is quite heavy, but we only have 10 threads. Keep cool. */
         msleep( 1000 );
@@ -279,7 +288,7 @@ static int Stress( vlc_object_t *p_this, char const *psz_cmd,
 
     /* Allocate required data */
     ppsz_name = malloc( MAXVAR * i_level * sizeof(char*) );
-    psz_blob = malloc( 20 * MAXVAR * i_level * sizeof(char) );
+    psz_blob = malloc( 20 * MAXVAR * i_level );
     for( i = 0; i < MAXVAR * i_level; i++ )
     {
         ppsz_name[i] = psz_blob + 20 * i;
@@ -296,21 +305,21 @@ static int Stress( vlc_object_t *p_this, char const *psz_cmd,
     start = mdate();
     for( i = 0; i < MAXOBJ * i_level; i++ )
     {
-        pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
+        pp_objects[i] = vlc_object_create( p_this, sizeof( vlc_object_t ) );
     }
 
     printf( " - randomly looking up %i objects\n", MAXLOOK * i_level );
     for( i = MAXLOOK * i_level; i--; )
     {
         int id = (int) (MAXOBJ * i_level * 1.0 * rand() / (RAND_MAX));
-        vlc_object_get( p_this, pp_objects[id]->i_object_id );
+        vlc_object_get( pp_objects[id]->i_object_id );
         vlc_object_release( p_this );
     }
 
     printf( " - destroying the objects (LIFO)\n" );
     for( i = MAXOBJ * i_level; i--; )
     {
-        vlc_object_destroy( pp_objects[i] );
+        vlc_object_release( pp_objects[i] );
     }
 
     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
@@ -381,16 +390,16 @@ static int Stress( vlc_object_t *p_this, char const *psz_cmd,
             MAXTH * i_level, MAXOBJ/MAXTH );
     for( i = 0; i < MAXTH * i_level; i++ )
     {
-        pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
-        vlc_thread_create( pp_objects[i], "foo", Dummy, 0, VLC_TRUE );
+        pp_objects[i] = vlc_object_create( p_this, sizeof( vlc_object_t ) );
+        vlc_thread_create( pp_objects[i], "foo", Dummy, 0, true );
     }
 
     printf( " - killing the threads (LIFO)\n" );
     for( i = MAXTH * i_level; i--; )
     {
-        pp_objects[i]->b_die = VLC_TRUE;
+        pp_objects[i]->b_die = true;
         vlc_thread_join( pp_objects[i] );
-        vlc_object_destroy( pp_objects[i] );
+        vlc_object_release( pp_objects[i] );
     }
 
     printf( "done (%fs).\n", (mdate() - start) / 1000000.0 );
@@ -413,19 +422,19 @@ static void * Dummy( vlc_object_t *p_this )
 
     for( i = 0; i < MAXOBJ/MAXTH; i++ )
     {
-        pp_objects[i] = vlc_object_create( p_this, VLC_OBJECT_GENERIC );
+        pp_objects[i] = vlc_object_create( p_this, sizeof( vlc_object_t ) );
     }
 
     vlc_thread_ready( p_this );
 
-    while( !p_this->b_die )
+    while( vlc_object_alive (p_this) )
     {
         msleep( 10000 );
     }
 
     for( i = MAXOBJ/MAXTH; i--; )
     {
-        vlc_object_destroy( pp_objects[i] );
+        vlc_object_release( pp_objects[i] );
     }
 
     return NULL;