ObjFW  Diff

Differences From Artifact [18efd35d69]:

To Artifact [ae7f24832c]:


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
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







+
+



+
+










+
+
+







 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#import "OFThread.h"
#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: (id)obj
{
	return [[[self alloc] initWithObject: obj] autorelease];
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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192







+
+
+
+
+
+









+
+
+
+
+
+
+
+
+
+





-
+




+
+
+
+
+
+
+
+
+
+
+
+




-
+

-
+

-
+
-
-
-
-






+
+
+

+
+
+
+








	[object release];
	[super dealloc];
}
@end

@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		tlskeys = [[OFList alloc] init];
}

+ tlsKey
{
	return [[[self alloc] init] autorelease];
}

+ tlsKeyWithDestructor: (void(*)(id))destructor
{
	return [[[self alloc] initWithDestructor: destructor] autorelease];
}

+ (void)callAllDestructors
{
	of_list_object_t *iter;

	@synchronized (tlskeys) {
		for (iter = [tlskeys first]; iter != NULL; iter = iter->next)
			((OFTLSKey*)iter->object)->destructor(iter->object);
	}
}

- init
{
	self = [super init];

	if (!of_tlskey_new(&key, NULL)) {
	if (!of_tlskey_new(&key)) {
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	destructor = NULL;

	@synchronized (tlskeys) {
		@try {
			listobj = [tlskeys append: self];
		} @catch (OFException *e) {
			listobj = NULL;
			[self dealloc];
			@throw e;
		}
	}

	return self;
}

- initWithDestructor: (void(*)(id))destructor
- initWithDestructor: (void(*)(id))destructor_
{
	self = [super init];
	self = [self init];

	if (!of_tlskey_new(&key, destructor)) {
	destructor = destructor_;
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	return self;
}

- (void)dealloc
{
	if (destructor != NULL)
		destructor(self);

	of_tlskey_free(key);

	@synchronized (tlskeys) {
		[tlskeys remove: listobj];
	}

	[super dealloc];
}
@end

@implementation OFMutex
+ mutex