comparison mac/dw.m @ 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
comparison
equal deleted inserted replaced
696:516e0c09d4b9 697:830e1f3672b9
1649 1649
1650 return mainMenu; 1650 return mainMenu;
1651 } 1651 }
1652 1652
1653 /* 1653 /*
1654 * Initializes the Dynamic Windows engine.
1655 * Parameters:
1656 * newthread: True if this is the only thread.
1657 * False if there is already a message loop running.
1658 */
1659 int API dw_init(int newthread, int argc, char *argv[])
1660 {
1661 /* Create the application object */
1662 DWApp = [NSApplication sharedApplication];
1663 /* Create object for handling timers */
1664 DWHandler = [[DWTimerHandler alloc] init];
1665 /* If we aren't using garbage collection we need autorelease pools */
1666 #if !defined(GARBAGE_COLLECT)
1667 pool = [[NSAutoreleasePool alloc] init];
1668 #endif
1669 /* Create a default main menu, with just the application menu */
1670 DWMainMenu = _generate_main_menu();
1671 [DWApp setMainMenu:DWMainMenu];
1672 DWObj = [[DWObject alloc] init];
1673 /* Create mutexes for thread safety */
1674 DWRunMutex = dw_mutex_new();
1675 DWThreadMutex = dw_mutex_new();
1676 DWThreadMutex2 = dw_mutex_new();
1677 /* Use NSThread to start a dummy thread to initialize the threading subsystem */
1678 NSThread *thread = [[ NSThread alloc] initWithTarget:DWObj selector:@selector(uselessThread:) object:nil];
1679 [thread start];
1680 [thread release];
1681 return 0;
1682 }
1683
1684 /*
1685 * Runs a message loop for Dynamic Windows. 1654 * Runs a message loop for Dynamic Windows.
1686 */ 1655 */
1687 void API dw_main(void) 1656 void API dw_main(void)
1688 { 1657 {
1689 dw_mutex_lock(DWRunMutex); 1658 dw_mutex_lock(DWRunMutex);
6986 free(eve); 6955 free(eve);
6987 } 6956 }
6988 return 0; 6957 return 0;
6989 } 6958 }
6990 6959
6991 /* 6960 #if !defined(GARBAGE_COLLECT)
6992 * Setup thread independent color sets. 6961 pthread_key_t _dw_pool_key;
6962 #endif
6963
6964 /* Mac specific function to cause garbage collection */
6965 void _dw_pool_drain(void)
6966 {
6967 #if !defined(GARBAGE_COLLECT)
6968 NSAutoreleasePool *pool = pthread_getspecific(_dw_pool_key);
6969 NSLog(@"Pool draining %x", (int)pool);
6970 [pool drain];
6971 #endif
6972 }
6973
6974 /*
6975 * Setup thread independent pools.
6993 */ 6976 */
6994 void _dwthreadstart(void *data) 6977 void _dwthreadstart(void *data)
6995 { 6978 {
6996 void (*threadfunc)(void *) = NULL; 6979 void (*threadfunc)(void *) = NULL;
6997 void **tmp = (void **)data; 6980 void **tmp = (void **)data;
6998 /* If we aren't using garbage collection we need autorelease pools */ 6981 /* If we aren't using garbage collection we need autorelease pools */
6999 #if !defined(GARBAGE_COLLECT) 6982 #if !defined(GARBAGE_COLLECT)
7000 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 6983 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6984 pthread_setspecific(_dw_pool_key, pool);
7001 #endif 6985 #endif
7002 6986
7003 threadfunc = (void (*)(void *))tmp[0]; 6987 threadfunc = (void (*)(void *))tmp[0];
7004 6988
7005 threadfunc(tmp[1]); 6989 threadfunc(tmp[1]);
7006 /* Release the pool when we are done so we don't leak */ 6990 /* Release the pool when we are done so we don't leak */
7007 #if !defined(GARBAGE_COLLECT) 6991 #if !defined(GARBAGE_COLLECT)
7008 [pool release]; 6992 [pool release];
7009 #endif 6993 #endif
7010 free(tmp); 6994 free(tmp);
6995 }
6996
6997 /*
6998 * Initializes the Dynamic Windows engine.
6999 * Parameters:
7000 * newthread: True if this is the only thread.
7001 * False if there is already a message loop running.
7002 */
7003 int API dw_init(int newthread, int argc, char *argv[])
7004 {
7005 /* Create the application object */
7006 DWApp = [NSApplication sharedApplication];
7007 /* Create object for handling timers */
7008 DWHandler = [[DWTimerHandler alloc] init];
7009 /* If we aren't using garbage collection we need autorelease pools */
7010 #if !defined(GARBAGE_COLLECT)
7011 pthread_key_create(&_dw_pool_key, NULL);
7012 pool = [[NSAutoreleasePool alloc] init];
7013 #endif
7014 /* Create a default main menu, with just the application menu */
7015 DWMainMenu = _generate_main_menu();
7016 [DWApp setMainMenu:DWMainMenu];
7017 DWObj = [[DWObject alloc] init];
7018 /* Create mutexes for thread safety */
7019 DWRunMutex = dw_mutex_new();
7020 DWThreadMutex = dw_mutex_new();
7021 DWThreadMutex2 = dw_mutex_new();
7022 /* Use NSThread to start a dummy thread to initialize the threading subsystem */
7023 NSThread *thread = [[ NSThread alloc] initWithTarget:DWObj selector:@selector(uselessThread:) object:nil];
7024 [thread start];
7025 [thread release];
7026 return 0;
7011 } 7027 }
7012 7028
7013 /* 7029 /*
7014 * Allocates a shared memory region with a name. 7030 * Allocates a shared memory region with a name.
7015 * Parameters: 7031 * Parameters:
7112 remove(h->path); 7128 remove(h->path);
7113 free(h->path); 7129 free(h->path);
7114 } 7130 }
7115 return rc; 7131 return rc;
7116 } 7132 }
7133
7117 /* 7134 /*
7118 * Creates a new thread with a starting point of func. 7135 * Creates a new thread with a starting point of func.
7119 * Parameters: 7136 * Parameters:
7120 * func: Function which will be run in the new thread. 7137 * func: Function which will be run in the new thread.
7121 * data: Parameter(s) passed to the function. 7138 * data: Parameter(s) passed to the function.