Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
Open
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
45 changes: 42 additions & 3 deletions Frameworks/Foundation/NSConditionLock.mm
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,50 @@ - (void)lockWhenCondition:(NSInteger)condition {
}

/**
@Status Stub
@Status Interoperable
*/
- (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate*)date {
UNIMPLEMENTED();
return NO;
int rc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume rc is for "return code"? Please don't choose single letter / abbreviated variable names as it is unclear to future readers what the abbreviation / implicit word is. Perhaps statusCode / returnCode, etc here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DHowett-MSFT I didn't see the surrounding context sorry. Probably ok then.

struct timespec t = { 0 };
struct EbrTimeval tv;
NSTimeInterval d = [date timeIntervalSinceNow];

EbrGetTimeOfDay(&tv);

t.tv_sec = tv.tv_sec;
t.tv_nsec = tv.tv_usec * 1000;

t.tv_sec += (unsigned int)d;
t.tv_nsec = (long)((double)t.tv_nsec + fmod(d, 1.0) * 1000000000.0);

t.tv_sec += t.tv_nsec / 1000000000;
t.tv_nsec %= 1000000000;

if ((rc = pthread_mutex_lock(&_mutex)) != 0) {
[NSException raise:NSInvalidArgumentException format:@"failed to lock %@ (errno: %d)", self, rc];
}

while (_value != condition) {
switch ((rc = pthread_cond_timedwait(&_cond, &_mutex, &t))) {
case 0:
break;

case ETIMEDOUT:
if ((rc = pthread_mutex_unlock(&_mutex)) != 0) {
[NSException raise:NSInvalidArgumentException format:@"failed to unlock %@ before date %@ (errno: %d)", self, date, rc];
}
return NO;

default:
if ((rc = pthread_mutex_unlock(&_mutex)) != 0) {
[NSException raise:NSInvalidArgumentException format:@"failed to unlock %@ before date %@ (errno: %d)", self, date, rc];
}
[NSException raise:NSInvalidArgumentException format:@"failed to lock %@ before date %@ (errno: %d)", self, date, rc];
return NO;
}
}

return YES;
}

/**
Expand Down