ObjFW  Check-in [ead7b7acd3]

Overview
Comment:Make the main thread an OFThread.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ead7b7acd3d4429dbc6dd689b5262e4e8a07e75deb023b1f10487401f18282db
User & Date: js on 2012-09-09 17:48:39
Other Links: manifest | tags
Context
2012-09-09
17:50
Make thread ivars private. check-in: 3b1c4cc681 user: js tags: trunk
17:48
Make the main thread an OFThread. check-in: ead7b7acd3 user: js tags: trunk
14:36
Add OFSortedList. check-in: c0f28cf861 user: js tags: trunk
Changes

Modified src/OFApplication.m from [a06fbcf0e7] to [6f344c5f2e].

291
292
293
294
295
296
297


298
299
300
301
302
303
304
	REGISTER_SIGNAL(SIGUSR2)
#endif
#undef REGISTER_SIGNAL
}

- (void)run
{


	[delegate applicationDidFinishLaunching];

	for (;;)
		[OFThread sleepForTimeInterval: 86400];
}

- (void)terminate







>
>







291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	REGISTER_SIGNAL(SIGUSR2)
#endif
#undef REGISTER_SIGNAL
}

- (void)run
{
	[OFThread _createMainThread];

	[delegate applicationDidFinishLaunching];

	for (;;)
		[OFThread sleepForTimeInterval: 86400];
}

- (void)terminate

Modified src/OFThread.h from [51a42ce92e] to [3a25c292f8].

152
153
154
155
156
157
158
159
160
161
162







163
164
165
166
167
168
169
 * \param key The Thread Local Storage key
 */
+ (id)objectForTLSKey: (OFTLSKey*)key;

/**
 * \brief Returns the current thread.
 *
 * \return The current thread or nil if we are in the main thread
 */
+ (OFThread*)currentThread;








/**
 * \brief Suspends execution of the current thread for the specified time
 *	  interval.
 *
 * \param seconds The number of seconds to sleep
 */
+ (void)sleepForTimeInterval: (double)seconds;







|



>
>
>
>
>
>
>







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
 * \param key The Thread Local Storage key
 */
+ (id)objectForTLSKey: (OFTLSKey*)key;

/**
 * \brief Returns the current thread.
 *
 * \return The current thread
 */
+ (OFThread*)currentThread;

/**
 * \brief Returns the main thread.
 *
 * \return The main thread
 */
+ (OFThread*)mainThread;

/**
 * \brief Suspends execution of the current thread for the specified time
 *	  interval.
 *
 * \param seconds The number of seconds to sleep
 */
+ (void)sleepForTimeInterval: (double)seconds;
188
189
190
191
192
193
194


195
196
197
198
199
200
201

/**
 * \brief Terminates the current thread, letting it return the specified object.
 *
 * \param object The object which the terminated thread will return
 */
+ (void)terminateWithObject: (id)object;



/**
 * \brief Initializes an already allocated thread with the specified object.
 *
 * \param object An object which is passed for use in the main method or nil
 * \return An initialized OFThread.
 */







>
>







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

/**
 * \brief Terminates the current thread, letting it return the specified object.
 *
 * \param object The object which the terminated thread will return
 */
+ (void)terminateWithObject: (id)object;

+ (void)_createMainThread;

/**
 * \brief Initializes an already allocated thread with the specified object.
 *
 * \param object An object which is passed for use in the main method or nil
 * \return An initialized OFThread.
 */

Modified src/OFThread.m from [b522bb5862] to [413ce95285].

48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"

#import "threading.h"

static OFList *TLSKeys;
static of_tlskey_t threadSelf;


static id
call_main(id object)
{
	OFThread *thread = (OFThread*)object;

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

	/*
	 * Nasty workaround for thread implementations which can't return a
	 * value on join.
	 */







|
>






|







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import "OFThreadJoinFailedException.h"
#import "OFThreadStartFailedException.h"
#import "OFThreadStillRunningException.h"

#import "threading.h"

static OFList *TLSKeys;
static of_tlskey_t threadSelfKey;
static OFThread *mainThread;

static id
call_main(id object)
{
	OFThread *thread = (OFThread*)object;

	if (!of_tlskey_set(threadSelfKey, thread))
		@throw [OFInitializationFailedException
		    exceptionWithClass: [thread class]];

	/*
	 * Nasty workaround for thread implementations which can't return a
	 * value on join.
	 */
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#endif

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

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

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







|







95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#endif

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

	if (!of_tlskey_new(&threadSelfKey))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

+ thread
{
	return [[[self alloc] init] autorelease];
142
143
144
145
146
147
148
149





150
151
152
153
154
155
156
+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return [[(id)of_tlskey_get(key->key) retain] autorelease];
}

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





}

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








|
>
>
>
>
>







143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return [[(id)of_tlskey_get(key->key) retain] autorelease];
}

+ (OFThread*)currentThread
{
	return [[(id)of_tlskey_get(threadSelfKey) retain] autorelease];
}

+ (OFThread*)mainThread
{
	return mainThread;
}

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

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
+ (void)terminate
{
	[self terminateWithObject: nil];
}

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

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

		[thread handleTermination];

		thread->running = OF_THREAD_WAITING_FOR_JOIN;
	}

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool _releaseAll];

	[thread release];

	of_thread_exit();
}











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

	object = [object_ retain];








|
















>
>
>
>
>
>
>
>
>
>







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
244
+ (void)terminate
{
	[self terminateWithObject: nil];
}

+ (void)terminateWithObject: (id)object
{
	OFThread *thread = of_tlskey_get(threadSelfKey);

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

		[thread handleTermination];

		thread->running = OF_THREAD_WAITING_FOR_JOIN;
	}

	[OFTLSKey callAllDestructors];
	[OFAutoreleasePool _releaseAll];

	[thread release];

	of_thread_exit();
}

+ (void)_createMainThread
{
	mainThread = [[OFThread alloc] init];
	mainThread->thread = of_thread_current();

	if (!of_tlskey_set(threadSelfKey, mainThread))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

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

	object = [object_ retain];