# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1299783485 0 # Node ID 130ca42c4ae32158f6cf99ed4224d21bbdb084a8 # Parent 2f21ee9d7c7b6919d953c3b5ab3ee8167b8e9026 Reimplementation of dw_main_iteration and dw_main_sleep that actually work. This fixes deadlocks in the control center application, but seems to be a bit slow. diff -r 2f21ee9d7c7b -r 130ca42c4ae3 mac/dw.m --- a/mac/dw.m Thu Mar 10 18:30:03 2011 +0000 +++ b/mac/dw.m Thu Mar 10 18:58:05 2011 +0000 @@ -227,7 +227,6 @@ @end NSApplication *DWApp; -NSRunLoop *DWRunLoop; NSMenu *DWMainMenu; DWTimerHandler *DWHandler; #if !defined(GARBAGE_COLLECT) @@ -1667,8 +1666,6 @@ { /* Create the application object */ DWApp = [NSApplication sharedApplication]; - /* Create a run loop for doing manual loops */ - DWRunLoop = [NSRunLoop alloc]; /* Create object for handling timers */ DWHandler = [[DWTimerHandler alloc] init]; /* If we aren't using garbage collection we need autorelease pools */ @@ -1709,10 +1706,35 @@ */ void API dw_main_sleep(int milliseconds) { - double seconds = (double)milliseconds/1000.0; - NSDate *time = [[NSDate alloc] initWithTimeIntervalSinceNow:seconds]; - [DWRunLoop runUntilDate:time]; - [time release]; + struct timeval tv, start; + DWTID curr = pthread_self(); + + gettimeofday(&start, NULL); + + if(DWThread == (DWTID)-1 || DWThread == curr) + { + DWTID orig = DWThread; + + gettimeofday(&tv, NULL); + + dw_mutex_lock(DWRunMutex); + while(((tv.tv_sec - start.tv_sec)*1000) + ((tv.tv_usec - start.tv_usec)/1000) <= milliseconds) + { + if(orig == (DWTID)-1) + { + DWThread = curr; + } + dw_main_iteration(); + if(orig == (DWTID)-1) + { + DWThread = orig; + } + gettimeofday(&tv, NULL); + } + dw_mutex_unlock(DWRunMutex); + } + else + usleep(milliseconds * 1000); } /* @@ -1720,7 +1742,15 @@ */ void API dw_main_iteration(void) { - [DWRunLoop limitDateForMode:NSDefaultRunLoopMode]; + NSDate *distant_future = [NSDate distantFuture]; + NSEvent *event = [DWApp nextEventMatchingMask:NSAnyEventMask + untilDate:distant_future + inMode:NSDefaultRunLoopMode + dequeue:YES]; + if(event) + { + [DWApp sendEvent:event]; + } } /*