2007-02-24 20:36:27 +00:00
|
|
|
//
|
|
|
|
// Semaphore.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 8/2/05.
|
|
|
|
// Copyright 2005 Vincent Spader. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Semaphore.h"
|
|
|
|
|
|
|
|
@implementation Semaphore
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
2007-02-24 20:36:27 +00:00
|
|
|
self = [super init];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(self) {
|
2007-02-24 20:36:27 +00:00
|
|
|
semaphore_create(mach_task_self(), &semaphore, SYNC_POLICY_FIFO, 0);
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)signal {
|
2007-02-24 20:36:27 +00:00
|
|
|
semaphore_signal_all(semaphore);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)timedWait:(int)microseconds {
|
|
|
|
mach_timespec_t timeout = { 0, microseconds * 1000UL };
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
semaphore_timedwait(semaphore, timeout);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)wait {
|
|
|
|
mach_timespec_t t = { 2.0, 0.0 }; // 2 second timeout
|
2007-02-24 20:36:27 +00:00
|
|
|
semaphore_timedwait(semaphore, t);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)waitIndefinitely {
|
|
|
|
semaphore_wait(semaphore);
|
2013-10-12 20:52:58 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@end
|