Revision: 27867
http://sourceforge.net/p/bibdesk/svn/27867
Author: hofman
Date: 2022-09-07 16:21:36 +0000 (Wed, 07 Sep 2022)
Log Message:
-----------
use stdatomic instead of osatomic for async object and subclasses
Modified Paths:
--------------
trunk/bibdesk/BDSKAsyncObject.h
trunk/bibdesk/BDSKAsyncObject.m
trunk/bibdesk/BDSKSharingClient.m
trunk/bibdesk/BDSKZoomGroupServer.h
trunk/bibdesk/BDSKZoomGroupServer.m
Modified: trunk/bibdesk/BDSKAsyncObject.h
===================================================================
--- trunk/bibdesk/BDSKAsyncObject.h 2022-09-07 14:40:33 UTC (rev 27866)
+++ trunk/bibdesk/BDSKAsyncObject.h 2022-09-07 16:21:36 UTC (rev 27867)
@@ -37,11 +37,12 @@
*/
#import <Cocoa/Cocoa.h>
+#import <stdatomic.h>
typedef struct _BDSKAsyncObjectFlags {
- volatile int32_t shouldKeepRunning;
+ _Atomic(BOOL) shouldKeepRunning;
#ifdef DEBUG
- volatile int32_t didStart;
+ _Atomic(BOOL) didStart;
#endif
} BDSKAsyncObjectFlags;
Modified: trunk/bibdesk/BDSKAsyncObject.m
===================================================================
--- trunk/bibdesk/BDSKAsyncObject.m 2022-09-07 14:40:33 UTC (rev 27866)
+++ trunk/bibdesk/BDSKAsyncObject.m 2022-09-07 16:21:36 UTC (rev 27867)
@@ -38,7 +38,6 @@
#import "BDSKAsyncObject.h"
#import <objc/runtime.h>
-#import <libkern/OSAtomic.h>
@interface BDSKAsyncObject (Private)
- (void)runLocalThread;
@@ -52,7 +51,7 @@
#ifdef DEBUG
- (void)checkStartup:(NSTimer *)ignored
{
- if (0 == aoFlags.didStart)
+ if (NO == atomic_load(&aoFlags.didStart))
NSLog(@"*** Warning *** %@ has not been started after 1 second", self);
}
#endif
@@ -62,9 +61,9 @@
self = [super init];
if (self) {
// set up flags
- aoFlags.shouldKeepRunning = 1;
+ aoFlags.shouldKeepRunning = YES;
#ifdef DEBUG
- aoFlags.didStart = 0;
+ aoFlags.didStart = NO;
// check for absentminded developers; there's no actual requirement
that start be called immediately
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(checkStartup:) userInfo:nil repeats:NO];
@@ -80,7 +79,7 @@
- (void)start;
{
#ifdef DEBUG
- aoFlags.didStart = 1;
+ atomic_store(&aoFlags.didStart, YES);
#endif
// run a background thread
localThread = [[NSThread alloc] initWithTarget:self
selector:@selector(runLocalThread) object:nil];
@@ -153,7 +152,7 @@
{
BDSKASSERT([NSThread isMainThread]);
// set the stop flag, so any long process (possibly with loops) knows it
can return
- OSAtomicCompareAndSwap32Barrier(1, 0, &aoFlags.shouldKeepRunning);
+ atomic_store(&aoFlags.shouldKeepRunning, NO);
// this is mainly to tickle the runloop on the local thread so it will
finish
[self performSelectorOnLocalThread:@selector(stopRunning) withObject:nil
waitUntilDone:NO];
BDSKDESTROY(localThread);
@@ -162,8 +161,7 @@
#pragma mark Thread Safe
- (BOOL)shouldKeepRunning {
- OSMemoryBarrier();
- return aoFlags.shouldKeepRunning == 1;
+ return atomic_load(&aoFlags.shouldKeepRunning) == YES;
}
@end
Modified: trunk/bibdesk/BDSKSharingClient.m
===================================================================
--- trunk/bibdesk/BDSKSharingClient.m 2022-09-07 14:40:33 UTC (rev 27866)
+++ trunk/bibdesk/BDSKSharingClient.m 2022-09-07 16:21:36 UTC (rev 27867)
@@ -42,16 +42,16 @@
#import "BDSKPasswordController.h"
#import "NSData_BDSKExtensions.h"
#import "CFString_BDSKExtensions.h"
-#import <libkern/OSAtomic.h>
static NSString *BDSKClientServiceNameForKeychain = @"BibDesk Sharing Access";
typedef struct _BDSKSharingClientFlags {
- volatile int32_t isRetrieving;
- volatile int32_t authenticationFailed;
- volatile int32_t canceledAuthentication;
- volatile int32_t needsAuthentication;
- volatile int32_t failedDownload;
+ _Atomic(BOOL) isRetrieving;
+ _Atomic(BOOL) authenticationFailed;
+ _Atomic(BOOL) canceledAuthentication;
+ _Atomic(BOOL) needsAuthentication;
+ _Atomic(BOOL) legacyServer;
+ _Atomic(BOOL) failedDownload;
} BDSKSharingClientFlags;
// private class for async object. We have it as a separate object so we don't
get a retain loop, we remove it from the thread runloop in the client's dealloc
@@ -224,28 +224,23 @@
}
- (BOOL)isRetrieving {
- OSMemoryBarrier();
- return flags.isRetrieving == 1;
+ return atomic_load(&flags.isRetrieving) == YES;
}
- (void)setRetrieving:(BOOL)flag {
- int32_t old = flag ? 0 : 1, new = flag ? 1 : 0;
- OSAtomicCompareAndSwap32Barrier(old, new, &flags.isRetrieving);
+ atomic_store(&flags.isRetrieving, flag);
}
- (BOOL)needsAuthentication {
- OSMemoryBarrier();
- return flags.needsAuthentication != 0;
+ return atomic_load(&flags.needsAuthentication) == YES;
}
- (BOOL)authenticationFailed {
- OSMemoryBarrier();
- return flags.authenticationFailed == 1 || flags.canceledAuthentication ==
1;
+ return atomic_load(&flags.authenticationFailed) == YES ||
atomic_load(&flags.canceledAuthentication) == YES;
}
- (BOOL)failedDownload {
- OSMemoryBarrier();
- return flags.failedDownload == 1;
+ return atomic_load(&flags.failedDownload) == YES;
}
#pragma mark Authentication
@@ -288,8 +283,7 @@
- (BOOL)authenticateIfNeeded:(BOOL)isNew {
// authenticate to a server of version 1+
- OSMemoryBarrier();
- if (flags.needsAuthentication != 1 || (isNew == NO &&
flags.canceledAuthentication == 0 && flags.authenticationFailed == 0)) {
+ if (atomic_load(&flags.needsAuthentication) == NO ||
atomic_load(&flags.legacyServer) == YES || (isNew == NO &&
atomic_load(&flags.canceledAuthentication) == NO &&
atomic_load(&flags.authenticationFailed) == NO)) {
// no need to authenticate, or legacy server, or we already
authenticated
return YES;
}
@@ -297,10 +291,9 @@
NSString *password = nil;
BOOL fromPanel = NO;
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.canceledAuthentication);
+ atomic_store(&flags.canceledAuthentication, NO);
- OSMemoryBarrier();
- if(flags.authenticationFailed == 0)
+ if(atomic_load(&flags.authenticationFailed) == NO)
password = [self passwordFromKeychain];
if ([self shouldKeepRunning] == NO)
@@ -312,7 +305,7 @@
if (password == nil) {
// cancceled by user
- OSAtomicCompareAndSwap32Barrier(0, 1,
&flags.canceledAuthentication);
+ atomic_store(&flags.canceledAuthentication, YES);
return NO;
} else {
fromPanel = YES;
@@ -328,7 +321,7 @@
}
@catch (id exception) {
NSLog(@"%@: unable to authenticate with remote server %@", [self
class], [service hostName]);
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.authenticationFailed);
+ atomic_store(&flags.authenticationFailed, YES);
// don't show the alert when we couldn't authenticate when
cleaning up
if ([self shouldKeepRunning])
[self runAuthenticationFailedAlert];
@@ -342,7 +335,7 @@
return YES;
} else {
// set the flag and try again, until we succeed or user cancels
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.authenticationFailed);
+ atomic_store(&flags.authenticationFailed, YES);
}
}
return NO;
@@ -351,16 +344,14 @@
// this can be called from any thread
- (NSData *)authenticationDataForComponents:(NSArray *)components;
{
- OSMemoryBarrier();
- if (flags.needsAuthentication == 3) {
+ if (atomic_load(&flags.needsAuthentication) == YES &&
atomic_load(&flags.legacyServer) == YES) {
// legacy server of version 0 expect authentication through this method
NSString *password = nil;
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.canceledAuthentication);
+ atomic_store(&flags.canceledAuthentication, NO);
- OSMemoryBarrier();
- if(flags.authenticationFailed == 0)
+ if(atomic_load(&flags.authenticationFailed) == NO)
password = [self passwordFromKeychain];
if(password == nil && [self shouldKeepRunning]){
@@ -371,11 +362,11 @@
// retry from the keychain
if (password){
// assume we succeeded; the exception handler for the
connection will change it back if we fail again
- OSAtomicCompareAndSwap32Barrier(1, 0,
&flags.authenticationFailed);
+ atomic_store(&flags.authenticationFailed, NO);
[self setPasswordFromKeychain:password];
}else{
// nil return, will throw NSGenericException
- OSAtomicCompareAndSwap32Barrier(0, 1,
&flags.canceledAuthentication);
+ atomic_store(&flags.canceledAuthentication, YES);
}
}
return [password sha1Signature];
@@ -400,12 +391,10 @@
BDSKASSERT(data != nil);
if(data){
NSDictionary *dict = [NSNetService dictionaryFromTXTRecordData:data];
- int32_t val = [[[[NSString alloc] initWithData:[dict
objectForKey:BDSKTXTAuthenticateKey] encoding:NSUTF8StringEncoding]
autorelease] integerValue];
- if (val == 1 && [[dict objectForKey:BDSKTXTVersionKey] isEqual:[@"0"
dataUsingEncoding:NSUTF8StringEncoding]])
- val |= 2;
- OSMemoryBarrier();
- int32_t oldVal = flags.needsAuthentication;
- OSAtomicCompareAndSwap32Barrier(oldVal, val,
&flags.needsAuthentication);
+ BOOL val = [[[[NSString alloc] initWithData:[dict
objectForKey:BDSKTXTAuthenticateKey] encoding:NSUTF8StringEncoding]
autorelease] boolValue];
+ atomic_store(&flags.needsAuthentication, val);
+ val = [[dict objectForKey:BDSKTXTVersionKey] isEqual:[@"0"
dataUsingEncoding:NSUTF8StringEncoding]];
+ atomic_store(&flags.legacyServer, val);
}
}
@@ -445,9 +434,8 @@
if([exception respondsToSelector:@selector(name)] && [[exception
name] isEqual:NSFailedAuthenticationException]){
// if the user didn't cancel, set an auth failure flag and
show an alert
- OSMemoryBarrier();
- if(flags.canceledAuthentication == 0){
- OSAtomicCompareAndSwap32Barrier(0, 1,
&flags.authenticationFailed);
+ if(atomic_load(&flags.canceledAuthentication) == NO){
+ atomic_store(&flags.authenticationFailed, YES);
// don't show the alert when we couldn't authenticate when
cleaning up
if([self shouldKeepRunning])
[self runAuthenticationFailedAlert];
@@ -456,8 +444,7 @@
} else if([exception respondsToSelector:@selector(name)] &&
[[exception name] isEqual:NSGenericException]){
// this is thrown when authentication is canceled
- OSMemoryBarrier();
- if(flags.canceledAuthentication == 0){
+ if(atomic_load(&flags.canceledAuthentication) == NO){
@throw [NSString stringWithFormat:@"%@: exception \"%@\"
while connecting to remote server %@", NSStringFromSelector(_cmd), exception,
[service hostName]];
}
@@ -500,8 +487,8 @@
- (oneway void)retrievePublications;
{
// set so we don't try calling this multiple times
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.isRetrieving);
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.failedDownload);
+ atomic_store(&flags.isRetrieving, YES);
+ atomic_store(&flags.failedDownload, NO);
NSAutoreleasePool *pool = [NSAutoreleasePool new];
@@ -527,7 +514,7 @@
}
@catch(id exception){
NSLog(@"%@: discarding exception \"%@\" while retrieving
publications", [self class], exception);
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.failedDownload);
+ atomic_store(&flags.failedDownload, YES);
[self setErrorMessage:NSLocalizedString(@"Failed to retrieve
publications", @"")];
// this posts a notification that the publications of the client
changed, forcing a redisplay of the table cell
Modified: trunk/bibdesk/BDSKZoomGroupServer.h
===================================================================
--- trunk/bibdesk/BDSKZoomGroupServer.h 2022-09-07 14:40:33 UTC (rev 27866)
+++ trunk/bibdesk/BDSKZoomGroupServer.h 2022-09-07 16:21:36 UTC (rev 27867)
@@ -43,9 +43,9 @@
typedef struct _BDSKZoomGroupFlags {
- volatile int32_t isRetrieving;
- volatile int32_t failedDownload;
- volatile int32_t needsReset;
+ _Atomic(BOOL) isRetrieving;
+ _Atomic(BOOL) failedDownload;
+ _Atomic(BOOL) needsReset;
} BDSKZoomGroupFlags;
@@ -56,8 +56,8 @@
id<BDSKSearchGroup> group;
ZOOMConnection *connection;
BDSKServerInfo *serverInfo;
- volatile int32_t availableResults;
- volatile int32_t fetchedResults;
+ _Atomic(NSInteger) availableResults;
+ _Atomic(NSInteger) fetchedResults;
BDSKZoomGroupFlags flags;
BDSKReadWriteLock *infoLock;
NSString *errorMessage;
Modified: trunk/bibdesk/BDSKZoomGroupServer.m
===================================================================
--- trunk/bibdesk/BDSKZoomGroupServer.m 2022-09-07 14:40:33 UTC (rev 27866)
+++ trunk/bibdesk/BDSKZoomGroupServer.m 2022-09-07 16:21:36 UTC (rev 27867)
@@ -44,7 +44,6 @@
#import "CFString_BDSKExtensions.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "BDSKReadWriteLock.h"
-#import <libkern/OSAtomic.h>
#define MAX_RESULTS 100
@@ -125,23 +124,23 @@
{
if ([self isRetrieving]) {
[self performSelectorOnLocalThread:@selector(terminateConnection)
withObject:nil waitUntilDone:NO];
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.isRetrieving);
+ atomic_store(&flags.isRetrieving, NO);
}
- OSAtomicCompareAndSwap32Barrier(availableResults, 0, &availableResults);
- OSAtomicCompareAndSwap32Barrier(fetchedResults, 0, &fetchedResults);
+ atomic_store(&availableResults, 0);
+ atomic_store(&fetchedResults, 0);
}
- (void)terminate
{
[self stop];
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.isRetrieving);
+ atomic_store(&flags.isRetrieving, NO);
}
- (void)retrieveWithSearchTerm:(NSString *)aSearchTerm
{
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.failedDownload);
+ atomic_store(&flags.failedDownload, NO);
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.isRetrieving);
+ atomic_store(&flags.isRetrieving, YES);
[self performSelectorOnLocalThread:@selector(downloadWithSearchTerm:)
withObject:aSearchTerm waitUntilDone:NO];
}
@@ -153,7 +152,7 @@
serverInfo = [info copy];
}
[infoLock unlock];
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.needsReset);
+ atomic_store(&flags.needsReset, YES);
}
- (BDSKServerInfo *)serverInfo;
@@ -166,19 +165,17 @@
- (NSInteger)numberOfAvailableResults;
{
- OSMemoryBarrier();
- return availableResults;
+ return atomic_load(&availableResults);
}
- (NSInteger)numberOfFetchedResults;
{
- OSMemoryBarrier();
- return fetchedResults;
+ return atomic_load(&fetchedResults);
}
-- (BOOL)failedDownload { OSMemoryBarrier(); return 1 == flags.failedDownload; }
+- (BOOL)failedDownload { return YES == atomic_load(&flags.failedDownload); }
-- (BOOL)isRetrieving { OSMemoryBarrier(); return 1 == flags.isRetrieving; }
+- (BOOL)isRetrieving { return YES == atomic_load(&flags.isRetrieving); }
- (NSString *)errorMessage {
NSString *msg;
@@ -246,7 +243,7 @@
}
// set this flag before adding pubs, or the client will think we're still
retrieving (and spinners don't stop)
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.isRetrieving);
+ atomic_store(&flags.isRetrieving, NO);
// this will create the array if it doesn't exist
[group addPublications:pubs];
@@ -278,25 +275,24 @@
[connection setOption:[[info options] objectForKey:key]
forKey:key];
}
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.needsReset);
+ atomic_store(&flags.needsReset, NO);
}
- OSAtomicCompareAndSwap32Barrier(availableResults, 0, &availableResults);
- OSAtomicCompareAndSwap32Barrier(fetchedResults, 0, &fetchedResults);
+ atomic_store(&availableResults, 0);
+ atomic_store(&fetchedResults, 0);
}
- (void)terminateConnection;
{
BDSKDESTROY(connection);
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.needsReset);
- OSAtomicCompareAndSwap32Barrier(1, 0, &flags.isRetrieving);
+ atomic_store(&flags.needsReset, YES);
+ atomic_store(&flags.isRetrieving, NO);
}
- (void)downloadWithSearchTerm:(NSString *)searchTerm;
{
// only reset the connection when we're actually going to use it, since a
mixed host/database/port won't work
- OSMemoryBarrier();
- if (flags.needsReset)
+ if (atomic_load(&flags.needsReset))
[self resetConnection];
NSMutableArray *results = nil;
@@ -320,12 +316,12 @@
ZOOMResultSet *resultSet = query ? [connection resultsForQuery:query]
: nil;
if (nil == resultSet) {
- OSAtomicCompareAndSwap32Barrier(0, 1, &flags.failedDownload);
+ atomic_store(&flags.failedDownload, YES);
[self setErrorMessage:NSLocalizedString(@"Could not retrieve
results", @"")];
}
- int32_t newAvailableResults = [resultSet countOfRecords];
- OSAtomicCompareAndSwap32Barrier(availableResults, newAvailableResults,
&availableResults);
+ NSInteger newAvailableResults = [resultSet countOfRecords];
+ atomic_store(&availableResults, newAvailableResults);
NSInteger numResults = MIN([self numberOfAvailableResults] - [self
numberOfFetchedResults], MAX_RESULTS);
//NSAssert(numResults >= 0, @"number of results to get must be
non-negative");
@@ -332,9 +328,9 @@
if(numResults > 0){
NSArray *records = [resultSet recordsInRange:NSMakeRange([self
numberOfFetchedResults], numResults)];
- int32_t newNumberOfFetchedResults = [self numberOfFetchedResults]
+ numResults;
+ NSInteger newNumberOfFetchedResults = [self
numberOfFetchedResults] + numResults;
- OSAtomicCompareAndSwap32Barrier(fetchedResults,
newNumberOfFetchedResults, &fetchedResults);
+ atomic_store(&fetchedResults, newNumberOfFetchedResults);
results = [NSMutableArray array];
for (id result in records) {
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit