ObjFW  Diff

Differences From Artifact [926b087d59]:

To Artifact [2f72c3fe38]:


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

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

208
209

210
211
212

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232
233
234
235
236

237
238
239
240
241
242
243
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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206

207
208

209
210
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

231
232
233
234
235

236
237
238
239
240
241
242
243







-
-
+
+


-
+





-
+

-
+





-
+

-
+

-
+




-
+














-
+








-
+

-
+


-
+


-
+

-
+



-
+









-
+


-
+

-
+



-
+


-
+

-
+


-
+



-
-
+
+

-
+



-
-
+
+

-
+


-
+







-
-
+
+

-
+


-
+




-
+


-
-
+
+

-
+


-
+

















-
+

-
+


-
+


















-
+




-
+







#import "OFOutOfRangeException.h"
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"

#import "threading.h"

static OFList *tlskeys;
static of_tlskey_t thread_self;
static OFList *TLSKeys;
static of_tlskey_t threadSelf;

static id
call_main(id obj)
call_main(OFThread *thread)
{
#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_add();
#endif

	if (!of_tlskey_set(thread_self, obj))
	if (!of_tlskey_set(threadSelf, thread))
		@throw [OFInitializationFailedException
		    newWithClass: [obj class]];
		    newWithClass: [thread class]];

	/*
	 * Nasty workaround for thread implementations which can't return a
	 * value on join.
	 */
	((OFThread*)obj)->retval = [[obj main] retain];
	thread->returnValue = [[thread main] retain];

	[obj handleTermination];
	[thread handleTermination];

	((OFThread*)obj)->running = OF_THREAD_WAITING_FOR_JOIN;
	thread->running = OF_THREAD_WAITING_FOR_JOIN;

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool releaseAll];

	[obj release];
	[thread release];

#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_remove();
#endif

	return 0;
}

@implementation OFThread
+ (void)initialize
{
	if (self != [OFThread class])
		return;

	if (!of_tlskey_new(&thread_self))
	if (!of_tlskey_new(&threadSelf))
		@throw [OFInitializationFailedException newWithClass: self];
}

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

+ threadWithObject: (id)obj
+ threadWithObject: (id)object
{
	return [[[self alloc] initWithObject: obj] autorelease];
	return [[[self alloc] initWithObject: object] autorelease];
}

+ (void)setObject: (id)obj
+ (void)setObject: (id)object
	forTLSKey: (OFTLSKey*)key
{
	id old = of_tlskey_get(key->key);
	id oldObject = of_tlskey_get(key->key);

	if (!of_tlskey_set(key->key, [obj retain]))
	if (!of_tlskey_set(key->key, [object retain]))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	[old release];
	[oldObject release];
}

+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return [[of_tlskey_get(key->key) retain] autorelease];
}

+ (OFThread*)currentThread
{
	return [[of_tlskey_get(thread_self) retain] autorelease];
	return [[of_tlskey_get(threadSelf) retain] autorelease];
}

+ (void)sleepForTimeInterval: (int64_t)sec
+ (void)sleepForTimeInterval: (int64_t)seconds
{
	if (sec < 0)
	if (seconds < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	if (sec > UINT_MAX)
	if (seconds > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	sleep((unsigned int)sec);
	sleep((unsigned int)seconds);
#else
	if (sec * 1000 > UINT_MAX)
	if (seconds * 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep((unsigned int)sec * 1000);
	Sleep((unsigned int)seconds * 1000);
#endif
}

+ (void)sleepForTimeInterval: (int64_t)sec
		microseconds: (uint32_t)usec
+ (void)sleepForTimeInterval: (int64_t)seconds
		microseconds: (uint32_t)microseconds
{
	if (sec < 0)
	if (seconds < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	sleep((unsigned int)sec);
	usleep(usec);
	sleep((unsigned int)seconds);
	usleep(microseconds);
#else
	if (sec * 1000 + usec / 1000 > UINT_MAX)
	if (seconds * 1000 + microseconds / 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep((unsigned int)sec * 1000 + usec / 1000);
	Sleep((unsigned int)seconds * 1000 + microseconds / 1000);
#endif
}

+ (void)sleepUntilDate: (OFDate*)date
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDate *now = [OFDate date];
	int64_t sec;
	uint32_t usec;
	int64_t seconds;
	uint32_t microseconds;

	if ((sec = [date timeIntervalSinceDate: now]) < 0)
	if ((seconds = [date timeIntervalSinceDate: now]) < 0)
		@throw [OFOutOfRangeException newWithClass: self];

	usec = [date microsecondsOfTimeIntervalSinceDate: now];
	microseconds = [date microsecondsOfTimeIntervalSinceDate: now];

	[pool release];

#ifndef _WIN32
	if (sec > UINT_MAX)
	if (seconds > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	sleep((unsigned int)sec);
	usleep(usec);
	sleep((unsigned int)seconds);
	usleep(microseconds);
#else
	if (sec * 1000 + usec / 1000 > UINT_MAX)
	if (seconds * 1000 + microseconds / 1000 > UINT_MAX)
		@throw [OFOutOfRangeException newWithClass: self];

	Sleep(sec * 1000 + usec / 1000);
	Sleep(seconds * 1000 + microseconds / 1000);
#endif
}

+ (void)yield
{
#ifdef OF_HAVE_SCHED_YIELD
	sched_yield();
#else
	[self sleepForTimeInterval: 0];
#endif
}

+ (void)terminate
{
	[self terminateWithObject: nil];
}

+ (void)terminateWithObject: (id)obj
+ (void)terminateWithObject: (id)object
{
	OFThread *thread = of_tlskey_get(thread_self);
	OFThread *thread = of_tlskey_get(threadSelf);

	if (thread != nil) {
		thread->retval = [obj retain];
		thread->returnValue = [object retain];

		[thread handleTermination];

		thread->running = OF_THREAD_WAITING_FOR_JOIN;
	}

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool releaseAll];

	[thread release];

#if defined(OF_GNU_RUNTIME) || defined(OF_OLD_GNU_RUNTIME)
	objc_thread_remove();
#endif

	of_thread_exit();
}

- initWithObject: (id)obj
- initWithObject: (id)object_
{
	self = [super init];

	@try {
		object = [obj retain];
		object = [object_ retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
258
259
260
261
262
263
264
265

266
267
268
269
270
271
272
258
259
260
261
262
263
264

265
266
267
268
269
270
271
272







-
+







{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException newWithClass: isa
							    thread: self];

	if (running == OF_THREAD_WAITING_FOR_JOIN) {
		of_thread_detach(thread);
		[retval release];
		[returnValue release];
	}

	[self retain];

	if (!of_thread_new(&thread, call_main, self)) {
		[self release];
		@throw [OFThreadStartFailedException newWithClass: isa
280
281
282
283
284
285
286
287

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354


355
356
357
358
359
360
361
280
281
282
283
284
285
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

304
305
306
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352


353
354
355
356
357
358
359
360
361







-
+
















-
+









-
+
















-
-
+
+




















-
-
+
+







{
	if (running == OF_THREAD_NOT_RUNNING || !of_thread_join(thread))
		@throw [OFThreadJoinFailedException newWithClass: isa
							  thread: self];

	running = OF_THREAD_NOT_RUNNING;

	return retval;
	return returnValue;
}

- (void)dealloc
{
	if (running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException newWithClass: isa
							    thread: self];

	/*
	 * We should not be running anymore, but call detach in order to free
	 * the resources.
	 */
	if (running == OF_THREAD_WAITING_FOR_JOIN)
		of_thread_detach(thread);

	[object release];
	[retval release];
	[returnValue release];

	[super dealloc];
}
@end

@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		tlskeys = [[OFList alloc] init];
		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 firstListObject]; iter != NULL;
	@synchronized (TLSKeys) {
		for (iter = [TLSKeys firstListObject]; iter != NULL;
		    iter = iter->next) {
			OFTLSKey *key = (OFTLSKey*)iter->object;

			if (key->destructor != NULL)
				key->destructor(iter->object);
		}
	}
}

- init
{
	self = [super init];

	@try {
		if (!of_tlskey_new(&key))
			@throw [OFInitializationFailedException
			    newWithClass: isa];

		initialized = YES;

		@synchronized (tlskeys) {
			listobj = [tlskeys appendObject: self];
		@synchronized (TLSKeys) {
			listObject = [TLSKeys appendObject: self];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
375
376
377
378
379
380
381
382
383
384



385
386
387
388
389
390
391
375
376
377
378
379
380
381



382
383
384
385
386
387
388
389
390
391







-
-
-
+
+
+







	if (destructor != NULL)
		destructor(self);

	if (initialized)
		of_tlskey_free(key);

	/* In case we called [self release] in init */
	if (listobj != NULL) {
		@synchronized (tlskeys) {
			[tlskeys removeListObject: listobj];
	if (listObject != NULL) {
		@synchronized (TLSKeys) {
			[TLSKeys removeListObject: listObject];
		}
	}

	[super dealloc];
}
@end

452
453
454
455
456
457
458
459

460
461
462
463
464
465
466
452
453
454
455
456
457
458

459
460
461
462
463
464
465
466







-
+








	if (!of_condition_new(&condition)) {
		Class c = isa;
		[self release];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	cond_initialized = YES;
	conditionInitialized = YES;

	return self;
}

- (void)wait
{
	if (!of_condition_wait(&condition, &mutex))
480
481
482
483
484
485
486
487

488
489
490
491
492
493
494
495
480
481
482
483
484
485
486

487
488
489
490
491
492
493
494
495







-
+








	if (!of_condition_broadcast(&condition))
		@throw [OFConditionBroadcastFailedException newWithClass: isa
							       condition: self];
}

- (void)dealloc
{
	if (cond_initialized)
	if (conditionInitialized)
		if (!of_condition_free(&condition))
			@throw [OFConditionStillWaitingException
			    newWithClass: isa
			       condition: self];

	[super dealloc];
}
@end