Merge pull request #252 from apple/das-darwin-008-merge-master

Merge tag darwin/darwin-008 on darwin/trunk.
diff --git a/PATCHES b/PATCHES
index 9fecb83..963c8b1 100644
--- a/PATCHES
+++ b/PATCHES
@@ -312,3 +312,23 @@
 [787dd92] APPLIED rdar://30568673
 [ba4cac5] APPLIED rdar://30568673
 [7974138] APPLIED rdar://30568673
+[cd12dcb] APPLIED rdar://32283666
+[ff05109] APPLIED rdar://32283666
+[73315ee] APPLIED rdar://32283666
+[fcc1924] APPLIED rdar://32283666
+[272e818] APPLIED rdar://32283666
+[b6f8908] APPLIED rdar://32283666
+[a6c16d0] APPLIED rdar://32283666
+[1cc64e1] APPLIED rdar://32283666
+[d137aa4] APPLIED rdar://32283666
+[a69853f] APPLIED rdar://32283666
+[eea0667] APPLIED rdar://32283666
+[f84d21d] APPLIED rdar://32283666
+[3da8398] APPLIED rdar://32283666
+[2df80a3] APPLIED rdar://32283666
+[97a2f06] APPLIED rdar://32283666
+[f76b8f5] APPLIED rdar://32283666
+[3828fbb] APPLIED rdar://32283666
+[5e8789e] APPLIED rdar://32283666
+[3fba60a] APPLIED rdar://32283666
+[d6eb245] APPLIED rdar://32283666
diff --git a/dispatch/dispatch.h b/dispatch/dispatch.h
index a0622e6..6f8b31b 100644
--- a/dispatch/dispatch.h
+++ b/dispatch/dispatch.h
@@ -24,30 +24,17 @@
 #ifdef __APPLE__
 #include <Availability.h>
 #include <TargetConditionals.h>
-#else
-#ifndef API_AVAILABLE
-#define API_AVAILABLE(...)
+#include <os/base.h>
+#elif defined(__linux__)
+#include <os/linux_base.h>
 #endif
-#ifndef API_DEPRECATED
-#define API_DEPRECATED(...)
-#endif
-#ifndef API_UNAVAILABLE
-#define API_UNAVAILABLE(...)
-#endif
-#ifndef API_DEPRECATED_WITH_REPLACEMENT
-#define API_DEPRECATED_WITH_REPLACEMENT(...)
-#endif
-#endif // __APPLE__
 
-#if HAVE_SYS_CDEFS_H
-#include <sys/cdefs.h>
-#endif
 #include <sys/types.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdarg.h>
-#if HAVE_UNISTD_H
+#if !defined(HAVE_UNISTD_H) || HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <fcntl.h>
diff --git a/os/linux_base.h b/os/linux_base.h
index d0048d6..c8b9cad 100644
--- a/os/linux_base.h
+++ b/os/linux_base.h
@@ -15,6 +15,23 @@
 
 #include <sys/param.h>
 
+#if HAVE_SYS_CDEFS_H
+#include <sys/cdefs.h>
+#endif
+
+#ifndef API_AVAILABLE
+#define API_AVAILABLE(...)
+#endif
+#ifndef API_DEPRECATED
+#define API_DEPRECATED(...)
+#endif
+#ifndef API_UNAVAILABLE
+#define API_UNAVAILABLE(...)
+#endif
+#ifndef API_DEPRECATED_WITH_REPLACEMENT
+#define API_DEPRECATED_WITH_REPLACEMENT(...)
+#endif
+
 #if __GNUC__
 #define OS_EXPECT(x, v) __builtin_expect((x), (v))
 #define OS_UNUSED __attribute__((__unused__))
diff --git a/os/object.h b/os/object.h
index b98a93e..100721f 100644
--- a/os/object.h
+++ b/os/object.h
@@ -23,13 +23,11 @@
 
 #ifdef __APPLE__
 #include <Availability.h>
-#include <TargetConditionals.h>
 #include <os/availability.h>
-#endif
-#ifdef __linux__
-#include <os/linux_base.h>
-#else
+#include <TargetConditionals.h>
 #include <os/base.h>
