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
|
#import "OFList.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "threading.h"
static OFList *tlskeys;
static id
call_main(id obj)
{
/*
* Nasty workaround for thread implementations which can't return a
* value on join.
*/
((OFThread*)obj)->retval = [obj main];
[OFTLSKey callAllDestructors];
[OFAutoreleasePool releaseAll];
return 0;
}
@implementation OFThread
+ threadWithObject: (OFObject <OFCopying>*)obj
{
return [[[self alloc] initWithObject: obj] autorelease];
}
+ setObject: (OFObject*)obj
forTLSKey: (OFTLSKey*)key
|
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
#import "OFList.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "threading.h"
static OFList *tlskeys;
static of_tlskey_t thread_self;
static id
call_run(id obj)
{
if (!of_tlskey_set(thread_self, obj))
@throw [OFInitializationFailedException
newWithClass: [obj class]];
/*
* Nasty workaround for thread implementations which can't return a
* value on join.
*/
((OFThread*)obj)->retval = [[obj run] retain];
[obj handleTermination];
((OFThread*)obj)->running = OF_THREAD_WAITING_FOR_JOIN;
[OFTLSKey callAllDestructors];
[OFAutoreleasePool releaseAll];
[obj release];
return 0;
}
@implementation OFThread
+ (void)initialize
{
if (self != [OFThread class])
return;
if (!of_tlskey_new(&thread_self))
@throw [OFInitializationFailedException newWithClass: self];
}
+ threadWithObject: (OFObject <OFCopying>*)obj
{
return [[[self alloc] initWithObject: obj] autorelease];
}
+ setObject: (OFObject*)obj
forTLSKey: (OFTLSKey*)key
|
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
109
110
111
112
113
114
115
116
|
return self;
}
+ (id)objectForTLSKey: (OFTLSKey*)key
{
return [[of_tlskey_get(key->key) retain] autorelease];
}
- init
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- initWithObject: (OFObject <OFCopying>*)obj
{
self = [super init];
object = [obj retain];
if (!of_thread_new(&thread, call_main, self)) {
Class c = isa;
[object release];
[super dealloc];
@throw [OFInitializationFailedException newWithClass: c];
}
running = YES;
return self;
}
- main
{
return nil;
}
- join
{
of_thread_join(thread);
running = NO;
return retval;
}
- (void)dealloc
{
/*
* No need to handle errors - if canceling the thread fails, we can't
* do anything anyway. Most likely, it finished already or was already
* canceled.
*/
if (running)
of_thread_cancel(thread);
[object release];
[super dealloc];
}
@end
@implementation OFTLSKey
+ (void)initialize
{
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
<
<
>
>
|
>
|
>
|
|
>
|
<
|
>
>
>
>
>
>
|
|
|
>
>
|
<
<
<
<
<
|
<
>
>
>
|
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
return self;
}
+ (id)objectForTLSKey: (OFTLSKey*)key
{
return [[of_tlskey_get(key->key) retain] autorelease];
}
+ (OFThread*)currentThread
{
return of_tlskey_get(thread_self);
}
+ (void)terminate
{
[self terminateWithObject: nil];
}
+ (void)terminateWithObject: (id)obj
{
OFThread *thread = of_tlskey_get(thread_self);
if (thread != nil) {
thread->retval = [obj retain];
[thread handleTermination];
thread->running = OF_THREAD_WAITING_FOR_JOIN;
}
[OFTLSKey callAllDestructors];
[OFAutoreleasePool releaseAll];
[thread release];
of_thread_exit();
}
- init
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- initWithObject: (OFObject <OFCopying>*)obj
{
self = [super init];
object = [obj retain];
return self;
}
- (id)run
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
return nil;
}
- (void)handleTermination
{
}
- start
{
if (!of_thread_new(&thread, call_run, self))
@throw [OFThreadStartFailedException newWithClass: isa];
running = OF_THREAD_RUNNING;
[self retain];
return self;
}
- (id)join
{
if (running != OF_THREAD_WAITING_FOR_JOIN || !of_thread_join(thread))
@throw [OFThreadJoinFailedException newWithClass: isa];
running = OF_THREAD_NOT_RUNNING;
return retval;
}
- (void)dealloc
{
if (running == OF_THREAD_RUNNING)
@throw [OFThreadStillRunningException newWithClass: isa];
[object release];
[retval release];
[super dealloc];
}
@end
@implementation OFTLSKey
+ (void)initialize
{
|