Skip to content

Commit f85f987

Browse files
committed
Rework of Dispatch overlay for Linux
The lack of Objective-C and associated bridging when importing the libdispatch header files into Swift caused a number of problems including (a) missing ref count operations in Swift compiled code (b) SR-739, dispatch types not being upcastable to dispatch_object_t (c) SR-740, dispatch types not being upcastable to AnyObject (d) SR-737, dispatch types being imported as COpaquePointer This commit fixes all of these issues by injecting a complete Swift overlay layer that wraps the native libdispatch objects and native APIs. The C header files are now imported into a distinct CDispatch module that is not made available to Swift client code (which continues to import Dispatch). Code is added to Dispatch.swift to mirror the native CDispatch APIs up to the Swift Dispatch API and to wrap/unwrap values as needed across that boundary. This wrapping layer does add some minor space and time overheads, but after extensively exploring implementing the Swift object model within the C code of libdispatch (to avoid introducing this overhead), I concluded that the necessary changes to libdispatch would be fairly invasive, and therefore were not justified at this point in the development of the Swift/Linux port of libdispatch.
1 parent 054d938 commit f85f987

File tree

7 files changed

+578
-65
lines changed

7 files changed

+578
-65
lines changed

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ AC_ARG_WITH([swift-toolchain],
108108
[AS_HELP_STRING([--with-swift-toolchain], [Specify path to Swift toolchain])],
109109
[swift_toolchain_path=${withval}
110110
AC_DEFINE(HAVE_SWIFT, 1, [Define if building for Swift])
111-
SWIFTC="$swift_toolchain_path/bin/swiftc"
111+
SWIFT_TOOLCHAIN_PATH="$swift_toolchain_path"
112112
have_swift=true],
113113
[have_swift=false]
114114
)
115115
AM_CONDITIONAL(HAVE_SWIFT, $have_swift)
116-
AC_SUBST([SWIFTC])
116+
AC_SUBST([SWIFT_TOOLCHAIN_PATH])
117117

118118
AC_USE_SYSTEM_EXTENSIONS
119119
AM_INIT_AUTOMAKE([foreign no-dependencies subdir-objects])

dispatch/dispatch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include <stdarg.h>
3333
#include <unistd.h>
3434
#include <fcntl.h>
35+
#ifndef __APPLE__
36+
#include <stdio.h>
37+
#include <sys/types.h>
38+
#endif
3539

3640
#ifndef __OSX_AVAILABLE_STARTING
3741
#define __OSX_AVAILABLE_STARTING(x, y)

dispatch/module.map

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
module Dispatch [system] {
1+
module Dispatch {
2+
requires blocks
3+
export *
4+
link "dispatch"
5+
link "BlocksRuntime"
6+
}
7+
8+
module CDispatch [system] {
29
umbrella header "dispatch.h"
310
requires blocks
411
export *

src/Makefile.am

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@ else
7373
endif
7474
endif
7575

76+
if HAVE_SWIFT
77+
SWIFTC=${SWIFT_TOOLCHAIN_PATH}/bin/swiftc
78+
SWIFT_RUNTIME_LIBS=$(SWIFT_TOOLCHAIN_PATH)/lib/swift/linux
79+
SWIFT_LIBS=-L$(SWIFT_RUNTIME_LIBS) -lswiftCore
80+
endif
81+
7682
libdispatch_la_LDFLAGS=-avoid-version
77-
libdispatch_la_LIBADD=$(KQUEUE_LIBS) $(PTHREAD_WORKQUEUE_LIBS) $(BSD_OVERLAY_LIBS)
83+
libdispatch_la_LIBADD=$(KQUEUE_LIBS) $(PTHREAD_WORKQUEUE_LIBS) $(BSD_OVERLAY_LIBS) $(SWIFT_LIBS)
7884

7985
if HAVE_DARWIN_LD
8086
libdispatch_la_LDFLAGS+=-Wl,-compatibility_version,1 \

0 commit comments

Comments
 (0)