ObjFW  Check-in [0cfbbb367a]

Overview
Comment:Slightly refactor OFRunLoop.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0cfbbb367adf079387b689f8d4538896bc710c7a243303cefd16c0eabe85283c
User & Date: js on 2012-09-16 13:23:02
Other Links: manifest | tags
Context
2012-09-16
13:23
Add -[performSelector:onThread:afterDelay:]. check-in: 4fd5559652 user: js tags: trunk
13:23
Slightly refactor OFRunLoop. check-in: 0cfbbb367a user: js tags: trunk
11:52
Update Xcode project. check-in: b42e3ea732 user: js tags: trunk
Changes

Modified src/OFApplication.m from [c50e9dd27b] to [e225bd8cdc].

296
297
298
299
300
301
302
303
304


305
306
307
308
309
310
311
296
297
298
299
300
301
302


303
304
305
306
307
308
309
310
311







-
-
+
+








- (void)run
{
	void *pool;
	OFRunLoop *runLoop;

	[OFThread _createMainThread];
	runLoop = [[[OFRunLoop alloc] init] autorelease];
	[OFRunLoop _setMainRunLoop: runLoop];
	runLoop = [OFRunLoop currentRunLoop];
	[OFRunLoop _setMainRunLoop];

	pool = objc_autoreleasePoolPush();
	[delegate applicationDidFinishLaunching];
	objc_autoreleasePoolPop(pool);

	[runLoop run];
}

Modified src/OFRunLoop.h from [07aad9ab59] to [49feceeeb4].

46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
46
47
48
49
50
51
52

53
54
55
56
57
58
59
60







-
+







/**
 * \brief Returns the run loop for the current thread.
 *
 * \return The run loop for the current thread
 */
+ (OFRunLoop*)currentRunLoop;

+ (void)_setMainRunLoop: (OFRunLoop*)mainRunLoop;
+ (void)_setMainRunLoop;
#ifdef OF_HAVE_BLOCKS
+ (void)_addAsyncReadForStream: (OFStream*)stream
			buffer: (void*)buffer
			length: (size_t)length
			 block: (of_stream_async_read_block_t)block;
+ (void)_addAsyncReadLineForStream: (OFStream*)stream
			  encoding: (of_string_encoding_t)encoding

Modified src/OFRunLoop.m from [646227ddee] to [f5145d052f].

24
25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
24
25
26
27
28
29
30


31
32
33
34
35
36
37
38







-
-
+







#import "OFSortedList.h"
#import "OFTimer.h"
#import "OFDate.h"

#import "autorelease.h"
#import "macros.h"

static OFTLSKey *currentRunLoopKey;
static OFRunLoop *mainRunLoop;
static OFRunLoop *mainRunLoop = nil;

#ifdef OF_HAVE_BLOCKS
@interface OFRunLoop_ReadQueueItem: OFObject
{
	void *buffer;
	size_t length;
	of_stream_async_read_block_t block;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

113

114
115

116
117

118
119



120
121

122
123
124

125

126

127
128

129
130
131
132
133
134
135
92
93
94
95
96
97
98






99
100
101
102
103
104
105
106

107
108

109


110


111
112
113
114

115
116
117

118
119
120

121


122
123
124
125
126
127
128
129







-
-
-
-
-
-







+
-
+

-
+
-
-
+
-
-
+
+
+

-
+


-
+

+
-
+
-
-
+








	[super dealloc];
}
@end
#endif

@implementation OFRunLoop
+ (void)initialize
{
	if (self == [OFRunLoop class])
		currentRunLoopKey = [[OFTLSKey alloc] init];
}

+ (OFRunLoop*)mainRunLoop
{
	return [[mainRunLoop retain] autorelease];
}

+ (OFRunLoop*)currentRunLoop
{
	OFThread *currentThread = [OFThread currentThread];
	OFRunLoop *runLoop = [OFThread objectForTLSKey: currentRunLoopKey];
	OFRunLoop *runLoop = [currentThread runLoop];

	if (runLoop == nil) {
	if (runLoop != nil)
		runLoop = [[OFRunLoop alloc] init];
		[OFThread setObject: runLoop
		return runLoop;
			  forTLSKey: currentRunLoopKey];
	}

	runLoop = [[[OFRunLoop alloc] init] autorelease];
	[currentThread _setRunLoop: runLoop];

	return [[runLoop retain] autorelease];
	return runLoop;
}

