Skip to content

Commit f3531a2

Browse files
authored
Merge pull request swiftlang#319 from Rogiel/feature/freebsd-support
Add support for building libdispatch on FreeBSD
2 parents 8b72f76 + 6436a89 commit f3531a2

33 files changed

+178
-72
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ option(BUILD_SHARED_LIBS "build shared libraries" ON)
8383
option(ENABLE_TESTING "build libdispatch tests" ON)
8484

8585
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
86+
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
8687
CMAKE_SYSTEM_NAME STREQUAL Android)
8788
set(USE_GOLD_LINKER_DEFAULT ON)
8889
else()
@@ -95,6 +96,7 @@ set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})
9596

9697
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
9798
CMAKE_SYSTEM_NAME STREQUAL Android OR
99+
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
98100
CMAKE_SYSTEM_NAME STREQUAL Windows)
99101
set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
100102
else()
@@ -120,6 +122,7 @@ option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as
120122

121123
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
122124
CMAKE_SYSTEM_NAME STREQUAL Android OR
125+
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
123126
CMAKE_SYSTEM_NAME STREQUAL Windows)
124127
add_library(BlocksRuntime
125128
STATIC
@@ -264,6 +267,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL Android)
264267
set(ENABLE_DTRACE_DEFAULT OFF)
265268
endif()
266269

270+
if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
271+
add_definitions(-D_WITH_DPRINTF)
272+
endif()
273+
267274
if(ENABLE_DTRACE STREQUAL "")
268275
find_program(dtrace_EXECUTABLE dtrace)
269276
if(dtrace_EXECUTABLE)

cmake/config.h.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@
139139
#cmakedefine HAVE_PTHREAD_MACHDEP_H
140140

141141
/* Define to 1 if you have the `pthread_main_np' function. */
142-
#cmakedefine HAVE_PTHREAD_MAIN_NP
142+
#cmakedefine01 HAVE_PTHREAD_MAIN_NP
143143

144144
/* Define to 1 if you have the <pthread_np.h> header file. */
145-
#cmakedefine HAVE_PTHREAD_NP_H
145+
#cmakedefine01 HAVE_PTHREAD_NP_H
146146

147147
/* Define to 1 if you have the <pthread/qos.h> header file. */
148148
#cmakedefine HAVE_PTHREAD_QOS_H

dispatch/base.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@
112112
#define DISPATCH_LINUX_UNAVAILABLE()
113113
#endif
114114

115+
#ifdef __FreeBSD__
116+
#define DISPATCH_FREEBSD_UNAVAILABLE() \
117+
DISPATCH_UNAVAILABLE_MSG( \
118+
"This interface is unavailable on FreeBSD systems")
119+
#else
120+
#define DISPATCH_FREEBSD_UNAVAILABLE()
121+
#endif
122+
115123
#ifndef DISPATCH_ALIAS_V2
116124
#if TARGET_OS_MAC
117125
#define DISPATCH_ALIAS_V2(sym) __asm__("_" #sym "$V2")

dispatch/dispatch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include <os/availability.h>
2727
#include <TargetConditionals.h>
2828
#include <os/base.h>
29-
#elif defined(__linux__)
30-
#include <os/linux_base.h>
29+
#elif defined(__linux__) || defined(__FreeBSD__)
30+
#include <os/generic_unix_base.h>
3131
#endif
3232

3333
#include <sys/types.h>
@@ -40,7 +40,7 @@
4040
#endif
4141
#include <fcntl.h>
4242

43-
#if defined(__linux__) && defined(__has_feature)
43+
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__has_feature)
4444
#if __has_feature(modules)
4545
#if !defined(__arm__)
4646
#include <stdio.h> // for off_t (to match Glibc.modulemap)

os/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
install(FILES
66
object.h
7-
linux_base.h
7+
generic_unix_base.h
88
DESTINATION
99
"${INSTALL_OS_HEADERS_DIR}")
1010

os/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ endif
1010

1111
os_HEADERS= \
1212
object.h \
13-
linux_base.h
13+
generic_unix_base.h
1414

1515
noinst_HEADERS= \
1616
object_private.h \