+#elif defined(__linux__)
+#include <os/linux_base.h>
 #endif
 
 /*!
diff --git a/os/object_private.h b/os/object_private.h
index ebaf854..2f8cdf4 100644
--- a/os/object_private.h
+++ b/os/object_private.h
@@ -27,15 +27,9 @@
 #ifndef __OS_OBJECT_PRIVATE__
 #define __OS_OBJECT_PRIVATE__
 
-#if HAVE_SYS_CDEFS_H
-#include <sys/cdefs.h>
-#endif
-#include <stddef.h>
 #include <os/object.h>
-
-#ifndef API_AVAILABLE
-#define API_AVAILABLE(...)
-#endif
+#include <stddef.h>
+#include <stdint.h>
 
 #if __GNUC__
 #define OS_OBJECT_NOTHROW __attribute__((__nothrow__))
diff --git a/src/apply.c b/src/apply.c
index 0f1d85d..40e6f32 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -234,20 +234,21 @@
 	if (slowpath(iterations == 0)) {
 		return;
 	}
-	int32_t thr_cnt = dispatch_hw_config(active_cpus);
-	dispatch_thread_context_t dtctxt = _dispatch_thread_context_find(_dispatch_apply_key);
+	int32_t thr_cnt = (int32_t)dispatch_hw_config(active_cpus);
+	dispatch_thread_context_t dtctxt =
+			_dispatch_thread_context_find(_dispatch_apply_key);
 	size_t nested = dtctxt ? dtctxt->dtc_apply_nesting : 0;
 	dispatch_queue_t old_dq = _dispatch_queue_get_current();
 
 	if (!slowpath(nested)) {
 		nested = iterations;
 	} else {
-		thr_cnt = nested < thr_cnt ? thr_cnt / nested : 1;
+		thr_cnt = nested < (size_t)thr_cnt ? thr_cnt / (int32_t)nested : 1;
 		nested = nested < DISPATCH_APPLY_MAX && iterations < DISPATCH_APPLY_MAX
 				? nested * iterations : DISPATCH_APPLY_MAX;
 	}
-	if (iterations < thr_cnt) {
-		thr_cnt = iterations;
+	if (iterations < (size_t)thr_cnt) {
+		thr_cnt = (int32_t)iterations;
 	}
 	if (slowpath(dq == DISPATCH_APPLY_CURRENT_ROOT_QUEUE)) {
 		dq = old_dq ? old_dq : _dispatch_get_root_queue(
diff --git a/src/init.c b/src/init.c
index 4ccb1f2..22a61e3 100644
--- a/src/init.c
+++ b/src/init.c
@@ -92,7 +92,7 @@
 	r |= sigdelset(&mask, SIGPIPE);
 	r |= sigdelset(&mask, SIGPROF);
 	r |= pthread_sigmask(SIG_BLOCK, &mask, NULL);
-	(void)dispatch_assume_zero(r);
+	return dispatch_assume_zero(r);
 }
 
 #pragma mark -
diff --git a/src/inline_internal.h b/src/inline_internal.h
index d76b77a..53548ed 100644
--- a/src/inline_internal.h
+++ b/src/inline_internal.h
@@ -592,18 +592,18 @@
 }
 
 DISPATCH_ALWAYS_INLINE
-static inline uint32_t
+static inline int32_t
 _dq_state_extract_width_bits(uint64_t dq_state)
 {
 	dq_state &= DISPATCH_QUEUE_WIDTH_MASK;
-	return (uint32_t)(dq_state >> DISPATCH_QUEUE_WIDTH_SHIFT);
+	return (int32_t)(dq_state >> DISPATCH_QUEUE_WIDTH_SHIFT);
 }
 
 DISPATCH_ALWAYS_INLINE
-static inline uint32_t
+static inline int32_t
 _dq_state_available_width(uint64_t dq_state)
 {
-	uint32_t full = DISPATCH_QUEUE_WIDTH_FULL;
+	int32_t full = DISPATCH_QUEUE_WIDTH_FULL;
 	if (likely(!(dq_state & DISPATCH_QUEUE_WIDTH_FULL_BIT))) {
 		return full - _dq_state_extract_width_bits(dq_state);
 	}
@@ -611,11 +611,11 @@
 }
 
 DISPATCH_ALWAYS_INLINE
-static inline uint32_t
+static inline int32_t
 _dq_state_used_width(uint64_t dq_state, uint16_t dq_width)
 {
-	uint32_t full = DISPATCH_QUEUE_WIDTH_FULL;
-	uint32_t width = _dq_state_extract_width_bits(dq_state);
+	int32_t full = DISPATCH_QUEUE_WIDTH_FULL;
+	int32_t width = _dq_state_extract_width_bits(dq_state);
 
 	if (dq_state & DISPATCH_QUEUE_PENDING_BARRIER) {
 		// DISPATCH_QUEUE_PENDING_BARRIER means (dq_width - 1) of the used width
@@ -1030,21 +1030,21 @@
  * possibly 0
  */
 DISPATCH_ALWAYS_INLINE DISPATCH_WARN_RESULT
