2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
27 #ifndef __DISPATCH_SHIMS_TIME__
28 #define __DISPATCH_SHIMS_TIME__
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
34 DISPATCH_ALWAYS_INLINE_NDEBUG
36 _dispatch_contention_usleep(unsigned int us
)
39 #if defined(SWITCH_OPTION_DISPATCH_CONTENTION) && !(TARGET_IPHONE_SIMULATOR && \
40 IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED < 1090)
41 thread_switch(MACH_PORT_NULL
, SWITCH_OPTION_DISPATCH_CONTENTION
, us
);
43 thread_switch(MACH_PORT_NULL
, SWITCH_OPTION_WAIT
, ((us
-1)/1000)+1);
51 static inline unsigned int
52 sleep(unsigned int seconds
)
54 Sleep(seconds
* 1000); // milliseconds
59 uint64_t _dispatch_get_nanoseconds(void);
61 #if defined(__i386__) || defined(__x86_64__) || !HAVE_MACH_ABSOLUTE_TIME
62 // x86 currently implements mach time in nanoseconds
63 // this is NOT likely to change
64 DISPATCH_ALWAYS_INLINE
65 static inline uint64_t
66 _dispatch_time_mach2nano(uint64_t machtime
)
71 DISPATCH_ALWAYS_INLINE
72 static inline uint64_t
73 _dispatch_time_nano2mach(uint64_t nsec
)
78 typedef struct _dispatch_host_time_data_s
{
82 } _dispatch_host_time_data_s
;
83 extern _dispatch_host_time_data_s _dispatch_host_time_data
;
84 void _dispatch_get_host_time_init(void *context
);
86 static inline uint64_t
87 _dispatch_time_mach2nano(uint64_t machtime
)
89 _dispatch_host_time_data_s
*const data
= &_dispatch_host_time_data
;
90 dispatch_once_f(&data
->pred
, NULL
, _dispatch_get_host_time_init
);
92 if (!machtime
|| slowpath(data
->ratio_1_to_1
)) {
95 if (machtime
>= INT64_MAX
) {
98 long double big_tmp
= ((long double)machtime
* data
->frac
) + .5;
99 if (slowpath(big_tmp
>= INT64_MAX
)) {
102 return (uint64_t)big_tmp
;
105 static inline uint64_t
106 _dispatch_time_nano2mach(uint64_t nsec
)
108 _dispatch_host_time_data_s
*const data
= &_dispatch_host_time_data
;
109 dispatch_once_f(&data
->pred
, NULL
, _dispatch_get_host_time_init
);
111 if (!nsec
|| slowpath(data
->ratio_1_to_1
)) {
114 if (nsec
>= INT64_MAX
) {
117 long double big_tmp
= ((long double)nsec
/ data
->frac
) + .5;
118 if (slowpath(big_tmp
>= INT64_MAX
)) {
121 return (uint64_t)big_tmp
;
125 static inline uint64_t
126 _dispatch_absolute_time(void)
128 #if HAVE_MACH_ABSOLUTE_TIME
129 return mach_absolute_time();
130 #elif TARGET_OS_WIN32
132 return QueryPerformanceCounter(&now
) ? now
.QuadPart
: 0;
137 #if HAVE_DECL_CLOCK_UPTIME
138 ret
= clock_gettime(CLOCK_UPTIME
, &ts
);
139 #elif HAVE_DECL_CLOCK_MONOTONIC
140 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
142 #error "clock_gettime: no supported absolute time clock"
144 (void)dispatch_assume_zero(ret
);
146 /* XXXRW: Some kind of overflow detection needed? */
147 return (ts
.tv_sec
* NSEC_PER_SEC
+ ts
.tv_nsec
);
148 #endif // HAVE_MACH_ABSOLUTE_TIME
152 #endif // __DISPATCH_SHIMS_TIME__