os/linux_base.h renamed to os/generic_unix_base.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
*
1111
*/
1212

13-
#ifndef __OS_LINUX_BASE__
14-
#define __OS_LINUX_BASE__
13+
#ifndef __OS_GENERIC_UNIX_BASE__
14+
#define __OS_GENERIC_UNIX_BASE__
1515

1616
#if __has_include(<sys/sysmacros.h>)
1717
#include <sys/sysmacros.h>
1818
#endif
19+
20+
#if defined(__FreeBSD__)
21+
#include <libutil.h>
22+
#include <fcntl.h>
23+
#endif
1924
#include <sys/param.h>
2025

2126
#if __has_include(<sys/cdefs.h>)
@@ -120,4 +125,4 @@ enum { __VA_ARGS__ }; typedef _type _name##_t
120125
#endif
121126
#define OS_NOTHROW
122127

123-
#endif /* __OS_LINUX_BASE__ */
128+
#endif /* __OS_GENERIC_UNIX_BASE__ */

os/object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include <os/availability.h>
2727
#include <TargetConditionals.h>
2828
#include <os/base.h>
29-
#elif defined(__linux__)
30-
#include <os/linux_base.h>
29+
#elif defined(__linux__) || defined(__FreeBSD__)
30+
#include <os/generic_unix_base.h>
3131
#endif
3232

