Revision: 15186
          http://bibdesk.svn.sourceforge.net/bibdesk/?rev=15186&view=rev
Author:   hofman
Date:     2009-04-25 17:14:24 +0000 (Sat, 25 Apr 2009)

Log Message:
-----------
use NSConditionLock rather than a third party trigger for the document search 
queue

Modified Paths:
--------------
    trunk/bibdesk/BDSKDocumentSearch.h
    trunk/bibdesk/BDSKDocumentSearch.m
    trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj

Modified: trunk/bibdesk/BDSKDocumentSearch.h
===================================================================
--- trunk/bibdesk/BDSKDocumentSearch.h  2009-04-25 14:42:27 UTC (rev 15185)
+++ trunk/bibdesk/BDSKDocumentSearch.h  2009-04-25 17:14:24 UTC (rev 15186)
@@ -38,21 +38,19 @@
 
 #import <Cocoa/Cocoa.h>
 
-...@class RALatchTrigger;
 
 @interface BDSKDocumentSearch : NSObject {
     @private;
     SKSearchRef search;                       // active search
     NSInvocation *callback;                   // encapsulates document target 
for callback messages
-    CGFloat maxScore;                           // maximum score encountered
+    CGFloat maxScore;                         // maximum score encountered
     NSMutableDictionary *originalScores;      // non-normalized scores, 
identifier URLs as keys
     volatile int32_t isSearching;
     volatile int32_t shouldKeepRunning;
     NSString *currentSearchString;            // avoids duplicate searches
     NSLock *searchLock;                       // for currentSearchString and 
invocation
-    NSLock *queueLock;                        // for queued invocations
+    NSConditionLock *queueLock;               // for queued invocations
     NSMutableArray *queue;
-    RALatchTrigger *trigger;
     
     // main thread access only
     NSArray *previouslySelectedPublications;  // convenience for the document

Modified: trunk/bibdesk/BDSKDocumentSearch.m
===================================================================
--- trunk/bibdesk/BDSKDocumentSearch.m  2009-04-25 14:42:27 UTC (rev 15185)
+++ trunk/bibdesk/BDSKDocumentSearch.m  2009-04-25 17:14:24 UTC (rev 15186)
@@ -41,7 +41,6 @@
 #import "BibItem.h"
 #import <libkern/OSAtomic.h>
 #import "NSInvocation_BDSKExtensions.h"
-#import "RALatchTrigger.h"
 
 
 @interface BDSKDocumentSearch (BDSKPrivate)
@@ -51,6 +50,9 @@
 
 @implementation BDSKDocumentSearch
 
+#define QUEUE_EMPTY 0
+#define QUEUE_HAS_INVOCATIONS 1
+
 - (id)initWithDocument:(id)doc;
 {
     self = [super init];
@@ -63,8 +65,7 @@
         [invocation setSelector:cb];
         [invocation setArgument:&self atIndex:2];
         searchLock = [[NSLock alloc] init];
-        queueLock = [[NSLock alloc] init];
-               trigger = [[RALatchTrigger alloc] init];
+        queueLock = [[NSConditionLock alloc] initWithCondition:QUEUE_EMPTY];
         queue = [[NSMutableArray alloc] init];
         
         callback = [invocation retain];
@@ -80,8 +81,8 @@
 // owner should have already sent -terminate; sending it from -dealloc causes 
resurrection
 - (void)dealloc
 {
-       [trigger release];
        [queue release];
+       [queueLock release];
     [currentSearchString release];
     [originalScores release];
     [callback release];
@@ -90,11 +91,17 @@
     [super dealloc];
 }
 
+- (BOOL)shouldKeepRunning {
+    OSMemoryBarrier();
+    return shouldKeepRunning;
+}
+
 - (void)queueInvocation:(NSInvocation *)invocation {
-    [queueLock lock];
-    [queue addObject:invocation];
-    [queueLock unlock];
-    [trigger signal];
+    if ([self shouldKeepRunning]) {
+        [queueLock lock];
+        [queue addObject:invocation];
+        [queueLock unlockWithCondition:QUEUE_HAS_INVOCATIONS];
+    }
 }
 
 - (void)_cancelSearch;
@@ -117,7 +124,10 @@
 - (void)terminate;
 {
     OSAtomicCompareAndSwap32Barrier(1, 0, &shouldKeepRunning);
-    [trigger signal];
+    [queueLock lock];
+    [queue removeAllObjects];
+    // make sure the worker thread wakes up
+    [queueLock unlockWithCondition:QUEUE_HAS_INVOCATIONS];
     [searchLock lock];
     NSInvocation *cb = callback;
     callback = nil;
@@ -260,32 +270,32 @@
     [self queueInvocation:invocation];
 }
 
-- (BOOL)shouldKeepRunning {
-    OSMemoryBarrier();
-    return shouldKeepRunning;
-}
-
 - (void)runSearchThread
 {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
        while ([self shouldKeepRunning]) {
-        // get the next invocation from the queue
         NSInvocation *invocation = nil;
-        [queueLock lock];
-        if ([queue count]) {
+        
+        // get the next invocation from the queue as soon as it's available
+        [queueLock lockWhenCondition:QUEUE_HAS_INVOCATIONS];
+        NSUInteger count = [queue count];
+        if (count) {
             invocation = [[queue objectAtIndex:0] retain];
             [queue removeObjectAtIndex:0];
+            count--;
         }
-        [queueLock unlock];
+        [queueLock unlockWithCondition:count > 0 ? QUEUE_HAS_INVOCATIONS : 
QUEUE_EMPTY];
         
         if (invocation) {
-            [invocation invoke];
-            [invocation release];
-               } else if ([self shouldKeepRunning]) {
-            // if there's no more invocation wait until we're woken up again, 
either for a new invocation or to stop
-            [trigger wait];
-        }
+            @try {
+                [invocation invoke];
+                [invocation release];
+            }
+            @catch(id e) {
+                NSLog(@"Ignored exception %@ when executing a document 
search", e);
+            }
+               }
         
                [pool release];
                pool = [[NSAutoreleasePool alloc] init];
@@ -293,10 +303,6 @@
        
     // make sure the last search was canceled
     [self _cancelSearch];
-    
-    [queueLock lock];
-    [queue removeAllObjects];
-    [queueLock unlock];
        
        [pool release];
 }

Modified: trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj
===================================================================
--- trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj     2009-04-25 14:42:27 UTC 
(rev 15185)
+++ trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj     2009-04-25 17:14:24 UTC 
(rev 15186)
@@ -244,7 +244,6 @@
                CED65AC30906BE65003EED90 /* BibPref_ScriptHooks.nib in 
Resources */ = {isa = PBXBuildFile; fileRef = CED65AC10906BE65003EED90 /* 
BibPref_ScriptHooks.nib */; };
                CED65DC90907A2FD003EED90 /* BDSKParseFormatCommand.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = CED65DC70907A2FD003EED90 /* 
BDSKParseFormatCommand.m */; };
                CED65DCD0907A338003EED90 /* BDSKScriptHook+Scripting.m in 
Sources */ = {isa = PBXBuildFile; fileRef = CED65DCB0907A338003EED90 /* 
BDSKScriptHook+Scripting.m */; };
-               CEDA7D410F9609D600F72C0A /* RALatchTrigger.m in Sources */ = 
{isa = PBXBuildFile; fileRef = CEDA7D3F0F9609D600F72C0A /* RALatchTrigger.m */; 
};
                CEDA7E0D0F96497B00F72C0A /* NSAlert_BDSKExtensions.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = CEDA7E0B0F96497B00F72C0A /* 
NSAlert_BDSKExtensions.m */; };
                CEDB58F40D3692A400E40354 /* ComplexStringEditor.nib in 
Resources */ = {isa = PBXBuildFile; fileRef = CEDB58F20D3692A400E40354 /* 
ComplexStringEditor.nib */; };
                CEDBDE4C0F4C863500190AF5 /* BDSKPreferenceRecord.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = CEDBDE4A0F4C863500190AF5 /* 
BDSKPreferenceRecord.m */; };
@@ -1256,8 +1255,6 @@
                CED65DC70907A2FD003EED90 /* BDSKParseFormatCommand.m */ = {isa 
= PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; 
path = BDSKParseFormatCommand.m; sourceTree = "<group>"; };
                CED65DCA0907A338003EED90 /* BDSKScriptHook+Scripting.h */ = 
{isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; 
path = "BDSKScriptHook+Scripting.h"; sourceTree = "<group>"; };
                CED65DCB0907A338003EED90 /* BDSKScriptHook+Scripting.m */ = 
{isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = 
sourcecode.c.objc; path = "BDSKScriptHook+Scripting.m"; sourceTree = "<group>"; 
};
-               CEDA7D3E0F9609D600F72C0A /* RALatchTrigger.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
RALatchTrigger.h; sourceTree = "<group>"; };
-               CEDA7D3F0F9609D600F72C0A /* RALatchTrigger.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= RALatchTrigger.m; sourceTree = "<group>"; };
                CEDA7E0A0F96497B00F72C0A /* NSAlert_BDSKExtensions.h */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path 
= NSAlert_BDSKExtensions.h; sourceTree = "<group>"; };
                CEDA7E0B0F96497B00F72C0A /* NSAlert_BDSKExtensions.m */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; 
