]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/htl/pt-timedblock.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / mach / htl / pt-timedblock.c
1 /* Block a thread with a timeout. Mach version.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <time.h>
22 #include <sys/time.h>
23
24 #include <mach.h>
25 #include <mach/message.h>
26
27 #include <pt-internal.h>
28
29 /* Block THREAD. */
30 error_t
31 __pthread_timedblock (struct __pthread *thread,
32 const struct timespec *abstime, clockid_t clock_id)
33 {
34 error_t err;
35 mach_msg_header_t msg;
36 mach_msg_timeout_t timeout;
37 struct timespec now;
38
39 /* We have an absolute time and now we have to convert it to a
40 relative time. Arg. */
41
42 err = clock_gettime (clock_id, &now);
43 assert (!err);
44
45 if (now.tv_sec > abstime->tv_sec
46 || (now.tv_sec == abstime->tv_sec && now.tv_nsec > abstime->tv_nsec))
47 return ETIMEDOUT;
48
49 timeout = (abstime->tv_sec - now.tv_sec) * 1000;
50
51 if (abstime->tv_nsec >= now.tv_nsec)
52 timeout += (abstime->tv_nsec - now.tv_nsec + 999999) / 1000000;
53 else
54 /* Need to do a carry. */
55 timeout -= (now.tv_nsec - abstime->tv_nsec + 999999) / 1000000;
56
57 err = __mach_msg (&msg, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
58 sizeof msg, thread->wakeupmsg.msgh_remote_port,
59 timeout, MACH_PORT_NULL);
60 if (err == EMACH_RCV_TIMED_OUT)
61 return ETIMEDOUT;
62
63 assert_perror (err);
64 return 0;
65 }