3333
/*!

os/voucher_activity_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <mach/mach_time.h>
2727
#include <firehose/tracepoint_private.h>
2828
#endif
29-
#ifndef __linux__
29+
#if __APPLE__
3030
#include <os/base.h>
3131
#include <os/availability.h>
3232
#endif

os/voucher_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef __OS_VOUCHER_PRIVATE__
2222
#define __OS_VOUCHER_PRIVATE__
2323

24-
#ifndef __linux__
24+
#if __APPLE__
2525
#include <os/base.h>
2626
#include <os/availability.h>
2727
#endif

private/private.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#include <os/availability.h>
3333
#include <TargetConditionals.h>
3434
#include <os/base.h>
35-
#elif defined(__linux__)
36-
#include <os/linux_base.h>
35+
#elif defined(__linux__) || defined(__FreeBSD__)
36+
#include <os/generic_unix_base.h>
3737
#endif
3838

3939
#if TARGET_OS_MAC
@@ -172,7 +172,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
172172

173173
#if TARGET_OS_MAC
174174
#define DISPATCH_COCOA_COMPAT 1
175-
#elif defined(__linux__)
175+
#elif defined(__linux__) || defined(__FreeBSD__)
176176
#define DISPATCH_COCOA_COMPAT 1
177177
#else
178178
#define DISPATCH_COCOA_COMPAT 0
@@ -184,7 +184,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);
184184

185185
#if TARGET_OS_MAC
186186
typedef mach_port_t dispatch_runloop_handle_t;
187-
#elif defined(__linux__)
187+
#elif defined(__linux__) || defined(__FreeBSD__)
188188
typedef int dispatch_runloop_handle_t;
189189
#else
190190
#error "runloop support not implemented on this platform"

private/queue_private.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ enum {
4949

5050
#define DISPATCH_QUEUE_FLAGS_MASK (DISPATCH_QUEUE_OVERCOMMIT)
5151

52+
// On FreeBSD pthread_attr_t is a typedef to a pointer type
53+
#if defined(__FreeBSD__)
54+
# define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR _Nullable
55+
#else
56+
# define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR
57+
#endif
58+
5259
/*!
5360
* @function dispatch_queue_attr_make_with_overcommit
5461
*
@@ -227,7 +234,7 @@ DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
227234
DISPATCH_NOTHROW
228235
dispatch_queue_t
229236
dispatch_pthread_root_queue_create(const char *_Nullable label,
230-
unsigned long flags, const pthread_attr_t *_Nullable attr,
237+
unsigned long flags, const pthread_attr_t DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR *_Nullable attr,
231238
dispatch_block_t _Nullable configure);
232239

233240
/*!

src/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ add_library(dispatch
4747
shims/atomic_sfb.h
4848
shims/getprogname.h
4949
shims/hw_config.h
50-
shims/linux_stubs.c
51-
shims/linux_stubs.h
50+
shims/generic_unix_stubs.c
51+
shims/generic_unix_stubs.h
5252
shims/lock.c
5353
shims/lock.h
5454
shims/perfmon.h
@@ -101,6 +101,7 @@ if(ENABLE_SWIFT)
101101
-fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap
102102
SWIFT_FLAGS
103103
-I ${CMAKE_SOURCE_DIR}
104+
-I/usr/include
104105
${swift_optimization_flags}
105106
DEPENDS
106107
${CMAKE_SOURCE_DIR}/dispatch/module.modulemap)

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ libdispatch_la_SOURCES= \
5959
shims/atomic_sfb.h \
6060
shims/getprogname.h \
6161
shims/hw_config.h \
62-
shims/linux_stubs.c \
63-
shims/linux_stubs.h \
62+
shims/generic_unix_stubs.c \
63+
shims/generic_unix_stubs.h \
6464
shims/lock.c \
6565
shims/lock.h \
6666
shims/perfmon.h \

src/block.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extern "C" {
109109
// The compiler hides the name of the function it generates, and changes it if
110110
// we try to reference it directly, but the linker still sees it.
111111
extern void DISPATCH_BLOCK_SPECIAL_INVOKE(void *)
112-
#ifdef __linux__
112+
#if defined(__linux__) || defined(__FreeBSD__)
113113
asm("___dispatch_block_create_block_invoke");
114114
#else
115115
asm("____dispatch_block_create_block_invoke");

src/event/event_config.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,14 @@
7676
#if DISPATCH_EVENT_BACKEND_KEVENT
7777
# if defined(EV_SET_QOS)
7878
# define DISPATCH_USE_KEVENT_QOS 1
79-
# ifndef KEVENT_FLAG_IMMEDIATE
80-
# define KEVENT_FLAG_IMMEDIATE 0x001
81-
# endif
82-
# ifndef KEVENT_FLAG_ERROR_EVENTS
83-
# define KEVENT_FLAG_ERROR_EVENTS 0x002
84-
# endif
8579
# else
8680
# define DISPATCH_USE_KEVENT_QOS 0
8781
# endif
8882

83+
# ifndef KEVENT_FLAG_ERROR_EVENTS
84+
# define KEVENT_FLAG_ERROR_EVENTS 0x002
85+
# endif
86+
8987
# ifdef NOTE_LEEWAY
9088
# define DISPATCH_HAVE_TIMER_COALESCING 1
9189
# else
@@ -106,6 +104,14 @@
106104
# define NOTE_FUNLOCK 0x00000100
107105
# endif
108106

107+
// FreeBSD's kevent does not support those
108+
# ifndef NOTE_ABSOLUTE
109+
# define NOTE_ABSOLUTE 0
110+
# endif
111+
# ifndef NOTE_EXITSTATUS
112+
# define NOTE_EXITSTATUS 0
113+
# endif
114+
109115
# if HAVE_DECL_NOTE_REAP
110116
# if defined(NOTE_REAP) && defined(__APPLE__)
111117
# undef NOTE_REAP
@@ -146,9 +152,15 @@
146152

147153
# define DISPATCH_HAVE_TIMER_QOS 0
148154
# define DISPATCH_HAVE_TIMER_COALESCING 0
149-
# define KEVENT_FLAG_IMMEDIATE 0x001
150155
#endif // !DISPATCH_EVENT_BACKEND_KEVENT
151156

157+
// These flags are used by dispatch generic code and
158+
// translated back by the various backends to similar semantics
159+
// hence must be defined even on non Darwin platforms
160+
#ifndef KEVENT_FLAG_IMMEDIATE
161+
# define KEVENT_FLAG_IMMEDIATE 0x001
162+
#endif
163+
152164
#ifdef EV_UDATA_SPECIFIC
153165
# define DISPATCH_EV_DIRECT (EV_UDATA_SPECIFIC|EV_DISPATCH)
154166
#else

0 commit comments

Comments
 (0)