path = NSAlert_BDSKExtensions.m; sourceTree = "<group>"; };
                CEDB58F30D3692A400E40354 /* English */ = {isa = 
PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = 
English.lproj/ComplexStringEditor.nib; sourceTree = "<group>"; };
@@ -2029,7 +2026,6 @@
                                CE3E4A080E75B9F20014C328 /* Ken Ferry */,
                                F924FC140BC4000B00672839 /* harmless */,
                                CEED2A820F4D85220078E87A /* IconFamily */,
-                               CEDA7D3A0F96098100F72C0A /* RAOperationQueue */,
                        );
                        name = "Third Party Sources";
                        path = vendorsrc;
@@ -2421,16 +2417,6 @@
                        name = Products;
                        sourceTree = "<group>";
                };
-               CEDA7D3A0F96098100F72C0A /* RAOperationQueue */ = {
-                       isa = PBXGroup;
-                       children = (
-                               CEDA7D3E0F9609D600F72C0A /* RALatchTrigger.h */,
-                               CEDA7D3F0F9609D600F72C0A /* RALatchTrigger.m */,
-                       );
-                       name = RAOperationQueue;
-                       path = RoqueAmoeba/RAOperationQueue;
-                       sourceTree = "<group>";
-               };
                CEE23BAE0BFBA5FE002B746B /* Shared Support */ = {
                        isa = PBXGroup;
                        children = (
@@ -3955,7 +3941,6 @@
                                CE1EF3890F8E6190004E7AE8 /* 
BDSKGradientTableView.m in Sources */,
                                6C5DE3E80F8FC33B00E02D5F /* 
BDSKMathSiteParser.m in Sources */,
                                6CD26A240F928EEE0089FDFD /* 
BDSKBibDeskProtocol.m in Sources */,
-                               CEDA7D410F9609D600F72C0A /* RALatchTrigger.m in 
Sources */,
                                CEDA7E0D0F96497B00F72C0A /* 
NSAlert_BDSKExtensions.m in Sources */,
                                6CAEE4CD0F98EC63009EA5FE /* BDSKCOinSParser.m 
in Sources */,
                        );


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Crystal Reports &#45; New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty&#45;free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to