Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building libdispatch on FreeBSD #319

Merged
merged 1 commit into from Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Expand Up @@ -83,6 +83,7 @@ option(BUILD_SHARED_LIBS "build shared libraries" ON)
option(ENABLE_TESTING "build libdispatch tests" ON)

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
CMAKE_SYSTEM_NAME STREQUAL Android)
set(USE_GOLD_LINKER_DEFAULT ON)
else()
Expand All @@ -95,6 +96,7 @@ set(DISPATCH_USE_THREAD_LOCAL_STORAGE ${ENABLE_THREAD_LOCAL_STORAGE})

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
CMAKE_SYSTEM_NAME STREQUAL Android OR
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
CMAKE_SYSTEM_NAME STREQUAL Windows)
set(ENABLE_INTERNAL_PTHREAD_WORKQUEUES_DEFAULT ON)
else()
Expand All @@ -120,6 +122,7 @@ option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
CMAKE_SYSTEM_NAME STREQUAL Android OR
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
CMAKE_SYSTEM_NAME STREQUAL Windows)
add_library(BlocksRuntime
STATIC
Expand Down Expand Up @@ -264,6 +267,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL Android)
set(ENABLE_DTRACE_DEFAULT OFF)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
add_definitions(-D_WITH_DPRINTF)
endif()

if(ENABLE_DTRACE STREQUAL "")
find_program(dtrace_EXECUTABLE dtrace)
if(dtrace_EXECUTABLE)
Expand Down
4 changes: 2 additions & 2 deletions cmake/config.h.in
Expand Up @@ -139,10 +139,10 @@
#cmakedefine HAVE_PTHREAD_MACHDEP_H

/* Define to 1 if you have the `pthread_main_np' function. */
#cmakedefine HAVE_PTHREAD_MAIN_NP
#cmakedefine01 HAVE_PTHREAD_MAIN_NP

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

/* Define to 1 if you have the <pthread/qos.h> header file. */
#cmakedefine HAVE_PTHREAD_QOS_H
Expand Down
8 changes: 8 additions & 0 deletions dispatch/base.h
Expand Up @@ -112,6 +112,14 @@
#define DISPATCH_LINUX_UNAVAILABLE()
#endif

#ifdef __FreeBSD__
#define DISPATCH_FREEBSD_UNAVAILABLE() \
DISPATCH_UNAVAILABLE_MSG( \
"This interface is unavailable on FreeBSD systems")
#else
#define DISPATCH_FREEBSD_UNAVAILABLE()
#endif

#ifndef DISPATCH_ALIAS_V2
#if TARGET_OS_MAC
#define DISPATCH_ALIAS_V2(sym) __asm__("_" #sym "$V2")
Expand Down
6 changes: 3 additions & 3 deletions dispatch/dispatch.h
Expand Up @@ -26,8 +26,8 @@
#include <os/availability.h>
#include <TargetConditionals.h>
#include <os/base.h>
#elif defined(__linux__)
#include <os/linux_base.h>
#elif defined(__linux__) || defined(__FreeBSD__)
#include <os/generic_unix_base.h>
#endif

#include <sys/types.h>
Expand All @@ -40,7 +40,7 @@
#endif
#include <fcntl.h>

#if defined(__linux__) && defined(__has_feature)
#if (defined(__linux__) || defined(__FreeBSD__)) && defined(__has_feature)
#if __has_feature(modules)
#if !defined(__arm__)
#include <stdio.h> // for off_t (to match Glibc.modulemap)
Expand Down
2 changes: 1 addition & 1 deletion os/CMakeLists.txt
Expand Up @@ -4,7 +4,7 @@

install(FILES
object.h
linux_base.h
generic_unix_base.h
DESTINATION
"${INSTALL_OS_HEADERS_DIR}")

2 changes: 1 addition & 1 deletion os/Makefile.am
Expand Up @@ -10,7 +10,7 @@ endif

