adium 4074:3bec03370bea: Replace weird OSAtomic* based singleton...
commits at adium.im
commits at adium.im
Wed Jul 27 03:33:28 UTC 2011
details: http://hg.adium.im/adium/rev/3bec03370bea
revision: 4074:3bec03370bea
branch: (none)
author: Stephen Holt <sholt at adium.im>
date: Tue Jul 26 20:32:29 2011 -0700
Replace weird OSAtomic* based singleton constructors and custom NSOperation implimentatinos with nicer dispatch_once and operationWithBlock: calls.
diffs (457 lines):
diff -r 732390b9b84a -r 3bec03370bea Frameworks/AIUtilities Framework/Source/AIDictionaryAdditions.m
--- a/Frameworks/AIUtilities Framework/Source/AIDictionaryAdditions.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Frameworks/AIUtilities Framework/Source/AIDictionaryAdditions.m Tue Jul 26 20:32:29 2011 -0700
@@ -22,43 +22,6 @@
#import "AIFileManagerAdditions.h"
#import "AISharedWriterQueue.h"
- at interface AIWriteDictionaryOperation : NSOperation
-{
- NSString *path;
- NSString *name;
- NSDictionary *dict;
-}
-
-- (AIWriteDictionaryOperation *)initWithPath:(NSString *)path name:(NSString *)s dictionary:(NSDictionary *)dict;
-
- at end
-
- at implementation AIWriteDictionaryOperation
-- (AIWriteDictionaryOperation *)initWithPath:(NSString *)p name:(NSString *)s dictionary:(NSDictionary *)d
-{
- if ((self = [super init]))
- {
- path = [p retain];
- dict = [d retain];
- name = [s retain];
- }
- return self;
-}
-
-- (void) dealloc
-{
- [name release];
- [dict release];
- [path release];
- [super dealloc];
-}
-
-- (void) main
-{
- [dict writeToPath:path withName:name];
-}
- at end
-
@implementation NSDictionary (AIDictionaryAdditions)
// Returns a dictionary from the owners bundle with the specified name
@@ -159,8 +122,9 @@
NSParameterAssert(path != nil); NSParameterAssert([path length] != 0);
NSParameterAssert(name != nil); NSParameterAssert([name length] != 0);
- AIWriteDictionaryOperation *op = [[[AIWriteDictionaryOperation alloc] initWithPath:path name:name dictionary:self] autorelease];
- [AISharedWriterQueue addOperation:op];
+ [AISharedWriterQueue addOperation:^{
+ [self writeToPath:path withName:name];
+ }];
}
- (NSDictionary *)dictionaryByTranslating:(NSDictionary *)translation adding:(NSDictionary *)addition removing:(NSSet *)removal
diff -r 732390b9b84a -r 3bec03370bea Frameworks/AIUtilities Framework/Source/AIHostReachabilityMonitor.m
--- a/Frameworks/AIUtilities Framework/Source/AIHostReachabilityMonitor.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Frameworks/AIUtilities Framework/Source/AIHostReachabilityMonitor.m Tue Jul 26 20:32:29 2011 -0700
@@ -20,12 +20,9 @@
#import <arpa/inet.h>
#import <netdb.h>
#import <netinet/in.h>
-#import <libkern/OSAtomic.h>
#define CONNECTIVITY_DEBUG FALSE
-static AIHostReachabilityMonitor *sharedMonitor = nil;
-
@interface AIHostReachabilityMonitor ()
- (void)scheduleReachabilityMonitoringForHost:(NSString *)nodename observer:(id)observer;
- (void)gotReachabilityRef:(SCNetworkReachabilityRef)reachabilityRef forHost:(NSString *)host observer:(id)observer;
@@ -50,11 +47,11 @@
*/
+ (id)defaultMonitor
{
- if (!sharedMonitor) {
- AIHostReachabilityMonitor *newMonitor = [[AIHostReachabilityMonitor alloc] init];
- if(!OSAtomicCompareAndSwapPtrBarrier(nil, newMonitor, (void *)&sharedMonitor))
- [newMonitor release];
- }
+ static AIHostReachabilityMonitor *sharedMonitor;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ sharedMonitor = [[AIHostReachabilityMonitor alloc] init];
+ });
return sharedMonitor;
}
diff -r 732390b9b84a -r 3bec03370bea Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.h
--- a/Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.h Tue Jul 26 23:56:35 2011 +0200
+++ b/Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.h Tue Jul 26 20:32:29 2011 -0700
@@ -15,6 +15,6 @@
*/
@interface AISharedWriterQueue : NSObject {}
-+ (void) addOperation:(NSOperation *)op;
++ (void) addOperation:(dispatch_block_t)op;
+ (void) waitUntilAllOperationsAreFinished;
@end
diff -r 732390b9b84a -r 3bec03370bea Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.m
--- a/Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Frameworks/AIUtilities Framework/Source/AISharedWriterQueue.m Tue Jul 26 20:32:29 2011 -0700
@@ -14,37 +14,25 @@
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AISharedWriterQueue.h"
-#import <libkern/OSAtomic.h>
-#import "AIApplicationAdditions.h"
-
- at interface AISharedWriterQueue()
-+ (NSOperationQueue *)queue;
- at end
@implementation AISharedWriterQueue
-+ (void) addOperation:(NSOperation *)op {
- [[self queue] addOperation:op];
-}
-
-+ (void) waitUntilAllOperationsAreFinished {
- [[self queue] waitUntilAllOperationsAreFinished];
-}
-
-+ (NSOperationQueue *)queue {
- static NSOperationQueue *sharedWriterQueue = nil;
-
-
- if (!sharedWriterQueue) {
- NSOperationQueue *newWriterQueue = [[NSOperationQueue alloc] init];
- if(!OSAtomicCompareAndSwapPtrBarrier(nil, newWriterQueue, (void *)&sharedWriterQueue))
- [newWriterQueue release];
-
- [sharedWriterQueue setMaxConcurrentOperationCount:1];
- [sharedWriterQueue setName:@"AISharedWriterQueue"];
- }
+static inline dispatch_queue_t queue() {
+ static dispatch_queue_t sharedWriterQueue = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ sharedWriterQueue = dispatch_queue_create("com.adium.sharedAsyncIOQueue", 0);
+ });
return sharedWriterQueue;
}
++ (void) addOperation:(dispatch_block_t)op {
+ dispatch_async(queue(), op);
+}
+
++ (void) waitUntilAllOperationsAreFinished {
+ dispatch_sync(queue(), ^{});
+}
+
@end
diff -r 732390b9b84a -r 3bec03370bea Source/AILogViewerWindowController.m
--- a/Source/AILogViewerWindowController.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Source/AILogViewerWindowController.m Tue Jul 26 20:32:29 2011 -0700
@@ -136,14 +136,13 @@
+ (NSOperationQueue *)sharedLogViewerQueue
{
static NSOperationQueue *logViewerQueue = nil;
- if(!logViewerQueue) {
- NSOperationQueue *newQueue = [[NSOperationQueue alloc] init];
- if(!OSAtomicCompareAndSwapPtrBarrier(nil, newQueue, (void *)&logViewerQueue))
- [newQueue release];
-
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ logViewerQueue = [[NSOperationQueue alloc] init];
if([logViewerQueue respondsToSelector:@selector(setName:)])
- [logViewerQueue performSelector:@selector(setName:) withObject:@"sharedLogViewerQueue"];
- }
+ [logViewerQueue performSelector:@selector(setName:) withObject:@"im.adium.AILogViewerWindowController.logViewerQueue"];
+ });
+
return logViewerQueue;
}
diff -r 732390b9b84a -r 3bec03370bea Source/AIXMLAppender.m
--- a/Source/AIXMLAppender.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Source/AIXMLAppender.m Tue Jul 26 20:32:29 2011 -0700
@@ -62,149 +62,9 @@
@property (readwrite) BOOL initialized;
@property (readwrite, copy, nonatomic) AIXMLElement *rootElement;
@property (readwrite, copy, nonatomic) NSString *path;
- at end
-
- at interface AIAppendXMLOperation : NSOperation
-{
- NSData *data;
- NSInteger seekBackLength;
- AIXMLAppender *appender;
-}
-- (AIAppendXMLOperation *)initWithData:(NSData *)d
- seekBackLength:(NSInteger)s
- appender:(AIXMLAppender *)a;
-
- (void) prepareFileHandle;
@end
- at implementation AIAppendXMLOperation
-- (AIAppendXMLOperation *)initWithData:(NSData *)d
- seekBackLength:(NSInteger)s
- appender:(AIXMLAppender *)a
-{
- if ((self = [super init]))
- {
- data = [d retain];
- seekBackLength = s;
- appender = [a retain];
- }
-
- return self;
-}
-
-- (void) dealloc
-{
- [data release];
- [appender release];
- [super dealloc];
-}
-
-- (void) main
-{
- BOOL success = YES;
- if (!appender.fileHandle)
- [self prepareFileHandle];
-
- @try {
- [appender.fileHandle writeData:data];
-
- } @catch (NSException *writingException) {
- /* NSFileHandle raises an exception if:
- * * the file descriptor is closed or is not valid - we should reopen the file and try again
- * * if the receiver represents an unconnected pipe or socket endpoint - this should never happen
- * * if no free space is left on the file system - this should be handled gracefully if possible.. but the user is probably in trouble.
- * * if any other writing error occurs - as with lack of free space.
- */
- if (appender.initialized &&
- [[writingException name] isEqualToString:NSFileHandleOperationException] &&
- [[writingException reason] rangeOfString:@"Bad file descriptor"].location != NSNotFound) {
-
- appender.fileHandle = nil;
-
- [self prepareFileHandle];
-
- @try {
- [appender.fileHandle writeData:data];
- success = YES;
-
- } @catch (NSException *secondWritingException) {
- NSLog(@"Exception while writing %@ log file %@: %@ (%@)",
- (appender.initialized ? @"initialized" : @"uninitialized"), appender.path, [secondWritingException name], [secondWritingException reason]);
- success = NO;
- }
-
- } else {
- NSLog(@"Exception while writing %@ log file %@: %@ (%@)",
- (appender.initialized ? @"initialized" : @"uninitialized"), appender.path, [writingException name], [writingException reason]);
- success = NO;
- }
- }
-
- if (success) {
- [appender.fileHandle synchronizeFile];
-
- @try {
- [appender.fileHandle seekToFileOffset:([appender.fileHandle offsetInFile] - seekBackLength)];
-
- } @catch (NSException *seekException) {
- /* -[NSFileHandler seekToFileOffset:] raises an exception if
- * * the message is sent to an NSFileHandle object representing a pipe or socket
- * * if the file descriptor is closed
- * * if any other error occurs in seeking.
- */
- NSLog(@"Exception while seeking in %@ log file %@: %@ (%@)",
- (appender.initialized ? @"initialized" : @"uninitialized"), appender.path, [seekException name], [seekException reason]);
- success = NO;
- }
- }
-}
-
-- (void)prepareFileHandle
-{
- NSFileManager *manager = [NSFileManager defaultManager];
-
- //Check if the file already exists
- if ([manager fileExistsAtPath:appender.path]) {
- //Get the root element name and set initialized
- NSString *rootElementName = [appender rootElementNameForFileAtPath:appender.path];
- if (rootElementName)
- appender.rootElement = [[[AIXMLElement alloc] initWithName:rootElementName] autorelease];
- appender.initialized = (rootElementName != nil);
-
- } else {
- //Create each component of the path, then change into it.
- NSError *error = nil;
- if (![manager createDirectoryAtPath:[appender.path stringByDeletingLastPathComponent]
- withIntermediateDirectories:YES
- attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0700UL] forKey:NSFilePosixPermissions]
- error:&error]) {
- AILogWithSignature(@"Error creating directory at %@: %@",
- [appender.path stringByDeletingLastPathComponent],
- error);
- }
-
- appender.initialized = NO;
- }
-
- //Open our file handle and seek if necessary
- const char *pathCString = [appender.path fileSystemRepresentation];
- int fd = open(pathCString, O_CREAT | O_WRONLY, 0600);
- if(fd == -1) {
- AILog(@"Couldn't open log file %@ (%s - length %u) for writing!",
- appender.path, pathCString, (pathCString ? strlen(pathCString) : 0));
- } else {
- appender.fileHandle = [[[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES] autorelease];
- if (appender.initialized) {
- struct stat sb;
- fstat(fd, &sb);
- NSInteger closingTagLength = [appender.rootElement.name length] + 4; //</rootElementName>
- [appender.fileHandle seekToFileOffset:sb.st_size - closingTagLength];
- }
- }
-}
-
- at end
-
/*!
* @class AIXMLAppender
* @brief Provides multiple-write access to an XML document while maintaining wellformedness.
@@ -280,8 +140,108 @@
- (void)writeData:(NSData *)data seekBackLength:(NSInteger)seekBackLength
{
- AIAppendXMLOperation *op = [[[AIAppendXMLOperation alloc] initWithData:data seekBackLength:seekBackLength appender:self] autorelease];
- [AISharedWriterQueue addOperation:op];
+ [AISharedWriterQueue addOperation:^{
+ BOOL success = YES;
+ if (!self.fileHandle)
+ [self prepareFileHandle];
+
+ @try {
+ [self.fileHandle writeData:data];
+
+ } @catch (NSException *writingException) {
+ /* NSFileHandle raises an exception if:
+ * * the file descriptor is closed or is not valid - we should reopen the file and try again
+ * * if the receiver represents an unconnected pipe or socket endpoint - this should never happen
+ * * if no free space is left on the file system - this should be handled gracefully if possible.. but the user is probably in trouble.
+ * * if any other writing error occurs - as with lack of free space.
+ */
+ if (self.initialized &&
+ [[writingException name] isEqualToString:NSFileHandleOperationException] &&
+ [[writingException reason] rangeOfString:@"Bad file descriptor"].location != NSNotFound) {
+
+ self.fileHandle = nil;
+
+ [self prepareFileHandle];
+
+ @try {
+ [self.fileHandle writeData:data];
+ success = YES;
+
+ } @catch (NSException *secondWritingException) {
+ NSLog(@"Exception while writing %@ log file %@: %@ (%@)",
+ (self.initialized ? @"initialized" : @"uninitialized"), self.path, [secondWritingException name], [secondWritingException reason]);
+ success = NO;
+ }
+
+ } else {
+ NSLog(@"Exception while writing %@ log file %@: %@ (%@)",
+ (self.initialized ? @"initialized" : @"uninitialized"), self.path, [writingException name], [writingException reason]);
+ success = NO;
+ }
+ }
+
+ if (success) {
+ [self.fileHandle synchronizeFile];
+
+ @try {
+ [self.fileHandle seekToFileOffset:([self.fileHandle offsetInFile] - seekBackLength)];
+
+ } @catch (NSException *seekException) {
+ /* -[NSFileHandler seekToFileOffset:] raises an exception if
+ * * the message is sent to an NSFileHandle object representing a pipe or socket
+ * * if the file descriptor is closed
+ * * if any other error occurs in seeking.
+ */
+ NSLog(@"Exception while seeking in %@ log file %@: %@ (%@)",
+ (self.initialized ? @"initialized" : @"uninitialized"), self.path, [seekException name], [seekException reason]);
+ success = NO;
+ }
+ }
+ }];
+}
+
+- (void)prepareFileHandle
+{
+ NSFileManager *manager = [NSFileManager defaultManager];
+
+ //Check if the file already exists
+ if ([manager fileExistsAtPath:self.path]) {
+ //Get the root element name and set initialized
+ NSString *rootElementName = [self rootElementNameForFileAtPath:self.path];
+ if (rootElementName)
+ self.rootElement = [[[AIXMLElement alloc] initWithName:rootElementName] autorelease];
+ self.initialized = (rootElementName != nil);
+
+ } else {
+ //Create each component of the path, then change into it.
+ NSError *error = nil;
+ if (![manager createDirectoryAtPath:[self.path stringByDeletingLastPathComponent]
+ withIntermediateDirectories:YES
+ attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0700UL] forKey:NSFilePosixPermissions]
+ error:&error]) {
+ AILogWithSignature(@"Error creating directory at %@: %@",
+ [self.path stringByDeletingLastPathComponent],
+ error);
+ }
+
+ self.initialized = NO;
+ }
+
+ //Open our file handle and seek if necessary
+ const char *pathCString = [self.path fileSystemRepresentation];
+ int fd = open(pathCString, O_CREAT | O_WRONLY, 0600);
+ if(fd == -1) {
+ AILog(@"Couldn't open log file %@ (%s - length %u) for writing!",
+ self.path, pathCString, (pathCString ? strlen(pathCString) : 0));
+ } else {
+ self.fileHandle = [[[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES] autorelease];
+ if (self.initialized) {
+ struct stat sb;
+ fstat(fd, &sb);
+ NSInteger closingTagLength = [self.rootElement.name length] + 4; //</rootElementName>
+ [self.fileHandle seekToFileOffset:sb.st_size - closingTagLength];
+ }
+ }
}
/*!
diff -r 732390b9b84a -r 3bec03370bea Source/AdiumPasswords.m
--- a/Source/AdiumPasswords.m Tue Jul 26 23:56:35 2011 +0200
+++ b/Source/AdiumPasswords.m Tue Jul 26 20:32:29 2011 -0700
@@ -23,7 +23,6 @@
#import <AIUtilities/AIObjectAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <objc/objc-runtime.h>
-#import <libkern/OSAtomic.h>
#import <AIUtilities/AIApplicationAdditions.h>
More information about the commits
mailing list