Mercurial > dwindows
comparison mac/dw.m @ 725:4e09c92363df
Experimental changes to dw_main_sleep() and dw_main_iteration() to hopefully solve some issues.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Mon, 14 Mar 2011 18:26:50 +0000 |
parents | 41080d22edc8 |
children | ecf47778caff |
comparison
equal
deleted
inserted
replaced
724:41080d22edc8 | 725:4e09c92363df |
---|---|
88 SignalHandler *Root = NULL; | 88 SignalHandler *Root = NULL; |
89 | 89 |
90 /* Some internal prototypes */ | 90 /* Some internal prototypes */ |
91 static void _do_resize(Box *thisbox, int x, int y); | 91 static void _do_resize(Box *thisbox, int x, int y); |
92 int _remove_userdata(UserData **root, char *varname, int all); | 92 int _remove_userdata(UserData **root, char *varname, int all); |
93 void _dw_main_iteration(void); | 93 int _dw_main_iteration(NSDate *date); |
94 | 94 |
95 SignalHandler *_get_handler(HWND window, int messageid) | 95 SignalHandler *_get_handler(HWND window, int messageid) |
96 { | 96 { |
97 SignalHandler *tmp = Root; | 97 SignalHandler *tmp = Root; |
98 | 98 |
1928 * Parameters: | 1928 * Parameters: |
1929 * milliseconds: Number of milliseconds to run the loop for. | 1929 * milliseconds: Number of milliseconds to run the loop for. |
1930 */ | 1930 */ |
1931 void API dw_main_sleep(int milliseconds) | 1931 void API dw_main_sleep(int milliseconds) |
1932 { | 1932 { |
1933 struct timeval tv, start; | |
1934 DWTID curr = pthread_self(); | 1933 DWTID curr = pthread_self(); |
1935 | |
1936 gettimeofday(&start, NULL); | |
1937 | 1934 |
1938 if(DWThread == (DWTID)-1 || DWThread == curr) | 1935 if(DWThread == (DWTID)-1 || DWThread == curr) |
1939 { | 1936 { |
1940 DWTID orig = DWThread; | 1937 DWTID orig = DWThread; |
1941 | 1938 NSDate *until = [NSDate dateWithTimeIntervalSinceNow:(milliseconds/1000.0)]; |
1942 gettimeofday(&tv, NULL); | |
1943 | 1939 |
1944 dw_mutex_lock(DWRunMutex); | 1940 dw_mutex_lock(DWRunMutex); |
1945 if(orig == (DWTID)-1) | 1941 if(orig == (DWTID)-1) |
1946 { | 1942 { |
1947 DWThread = curr; | 1943 DWThread = curr; |
1948 } | 1944 } |
1949 while(((tv.tv_sec - start.tv_sec)*1000) + ((tv.tv_usec - start.tv_usec)/1000) <= milliseconds) | 1945 /* Process any pending events */ |
1946 while(_dw_main_iteration(until)) | |
1950 { | 1947 { |
1951 _dw_main_iteration(); | 1948 /* Just loop */ |
1952 gettimeofday(&tv, NULL); | |
1953 } | 1949 } |
1954 if(orig == (DWTID)-1) | 1950 if(orig == (DWTID)-1) |
1955 { | 1951 { |
1956 DWThread = orig; | 1952 DWThread = orig; |
1957 } | 1953 } |
1958 dw_mutex_unlock(DWRunMutex); | 1954 dw_mutex_unlock(DWRunMutex); |
1959 } | 1955 } |
1960 else | 1956 else |
1957 { | |
1961 usleep(milliseconds * 1000); | 1958 usleep(milliseconds * 1000); |
1959 } | |
1962 } | 1960 } |
1963 | 1961 |
1964 /* Internal version that doesn't lock the run mutex */ | 1962 /* Internal version that doesn't lock the run mutex */ |
1965 void _dw_main_iteration(void) | 1963 int _dw_main_iteration(NSDate *date) |
1966 { | 1964 { |
1967 NSDate *distant_future = [NSDate distantFuture]; | |
1968 NSEvent *event = [DWApp nextEventMatchingMask:NSAnyEventMask | 1965 NSEvent *event = [DWApp nextEventMatchingMask:NSAnyEventMask |
1969 untilDate:distant_future | 1966 untilDate:date |
1970 inMode:NSDefaultRunLoopMode | 1967 inMode:NSDefaultRunLoopMode |
1971 dequeue:YES]; | 1968 dequeue:YES]; |
1972 if(event) | 1969 if(event) |
1973 { | 1970 { |
1974 [DWApp sendEvent:event]; | 1971 [DWApp sendEvent:event]; |
1972 [DWApp updateWindows]; | |
1973 return 1; | |
1975 } | 1974 } |
1975 return 0; | |
1976 } | 1976 } |
1977 | 1977 |
1978 /* | 1978 /* |
1979 * Processes a single message iteration and returns. | 1979 * Processes a single message iteration and returns. |
1980 */ | 1980 */ |
1981 void API dw_main_iteration(void) | 1981 void API dw_main_iteration(void) |
1982 { | 1982 { |
1983 dw_mutex_lock(DWRunMutex); | 1983 dw_mutex_lock(DWRunMutex); |
1984 _dw_main_iteration(); | 1984 _dw_main_iteration([NSDate distantPast]); |
1985 dw_mutex_unlock(DWRunMutex); | 1985 dw_mutex_unlock(DWRunMutex); |
1986 } | 1986 } |
1987 | 1987 |
1988 /* | 1988 /* |
1989 * Cleanly terminates a DW session, should be signal handler safe. | 1989 * Cleanly terminates a DW session, should be signal handler safe. |
2244 { | 2244 { |
2245 void *tmp; | 2245 void *tmp; |
2246 | 2246 |
2247 while(!dialog->done) | 2247 while(!dialog->done) |
2248 { | 2248 { |
2249 _dw_main_iteration(); | 2249 _dw_main_iteration([NSDate dateWithTimeIntervalSinceNow:0.01]); |
2250 } | 2250 } |
2251 dw_event_close(&dialog->eve); | 2251 dw_event_close(&dialog->eve); |
2252 tmp = dialog->result; | 2252 tmp = dialog->result; |
2253 free(dialog); | 2253 free(dialog); |
2254 return tmp; | 2254 return tmp; |
6751 /* | 6751 /* |
6752 * Returns the handle to an unnamed mutex semaphore. | 6752 * Returns the handle to an unnamed mutex semaphore. |
6753 */ | 6753 */ |
6754 HMTX dw_mutex_new(void) | 6754 HMTX dw_mutex_new(void) |
6755 { | 6755 { |
6756 HMTX mutex = malloc(sizeof(pthread_mutex_t)); | 6756 HMTX mutex = malloc(sizeof(pthread_mutex_t)); |
6757 | 6757 #if 0 |
6758 pthread_mutex_init(mutex, NULL); | 6758 pthread_mutexattr_t attr; |
6759 return mutex; | 6759 |
6760 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
6761 pthread_mutex_init(mutex, &attr); | |
6762 #endif | |
6763 pthread_mutex_init(mutex, NULL); | |
6764 return mutex; | |
6760 } | 6765 } |
6761 | 6766 |
6762 /* | 6767 /* |
6763 * Closes a semaphore created by dw_mutex_new(). | 6768 * Closes a semaphore created by dw_mutex_new(). |
6764 * Parameters: | 6769 * Parameters: |
6787 */ | 6792 */ |
6788 if(DWThread == pthread_self()) | 6793 if(DWThread == pthread_self()) |
6789 { | 6794 { |
6790 while(pthread_mutex_trylock(mutex) != 0) | 6795 while(pthread_mutex_trylock(mutex) != 0) |
6791 { | 6796 { |
6792 _dw_main_iteration(); | 6797 /* Process any pending events */ |
6798 while(_dw_main_iteration([NSDate dateWithTimeIntervalSinceNow:0.01])) | |
6799 { | |
6800 /* Just loop */ | |
6801 } | |
6793 } | 6802 } |
6794 } | 6803 } |
6795 else | 6804 else |
6796 { | 6805 { |
6797 pthread_mutex_lock(mutex); | 6806 pthread_mutex_lock(mutex); |