aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes/MCMainThreadMac.mm
blob: fe1ebb240b2eb27c32a888a13af48f87d949b03b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "MCMainThread.h"

#import <Foundation/Foundation.h>

#include "MCAutoreleasePool.h"

using namespace mailcore;

static void destroyDelayedCall(void * caller);

@interface LEPPPMainThreadCaller : NSObject {
    void (* _function)(void *);
    void * _context;
    NSTimer * _timer;
}

@property (nonatomic, assign) void (* function)(void *);
@property (nonatomic, assign) void * context;
@property (nonatomic, retain) NSTimer * timer;

- (void) call;
- (void) cancel;

@end

@implementation LEPPPMainThreadCaller

@synthesize function = _function;
@synthesize context = _context;
@synthesize timer = _timer;

- (instancetype) init
{
    self = [super init];
    return self;
}

- (void) dealloc
{
    [_timer release];
    [super dealloc];
}

- (void) call
{
    AutoreleasePool * pool = new AutoreleasePool();
    _function(_context);
    pool->release();
    
    [self setTimer:nil];
    
    destroyDelayedCall((void *) self);
}

- (void) cancel
{
    [_timer invalidate];
    [self setTimer:nil];
    destroyDelayedCall((void *) self);
}

@end

void mailcore::callOnMainThread(void (* function)(void *), void * context)
{
    LEPPPMainThreadCaller * caller;
    caller = [[LEPPPMainThreadCaller alloc] init];
    [caller setFunction:function];
    [caller setContext:context];
    [caller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:NO];
}

void mailcore::callOnMainThreadAndWait(void (* function)(void *), void * context)
{
    LEPPPMainThreadCaller * caller;
    caller = [[LEPPPMainThreadCaller alloc] init];
    [caller setFunction:function];
    [caller setContext:context];
    [caller performSelectorOnMainThread:@selector(call) withObject:nil waitUntilDone:YES];
}

void * mailcore::callAfterDelay(void (* function)(void *), void * context, double time)
{
    LEPPPMainThreadCaller * caller;
    caller = [[LEPPPMainThreadCaller alloc] init];
    [caller setFunction:function];
    [caller setContext:context];
    
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval) time
                                                       target:caller
                                                     selector:@selector(call)
                                                     userInfo:nil
                                                      repeats:NO];
    [caller setTimer:timer];
    
    return caller;
}

static void destroyDelayedCall(void * caller)
{
    [(LEPPPMainThreadCaller *) caller release];
}

void mailcore::cancelDelayedCall(void * delayedCall)
{
    LEPPPMainThreadCaller * caller = (LEPPPMainThreadCaller *) delayedCall;
    [caller cancel];
}