os_HEADERS= \
object.h \
linux_base.h
generic_unix_base.h

noinst_HEADERS= \
object_private.h \
Expand Down
11 changes: 8 additions & 3 deletions os/linux_base.h → os/generic_unix_base.h
Expand Up @@ -10,12 +10,17 @@
*
*/

#ifndef __OS_LINUX_BASE__
#define __OS_LINUX_BASE__
#ifndef __OS_GENERIC_UNIX_BASE__
#define __OS_GENERIC_UNIX_BASE__

#if __has_include(<sys/sysmacros.h>)
#include <sys/sysmacros.h>
#endif

#if defined(__FreeBSD__)
#include <libutil.h>
#include <fcntl.h>
#endif
#include <sys/param.h>

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

#endif /* __OS_LINUX_BASE__ */
#endif /* __OS_GENERIC_UNIX_BASE__ */
4 changes: 2 additions & 2 deletions os/object.h
Expand Up @@ -26,8 +26,8 @@
#include <os/availability.h>
#include <TargetConditionals.h>
#include <os/base.h>
#elif defined(__linux__)
#include <os/linux_base.h>
#elif defined(__linux__) || defined(__FreeBSD__)
#include <os/generic_unix_base.h>
#endif

/*!
Expand Down
2 changes: 1 addition & 1 deletion os/voucher_activity_private.h
Expand Up @@ -26,7 +26,7 @@
#include <mach/mach_time.h>
#include <firehose/tracepoint_private.h>
#endif
#ifndef __linux__
#if __APPLE__
#include <os/base.h>
#include <os/availability.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion os/voucher_private.h
Expand Up @@ -21,7 +21,7 @@
#ifndef __OS_VOUCHER_PRIVATE__
#define __OS_VOUCHER_PRIVATE__

#ifndef __linux__
#if __APPLE__
#include <os/base.h>
#include <os/availability.h>
#endif
Expand Down
8 changes: 4 additions & 4 deletions private/private.h
Expand Up @@ -32,8 +32,8 @@
#include <os/availability.h>
#include <TargetConditionals.h>
#include <os/base.h>
#elif defined(__linux__)
#include <os/linux_base.h>
#elif defined(__linux__) || defined(__FreeBSD__)
#include <os/generic_unix_base.h>
#endif

#if TARGET_OS_MAC
Expand Down Expand Up @@ -172,7 +172,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);

#if TARGET_OS_MAC
#define DISPATCH_COCOA_COMPAT 1
#elif defined(__linux__)
#elif defined(__linux__) || defined(__FreeBSD__)
#define DISPATCH_COCOA_COMPAT 1
#else
#define DISPATCH_COCOA_COMPAT 0
Expand All @@ -184,7 +184,7 @@ void _dispatch_prohibit_transition_to_multithreaded(bool prohibit);

#if TARGET_OS_MAC
typedef mach_port_t dispatch_runloop_handle_t;
#elif defined(__linux__)
#elif defined(__linux__) || defined(__FreeBSD__)
typedef int dispatch_runloop_handle_t;
#else
#error "runloop support not implemented on this platform"
Expand Down
9 changes: 8 additions & 1 deletion private/queue_private.h
Expand Up @@ -49,6 +49,13 @@ enum {

#define DISPATCH_QUEUE_FLAGS_MASK (DISPATCH_QUEUE_OVERCOMMIT)

// On FreeBSD pthread_attr_t is a typedef to a pointer type
#if defined(__FreeBSD__)
# define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR _Nullable
#else
# define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR
#endif

/*!
* @function dispatch_queue_attr_make_with_overcommit
*
Expand Down Expand Up @@ -227,7 +234,7 @@ DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
DISPATCH_NOTHROW
dispatch_queue_t
dispatch_pthread_root_queue_create(const char *_Nullable label,
unsigned long flags, const pthread_attr_t *_Nullable attr,
unsigned long flags, const pthread_attr_t DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR *_Nullable attr,
dispatch_block_t _Nullable configure);

/*!
Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -47,8 +47,8 @@ add_library(dispatch
shims/atomic_sfb.h
shims/getprogname.h
shims/hw_config.h
shims/linux_stubs.c
shims/linux_stubs.h
shims/generic_unix_stubs.c
shims/generic_unix_stubs.h
shims/lock.c
shims/lock.h
shims/perfmon.h
Expand Down Expand Up @@ -101,6 +101,7 @@ if(ENABLE_SWIFT)
-fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap
SWIFT_FLAGS
-I ${CMAKE_SOURCE_DIR}
-I/usr/include
${swift_optimization_flags}
DEPENDS
${CMAKE_SOURCE_DIR}/dispatch/module.modulemap)
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Expand Up @@ -59,8 +59,8 @@ libdispatch_la_SOURCES= \
shims/atomic_sfb.h \
shims/getprogname.h \
shims/hw_config.h \
shims/linux_stubs.c \
shims/linux_stubs.h \
shims/generic_unix_stubs.c \
shims/generic_unix_stubs.h \
shims/lock.c \
shims/lock.h \
shims/perfmon.h \
Expand Down
2 changes: 1 addition & 1 deletion src/block.cpp
Expand Up @@ -109,7 +109,7 @@ extern "C" {
// The compiler hides the name of the function it generates, and changes it if
// we try to reference it directly, but the linker still sees it.
extern void DISPATCH_BLOCK_SPECIAL_INVOKE(void *)
#ifdef __linux__
#if defined(__linux__) || defined(__FreeBSD__)
asm("___dispatch_block_create_block_invoke");
#else
asm("____dispatch_block_create_block_invoke");
Expand Down
26 changes: 19 additions & 7 deletions src/event/event_config.h
Expand Up @@ -76,16 +76,14 @@
#if DISPATCH_EVENT_BACKEND_KEVENT
# if defined(EV_SET_QOS)
# define DISPATCH_USE_KEVENT_QOS 1
# ifndef KEVENT_FLAG_IMMEDIATE
# define KEVENT_FLAG_IMMEDIATE 0x001
# endif
# ifndef KEVENT_FLAG_ERROR_EVENTS
# define KEVENT_FLAG_ERROR_EVENTS 0x002
# endif
# else
# define DISPATCH_USE_KEVENT_QOS 0
# endif

# ifndef KEVENT_FLAG_ERROR_EVENTS
# define KEVENT_FLAG_ERROR_EVENTS 0x002
# endif

# ifdef NOTE_LEEWAY
# define DISPATCH_HAVE_TIMER_COALESCING 1
# else
Expand All @@ -106,6 +104,14 @@
# define NOTE_FUNLOCK 0x00000100
# endif

// FreeBSD's kevent does not support those
# ifndef NOTE_ABSOLUTE
# define NOTE_ABSOLUTE 0
# endif
# ifndef NOTE_EXITSTATUS
# define NOTE_EXITSTATUS 0
# endif

# if HAVE_DECL_NOTE_REAP
# if defined(NOTE_REAP) && defined(__APPLE__)
# undef NOTE_REAP
Expand Down Expand Up @@ -146,9 +152,15 @@

# define DISPATCH_HAVE_TIMER_QOS 0
# define DISPATCH_HAVE_TIMER_COALESCING 0
# define KEVENT_FLAG_IMMEDIATE 0x001
#endif // !DISPATCH_EVENT_BACKEND_KEVENT

// These flags are used by dispatch generic code and
// translated back by the various backends to similar semantics
// hence must be defined even on non Darwin platforms
#ifndef KEVENT_FLAG_IMMEDIATE
# define KEVENT_FLAG_IMMEDIATE 0x001
#endif

#ifdef EV_UDATA_SPECIFIC
# define DISPATCH_EV_DIRECT (EV_UDATA_SPECIFIC|EV_DISPATCH)
#else
Expand Down