Skip to content

Commit

Permalink
Merge pull request #27 from heiberg/remove_osspinlock
Browse files Browse the repository at this point in the history
Remove OSSpinLockSynchronization
  • Loading branch information
mishagray committed Jan 4, 2016
2 parents e141944 + 08c1a7f commit 6745bb8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 108 deletions.
2 changes: 1 addition & 1 deletion FutureKit/Future.swift
Expand Up @@ -35,7 +35,7 @@ public struct GLOBAL_PARMS {
static let CURRENT_EXECUTOR_PROPERTY = "FutureKit.Executor.Current"
static let STACK_CHECKING_MAX_DEPTH = 20

public static var LOCKING_STRATEGY : SynchronizationType = .OSSpinLock
public static var LOCKING_STRATEGY : SynchronizationType = .PThreadMutex

}

Expand Down
108 changes: 2 additions & 106 deletions FutureKit/Synchronization.swift
Expand Up @@ -94,7 +94,6 @@ public enum SynchronizationType : CustomStringConvertible, CustomDebugStringConv
case NSObjectLock
case NSLock
case NSRecursiveLock
case OSSpinLock
case PThreadMutex
// case NSLockWithSafetyChecks
// case NSRecursiveLockWithSafetyChecks
Expand All @@ -104,7 +103,7 @@ public enum SynchronizationType : CustomStringConvertible, CustomDebugStringConv
return 30.0
}

public static let allValues = [BarrierConcurrent, BarrierSerial, SerialQueue,NSObjectLock,NSLock,NSRecursiveLock,OSSpinLock,PThreadMutex]
public static let allValues = [BarrierConcurrent, BarrierSerial, SerialQueue,NSObjectLock,NSLock,NSRecursiveLock,PThreadMutex]

public func lockObject() -> SynchronizationProtocol {
switch self {
Expand All @@ -120,8 +119,6 @@ public enum SynchronizationType : CustomStringConvertible, CustomDebugStringConv
return NSLockSynchronization()
case NSRecursiveLock:
return NSRecursiveLockSynchronization()
case OSSpinLock:
return OSSpinLockSynchronization()
case PThreadMutex:
return PThreadMutexSynchronization()
/* case NSLockWithSafetyChecks:
Expand All @@ -147,8 +144,6 @@ public enum SynchronizationType : CustomStringConvertible, CustomDebugStringConv
return "NSLock"
case NSRecursiveLock:
return "NSRecursiveLock"
case OSSpinLock:
return "OSSpinLock"
case PThreadMutex:
return "PThreadMutex"
case Unsafe:
Expand All @@ -161,7 +156,7 @@ public enum SynchronizationType : CustomStringConvertible, CustomDebugStringConv
}

// some typealias for the default recommended Objects
typealias LightAndFastSyncType = OSSpinLockSynchronization
typealias LightAndFastSyncType = PThreadMutexSynchronization
typealias SlowOrComplexSyncType = QueueBarrierSynchronization

}
Expand Down Expand Up @@ -580,105 +575,6 @@ public class NSLockSynchronization : SynchronizationProtocol {
}


}

func synchronizedWithSpinLock<T>(l: UnSafeMutableContainer<OSSpinLock>, @noescape closure: ()->T) -> T {
OSSpinLockLock(l.unsafe_pointer)
let retVal: T = closure()
OSSpinLockUnlock(l.unsafe_pointer)
return retVal
}

public class OSSpinLockSynchronization : SynchronizationProtocol {

var lock = UnSafeMutableContainer<OSSpinLock>(OS_SPINLOCK_INIT)

required public init() {

}
final func synchronized<T>(block:() -> T) -> T {
return synchronizedWithSpinLock(self.lock) { () -> T in
return block()
}
}

public final func lockAndModify<T>(
waitUntilDone wait: Bool = false,
modifyBlock:() -> T,
then : ((T) -> Void)? = nil) {

if let then = then {
let retVal = self.synchronized(modifyBlock)
then( retVal)
}
else {
self.synchronized(modifyBlock)
}
}

public final func lockAndRead<T>(
waitUntilDone wait: Bool = false,
readBlock:() -> T,
then : ((T) -> Void)? = nil) {

self.lockAndModify(waitUntilDone: wait, modifyBlock: readBlock, then: then)
}


// this should be in the swift 2.0 protocol extension, for now we do a big cut/paste

public final func lockAndModify(modifyBlock:() -> Void) {
self.lockAndModify(waitUntilDone: false, modifyBlock: modifyBlock, then: nil)
}

public final func lockAndModifyAsync<T>(modifyBlock:() -> T, then : (T) -> Void) {
self.lockAndModify(waitUntilDone: false, modifyBlock: modifyBlock, then: then)
}

public final func lockAndModifySync<T>(modifyBlock:() -> T) -> T {

var retVal : T?
self.lockAndModify(waitUntilDone: true, modifyBlock: modifyBlock) { (modifyBlockReturned) -> Void in
retVal = modifyBlockReturned
}
return retVal!
}

public final func lockAndRead(readBlock:() -> Void) {
self.lockAndRead(waitUntilDone: false, readBlock: readBlock, then: nil)
}

public final func lockAndReadAsync<T>(readBlock:() -> T, then : (T) -> Void) {
self.lockAndRead(waitUntilDone: false, readBlock: readBlock, then: then)
}

public final func lockAndReadSync<T>(readBlock:() -> T) -> T {

var retVal : T?
self.lockAndRead(waitUntilDone: true, readBlock: readBlock) { (readBlockReturned) -> Void in
retVal = readBlockReturned
}
return retVal!
}
public final func readFuture<T>(executor executor : Executor = .Primary, block:() -> T) -> Future<T> {
let p = Promise<T>()

self.lockAndRead(waitUntilDone: false, readBlock: block) { (readBlockReturned) -> Void in
p.completeWithSuccess(readBlockReturned)
}

return p.future
}
public final func modifyFuture<T>(executor executor : Executor = .Primary, block:() -> T) -> Future<T> {
let p = Promise<T>()

self.lockAndModify(waitUntilDone: false, modifyBlock: block) { (modifyBlockReturned) -> Void in
p.completeWithSuccess(modifyBlockReturned)
}
return p.future
}


}


Expand Down
2 changes: 1 addition & 1 deletion FutureKitTests/LockPerformanceTests.swift
Expand Up @@ -202,7 +202,7 @@ class LockPerformanceTests: BlockBasedTestCase {
thread.start()
}

self.expectationTestForFutureSuccess("threads", future: FutureBatchOf(f:futures).future)
self.expectationTestForFutureSuccess("threads", future: FutureBatchOf(futures:futures).future)
self.waitForExpectationsWithTimeout(600, handler: nil)
}

Expand Down

0 comments on commit 6745bb8

Please sign in to comment.