-static inline uint32_t
-_dispatch_queue_try_reserve_apply_width(dispatch_queue_t dq, uint32_t da_width)
+static inline int32_t
+_dispatch_queue_try_reserve_apply_width(dispatch_queue_t dq, int32_t da_width)
 {
 	uint64_t old_state, new_state;
-	uint32_t width;
+	int32_t width;
 
 	(void)os_atomic_rmw_loop2o(dq, dq_state, old_state, new_state, relaxed, {
-		width = _dq_state_available_width(old_state);
+		width = (int32_t)_dq_state_available_width(old_state);
 		if (unlikely(!width)) {
 			os_atomic_rmw_loop_give_up(return 0);
 		}
 		if (width > da_width) {
 			width = da_width;
 		}
-		new_state = old_state + width * DISPATCH_QUEUE_WIDTH_INTERVAL;
+		new_state = old_state + (uint64_t)width * DISPATCH_QUEUE_WIDTH_INTERVAL;
 	});
 	return width;
 }
@@ -1055,10 +1055,10 @@
  */
 DISPATCH_ALWAYS_INLINE
 static inline void
-_dispatch_queue_relinquish_width(dispatch_queue_t dq, uint32_t da_width)
+_dispatch_queue_relinquish_width(dispatch_queue_t dq, int32_t da_width)
 {
 	(void)os_atomic_sub2o(dq, dq_state,
-			da_width * DISPATCH_QUEUE_WIDTH_INTERVAL, relaxed);
+			(uint64_t)da_width * DISPATCH_QUEUE_WIDTH_INTERVAL, relaxed);
 }
 
 /* Used by target-queue recursing code
@@ -1474,7 +1474,7 @@
 DISPATCH_ALWAYS_INLINE
 static inline void
 _dispatch_root_queue_push_inline(dispatch_queue_t dq, dispatch_object_t _head,
-		dispatch_object_t _tail, unsigned int n)
+		dispatch_object_t _tail, int n)
 {
 	struct dispatch_object_s *head = _head._do, *tail = _tail._do;
 	if (unlikely(_dispatch_queue_push_update_tail_list(dq, head, tail))) {
diff --git a/src/queue.c b/src/queue.c
index 33ebfdf..6d74b79 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -695,7 +695,7 @@
 {
 	dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
 	int32_t thread_pool_size = overcommit ? DISPATCH_WORKQ_MAX_PTHREAD_COUNT :
-			dispatch_hw_config(active_cpus);
+			(int32_t)dispatch_hw_config(active_cpus);
 	if (slowpath(pool_size) && pool_size < thread_pool_size) {
 		thread_pool_size = pool_size;
 	}
diff --git a/src/shims.h b/src/shims.h
index fe9c3e4..8dd23ee 100644
--- a/src/shims.h
+++ b/src/shims.h
@@ -50,7 +50,7 @@
 #endif // HAVE_PTHREAD_WORKQUEUES
 
 #if DISPATCH_USE_INTERNAL_WORKQUEUE
-#include <event/workqueue_internal.h>
+#include "event/workqueue_internal.h"
 #endif
 
 #if HAVE_PTHREAD_NP_H
diff --git a/src/trace.h b/src/trace.h
index 3572204..872cd6f 100644
--- a/src/trace.h
+++ b/src/trace.h
@@ -132,7 +132,7 @@
 DISPATCH_ALWAYS_INLINE
 static inline void
 _dispatch_trace_root_queue_push_list(dispatch_queue_t dq,
-		dispatch_object_t _head, dispatch_object_t _tail, unsigned int n)
+		dispatch_object_t _head, dispatch_object_t _tail, int n)
 {
 	if (slowpath(DISPATCH_QUEUE_PUSH_ENABLED())) {
 		struct dispatch_object_s *dou = _head._do;