Skip to content

Commit

Permalink
Merge NSLock candidate fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasa committed Sep 18, 2023
2 parents 558bc26 + 1feeb27 commit f7376a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Source/NSLock.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
#import "GSPrivate.h"
#import "GSPThread.h"
#include <math.h>
#include <stdlib.h>

#import "common.h"

#import "Foundation/NSLock.h"
#import "Foundation/NSException.h"
#import "Foundation/NSThread.h"
// #import "Foundation/NSUserDefaults.h"

#define class_createInstance(C,E) NSAllocateObject(C,E,NSDefaultMallocZone())

Expand Down Expand Up @@ -247,7 +249,16 @@ - (void) unlock\
{\
if (0 != GS_MUTEX_UNLOCK(_mutex))\
{\
NSLog(@"failed to unlock mutex");\
if (GSPrivateDefaultsFlag(GSMacOSXCompatible))\
{\
NSLog(@"Failed to unlock mutex %@ at %@",\
self, [NSThread callStackSymbols]);\
}\
else \
{\
[NSException raise: NSLockException\
format: @"failed to unlock mutex %@", self];\
}\
}\
CHK(Drop) \
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/base/NSLock/unbalancedUnlock.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import <Foundation/Foundation.h>
#import "Testing.h"
#import "ObjectTesting.h"

int main()
{
START_SET("Unbalanced unlocking")

NSLock *lock = [NSLock new];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
BOOL mode = [defs boolForKey: @"GSMacOSXCompatible"];

[defs setBool: NO forKey: @"GSMacOSXCompatible"];
PASS_EXCEPTION([lock unlock], @"NSLockException",
"unlocking an unlocked lock raises NSLockException")

[defs setBool: YES forKey: @"GSMacOSXCompatible"];
PASS_RUNS([lock unlock],
"unlocking an unlocked lock does not raise in MacOSX compatibility mode")

[defs setBool: mode forKey: @"GSMacOSXCompatible"];

END_SET("Unbalanced unlocking")
return 0;
}

0 comments on commit f7376a9

Please sign in to comment.