[libdispatch-dev] [PATCH 11/17] move mach2nano/nano2mach from source.c to shims/

Paolo Bonzini bonzini at gnu.org
Sun Oct 18 08:04:11 PDT 2009


This code does not really belong in source.c, we have a better
place for OS-specific stuff.
---
 src/Makefile.am  |    3 +-
 src/shims/time.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shims/time.h |    9 +++++++
 src/source.c     |   51 ----------------------------------------
 4 files changed, 79 insertions(+), 52 deletions(-)
 create mode 100644 src/shims/time.c

diff --git a/src/Makefile.am b/src/Makefile.am
index f4b2cde..360f1d8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -16,7 +16,8 @@ libdispatch_la_SOURCES=	\
 	time.c
 
 libshims_la_SOURCES=	\
-	shims/mach.c
+	shims/mach.c	\
+	shims/time.c
 
 libdispatch_la_CFLAGS=-Wall
 INCLUDES=-I$(top_builddir) -I$(top_srcdir) \
diff --git a/src/shims/time.c b/src/shims/time.c
new file mode 100644
index 0000000..26f006b
--- /dev/null
+++ b/src/shims/time.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+// for architectures that don't always return mach_absolute_time() in nanoseconds
+#if !defined(__i386__) && !defined(__x86_64__) && defined(HAVE_MACH_ABSOLUTE_TIME)
+static mach_timebase_info_data_t tbi;
+static dispatch_once_t tbi_pred;
+
+static void
+_dispatch_convert_init(void *context __attribute__((unused)))
+{
+        dispatch_assume_zero(mach_timebase_info(&tbi));
+}
+
+uint64_t
+_dispatch_convert_mach2nano(uint64_t val)
+{
+#ifdef __LP64__
+        __uint128_t tmp;
+#else
+        long double tmp;
+#endif
+
+        dispatch_once_f(&tbi_pred, NULL, _dispatch_convert_init);
+
+        tmp = val;
+        tmp *= tbi.numer;
+        tmp /= tbi.denom;
+
+        return tmp;
+}
+
+uint64_t
+_dispatch_convert_nano2mach(uint64_t val)
+{
+#ifdef __LP64__
+        __uint128_t tmp;
+#else
+        long double tmp;
+#endif
+
+        dispatch_once_f(&tbi_pred, NULL, _dispatch_convert_init);
+
+        tmp = val;
+        tmp *= tbi.denom;
+        tmp /= tbi.numer;
+
+        return tmp;
+}
+#endif
+
diff --git a/src/shims/time.h b/src/shims/time.h
index b786b88..455666b 100644
--- a/src/shims/time.h
+++ b/src/shims/time.h
@@ -27,6 +27,15 @@
 #ifndef __DISPATCH_SHIMS_TIME__
 #define __DISPATCH_SHIMS_TIME__
 
+#if defined(__i386__) || defined(__x86_64__) || !defined(HAVE_MACH_ABSOLUTE_TIME)
+// these architectures always return mach_absolute_time() in nanoseconds
+#define _dispatch_convert_mach2nano(x) (x)
+#define _dispatch_convert_nano2mach(x) (x)
+#else
+extern uint64_t _dispatch_convert_mach2nano(uint64_t val);
+extern uint64_t _dispatch_convert_nano2mach(uint64_t val);
+#endif
+
 static inline uint64_t
 _dispatch_absolute_time(void)
 {
diff --git a/src/source.c b/src/source.c
index 031d204..b4f769d 100644
--- a/src/source.c
+++ b/src/source.c
@@ -1423,57 +1423,6 @@ _dispatch_run_timers(void)
 	}
 }
 
-#if defined(__i386__) || defined(__x86_64__) || !defined(HAVE_MACH_ABSOLUTE_TIME)
-// these architectures always return mach_absolute_time() in nanoseconds
-#define _dispatch_convert_mach2nano(x) (x)
-#define _dispatch_convert_nano2mach(x) (x)
-#else
-static mach_timebase_info_data_t tbi;
-static dispatch_once_t tbi_pred;
-
-static void
-_dispatch_convert_init(void *context __attribute__((unused)))
-{
-	dispatch_assume_zero(mach_timebase_info(&tbi));
-}
-
-static uint64_t
-_dispatch_convert_mach2nano(uint64_t val)
-{
-#ifdef __LP64__
-	__uint128_t tmp;
-#else
-	long double tmp;
-#endif
-
-	dispatch_once_f(&tbi_pred, NULL, _dispatch_convert_init);
-
-	tmp = val;
-	tmp *= tbi.numer;
-	tmp /= tbi.denom;
-
-	return tmp;
-}
-
-static uint64_t
-_dispatch_convert_nano2mach(uint64_t val)
-{
-#ifdef __LP64__
-	__uint128_t tmp;
-#else
-	long double tmp;
-#endif
-
-	dispatch_once_f(&tbi_pred, NULL, _dispatch_convert_init);
-
-	tmp = val;
-	tmp *= tbi.denom;
-	tmp /= tbi.numer;
-
-	return tmp;
-}
-#endif
-
 // approx 1 year (60s * 60m * 24h * 365d)
 #define FOREVER_SEC 3153600l
 #define FOREVER_NSEC 31536000000000000ull
-- 
1.6.2.5





More information about the libdispatch-dev mailing list