1 |
/* Block a thread with a timeout. Mach version. |
/* Block a thread with a timeout. Mach version. |
2 |
Copyright (C) 2000,02 Free Software Foundation, Inc. |
Copyright (C) 2000, 2002, 2005 Free Software Foundation, Inc. |
3 |
This file is part of the GNU C Library. |
This file is part of the GNU C Library. |
4 |
|
|
5 |
The GNU C Library is free software; you can redistribute it and/or |
The GNU C Library is free software; you can redistribute it and/or |
34 |
{ |
{ |
35 |
error_t err; |
error_t err; |
36 |
mach_msg_header_t msg; |
mach_msg_header_t msg; |
37 |
mach_msg_timeout_t ms; |
mach_msg_timeout_t timeout; |
38 |
struct timeval now; |
struct timeval now; |
39 |
|
|
40 |
/* We have an absolute time and now we have to convert it to a |
/* We have an absolute time and now we have to convert it to a |
43 |
err = gettimeofday(&now, NULL); |
err = gettimeofday(&now, NULL); |
44 |
assert (! err); |
assert (! err); |
45 |
|
|
46 |
ms = abstime->tv_sec * 1000 + (abstime->tv_nsec + 999999) / 1000000 |
if (now.tv_sec > abstime->tv_sec |
47 |
- now.tv_sec * 1000 - (now.tv_usec + 999) / 1000; |
|| (now.tv_sec == abstime->tv_sec |
48 |
|
&& now.tv_usec > ((abstime->tv_nsec + 999) / 1000))) |
|
if (ms <= 0) |
|
49 |
return ETIMEDOUT; |
return ETIMEDOUT; |
50 |
|
|
51 |
|
timeout = (abstime->tv_sec - now.tv_sec) * 1000; |
52 |
|
|
53 |
|
if (((abstime->tv_nsec + 999) / 1000) >= now.tv_usec) |
54 |
|
timeout -= (((abstime->tv_nsec + 999) / 1000) - now.tv_usec + 999) / 1000; |
55 |
|
else |
56 |
|
/* Need to do a carry. */ |
57 |
|
timeout -= 1000 + ((abstime->tv_nsec + 999999) / 1000000) |
58 |
|
- (now.tv_usec + 999) / 1000; |
59 |
|
|
60 |
err = __mach_msg (&msg, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, |
err = __mach_msg (&msg, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, |
61 |
sizeof msg, thread->wakeupmsg.msgh_remote_port, |
sizeof msg, thread->wakeupmsg.msgh_remote_port, |
62 |
ms, MACH_PORT_NULL); |
timeout, MACH_PORT_NULL); |
63 |
if (err == EMACH_RCV_TIMED_OUT) |
if (err == EMACH_RCV_TIMED_OUT) |
64 |
return ETIMEDOUT; |
return ETIMEDOUT; |
65 |
|
|