From f9c665a542448da5152c96cddbb7878a518f5f13 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 25 Jan 2016 17:23:57 -0500 Subject: [PATCH] for whatever reason I was able trigger a race condition because of the last commit --- Source/SwiftRegex.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Source/SwiftRegex.swift b/Source/SwiftRegex.swift index 8ea2d0e..b7c134b 100644 --- a/Source/SwiftRegex.swift +++ b/Source/SwiftRegex.swift @@ -15,6 +15,7 @@ import Foundation infix operator <~ { associativity none precedence 130 } +private let lock = dispatch_semaphore_create(1) private var swiftRegexCache = [String: NSRegularExpression]() internal final class SwiftRegex: NSObject, BooleanType { @@ -22,6 +23,10 @@ internal final class SwiftRegex: NSObject, BooleanType { var regex: NSRegularExpression init(target:String, pattern:String, options:NSRegularExpressionOptions?) { + if dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, Int64(10 * NSEC_PER_MSEC))) != 0 { + fatalError("This should never happen") + } + self.target = target if let regex = swiftRegexCache[pattern] { self.regex = regex @@ -36,6 +41,7 @@ internal final class SwiftRegex: NSObject, BooleanType { self.regex = NSRegularExpression() } } + dispatch_semaphore_signal(lock) super.init() }