[BZ #2172]
[glibc.git] / sysdeps / mach / nanosleep.c
blobe433adb8bc26eefb4e83400b0f5aacf30d338350
1 /* nanosleep -- sleep for a period specified with a struct timespec
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <mach.h>
22 #include <sys/time.h>
23 #include <unistd.h>
25 int
26 __nanosleep (const struct timespec *requested_time,
27 struct timespec *remaining)
29 mach_port_t recv;
30 struct timeval before, after;
31 const mach_msg_timeout_t ms
32 = requested_time->tv_sec * 1000
33 + (requested_time->tv_nsec + 999999) / 1000000;
35 recv = __mach_reply_port ();
37 if (remaining && __gettimeofday (&before, NULL) < 0)
38 return -1;
39 (void) __mach_msg (NULL, MACH_RCV_MSG|MACH_RCV_TIMEOUT|MACH_RCV_INTERRUPT,
40 0, 0, recv, ms, MACH_PORT_NULL);
41 __mach_port_destroy (mach_task_self (), recv);
42 if (remaining && __gettimeofday (&after, NULL) < 0)
43 return -1;
45 if (remaining)
47 timersub (&after, &before, &after);
48 TIMEVAL_TO_TIMESPEC (&after, remaining);
51 return 0;
53 libc_hidden_def (__nanosleep)
54 weak_alias (__nanosleep, nanosleep)