1/*
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21/*
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.
25 */
26
27#ifndef __DISPATCH_OS_SHIMS__
28#define __DISPATCH_OS_SHIMS__
29
30#include <pthread.h>
31#if HAVE_PTHREAD_WORKQUEUES
32#include <pthread_workqueue.h>
33#endif
34#if HAVE_PTHREAD_NP_H
35#include <pthread_np.h>
36#endif
37
38#if !HAVE_DECL_FD_COPY
39#define FD_COPY(f, t) (void)(*(t) = *(f))
40#endif
41
42#if TARGET_OS_WIN32
43#define bzero(ptr,len) memset((ptr), 0, (len))
44#define snprintf _snprintf
45
46inline size_t strlcpy(char *dst, const char *src, size_t size) {
47	int res = strlen(dst) + strlen(src) + 1;
48	if (size > 0) {
49		size_t n = size - 1;
50		strncpy(dst, src, n);
51		dst[n] = 0;
52	}
53	return res;
54}
55#endif // TARGET_OS_WIN32
56
57#if !HAVE_NORETURN_BUILTIN_TRAP
58/*
59 * XXXRW: Work-around for possible clang bug in which __builtin_trap() is not
60 * marked noreturn, leading to a build error as dispatch_main() *is* marked
61 * noreturn. Mask by marking __builtin_trap() as noreturn locally.
62 */
63DISPATCH_NORETURN
64void __builtin_trap(void);
65#endif
66
67#if DISPATCH_HW_CONFIG_UP
68#define DISPATCH_ATOMIC_UP 1
69#endif
70
71#include "shims/atomic.h"
72#include "shims/atomic_sfb.h"
73#include "shims/tsd.h"
74#include "shims/hw_config.h"
75#include "shims/perfmon.h"
76
77#include "shims/getprogname.h"
78#include "shims/time.h"
79
80#ifdef __APPLE__
81// Clear the stack before calling long-running thread-handler functions that
82// never return (and don't take arguments), to facilitate leak detection and
83// provide cleaner backtraces. <rdar://problem/9050566>
84#define _dispatch_clear_stack(s) do { \
85		void *a[(s)/sizeof(void*) ? (s)/sizeof(void*) : 1]; \
86		a[0] = pthread_get_stackaddr_np(pthread_self()); \
87		bzero((void*)&a[1], (size_t)(a[0] - (void*)&a[1])); \
88	} while (0)
89#else
90#define _dispatch_clear_stack(s)
91#endif
92
93#endif
94