ObjFW  Check-in [2d09137a6b]

Overview
Comment:Use OFThread's TLS in OFAutoreleasePool.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2d09137a6b888b7a831c1676116a890849df3e95204c8af3bf380b45dbd8c7f3
User & Date: js on 2009-05-04 13:01:19
Other Links: manifest | tags
Context
2009-05-04
14:19
Make OFString a common class for all strings. check-in: 372211deb7 user: js tags: trunk
13:01
Use OFThread's TLS in OFAutoreleasePool. check-in: 2d09137a6b user: js tags: trunk
12:57
Add Thread Local Storage support to OFThread. check-in: 4d1d644283 user: js tags: trunk
Changes

Modified src/OFAutoreleasePool.m from [f928f923c8] to [c9f95c0438].

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
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
 * the packaging of this file.
 */

#import "config.h"

#include <stdlib.h>

#ifndef _WIN32
#include <pthread.h>
#endif

#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFList.h"

#ifdef _WIN32
#include <windows.h>
#endif

#ifndef _WIN32
#define get_tls(t) pthread_getspecific(pool_list_key)
#define set_tls(t, v) pthread_setspecific(t, v)
static pthread_key_t pool_list_key;
#else
#define get_tls(t) TlsGetValue(t)
#define set_tls(t, v) TlsSetValue(t, v)
static DWORD pool_list_key;
#endif

#ifndef _WIN32
static void
release_list(void *list)
{
	of_list_object_t *first, *iter;
	IMP release;

	if ((first = [(OFList*)list first]) != NULL)
		release = [first->object methodFor: @selector(release)];

	for (iter = first; iter != NULL; iter = iter->next)
		release(iter->object, @selector(release));

	[(OFList*)list release];
}
#endif

@implementation OFAutoreleasePool
+ initialize
{
#ifndef _WIN32
	if (pthread_key_create(&pool_list_key, release_list))
		@throw [OFInitializationFailedException newWithClass: self];
#else
	/* FIXME: Free stuff when thread is terminated! */
	if ((pool_list_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
		@throw [OFInitializationFailedException newWithClass: self];
#endif

	return self;
}

+ (void)addToPool: (OFObject*)obj
{
	OFList *pool_list = get_tls(pool_list_key);


	if (pool_list == nil || [pool_list last] == NULL) {


		[[self alloc] init];
		pool_list = get_tls(pool_list_key);
	}

	if (pool_list == nil || [pool_list last] == NULL)



		@throw [OFInitializationFailedException newWithClass: self];

	[[pool_list last]->object addToPool: obj];
}

- init
{
	OFList *pool_list;

	self = [super init];

	objects = nil;
	pool_list = get_tls(pool_list_key);


	if (pool_list == nil) {


		pool_list = [[OFList alloc] initWithoutRetainAndRelease];

		set_tls(pool_list_key, pool_list);

	}

	listobj = [pool_list append: self];

	return self;
}

- free
{
	[(OFList*)get_tls(pool_list_key) remove: listobj];

	return [super free];
}

- addToPool: (OFObject*)obj
{
	if (objects == nil)







<
<
<
<

|
|
|
<
<
<

<
<
<
<
<
<
<
|
<

<














<




<
<
<
<
<
|
<
<






|

>
|
>
>

|


|
>
>
>












<

>
|
>
>

>
|
>









|







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
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
 * the packaging of this file.
 */

#import "config.h"

#include <stdlib.h>





#import "OFAutoreleasePool.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"











static OFTLSKey *pool_list_key;



static void
release_list(void *list)
{
	of_list_object_t *first, *iter;
	IMP release;

	if ((first = [(OFList*)list first]) != NULL)
		release = [first->object methodFor: @selector(release)];

	for (iter = first; iter != NULL; iter = iter->next)
		release(iter->object, @selector(release));

	[(OFList*)list release];
}


@implementation OFAutoreleasePool
+ initialize
{





	pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list];



	return self;
}

+ (void)addToPool: (OFObject*)obj
{
	OFList *pool_list;

	@try {
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	} @catch (OFNotInSetException *e) {
		[e free];
		[[self alloc] init];
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	}

	if ([pool_list last] == NULL)
		[[self alloc] init];

	if ([pool_list last] == NULL)
		@throw [OFInitializationFailedException newWithClass: self];

	[[pool_list last]->object addToPool: obj];
}

- init
{
	OFList *pool_list;

	self = [super init];

	objects = nil;


	@try {
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	} @catch (OFNotInSetException *e) {
		[e free];
		pool_list = [[OFList alloc] initWithoutRetainAndRelease];
		[OFThread setObject: pool_list
			  forTLSKey: pool_list_key];
		[pool_list release];
	}

	listobj = [pool_list append: self];

	return self;
}

- free
{
	[[OFThread objectForTLSKey: pool_list_key] remove: listobj];

	return [super free];
}

- addToPool: (OFObject*)obj
{
	if (objects == nil)

Modified tests/OFAutoreleasePool/OFAutoreleasePool.m from [d3298de97f] to [2541cf0951].

98
99
100
101
102
103
104
105
106
	o3 = [[[OFObject alloc] init] autorelease];

	[pool1 retain];
	[pool1 release];
	[pool1 release];
	[o3 free];

	return (inits == 12 && retains == 1 && releases == 6 ? 0 : 1);
}







|

98
99
100
101
102
103
104
105
106
	o3 = [[[OFObject alloc] init] autorelease];

	[pool1 retain];
	[pool1 release];
	[pool1 release];
	[o3 free];

	return (inits == 16 && retains == 2 && releases == 7 ? 0 : 1);
}