Skip to content

Commit a406011

Browse files
spevansamraboelela
authored andcommitted
NSLock: lock(before:) - Add some precondition() tests
1 parent faf722e commit a406011

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Foundation/NSLock.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,22 @@ private func timedLock(mutex: _PthreadMutexPointer, endTime: Date,
320320

321321
var timeSpec = timeSpecFrom(date: endTime)
322322
while var ts = timeSpec {
323-
pthread_mutex_lock(timeoutMutex)
324-
pthread_cond_timedwait(timeoutCond, timeoutMutex, &ts)
325-
pthread_mutex_unlock(timeoutMutex)
326-
if pthread_mutex_trylock(mutex) == 0 {
323+
let lockval = pthread_mutex_lock(timeoutMutex)
324+
precondition(lockval == 0)
325+
let waitval = pthread_cond_timedwait(timeoutCond, timeoutMutex, &ts)
326+
precondition(waitval == 0 || waitval == ETIMEDOUT)
327+
let unlockval = pthread_mutex_unlock(timeoutMutex)
328+
precondition(unlockval == 0)
329+
330+
if waitval == ETIMEDOUT {
331+
return false
332+
}
333+
let tryval = pthread_mutex_trylock(mutex)
334+
precondition(tryval == 0 || tryval == EBUSY)
335+
if tryval == 0 { // The lock was obtained.
327336
return true
328337
}
338+
// pthread_cond_timedwait didnt timeout so wait some more.
329339
timeSpec = timeSpecFrom(date: endTime)
330340
}
331341
return false

0 commit comments

Comments
 (0)