ObjFW  Check-in [6069030651]

Overview
Comment:Add OFRecursiveMutex class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 60690306510eec144bb497f841f0001e769c00f80a43644d18b8f868d14ef67d
User & Date: js on 2012-08-05 17:34:39
Other Links: manifest | tags
Context
2012-08-05
18:11
Initial ARC support. check-in: 90eae0b1fc user: js tags: trunk
17:34
Add OFRecursiveMutex class. check-in: 6069030651 user: js tags: trunk
17:22
Use recursive pthread mutexes if available. check-in: 1e10b33066 user: js tags: trunk
Changes

Modified src/OFThread.h from [2dc62c0de7] to [51a42ce92e].

281
282
283
284
285
286
287










288
289
290
291
292
293
294

/**
 * \brief Unlocks the mutex.
 */
- (void)unlock;
@end











/**
 * \brief A class implementing a condition variable for thread synchronization.
 */
@interface OFCondition: OFMutex
{
	of_condition_t condition;
	BOOL conditionInitialized;







>
>
>
>
>
>
>
>
>
>







281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

/**
 * \brief Unlocks the mutex.
 */
- (void)unlock;
@end

/**
 * \brief A class for creating mutual exclusions which can be entered
 *	  recursively.
 */
@interface OFRecursiveMutex: OFMutex
{
	of_rmutex_t rmutex;
}
@end

/**
 * \brief A class implementing a condition variable for thread synchronization.
 */
@interface OFCondition: OFMutex
{
	of_condition_t condition;
	BOOL conditionInitialized;

Modified src/OFThread.m from [d622539c3c] to [03f9920eb4].

415
416
417
418
419
420
421





422
423
424
425
426
427
428
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}






- (void)lock
{
	if (!of_mutex_lock(&mutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];







>
>
>
>
>







415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- _initWithoutCreatingMutex
{
	return [super init];
}

- (void)lock
{
	if (!of_mutex_lock(&mutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
441
442
443
444
445
446
447

















































448
449
450
451
452
453
454
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_mutex_free(&mutex))

















































			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_mutex_free(&mutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

@implementation OFRecursiveMutex
- init
{
	self = [super _initWithoutCreatingMutex];

	if (!of_rmutex_new(&rmutex)) {
		Class c = [self class];
		[self release];
		@throw [OFInitializationFailedException exceptionWithClass: c];
	}

	initialized = YES;

	return self;
}

- (void)lock
{
	if (!of_rmutex_lock(&rmutex))
		@throw [OFMutexLockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (BOOL)tryLock
{
	return of_rmutex_trylock(&rmutex);
}

- (void)unlock
{
	if (!of_rmutex_unlock(&rmutex))
		@throw [OFMutexUnlockFailedException
		    exceptionWithClass: [self class]
				 mutex: self];
}

- (void)dealloc
{
	if (initialized)
		if (!of_rmutex_free(&rmutex))
			@throw [OFMutexStillLockedException
			    exceptionWithClass: [self class]
					 mutex: self];

	[super dealloc];
}
@end

Modified src/threading.h from [aa1078d8fa] to [a1aae6b58b].

391
392
393
394
395
396
397

398
399
400
401
402
403
404
	if (pthread_mutexattr_destroy(&attr))
		return NO;

	return YES;
}

# define of_rmutex_lock of_mutex_lock

# define of_rmutex_unlock of_mutex_unlock
# define of_rmutex_free of_mutex_free
#else
static OF_INLINE BOOL
of_rmutex_new(of_rmutex_t *rmutex)
{
	if (!of_mutex_new(&rmutex->mutex))







>







391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	if (pthread_mutexattr_destroy(&attr))
		return NO;

	return YES;
}

# define of_rmutex_lock of_mutex_lock
# define of_rmutex_trylock of_mutex_trylock
# define of_rmutex_unlock of_mutex_unlock
# define of_rmutex_free of_mutex_free
#else
static OF_INLINE BOOL
of_rmutex_new(of_rmutex_t *rmutex)
{
	if (!of_mutex_new(&rmutex->mutex))
419
420
421
422
423
424
425






















426
427
428
429
430
431
432
		if (!of_tlskey_set(rmutex->count, (void*)(count + 1)))
			return NO;
		return YES;
	}

	if (!of_mutex_lock(&rmutex->mutex))
		return NO;























	if (!of_tlskey_set(rmutex->count, (void*)1)) {
		of_mutex_unlock(&rmutex->mutex);
		return NO;
	}

	return YES;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
		if (!of_tlskey_set(rmutex->count, (void*)(count + 1)))
			return NO;
		return YES;
	}

	if (!of_mutex_lock(&rmutex->mutex))
		return NO;

	if (!of_tlskey_set(rmutex->count, (void*)1)) {
		of_mutex_unlock(&rmutex->mutex);
		return NO;
	}

	return YES;
}

static OF_INLINE BOOL
of_rmutex_trylock(of_rmutex_t *rmutex)
{
	uintptr_t count = (uintptr_t)of_tlskey_get(rmutex->count);

	if (count > 0) {
		if (!of_tlskey_set(rmutex->count, (void*)(count + 1)))
			return NO;
		return YES;
	}

	if (!of_mutex_trylock(&rmutex->mutex))
		return NO;

	if (!of_tlskey_set(rmutex->count, (void*)1)) {
		of_mutex_unlock(&rmutex->mutex);
		return NO;
	}

	return YES;