-
Notifications
You must be signed in to change notification settings - Fork 471
Add support for building libdispatch on FreeBSD #319
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
Conversation
For those now following swift-corelibs-dev@:
|
CMakeLists.txt
Outdated
@@ -73,7 +75,8 @@ 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 Android) | |||
CMAKE_SYSTEM_NAME STREQUAL Android OR | |||
CMAKE_SYSTEM_NAME STREQUAL FreeBSD) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a less intrusive patch if you put the conditional above Android rather than below. If you had it above then you'd only generate one line of addition:
if(CMAKE_SYSTEM STREQUAL Linux OR
+ CMAKE_SYSTEM STREQUAL FreeBSD OR
CMAKE_SYSTEM STREQUAL Android)
src/CMakeLists.txt
Outdated
@@ -100,7 +111,8 @@ if(ENABLE_SWIFT) | |||
-fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap | |||
SWIFT_FLAGS | |||
-I ${CMAKE_SOURCE_DIR} | |||
${swift_optimization_flags}) | |||
-I/usr/include | |||
${swift_optimization_flags}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it's changed the indentation here - at least, as far as GitHub is concerned. Why do we need to add /usr/include
here anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is a issue with swiftc driver issue, for some reason it appears to not be appending /usr/include to path. Maybe I should make it a variable and mark it explicitly as a workaround.
I tried to find the root issue in the swift compiler but I couldn't.
And yes, indentation has changed... my IDE insists in reformatting after each line and messed on this and some other places too.
src/CMakeLists.txt
Outdated
@@ -123,11 +135,13 @@ target_include_directories(dispatch | |||
${CMAKE_CURRENT_SOURCE_DIR} | |||
${CMAKE_CURRENT_BINARY_DIR} | |||
${CMAKE_SOURCE_DIR}/private) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend against adding blank lines as it adds more noise to the patch than is necessary to get your changes through
src/block.cpp
Outdated
@@ -28,6 +28,8 @@ | |||
#error Must build without C++ exceptions | |||
#endif | |||
|
|||
#define _Bool bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it might cause problems for other builds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. This shouldn't even be there!
src/event/event_config.h
Outdated
# else | ||
# define DISPATCH_USE_KEVENT_QOS 0 | ||
# endif | ||
|
||
/// The flags here are set unconditionally (from DISPATCH_USE_KEVENT_QOS). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also looks like an odd change - why has the indentation changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These flags were being used everywhere and unconditionally. Originally they were only defined if defined(EV_SET_QOS)
, which is not true on FreeBSD. The indentation didn't change per se, it was moved one level down and indentation corrected accordingly.
- In epoll
KEVENT_FLAG_IMMEDIATE
is emulated with a timeout, the same must be done on kevent (this is not done yet -- this PR is only to get the library building and submitting things in small batches) KEVENT_FLAG_ERROR_EVENTS
already has a emulation (line 611)
@@ -601,7 +614,7 @@ _dispatch_kq_poll(dispatch_wlh_t wlh, dispatch_kevent_t ke, int n, | |||
for (r = 0; r < n; r++) { | |||
ke[r].flags |= EV_RECEIPT; | |||
} | |||
out_n = n; | |||
n_out = n; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bug in your change, or is it a bug in the existing code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug in the existing code. This is probably due to a refactoring and the fact that code with !DISPATCH_USE_KEVENT_QOS
not being exercised recently.
src/event/event_kevent.c
Outdated
@@ -1129,7 +1152,7 @@ _dispatch_event_loop_poke(dispatch_wlh_t wlh, uint64_t dq_state, uint32_t flags) | |||
.ident = 1, | |||
.filter = EVFILT_USER, | |||
.fflags = NOTE_TRIGGER, | |||
.udata = (uintptr_t)DISPATCH_WLH_MANAGER, | |||
.udata = (dispatch_kqueue_udata)DISPATCH_WLH_MANAGER, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a lot of these kind of changes here. It might make sense to break this out (uintptr_t
-> dispatch_kqueue_udata
) into its own change first, which can then be merged ahead of the rest of the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... I think kevent is a horrible mess with these changes, I hate it, but I don't see it could be made much better than this.
I think it would make sense to split into separate changes.
src/event/event_kevent.c
Outdated
@@ -1271,6 +1294,8 @@ _dispatch_event_loop_timer_program(uint32_t tidx, | |||
#endif | |||
}; | |||
|
|||
(void) leeway; // if DISPATCH_HAVE_TIMER_COALESCING == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems odd ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for -Werror
compliance. Maybe it should be:
DISPATCH_NOINLINE
static void
_dispatch_event_loop_timer_program(uint32_t tidx,
uint64_t target, uint64_t leeway, uint16_t action)
{
dispatch_kevent_s ke = {
.ident = DISPATCH_KEVENT_TIMEOUT_IDENT_MASK | tidx,
.filter = EVFILT_TIMER,
.flags = action | EV_ONESHOT,
.fflags = _dispatch_timer_index_to_fflags[tidx],
.data = (int64_t)target,
.udata = (dispatch_kqueue_udata)&_dispatch_timers_heap[tidx],
#if DISPATCH_HAVE_TIMER_COALESCING
.ext[1] = leeway,
#endif
#if DISPATCH_USE_KEVENT_QOS
.qos = _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG,
#endif
};
#if !DISPATCH_HAVE_TIMER_COALESCING
(void) leeway;
#endif
_dispatch_kq_deferred_update(DISPATCH_WLH_ANON, &ke);
}
(added the whole function for more context)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no the change isn't odd, this is because leeway is unused, and it's fine to do it in an unguarded part. (void)foo;
to avoid an unused warning is idiomatic in libdispatch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, a lot is about not being darwin and looks like the linux_* headers.
When the diffs are so small, I think it says we should have a:
generic_unix_port.h
or generic_unix_base.h
header and have small localized ifdefs for unix variants (linux, android, FreeBSD, *BSD, ...).
generic_unix
allows us to leave out a namespace for windows which will definitely not look similar presumably ;)
In general, I think we should check in code that is correct, and not put stubs for things that are obviously non functional: leaving a lock implementation as "no ops" is IMO criminal ;) a build failure is a way better outcome.
ALso for the part about locks, having a single pull request just about that is much better anyway, I agree all the rest makes sense in a single initial porting pull request.
Last, please adhere to the coding style for libdispatch which is:
- no space for casts
- 80 columns wrapping with tabs for indent, 2 incremental tabs for rejects, but no more than 1 level of rejects.
It is almost similar to what vim does with:
set cinoptions=
set cinoptions+=Ls
set cinoptions+=:0,=1s
set cinoptions+=t0
set cinoptions+=+2s,(2s,u2s,W2s
set cinoptions+=j1,J1,m1
os/freebsd_base.h
Outdated
@@ -0,0 +1,125 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file is mostly a copy of linux_base I'd rather have a non_darwin_base
or similar that has a few defines inside for the rare differences between the two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree; a generic_unix_base with a few small #ifdef clauses will be easier to maintain.
os/voucher_activity_private.h
Outdated
@@ -26,7 +26,7 @@ | |||
#include <mach/mach_time.h> | |||
#include <firehose/tracepoint_private.h> | |||
#endif | |||
#ifndef __linux__ | |||
#if !defined(__linux__) && !defined(__FreeBSD__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#if __APPLE__
clearly makes more sense here IMO
os/voucher_private.h
Outdated
@@ -21,7 +21,7 @@ | |||
#ifndef __OS_VOUCHER_PRIVATE__ | |||
#define __OS_VOUCHER_PRIVATE__ | |||
|
|||
#ifndef __linux__ | |||
#if !defined(__linux__) && !defined(__FreeBSD__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#if __APPLE__
private/queue_private.h
Outdated
@@ -227,7 +227,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, _Nullable const pthread_attr_t *_Nullable attr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bogus, please revert this hunk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be a Clang bug then?
/home/rogiel/Swift/swift-corelibs-libdispatch/private/queue_private.h:230:29: error: pointer is missing a nullability type specifier
(_Nonnull, _Nullable, or _Null_unspecified) [-Werror,-Wnullability-completeness]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe not, if pthread_attr_t is a pointer type on FreeBSD, but it's not on Darwin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... thats absolutely the case then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure where I should and and #ifdef for this... the entire function signature? only that line? define a #define NULLBLE_PTHREAD_ATTR _Nullable
on FreeBSD and #define NULLBLE_PTHREAD_ATTR
on other platforms?
src/CMakeLists.txt
Outdated
@@ -100,7 +111,8 @@ if(ENABLE_SWIFT) | |||
-fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap | |||
SWIFT_FLAGS | |||
-I ${CMAKE_SOURCE_DIR} | |||
${swift_optimization_flags}) | |||
-I/usr/include | |||
${swift_optimization_flags}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like you broke the indenting
src/swift/Wrapper.swift
Outdated
@@ -180,7 +180,7 @@ extension DispatchSource : DispatchSourceMachSend, | |||
} | |||
#endif | |||
|
|||
#if !os(Linux) && !os(Android) | |||
#if !os(Linux) && !os(Android) && !os(FreeBSD) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert all in this, FreeBSD has all the underlying filters
src/transform.c
Outdated
@@ -22,14 +22,22 @@ | |||
|
|||
#ifdef __APPLE__ | |||
#include <libkern/OSByteOrder.h> | |||
#elif __linux__ | |||
#elif defined(__linux__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gratuitous change, please revert
tests/bsdtests.h
Outdated
@@ -29,6 +29,10 @@ | |||
#include <CoreFoundation/CoreFoundation.h> | |||
#endif | |||
|
|||
#if defined(__linux__) || defined(__FreeBSD__) | |||
#include <unistd.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just always #include it
it will not harm, and remove the empty lines
tests/dispatch_test.c
Outdated
.ident = fd, | ||
#else | ||
.ident = (uintptr_t) fd, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always do the cast it will not harm
tests/freebsd_port.h
Outdated
@@ -0,0 +1,54 @@ | |||
#include <limits.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems a lot is like the linux port header, same as other comments, should be more of a "non_darwin_port.h"
What I have done:
What still remains to be done:
|
All right @MadCoder, I have fixed the I guess the ugly macro is better than redefining the whole signature? It a private header anyways, maybe it's not that bad. Just to recap, these changes makes the library almost compile on FreeBSD. There are still 4 files with issues:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite better, I like the patch thanks for this, there are a few minor nits to address and I'll test + merge.
os/generic_unix_base.h
Outdated
|
||
#if __has_include(<sys/sysmacros.h>) | ||
#include <sys/sysmacros.h> | ||
#if defined(__FreeBSD) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be __FreeBSD__
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also it's weird it's inside the __has_include(<sys/sysmacros.h>)
, I'd move it outside.
src/event/event_config.h
Outdated
# else | ||
# define DISPATCH_USE_KEVENT_QOS 0 | ||
# endif | ||
|
||
# ifndef KEVENT_FLAG_ERROR_EVENTS | ||
# define KEVENT_FLAG_ERROR_EVENTS 0x002 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the indent is wrong here
src/event/event_config.h
Outdated
@@ -106,6 +104,14 @@ | |||
# define NOTE_FUNLOCK 0x00000100 | |||
# endif | |||
|
|||
/// FreeBSD's kevent does not support those |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double //
triple is for headerdoc.
src/event/event_config.h
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the leading space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All other #defines are with the leading space on this file... should I still remove them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the big hunk of configuration has a lot of nesting so it is pseudo indented for readability, but once it's at top level we don't use leading indent.
ALso I think it is tab-indented for the big fat one at the top not space ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MadCoder changed to a tab AND fixed my tab size... now I can see them 😝
I kept a tab for consistency with the previous defines.
src/event/event_kevent.c
Outdated
@@ -614,6 +631,8 @@ _dispatch_kq_poll(dispatch_wlh_t wlh, dispatch_kevent_t ke, int n, | |||
} | |||
r = kevent_qos(kqfd, ke, n, ke_out, n_out, buf, avail, flags); | |||
#else | |||
(void)buf; // buf is unused here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the comment, (void)variable
means it's unused throughout the repository already
src/event/event_kevent.c
Outdated
@@ -961,7 +983,8 @@ _dispatch_unote_register_muxed(dispatch_unote_t du, dispatch_wlh_t wlh) | |||
#if DISPATCH_USE_KEVENT_QOS | |||
dmn->dmn_kev.qos = _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG; | |||
#endif | |||
dmn->dmn_kev.udata = (uintptr_t)dmn | DISPATCH_KEVENT_MUXED_MARKER; | |||
dmn->dmn_kev.udata = (dispatch_kevent_udata_t)((uintptr_t)dmn | | |||
DISPATCH_KEVENT_MUXED_MARKER); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this indent looks wrong, it should be led by 4 tabs (the 2 from the line above + 2 because it's a split line)
src/io.c
Outdated
@@ -2165,7 +2171,7 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size) | |||
} | |||
advise.ra_offset = op->advise_offset; | |||
op->advise_offset += advise.ra_count; | |||
#ifdef __linux__ | |||
#if __linux__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change is wrong, #if defined()
is what you use elswhere, please revert
src/shims/atomic.h
Outdated
@@ -31,6 +31,10 @@ | |||
#error libdispatch requires C11 with <stdatomic.h> | |||
#endif | |||
|
|||
// FreeBSD only defines _Bool in C mode. In C++ mode _Bool is not being defined. | |||
#if defined(__cplusplus) && defined(__FreeBSD__) | |||
# define _Bool bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove the leading space.
tests/dispatch_test.c
Outdated
@@ -56,7 +56,7 @@ dispatch_test_check_evfilt_read_for_fd(int fd) | |||
int kq = kqueue(); | |||
assert(kq != -1); | |||
struct kevent ke = { | |||
.ident = fd, | |||
.ident = (uintptr_t) fd, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove space after cast
Btw @Rogiel it seems your patch has been stuck for a long time if I read history right. For some reason, when someone pushes a commit on a branch I'm reviewing I get no notification. Do not hesitate to ping me explicitly in the comments with an @MadCoder, that definitely sends me an email that I react to (which is how I looked at it again). I'm really sorry it felt like I forgot you for months, I just don't poll pull requests for lack of time. |
No problem, I just got busy too and my FreeBSD machine died 😕 Okay, I made the changes (except the one about the leading space in the a #define, see the inline comment). I will wait until CI is done in case anything else comes up. |
This is the first commit of several to bring libdispatch support up to date on FreeBSD
@swift-ci please test |
Add support for building libdispatch on FreeBSD Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
This mostly adds
defined(__FreeBSD__)
conditionals where necessary. I am 99% sure this will break builds on Darwin, specially on kevent stuff. I tried to build the Xcode project, but there are a LOT of files missing which probably are internal Apple headers and sources.Please note that the library is not yet completely working on FreeBSD. It compiles, few tests are working, but most are still failing.