Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | /* * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <errno.h> #include <sys/time.h> #include <mach/mach_error.h> #include <mach/mach_time.h> #include <stdio.h> #ifdef BUILDING_VARIANT #include "pthread_internals.h" extern int __unix_conforming; extern mach_port_t clock_port; extern semaphore_t clock_sem; int nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) { kern_return_t kret; int ret; mach_timespec_t remain; mach_timespec_t current; if (__unix_conforming == 0) __unix_conforming = 1; if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec >= NSEC_PER_SEC)) { errno = EINVAL; return -1; } if (remaining_time != NULL) { kret = clock_get_time(clock_port, ¤t); if (kret != KERN_SUCCESS) { fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(ret)); return -1; } } ret = __semwait_signal(clock_sem, MACH_PORT_NULL, 1, 1, requested_time->tv_sec, requested_time->tv_nsec); if (ret < 0) { if (errno == ETIMEDOUT) { return 0; } else if (errno == EINTR) { if (remaining_time != NULL) { ret = clock_get_time(clock_port, &remain); if (ret != KERN_SUCCESS) { fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(ret)); return -1; } /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */ ADD_MACH_TIMESPEC(¤t, requested_time); SUB_MACH_TIMESPEC(¤t, &remain); remaining_time->tv_sec = current.tv_sec; remaining_time->tv_nsec = current.tv_nsec; } } else { errno = EINVAL; } } return -1; } #else /* BUILDING_VARIANT */ int nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) { kern_return_t ret; mach_timespec_t remain; mach_timespec_t current; uint64_t end; static double ratio = 0.0, rratio; if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec > NSEC_PER_SEC)) { errno = EINVAL; return -1; } if (ratio == 0.0) { struct mach_timebase_info info; ret = mach_timebase_info(&info); if (ret != KERN_SUCCESS) { fprintf(stderr, "mach_timebase_info() failed: %s\n", mach_error_string(ret)); errno = EAGAIN; return -1; } ratio = (double)info.numer / ((double)info.denom * NSEC_PER_SEC); rratio = (double)info.denom / (double)info.numer; } /* use rratio to avoid division */ end = mach_absolute_time() + (uint64_t)(((double)requested_time->tv_sec * NSEC_PER_SEC + (double)requested_time->tv_nsec) * rratio); ret = mach_wait_until(end); if (ret != KERN_SUCCESS) { if (ret == KERN_ABORTED) { errno = EINTR; if (remaining_time != NULL) { uint64_t now = mach_absolute_time(); double delta; if (now > end) now = end; delta = (end - now) * ratio; remaining_time->tv_sec = delta; remaining_time->tv_nsec = NSEC_PER_SEC * (delta - remaining_time->tv_sec); } } else { errno = EINVAL; } return -1; } return 0; } #endif /* BUILDING_VARIANT */ |