]> git.sesse.net Git - vlc/commitdiff
macosx: Merge the KillerThread and the manage thread. Make sure we properly exit...
authorPierre d'Herbemont <pdherbemont@videolan.org>
Fri, 4 Jul 2008 23:45:20 +0000 (01:45 +0200)
committerPierre d'Herbemont <pdherbemont@videolan.org>
Fri, 4 Jul 2008 23:46:14 +0000 (01:46 +0200)
modules/gui/macosx/intf.h
modules/gui/macosx/intf.m

index d0b229f2c11fd07f13d996ce98e8059a0e0c654c..4b5410564602e156b580e300f9cbf3ff0651c084 100644 (file)
@@ -92,7 +92,6 @@ struct intf_sys_t
 
     /* The messages window */
     msg_subscription_t * p_sub;
-
 };
 
 /*****************************************************************************
@@ -305,6 +304,9 @@ struct intf_sys_t
 
     int     i_lastShownVolume;
 
+    /* the manage thread */
+    pthread_t manage_thread;
+
     AppleRemote * o_remote;
     BOOL b_remote_button_hold; /* true as long as the user holds the left,right,plus or minus on the remote control */
 }
index 3addd181c3f57a7de64378fc5be0099ed3a6351a..47387cb8031069226ff7b6087d6a9ccf7754dbad 100644 (file)
@@ -131,28 +131,6 @@ void CloseIntf ( vlc_object_t *p_this )
     free( p_intf->p_sys );
 }
 
-/*****************************************************************************
- * KillerThread: Thread that kill the application
- *****************************************************************************/
-static void * KillerThread( void *user_data )
-{
-    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
-
-    intf_thread_t *p_intf = user_data;
-    
-    vlc_object_lock ( p_intf );
-    while( vlc_object_alive( p_intf ) )
-        vlc_object_wait( p_intf );
-    vlc_object_unlock( p_intf );
-
-    msg_Dbg( p_intf, "Killing the Mac OS X module" );
-
-    /* We are dead, terminate */
-    [NSApp terminate: nil];
-    [o_pool release];
-    return NULL;
-}
-
 /*****************************************************************************
  * Run: main loop
  *****************************************************************************/
@@ -186,20 +164,26 @@ static void Run( intf_thread_t *p_intf )
     [[VLCMain sharedInstance] setIntf: p_intf];
     [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
 
-    /* Setup a thread that will monitor the module killing */
-    pthread_t killer_thread;
-    pthread_create( &killer_thread, NULL, KillerThread, p_intf );
-
     /* Install a jmpbuffer to where we can go back before the NSApp exit
      * see applicationWillTerminate: */
     if(setjmp(jmpbuffer) == 0)
         [NSApp run];
 
-    pthread_join( killer_thread, NULL );
-
     [o_pool release];
 }
 
+/*****************************************************************************
+ * ManageThread: An ugly thread that polls
+ *****************************************************************************/
+static void * ManageThread( void *user_data )
+{
+    id self = user_data;
+
+    [self manage];
+
+    return NULL;
+}
+
 int ExecuteOnMainThread( id target, SEL sel, void * p_arg )
 {
     int i_ret = 0;
@@ -798,9 +782,8 @@ static VLCMain *_o_sharedMainInstance = nil;
                                      target: self selector: @selector(manageIntf:)
                                    userInfo: nil repeats: FALSE];
 
-    /* FIXME: don't poll */
-    [NSThread detachNewThreadSelector: @selector(manage)
-                             toTarget: self withObject: nil];
+    /* Note: we use the pthread API to support pre-10.5 */
+    pthread_create( &manage_thread, NULL, ManageThread, self );
 
     [o_controls setupVarMenuItem: o_mi_add_intf target: (vlc_object_t *)p_intf
         var: "intf-add" selector: @selector(toggleVar:)];
@@ -1267,12 +1250,18 @@ static VLCMain *_o_sharedMainInstance = nil;
         [self manageVolumeSlider];
 
         vlc_mutex_unlock( &p_intf->change_lock );
-        vlc_object_unlock( p_intf );
-        msleep( 100000 );
-        vlc_object_lock( p_intf );
+
+        vlc_object_timedwait( p_intf, 100000 + mdate());
     }
     vlc_object_unlock( p_intf );
     [o_pool release];
+
+    pthread_testcancel(); /* If we were cancelled stop here */
+
+    msg_Info( p_intf, "Killing the Mac OS X module" );
+
+    /* We are dead, terminate */
+    [NSApp performSelectorOnMainThread: @selector(terminate:) withObject:nil waitUntilDone:NO];
 }
 
 - (void)manageIntf:(NSTimer *)o_timer
@@ -1281,15 +1270,6 @@ static VLCMain *_o_sharedMainInstance = nil;
     playlist_t * p_playlist;
     input_thread_t * p_input;
 
-    vlc_object_lock( p_intf );
-
-    if( !vlc_object_alive( p_intf ) )
-    {
-        vlc_object_unlock( p_intf );
-        [o_timer invalidate];
-        return;
-    }
-
     if( p_intf->p_sys->b_input_update )
     {
         /* Called when new input is opened */
@@ -1466,7 +1446,6 @@ static VLCMain *_o_sharedMainInstance = nil;
     [NSTimer scheduledTimerWithTimeInterval: 0.3
         target: self selector: @selector(manageIntf:)
         userInfo: nil repeats: FALSE];
-    vlc_object_unlock( p_intf );
 }
 
 - (void)setupMenus
@@ -1777,6 +1756,15 @@ static VLCMain *_o_sharedMainInstance = nil;
  
     msg_Dbg( p_intf, "Terminating" );
 
+    /* Make sure the manage_thread won't call -terminate: again */
+    pthread_cancel( manage_thread );
+
+    /* Make sure the intf object is getting killed */
+    vlc_object_kill( p_intf );
+
+    /* Make sure our manage_thread ends */
+    pthread_join( manage_thread, NULL );
+
     /* make sure that the current volume is saved */
     config_PutInt( p_intf->p_libvlc, "volume", i_lastShownVolume );
     returnedValue = config_SaveConfigFile( p_intf->p_libvlc, "main" );