Mercurial > dwindows
comparison mac/dw.m @ 1155:e6a2f57c0842
Added support for infinite wait for dw_event_wait() on Mac, Unix and Windows.
Return code cleanup for dw_event_* and dw_named_event_* on Windows.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Thu, 15 Sep 2011 03:13:50 +0000 |
parents | 58b5374355ab |
children | f86f556ff29d |
comparison
equal
deleted
inserted
replaced
1154:052f01522c53 | 1155:e6a2f57c0842 |
---|---|
8531 * Parameters: | 8531 * Parameters: |
8532 * eve: The handle to the event returned by dw_event_new(). | 8532 * eve: The handle to the event returned by dw_event_new(). |
8533 */ | 8533 */ |
8534 int dw_event_wait(HEV eve, unsigned long timeout) | 8534 int dw_event_wait(HEV eve, unsigned long timeout) |
8535 { | 8535 { |
8536 int rc; | 8536 int rc; |
8537 struct timeval now; | 8537 |
8538 struct timespec timeo; | 8538 if(!eve) |
8539 | 8539 return DW_ERROR_NON_INIT; |
8540 if(!eve) | 8540 |
8541 return DW_ERROR_NON_INIT; | 8541 if(eve->posted) |
8542 | 8542 return DW_ERROR_GENERAL; |
8543 if(eve->posted) | 8543 |
8544 return DW_ERROR_GENERAL; | 8544 pthread_mutex_lock (&(eve->mutex)); |
8545 | 8545 if(timeout != -1) |
8546 pthread_mutex_lock (&(eve->mutex)); | 8546 { |
8547 gettimeofday(&now, 0); | 8547 struct timeval now; |
8548 timeo.tv_sec = now.tv_sec + (timeout / 1000); | 8548 struct timespec timeo; |
8549 timeo.tv_nsec = now.tv_usec * 1000; | 8549 |
8550 rc = pthread_cond_timedwait (&(eve->event), &(eve->mutex), &timeo); | 8550 gettimeofday(&now, 0); |
8551 pthread_mutex_unlock (&(eve->mutex)); | 8551 timeo.tv_sec = now.tv_sec + (timeout / 1000); |
8552 if(!rc) | 8552 timeo.tv_nsec = now.tv_usec * 1000; |
8553 return DW_ERROR_NONE; | 8553 rc = pthread_cond_timedwait(&(eve->event), &(eve->mutex), &timeo); |
8554 if(rc == ETIMEDOUT) | 8554 } |
8555 return DW_ERROR_TIMEOUT; | 8555 else |
8556 return DW_ERROR_GENERAL; | 8556 rc = pthread_cond_wait(&(eve->event), &(eve->mutex)); |
8557 pthread_mutex_unlock (&(eve->mutex)); | |
8558 if(!rc) | |
8559 return DW_ERROR_NONE; | |
8560 if(rc == ETIMEDOUT) | |
8561 return DW_ERROR_TIMEOUT; | |
8562 return DW_ERROR_GENERAL; | |
8557 } | 8563 } |
8558 | 8564 |
8559 /* | 8565 /* |
8560 * Closes a semaphore created by dw_event_new(). | 8566 * Closes a semaphore created by dw_event_new(). |
8561 * Parameters: | 8567 * Parameters: |
8870 * or -1 if indefinite. | 8876 * or -1 if indefinite. |
8871 */ | 8877 */ |
8872 int dw_named_event_wait(HEV eve, unsigned long timeout) | 8878 int dw_named_event_wait(HEV eve, unsigned long timeout) |
8873 { | 8879 { |
8874 fd_set rd; | 8880 fd_set rd; |
8875 struct timeval tv, *useme; | 8881 struct timeval tv, *useme = NULL; |
8876 int retval = 0; | 8882 int retval = 0; |
8877 char tmp; | 8883 char tmp; |
8878 | 8884 |
8879 if(!eve || eve->alive < 0) | 8885 if(!eve || eve->alive < 0) |
8880 return DW_ERROR_NON_INIT; | 8886 return DW_ERROR_NON_INIT; |
8881 | 8887 |
8882 /* Set the timout or infinite */ | 8888 /* Set the timout or infinite */ |
8883 if(timeout == -1) | 8889 if(timeout != -1) |
8884 useme = NULL; | |
8885 else | |
8886 { | 8890 { |
8887 tv.tv_sec = timeout / 1000; | 8891 tv.tv_sec = timeout / 1000; |
8888 tv.tv_usec = (int)timeout % 1000; | 8892 tv.tv_usec = (int)timeout % 1000; |
8889 | 8893 |
8890 useme = &tv; | 8894 useme = &tv; |