+ (void)_setMainRunLoop: (OFRunLoop*)mainRunLoop_
+ (void)_setMainRunLoop
{
	void *pool = objc_autoreleasePoolPush();
	mainRunLoop = [mainRunLoop_ retain];
	mainRunLoop = [[[OFThread currentThread] runLoop] retain];
	[OFThread setObject: mainRunLoop
		  forTLSKey: currentRunLoopKey];
	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
+ (void)_addAsyncReadForStream: (OFStream*)stream
			buffer: (void*)buffer
			length: (size_t)length
			 block: (of_stream_async_read_block_t)block
209
210
211
212
213
214
215
216
217

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
203
204
205
206
207
208
209


210

211
212
213
214
215


216
217
218
219
220
221
222







-
-
+
-





-
-







#endif

- init
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();

		timersQueue = [[OFSortedList alloc] init];
		timersQueue = [[[OFThread currentThread] _timersQueue] retain];

		streamObserver = [[OFStreamObserver alloc] init];
		[streamObserver setDelegate: self];

		readQueues = [[OFMutableDictionary alloc] init];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

Modified src/OFThread.h from [053c62f685] to [cf6df87507].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31







+







#import "OFObject.h"
#import "OFList.h"

#import "threading.h"

@class OFDate;
@class OFSortedList;
@class OFRunLoop;

#ifdef OF_HAVE_BLOCKS
typedef id (^of_thread_block_t)(id object);
#endif

/**
 * \brief A class for Thread Local Storage keys.
96
97
98
99
100
101
102
103

104
105
106
107
108
109
110
111
112
113
114
115
97
98
99
100
101
102
103

104





105
106
107
108
109
110
111







-
+
-
-
-
-
-







		OF_THREAD_RUNNING,
		OF_THREAD_WAITING_FOR_JOIN
	} running;
#ifdef OF_HAVE_BLOCKS
	of_thread_block_t block;
#endif
	id returnValue;
#ifdef OF_THREAD_M
	OFRunLoop *runLoop;
@protected
#else
@private
#endif
	OFSortedList *timersQueue;
}

#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS)
@property (copy) of_thread_block_t block;
#endif

/**
274
275
276
277
278
279
280






281


282
283
284
285
286
287
288
270
271
272
273
274
275
276
277
278
279
280
281
282

283
284
285
286
287
288
289
290
291







+
+
+
+
+
+
-
+
+







/**
 * \brief Joins a thread.
 *
 * \return The object returned by the main method of the thread.
 */
- (id)join;

/**
 * \brief Returns the run loop for the thread or nil if it has no run loop.
 *
 * \return The run loop for the thread or nil if it has no run loop
 */
- (OFRunLoop*)runLoop;
- (OFSortedList*)_timersQueue;

- (void)_setRunLoop: (OFRunLoop*)runLoop;
@end

/**
 * \brief A class for creating mutual exclusions.
 */
@interface OFMutex: OFObject
{

Modified src/OFThread.m from [dc21f4b704] to [28fcccac40].

27
28
29
30
31
32
33

34
35
36
37
38
39
40
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41







+







# include <sched.h>
#endif

#import "OFThread.h"
#import "OFList.h"
#import "OFDate.h"
#import "OFSortedList.h"
#import "OFRunLoop.h"
#import "OFAutoreleasePool.h"

#ifdef _WIN32
# include <windows.h>
#endif

#import "OFConditionBroadcastFailedException.h"
278
279
280
281
282
283
284
285
286
287
288

289
290
291

292
293
294
295
296
297
298
279
280
281
282
283
284
285




286



287
288
289
290
291
292
293
294







-
-
-
-
+
-
-
-
+







						    selector: _cmd];

	return nil;
}

- (void)handleTermination
{
	@synchronized (timersQueue) {
		/*
		 * Make sure we don't run old timers when the thread is
		 * restarted.
	[runLoop release];
		 */
		[timersQueue removeAllObjects];
	}
	runLoop = nil;
}

- (void)start
{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException
		    exceptionWithClass: [self class]
323
324
325
326
327
328
329
330

331
332

333
334

335
336
337
338
339






340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
319
320
321
322
323
324
325

326
327

328


329





330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

354
355
356
357
358
359
360







-
+

-
+
-
-
+
-
-
-
-
-
+
+
+
+
+
+


















-







				thread: self];

	running = OF_THREAD_NOT_RUNNING;

	return returnValue;
}

- (OFSortedList*)_timersQueue
- (OFRunLoop*)runLoop
{
	if (timersQueue == nil) {
	return [[runLoop retain] autorelease];
		OFSortedList *tmp = [[OFSortedList alloc] init];

}
		if (!of_atomic_cmpswap_ptr((void**)&timersQueue, nil, tmp))
			[tmp release];
	}

	return [[timersQueue retain] autorelease];

- (void)_setRunLoop: (OFRunLoop*)runLoop_
{
	OFRunLoop *old = runLoop;
	runLoop = [runLoop_ retain];
	[old release];
}

- (void)dealloc
{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException
		    exceptionWithClass: [self class]
				thread: self];

	/*
	 * We should not be running anymore, but call detach in order to free
	 * the resources.
	 */
	if (running == OF_THREAD_WAITING_FOR_JOIN)
		of_thread_detach(thread);

	[object release];
	[returnValue release];
	[timersQueue release];

	[super dealloc];
}

- copy
{
	return [self retain];