ObjFW  Check-in [35ad639b7c]

Overview
Comment:OFRunLoop: Use OFMutex instead of @synchronized.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 35ad639b7c52b215aa01bf4489120e6a94b0aa1fdbdfd5e0a22c5b5802c1cab0
User & Date: js on 2012-12-16 13:39:11
Other Links: manifest | tags
Context
2012-12-16
14:57
Add -[stringByAppendingFormat:arguments:]. check-in: aecadefe62 user: js tags: trunk
13:39
OFRunLoop: Use OFMutex instead of @synchronized. check-in: 35ad639b7c user: js tags: trunk
01:24
OFStream: Fix a FIXME. check-in: 28ffd3d914 user: js tags: trunk
Changes

Modified src/OFRunLoop.h from [3854375363] to [eb85eb1d39].

16
17
18
19
20
21
22

23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38

#import "OFObject.h"
#import "OFStream.h"
#import "OFStreamObserver.h"
#import "OFTCPSocket.h"

@class OFSortedList;

@class OFTimer;
@class OFMutableDictionary;

/*!
 * @brief A class providing a run loop for the application and its processes.
 */
@interface OFRunLoop: OFObject
{
	OFSortedList *timersQueue;

	OFStreamObserver *streamObserver;
	OFMutableDictionary *readQueues;
}

/*!
 * @brief Returns the main run loop.
 *







>









>







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#import "OFObject.h"
#import "OFStream.h"
#import "OFStreamObserver.h"
#import "OFTCPSocket.h"

@class OFSortedList;
@class OFMutex;
@class OFTimer;
@class OFMutableDictionary;

/*!
 * @brief A class providing a run loop for the application and its processes.
 */
@interface OFRunLoop: OFObject
{
	OFSortedList *timersQueue;
	OFMutex *timersQueueLock;
	OFStreamObserver *streamObserver;
	OFMutableDictionary *readQueues;
}

/*!
 * @brief Returns the main run loop.
 *

Modified src/OFRunLoop.m from [a5c17b66d7] to [5845195cdf].

16
17
18
19
20
21
22

23
24
25
26
27
28
29

#include "config.h"

#import "OFRunLoop.h"
#import "OFDictionary.h"
#import "OFThread.h"
#import "OFSortedList.h"

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

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

static OFRunLoop *mainRunLoop = nil;







>







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

#include "config.h"

#import "OFRunLoop.h"
#import "OFDictionary.h"
#import "OFThread.h"
#import "OFSortedList.h"
#import "OFMutex.h"
#import "OFTimer.h"
#import "OFDate.h"

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

static OFRunLoop *mainRunLoop = nil;
262
263
264
265
266
267
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
292
293

294


295

296
297
298
299
300
301
302

- init
{
	self = [super init];

	@try {
		timersQueue = [[OFSortedList alloc] init];


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

		readQueues = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[timersQueue release];

	[streamObserver release];
	[readQueues release];

	[super dealloc];
}

- (void)addTimer: (OFTimer*)timer
{
	@synchronized (timersQueue) {

		[timersQueue insertObject: timer];


	}

	[streamObserver cancel];
}

- (void)streamIsReadyForReading: (OFStream*)stream
{
	OFList *queue = [readQueues objectForKey: stream];
	of_list_object_t *listObject;







>
















>








|
>

>
>

>







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

- init
{
	self = [super init];

	@try {
		timersQueue = [[OFSortedList alloc] init];
		timersQueueLock = [[OFMutex alloc] init];

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

		readQueues = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[timersQueue release];
	[timersQueueLock release];
	[streamObserver release];
	[readQueues release];

	[super dealloc];
}

- (void)addTimer: (OFTimer*)timer
{
	[timersQueueLock lock];
	@try {
		[timersQueue insertObject: timer];
	} @finally {
		[timersQueueLock unlock];
	}

	[streamObserver cancel];
}

- (void)streamIsReadyForReading: (OFStream*)stream
{
	OFList *queue = [readQueues objectForKey: stream];
	of_list_object_t *listObject;
526
527
528
529
530
531
532
533

534
535
536
537
538
539
540
541
542
543
544
545


546
547
548
549
550
551

552


553
554
555
556
557
558
559
{
	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		OFDate *now = [OFDate date];
		OFTimer *timer;
		OFDate *nextTimer;

		@synchronized (timersQueue) {

			of_list_object_t *listObject =
			    [timersQueue firstListObject];

			if (listObject != NULL &&
			    [[listObject->object fireDate] compare: now] !=
			    OF_ORDERED_DESCENDING) {
				timer =
				    [[listObject->object retain] autorelease];

				[timersQueue removeListObject: listObject];
			} else
				timer = nil;


		}

		if ([timer isValid])
			[timer fire];

		@synchronized (timersQueue) {

			nextTimer = [[timersQueue firstObject] fireDate];


		}

		/* Watch for stream events until the next timer is due */
		if (nextTimer != nil) {
			double timeout = [nextTimer timeIntervalSinceNow];

			if (timeout > 0)







|
>












>
>





|
>

>
>







533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
{
	for (;;) {
		void *pool = objc_autoreleasePoolPush();
		OFDate *now = [OFDate date];
		OFTimer *timer;
		OFDate *nextTimer;

		[timersQueueLock lock];
		@try {
			of_list_object_t *listObject =
			    [timersQueue firstListObject];

			if (listObject != NULL &&
			    [[listObject->object fireDate] compare: now] !=
			    OF_ORDERED_DESCENDING) {
				timer =
				    [[listObject->object retain] autorelease];

				[timersQueue removeListObject: listObject];
			} else
				timer = nil;
		} @finally {
			[timersQueueLock unlock];
		}

		if ([timer isValid])
			[timer fire];

		[timersQueueLock lock];
		@try {
			nextTimer = [[timersQueue firstObject] fireDate];
		} @finally {
			[timersQueueLock unlock];
		}

		/* Watch for stream events until the next timer is due */
		if (nextTimer != nil) {
			double timeout = [nextTimer timeIntervalSinceNow];

			if (timeout > 0)