SharedTimerMac.cpp [plain text]
#import "config.h"
#import "SharedTimer.h"
#import "WebCoreThread.h"
#include <Foundation/Foundation.h>
#include <wtf/Assertions.h>
namespace WebCore {
static void (*sharedTimerFiredFunction)();
static void timerFired(CFRunLoopTimerRef, void*);
}
namespace WebCore {
void setSharedTimerFiredFunction(void (*f)())
{
ASSERT(!sharedTimerFiredFunction || sharedTimerFiredFunction == f);
sharedTimerFiredFunction = f;
}
static void timerFired(CFRunLoopTimerRef, void*)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sharedTimerFiredFunction();
[pool drain];
}
void setSharedTimerFireTime(double fireTime)
{
ASSERT(sharedTimerFiredFunction);
WebThreadContext *threadContext = WebThreadCurrentContext();
CFRunLoopTimerRef sharedTimer = threadContext->sharedTimer;
if (sharedTimer) {
CFRunLoopTimerInvalidate(sharedTimer);
CFRelease(sharedTimer);
}
CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970;
sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0);
threadContext->sharedTimer = sharedTimer;
CFRunLoopAddTimer(threadContext->runLoop, sharedTimer, kCFRunLoopCommonModes);
}
void stopSharedTimer()
{
WebThreadContext *threadContext = WebThreadCurrentContext();
CFRunLoopTimerRef sharedTimer = threadContext->sharedTimer;
if (sharedTimer) {
CFRunLoopTimerInvalidate(sharedTimer);
CFRelease(sharedTimer);
threadContext->sharedTimer = NULL;
}
}
}