Skip to content

Commit

Permalink
Memory fences for non-Darwin, non-x86 platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed Nov 30, 2016
1 parent 9e9de82 commit 27588c4
Show file tree
Hide file tree
Showing 26 changed files with 149 additions and 65 deletions.
14 changes: 13 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PackageDescription

let package = Package(
var package = Package(
name: "ReactiveSwift",
dependencies: [
.Package(url: "https://github.com/antitypical/Result.git", majorVersion: 3),
Expand All @@ -9,3 +9,15 @@ let package = Package(
"Sources/Deprecations+Removals.swift",
]
)

#if os(Linux)
package.targets = [
Target(name: "ReactiveSwift", dependencies: ["ReactiveSwiftSupport"])
]
#endif

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
package.targets = [
Target(name: "ReactiveSwift", dependencies: [])
]
#endif
151 changes: 91 additions & 60 deletions ReactiveSwift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions Sources/ReactiveSwift/MemoryOrdering.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if os(Linux)
import ReactiveSwiftSupport
#endif

#if os(iOS) || os(tvOS) || os(watchOS)
import Darwin.C.stdatomic
#endif

@inline(__always)
internal func acquireFence() {
#if !(arch(x86_64) || arch(i386))
#if os(iOS) || os(tvOS) || os(watchOS)
atomic_thread_fence(memory_order_acquire)
#else
_reactiveSwift_acquire_fence()
#endif
#endif
}

@inline(__always)
internal func releaseFence() {
#if !(arch(x86_64) || arch(i386))
#if os(iOS) || os(tvOS) || os(watchOS)
atomic_thread_fence(memory_order_release)
#else
_reactiveSwift_release_fence()
#endif
#endif
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions Sources/Signal.swift → Sources/ReactiveSwift/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final class Signal<Value, Error: Swift.Error> {
// occasionally one of the senders waiting on `sendLock`.
if let state = signal.state.swap(nil) {
terminationEvent = event
atomic_thread_fence(memory_order_release)
releaseFence()
terminationState = state

// Writes to `terminationState` are implicitly synchronized. So we do
Expand All @@ -116,7 +116,7 @@ public final class Signal<Value, Error: Swift.Error> {
// guaranteed to be blocked.
if sendLock.try() {
if !terminated, let state = terminationState {
atomic_thread_fence(memory_order_acquire)
acquireFence()
interrupt(state.observers, event: terminationEvent!)
}
sendLock.unlock()
Expand All @@ -136,7 +136,7 @@ public final class Signal<Value, Error: Swift.Error> {
// Check if a downstream consumer or a concurrent sender has
// interrupted the signal.
if let state = terminationState {
atomic_thread_fence(memory_order_acquire)
acquireFence()
interrupt(state.observers, event: terminationEvent!)
shouldDispose = true
}
Expand All @@ -156,7 +156,7 @@ public final class Signal<Value, Error: Swift.Error> {
// lock acquisition above succeeds. So we have to check again if the
// signal is really still alive.
if !terminated {
atomic_thread_fence(memory_order_acquire)
acquireFence()
interrupt(state.observers, event: terminationEvent!)
shouldDispose = true
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions Sources/ReactiveSwiftSupport/ReactiveSwiftSupport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
static void _reactiveSwift_release_fence(void) __attribute__((unused));
static void _reactiveSwift_acquire_fence(void) __attribute__((unused));
10 changes: 10 additions & 0 deletions Sources/ReactiveSwiftSupport/include/ReactiveSwiftSupport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import <stdatomic.h>
#import <string.h>

static void _reactiveSwift_release_fence() {
atomic_thread_fence(memory_order_release);
}

static void _reactiveSwift_acquire_fence() {
atomic_thread_fence(memory_order_acquire);
}

0 comments on commit 27588c4

Please sign in to comment.