ObjFW  Check-in [12101c192f]

Overview
Comment:New solution for TLS key destructors that works on any OS.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 12101c192f731fa0d2f3f3a91b1ecc5b51293b4fe326c8bbc4cd8c55b71bf214
User & Date: js on 2009-11-14 12:17:37
Other Links: manifest | tags
Context
2009-11-14
12:46
Fix two more FIXMEs. check-in: 8afa16c9e9 user: js tags: trunk
12:17
New solution for TLS key destructors that works on any OS. check-in: 12101c192f user: js tags: trunk
2009-11-13
23:18
Implement -[isEqual:] and -[hash] for OFDictionary. check-in: 2555952c42 user: js tags: trunk
Changes

Modified src/OFAutoreleasePool.h from [07ca47b0fe] to [19776ae923].

27
28
29
30
31
32
33


34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool
 */
+ (void)addObjectToTopmostPool: (OFObject*)obj;



/**
 * Adds an object to the specific autorelease pool.
 *
 * \param obj The object to add to the autorelease pool
 */
- addObject: (OFObject*)obj;

/**
 * Releases all objects in the autorelease pool.
 */
- releaseObjects;
@end







>
>













27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool
 */
+ (void)addObjectToTopmostPool: (OFObject*)obj;

+ (void)releaseAll;

/**
 * Adds an object to the specific autorelease pool.
 *
 * \param obj The object to add to the autorelease pool
 */
- addObject: (OFObject*)obj;

/**
 * Releases all objects in the autorelease pool.
 */
- releaseObjects;
@end

Modified src/OFAutoreleasePool.m from [7523e6d739] to [8c6dcd3941].

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
#import "OFAutoreleasePool.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"

#import "threading.h"

/*
 * Pay special attention to NULL and nil in this file, they might be different!
 * Use NULL for TLS values and nil for instance variables.
 */

static of_tlskey_t first_key, last_key;

static void
release_all(id obj)
{
	[of_tlskey_get(first_key) release];
}

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

	if (!of_tlskey_new(&first_key, release_all) ||
	    !of_tlskey_new(&last_key, NULL))
		@throw [OFInitializationFailedException newWithClass: self];
}

+ (void)addObjectToTopmostPool: (OFObject*)obj
{
	id last = of_tlskey_get(last_key);








<
<
<
<
<


<
<
<
<
<
<






|
<







16
17
18
19
20
21
22





23
24






25
26
27
28
29
30
31

32
33
34
35
36
37
38
#import "OFAutoreleasePool.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"

#import "threading.h"






static of_tlskey_t first_key, last_key;







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

	if (!of_tlskey_new(&first_key) || !of_tlskey_new(&last_key))

		@throw [OFInitializationFailedException newWithClass: self];
}

+ (void)addObjectToTopmostPool: (OFObject*)obj
{
	id last = of_tlskey_get(last_key);

67
68
69
70
71
72
73





74
75
76
77
78
79
80
	@try {
		[last addObject: obj];
	} @catch (OFException *e) {
		[obj release];
		@throw e;
	}
}






- init
{
	id first;

	self = [super init];








>
>
>
>
>







55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	@try {
		[last addObject: obj];
	} @catch (OFException *e) {
		[obj release];
		@throw e;
	}
}

+ (void)releaseAll
{
	[of_tlskey_get(first_key) release];
}

- init
{
	id first;

	self = [super init];

Modified src/OFThread.h from [6639eedccb] to [03b7369e6c].

1
2
3
4
5
6
7
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
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"


#import "threading.h"

/**
 * A Thread Local Storage key.
 */
@interface OFTLSKey: OFObject
{
@public
	of_tlskey_t key;



}

/**
 * \return A new autoreleased Thread Local Storage key
 */
+ tlsKey;

/**
 * \param destructor A destructor that is called when the thread is terminated
 * \return A new autoreleased Thread Local Storage key
 */
+ tlsKeyWithDestructor: (void(*)(id))destructor;



/**
 * \return An initialized Thread Local Storage key
 */
- init;

/**












>










>
>
>












>
>







1
2
3
4
5
6
7
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
42
43
44
45
46
47
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFList.h"

#import "threading.h"

/**
 * A Thread Local Storage key.
 */
@interface OFTLSKey: OFObject
{
@public
	of_tlskey_t key;
@protected
	void (*destructor)(id);
	of_list_object_t *listobj;
}

/**
 * \return A new autoreleased Thread Local Storage key
 */
+ tlsKey;

/**
 * \param destructor A destructor that is called when the thread is terminated
 * \return A new autoreleased Thread Local Storage key
 */
+ tlsKeyWithDestructor: (void(*)(id))destructor;

+ (void)callAllDestructors;

/**
 * \return An initialized Thread Local Storage key
 */
- init;

/**

Modified src/OFThread.m from [18efd35d69] to [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
 * 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 "OFExceptions.h"

#import "threading.h"



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




	return 0;
}

@implementation OFThread
+ threadWithObject: (id)obj
{
	return [[[self alloc] initWithObject: obj] autorelease];







>
>



>
>










>
>
>







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

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

@implementation OFTLSKey






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

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











- init
{
	self = [super init];

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













	return self;
}

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

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

	return self;
}

- (void)dealloc
{



	of_tlskey_free(key);





	[super dealloc];
}
@end

@implementation OFMutex
+ mutex







>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>





|




>
>
>
>
>
>
>
>
>
>
>
>




|

|

|
<
<
<
<






>
>
>

>
>
>
>







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)) {
		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_
{
	self = [self init];

	destructor = destructor_;





	return self;
}

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

	of_tlskey_free(key);

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

	[super dealloc];
}
@end

@implementation OFMutex
+ mutex

Modified src/threading.h from [44dc680850] to [04a0546353].

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#else
	LeaveCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key, void (*destructor)(id))
{
#ifndef _WIN32
	return (pthread_key_create(key, (void(*)(void*))destructor) ? NO : YES);
#else
	/* FIXME: Call destructor */
	return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
#endif
}

static OF_INLINE id
of_tlskey_get(of_tlskey_t key)
{







|


|

<







121
122
123
124
125
126
127
128
129
130
131
132

133
134
135
136
137
138
139
#else
	LeaveCriticalSection(mutex);
	return YES;
#endif
}

static OF_INLINE BOOL
of_tlskey_new(of_tlskey_t *key)
{
#ifndef _WIN32
	return (pthread_key_create(key, NULL) ? NO : YES);
#else

	return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
#endif
}

static OF_INLINE id
of_tlskey_get(of_tlskey_t key)
{