ObjFW  Diff

Differences From Artifact [cdfc8ccff6]:

To Artifact [6bf58b8f5f]:

  • File src/OFThread.m — part of check-in [3d16a30f41] at 2013-06-22 12:12:36 on branch trunk — Rework exceptions.

    This mostly removes the argument for the class in which the exception
    occurred. As backtraces were recently added for all platforms, the
    passed class does not give any extra information on where the exception
    occurred anymore.

    This also removes a few other arguments which were not too helpful. In
    the past, the idea was to pass as many arguments as possible so that it
    is easier to find the origin of the exception. However, as backtraces
    are a much better way to find the origin, those are not useful anymore
    and just make the exception more cumbersome to use. The rule is now to
    only pass arguments that might help in recovering from the exception or
    provide information that is otherwise not easily accessible. (user: js, size: 7640) [annotate] [blame] [check-ins using]


155
156
157
158
159
160
161

162

163
164
165
166
167
168
169
170
155
156
157
158
159
160
161
162

163

164
165
166
167
168
169
170







+
-
+
-








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

	if (!of_tlskey_set(key->_key, [object retain]))
		/* FIXME: Find a better exception */
		@throw [OFInvalidArgumentException exceptionWithClass: self
		@throw [OFInvalidArgumentException exception];
							     selector: _cmd];

	[oldObject release];
}

+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return [[(id)of_tlskey_get(key->_key) retain] autorelease];
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
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







-
+








-
+




-
+





-
+







	return mainThread;
}
#endif

+ (void)sleepForTimeInterval: (double)seconds
{
	if (seconds < 0)
		@throw [OFOutOfRangeException exceptionWithClass: self];
		@throw [OFOutOfRangeException exception];

#if defined(HAVE_NANOSLEEP)
	struct timespec rqtp;

	rqtp.tv_sec = (time_t)seconds;
	rqtp.tv_nsec = lrint((seconds - rqtp.tv_sec) * 1000000000);

	if (rqtp.tv_sec != floor(seconds))
		@throw [OFOutOfRangeException exceptionWithClass: self];
		@throw [OFOutOfRangeException exception];

	nanosleep(&rqtp, NULL);
#elif !defined(_WIN32)
	if (seconds > UINT_MAX)
		@throw [OFOutOfRangeException exceptionWithClass: self];
		@throw [OFOutOfRangeException exception];

	sleep((unsigned int)seconds);
	usleep((useconds_t)lrint((seconds - floor(seconds)) * 1000000));
#else
	if (seconds * 1000 > UINT_MAX)
		@throw [OFOutOfRangeException exceptionWithClass: self];
		@throw [OFOutOfRangeException exception];

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

+ (void)sleepUntilDate: (OFDate*)date
{
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
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







-
-
+












-
+
-
-








-
+
-
-







	[oldRunLoop release];
}

- (void)start
{
	if (_running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException
		    exceptionWithClass: [self class]
				thread: self];
		    exceptionWithThread: self];

	if (_running == OF_THREAD_WAITING_FOR_JOIN) {
		of_thread_detach(_thread);
		[_returnValue release];
	}

	[self retain];

	_running = OF_THREAD_RUNNING;

	if (!of_thread_new(&_thread, call_main, self)) {
		[self release];
		@throw [OFThreadStartFailedException
		@throw [OFThreadStartFailedException exceptionWithThread: self];
		    exceptionWithClass: [self class]
				thread: self];
	}

	set_thread_name(self);
}

- (id)join
{
	if (_running == OF_THREAD_NOT_RUNNING || !of_thread_join(_thread))
		@throw [OFThreadJoinFailedException
		@throw [OFThreadJoinFailedException exceptionWithThread: self];
		    exceptionWithClass: [self class]
				thread: self];

	_running = OF_THREAD_NOT_RUNNING;

	return _returnValue;
}

- copy
372
373
374
375
376
377
378
379
380

381
382
383
384
385
386
387
367
368
369
370
371
372
373


374
375
376
377
378
379
380
381







-
-
+







		set_thread_name(self);
}

- (void)dealloc
{
	if (_running == OF_THREAD_RUNNING)
		@throw [OFThreadStillRunningException
		    exceptionWithClass: [self class]
				thread: self];
		    exceptionWithThread: 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);