changeset 697:830e1f3672b9

Added draining mechanism for threads that don't have run loops.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 10 Mar 2011 23:28:10 +0000
parents 516e0c09d4b9
children e19f69a78f21
files compat.c dw.h mac/dw.m
diffstat 3 files changed, 56 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/compat.c	Thu Mar 10 19:49:57 2011 +0000
+++ b/compat.c	Thu Mar 10 23:28:10 2011 +0000
@@ -1,6 +1,7 @@
 /* $Id$ */
 
 #include "compat.h"
+#include "dw.h"
 #if defined(__OS2__) || defined(__WIN32__)
 #include <share.h>
 #endif
@@ -35,6 +36,9 @@
 	nanosleep(&req, NULL);
 #else
 	usleep(period * 1000);
+#ifdef __MAC__
+    _dw_pool_drain();
+#endif
 #endif
 }
 #endif
--- a/dw.h	Thu Mar 10 19:49:57 2011 +0000
+++ b/dw.h	Thu Mar 10 23:28:10 2011 +0000
@@ -304,6 +304,8 @@
    HWND handle;
 } *HPIXMAP;
 
+void _dw_pool_drain(void);
+
 #define DW_DT_LEFT               0 /* NSLeftTextAlignment */
 #define DW_DT_QUERYEXTENT        0
 #define DW_DT_UNDERSCORE         0
--- a/mac/dw.m	Thu Mar 10 19:49:57 2011 +0000
+++ b/mac/dw.m	Thu Mar 10 23:28:10 2011 +0000
@@ -1651,37 +1651,6 @@
 }
 
 /*
- * Initializes the Dynamic Windows engine.
- * Parameters:
- *           newthread: True if this is the only thread.
- *                      False if there is already a message loop running.
- */
-int API dw_init(int newthread, int argc, char *argv[])
-{
-	/* Create the application object */
-	DWApp = [NSApplication sharedApplication];
-	/* Create object for handling timers */
-	DWHandler = [[DWTimerHandler alloc] init];
-    /* If we aren't using garbage collection we need autorelease pools */
-#if !defined(GARBAGE_COLLECT)
-	pool = [[NSAutoreleasePool alloc] init];
-#endif
-    /* Create a default main menu, with just the application menu */
-	DWMainMenu = _generate_main_menu();
-	[DWApp setMainMenu:DWMainMenu];
-	DWObj = [[DWObject alloc] init];
-    /* Create mutexes for thread safety */
-    DWRunMutex = dw_mutex_new();
-    DWThreadMutex = dw_mutex_new();
-    DWThreadMutex2 = dw_mutex_new();
-    /* Use NSThread to start a dummy thread to initialize the threading subsystem */
-	NSThread *thread = [[ NSThread alloc] initWithTarget:DWObj selector:@selector(uselessThread:) object:nil];
-	[thread start];
-    [thread release];
-	return 0;
-}
-
-/*
  * Runs a message loop for Dynamic Windows.
  */
 void API dw_main(void)
@@ -6988,8 +6957,22 @@
 	return 0;
 }
 
-/*
- * Setup thread independent color sets.
+#if !defined(GARBAGE_COLLECT)
+pthread_key_t _dw_pool_key;
+#endif
+
+/* Mac specific function to cause garbage collection */
+void _dw_pool_drain(void)
+{
+#if !defined(GARBAGE_COLLECT)
+    NSAutoreleasePool *pool = pthread_getspecific(_dw_pool_key);
+    NSLog(@"Pool draining %x", (int)pool);
+    [pool drain];
+#endif    
+}
+
+/*
+ * Setup thread independent pools.
  */
 void _dwthreadstart(void *data)
 {
@@ -6998,6 +6981,7 @@
     /* If we aren't using garbage collection we need autorelease pools */
 #if !defined(GARBAGE_COLLECT)
 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    pthread_setspecific(_dw_pool_key, pool);
 #endif
 	
    threadfunc = (void (*)(void *))tmp[0];
@@ -7011,6 +6995,38 @@
 }
 
 /*
+ * Initializes the Dynamic Windows engine.
+ * Parameters:
+ *           newthread: True if this is the only thread.
+ *                      False if there is already a message loop running.
+ */
+int API dw_init(int newthread, int argc, char *argv[])
+{
+	/* Create the application object */
+	DWApp = [NSApplication sharedApplication];
+	/* Create object for handling timers */
+	DWHandler = [[DWTimerHandler alloc] init];
+    /* If we aren't using garbage collection we need autorelease pools */
+#if !defined(GARBAGE_COLLECT)
+    pthread_key_create(&_dw_pool_key, NULL);
+	pool = [[NSAutoreleasePool alloc] init];
+#endif
+    /* Create a default main menu, with just the application menu */
+	DWMainMenu = _generate_main_menu();
+	[DWApp setMainMenu:DWMainMenu];
+	DWObj = [[DWObject alloc] init];
+    /* Create mutexes for thread safety */
+    DWRunMutex = dw_mutex_new();
+    DWThreadMutex = dw_mutex_new();
+    DWThreadMutex2 = dw_mutex_new();
+    /* Use NSThread to start a dummy thread to initialize the threading subsystem */
+	NSThread *thread = [[ NSThread alloc] initWithTarget:DWObj selector:@selector(uselessThread:) object:nil];
+	[thread start];
+    [thread release];
+	return 0;
+}
+
+/*
  * Allocates a shared memory region with a name.
  * Parameters:
  *         handle: A pointer to receive a SHM identifier.
@@ -7114,6 +7130,7 @@
    }
    return rc;
 }
+
 /*
  * Creates a new thread with a starting point of func.
  * Parameters: