comparison mac/dw.m @ 700:7953f0471e4d

Some cleanups for the run loop iteration and thread system.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 11 Mar 2011 03:02:58 +0000
parents b79300831495
children c91a1b345f2e
comparison
equal deleted inserted replaced
699:b79300831495 700:7953f0471e4d
21 #define DW_MUTEX_LOCK { \ 21 #define DW_MUTEX_LOCK { \
22 if(DWThread != (DWTID)-1 && pthread_self() != DWThread && pthread_self() != _dw_mutex_locked) { \ 22 if(DWThread != (DWTID)-1 && pthread_self() != DWThread && pthread_self() != _dw_mutex_locked) { \
23 dw_mutex_lock(DWThreadMutex); \ 23 dw_mutex_lock(DWThreadMutex); \
24 _dw_mutex_locked = pthread_self(); \ 24 _dw_mutex_locked = pthread_self(); \
25 dw_mutex_lock(DWThreadMutex2); \ 25 dw_mutex_lock(DWThreadMutex2); \
26 /*NSLog(@"Thread %d asking the main thread to stop %x", (int)pthread_self(), (int)DWObj);*/ \
27 [DWObj performSelectorOnMainThread:@selector(synchronizeThread:) withObject:nil waitUntilDone:NO]; \ 26 [DWObj performSelectorOnMainThread:@selector(synchronizeThread:) withObject:nil waitUntilDone:NO]; \
28 dw_mutex_lock(DWRunMutex); \ 27 dw_mutex_lock(DWRunMutex); \
29 /*NSLog(@"Thread %d proceeding", (int)pthread_self());*/ \
30 _locked_by_me = TRUE; } } 28 _locked_by_me = TRUE; } }
31 #define DW_MUTEX_UNLOCK { \ 29 #define DW_MUTEX_UNLOCK { \
32 if(pthread_self() != DWThread && _locked_by_me == TRUE) { \ 30 if(pthread_self() != DWThread && _locked_by_me == TRUE) { \
33 /*NSLog(@"Thread %d releasing", (int)pthread_self());*/ \
34 dw_mutex_unlock(DWRunMutex); \ 31 dw_mutex_unlock(DWRunMutex); \
35 dw_mutex_unlock(DWThreadMutex2); \ 32 dw_mutex_unlock(DWThreadMutex2); \
36 _dw_mutex_locked = (pthread_t)-1; \ 33 _dw_mutex_locked = (pthread_t)-1; \
37 dw_mutex_unlock(DWThreadMutex); \ 34 dw_mutex_unlock(DWThreadMutex); \
38 _locked_by_me = FALSE; } } 35 _locked_by_me = FALSE; } }
85 SignalHandler *Root = NULL; 82 SignalHandler *Root = NULL;
86 83
87 /* Some internal prototypes */ 84 /* Some internal prototypes */
88 static void _do_resize(Box *thisbox, int x, int y); 85 static void _do_resize(Box *thisbox, int x, int y);
89 int _remove_userdata(UserData **root, char *varname, int all); 86 int _remove_userdata(UserData **root, char *varname, int all);
87 void _dw_main_iteration(void);
90 88
91 SignalHandler *_get_handler(HWND window, int messageid) 89 SignalHandler *_get_handler(HWND window, int messageid)
92 { 90 {
93 SignalHandler *tmp = Root; 91 SignalHandler *tmp = Root;
94 92
1735 { 1733 {
1736 DWThread = curr; 1734 DWThread = curr;
1737 } 1735 }
1738 while(((tv.tv_sec - start.tv_sec)*1000) + ((tv.tv_usec - start.tv_usec)/1000) <= milliseconds) 1736 while(((tv.tv_sec - start.tv_sec)*1000) + ((tv.tv_usec - start.tv_usec)/1000) <= milliseconds)
1739 { 1737 {
1740 dw_main_iteration(); 1738 _dw_main_iteration();
1741 gettimeofday(&tv, NULL); 1739 gettimeofday(&tv, NULL);
1742 } 1740 }
1743 if(orig == (DWTID)-1) 1741 if(orig == (DWTID)-1)
1744 { 1742 {
1745 DWThread = orig; 1743 DWThread = orig;
1748 } 1746 }
1749 else 1747 else
1750 usleep(milliseconds * 1000); 1748 usleep(milliseconds * 1000);
1751 } 1749 }
1752 1750
1753 /* 1751 /* Internal version that doesn't lock the run mutex */
1754 * Processes a single message iteration and returns. 1752 void _dw_main_iteration(void)
1755 */
1756 void API dw_main_iteration(void)
1757 { 1753 {
1758 NSDate *distant_future = [NSDate distantFuture]; 1754 NSDate *distant_future = [NSDate distantFuture];
1759 NSEvent *event = [DWApp nextEventMatchingMask:NSAnyEventMask 1755 NSEvent *event = [DWApp nextEventMatchingMask:NSAnyEventMask
1760 untilDate:distant_future 1756 untilDate:distant_future
1761 inMode:NSDefaultRunLoopMode 1757 inMode:NSDefaultRunLoopMode
1762 dequeue:YES]; 1758 dequeue:YES];
1763 if(event) 1759 if(event)
1764 { 1760 {
1765 [DWApp sendEvent:event]; 1761 [DWApp sendEvent:event];
1766 } 1762 }
1763 }
1764
1765 /*
1766 * Processes a single message iteration and returns.
1767 */
1768 void API dw_main_iteration(void)
1769 {
1770 dw_mutex_lock(DWRunMutex);
1771 _dw_main_iteration();
1772 dw_mutex_unlock(DWRunMutex);
1767 } 1773 }
1768 1774
1769 /* 1775 /*
1770 * Cleanly terminates a DW session, should be signal handler safe. 1776 * Cleanly terminates a DW session, should be signal handler safe.
1771 * Parameters: 1777 * Parameters:
2023 { 2029 {
2024 void *tmp; 2030 void *tmp;
2025 2031
2026 while(!dialog->done) 2032 while(!dialog->done)
2027 { 2033 {
2028 dw_main_iteration(); 2034 _dw_main_iteration();
2029 } 2035 }
2030 dw_event_close(&dialog->eve); 2036 dw_event_close(&dialog->eve);
2031 tmp = dialog->result; 2037 tmp = dialog->result;
2032 free(dialog); 2038 free(dialog);
2033 return tmp; 2039 return tmp;
6509 */ 6515 */
6510 if(DWThread == pthread_self()) 6516 if(DWThread == pthread_self())
6511 { 6517 {
6512 while(pthread_mutex_trylock(mutex) != 0) 6518 while(pthread_mutex_trylock(mutex) != 0)
6513 { 6519 {
6514 dw_main_iteration(); 6520 _dw_main_iteration();
6515 } 6521 }
6516 } 6522 }
6517 else 6523 else
6518 { 6524 {
6519 pthread_mutex_lock(mutex); 6525 pthread_mutex_lock(mutex);