Skip to content

Initialize pthread mutexes in NSRecursiveLock #2276

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

Merged
merged 1 commit into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ open class NSRecursiveLock: NSObject, NSLocking {
var attrib = pthread_mutexattr_t()
#endif
withUnsafeMutablePointer(to: &attrib) { attrs in
pthread_mutexattr_init(attrs)
pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE))
pthread_mutex_init(mutex, attrs)
}
#if os(macOS) || os(iOS)
pthread_cond_init(timeoutCond, nil)
pthread_mutex_init(timeoutMutex, nil)
#endif
#endif
}

Expand Down
74 changes: 73 additions & 1 deletion TestFoundation/TestNSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class TestNSLock: XCTestCase {

("test_lockWait", test_lockWait),
("test_threadsAndLocks", test_threadsAndLocks),

("test_recursiveLock", test_recursiveLock),

]
}

Expand Down Expand Up @@ -115,4 +116,75 @@ class TestNSLock: XCTestCase {
lock.unlock()
}
}

func test_recursiveLock() {
// We will start 10 threads, and their collective task will be to reduce
// the countdown value to zero in less than 30 seconds.

let threadCount = 10
let countdownPerThread = 1000
let endTime = Date(timeIntervalSinceNow: 30)

var completedThreadCount = 0
var countdownValue = threadCount * countdownPerThread

let threadCompletedCondition = NSCondition()
let countdownValueLock = NSRecursiveLock()

for _ in 0..<threadCount {
let thread = Thread {

// Some dummy work on countdown.
// Add/substract operations are balanced to decrease countdown
// exactly by countdownPerThread

for _ in 0..<countdownPerThread {
countdownValueLock.lock()
countdownValue -= 3
countdownValueLock.lock()
countdownValue += 4
countdownValueLock.unlock()
countdownValueLock.unlock()

countdownValueLock.lock()
countdownValue += 2
countdownValueLock.lock()
countdownValue -= 1
countdownValueLock.unlock()
countdownValue -= 3
countdownValueLock.unlock()

countdownValueLock.lock()
countdownValue += 1
countdownValueLock.unlock()

countdownValueLock.lock()
countdownValue -= 1
countdownValueLock.unlock()
}

threadCompletedCondition.lock()
completedThreadCount += 1
threadCompletedCondition.broadcast()
threadCompletedCondition.unlock()
}
thread.start()
}

threadCompletedCondition.lock()

var isTimedOut = false
while !isTimedOut && completedThreadCount < threadCount {
isTimedOut = !threadCompletedCondition.wait(until: endTime)
}

countdownValueLock.lock()
let resultCountdownValue = countdownValue
countdownValueLock.unlock()

XCTAssertEqual(completedThreadCount, threadCount, "Some threads are still not finished, could be hung")
XCTAssertEqual(resultCountdownValue, 0, "Wrong coundtdown means unresolved race conditions, locks broken")

threadCompletedCondition.unlock()
}
}