Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 189 lines (149 sloc) 4.884 kB
720b0e7 @uliwitness Added licensing information to the source files.
authored
1 //
2 // UKThreadMessenger.m
3 // Shovel
4 //
5 // Created 14.10.2004 by Uli Kusterer.
6 // Copyright 2004 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
dc5be1c Initial check-in.
uli authored
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "UKThreadMessenger.h"
33 #include <unistd.h>
34
35
36 @implementation UKThreadMessenger
37
38 -(id) initWithTarget: (id)trg newestFirst: (BOOL)nwf
39 {
40 self = [super init];
41
42 if( self )
43 {
44 messages = [[NSMutableArray alloc] init];
45 target = trg;
46 newestFirst = nwf;
47 [NSThread detachNewThreadSelector: @selector(sendMessages:) toTarget: self withObject: nil];
48 }
49
50 return self;
51 }
52
53 -(void) dealloc
54 {
55 [messages release];
56 [super dealloc];
57
58 //NSLog(@"thread messenger released.");
59 }
60
61 // -----------------------------------------------------------------------------
62 // release:
63 // Since NSThread retains its target, we need this method to terminate the
64 // thread when we reach a retain-count of two.
65 //
66 // REVISIONS:
67 // 2004-11-12 UK Created.
68 // -----------------------------------------------------------------------------
69
70 -(void) release
71 {
72 if( [self retainCount] == 2 && threadRunning )
73 threadRunning = NO;
74
75 [super release];
76 }
77
78 -(void) sendMessages: (id)sender
79 {
80 threadRunning = YES;
81
82 [NSThread setThreadPriority: 0.3]; // Lower this thread's priority.
83
84 while( threadRunning )
85 {
86 while( threadRunning && (!messages || [messages count] <= 0) )
87 {
88 usleep(1000);
89 }
90
91 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
92 NSArray* msgs = nil;
93
94 @synchronized( self )
95 {
96 msgs = [messages autorelease];
97 messages = [[NSMutableArray alloc] init];
98 }
99
100 NSEnumerator* enny;
101 if( newestFirst )
102 enny = [msgs reverseObjectEnumerator];
103 else
104 enny = [msgs objectEnumerator];
105 NSInvocation* inv;
106 while( (inv = [enny nextObject]) )
107 {
108 NSAutoreleasePool* pool2 = [[NSAutoreleasePool alloc] init];
109 [inv invoke];
110 [pool2 release];
111 //usleep(1);
112 }
113 [pool release];
114 }
115
116 //NSLog(@"sendMessages terminated.");
117 }
118
119
120 -(id) performSelector: (SEL)itemAction withObject: (id)obj
121 {
122 BOOL does = [super respondsToSelector: itemAction];
123 if( does )
124 return [super performSelector: itemAction withObject: obj];
125
126 if( ![target respondsToSelector: itemAction] )
127 [self doesNotRecognizeSelector: itemAction];
128
129 NSInvocation* inv = [NSInvocation invocationWithMethodSignature: [target methodSignatureForSelector: @selector(itemAction)]];
130
131 [inv setSelector: itemAction];
132 [inv setTarget: target];
133 [inv retainArguments];
134
135 @synchronized( self )
136 {
137 [messages addObject: inv];
138 }
139
140 return nil;
141 }
142
143 -(BOOL) respondsToSelector: (SEL)itemAction
144 {
145 BOOL does = [super respondsToSelector: itemAction];
146
147 return( does || [target respondsToSelector: itemAction] );
148 }
149
150
151 // -----------------------------------------------------------------------------
152 // Forwarding unknown methods to the first responder:
153 // -----------------------------------------------------------------------------
154
155 -(NSMethodSignature*) methodSignatureForSelector: (SEL)itemAction
156 {
157 NSMethodSignature* sig = [super methodSignatureForSelector: itemAction];
158
159 if( sig )
160 return sig;
161
162 return [target methodSignatureForSelector: itemAction];
163
164 return nil;
165 }
166
167 -(void) forwardInvocation: (NSInvocation*)invocation
168 {
169 SEL itemAction = [invocation selector];
170 NSInvocation* inv;
171
172 if( [target respondsToSelector: itemAction] )
173 {
174 inv = invocation;
175 [inv setTarget: target];
176 [inv retainArguments];
177 @synchronized( self )
178 {
179 [messages addObject: inv];
180 }
181 }
182 else
183 [self doesNotRecognizeSelector: itemAction];
184 }
185
186
187
188 @end
Something went wrong with that request. Please try again.