ObjFW  Diff

Differences From Artifact [992a58ee6a]:

To Artifact [2c07550cc7]:


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
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
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
114
115


116
117






118
119
120

121
122
123
124
125
126
127







-
-
-
-
-
-




+
+






-
-
+
-
-
-



-
+

-
-
-
-
-
-
+
-
-









-
-
-
+
+
-
-
-
-
-





-
-
-
-
+
-

-
+







-
-
-
-
+
-


-
+





-
+








+
+


-
-
-
-
-
-
+
+
-

-
-
-
-
+
-





-
+
-
-
-
-
+
-


-
-
-
-
+
-







-
+
-

-










-
-
+
+
-
-
-
-
-
-
+
+
+
-







 * the packaging of this file.
 */

#include "config.h"

#include <stdlib.h>

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

#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.
 */

#ifndef _WIN32
static pthread_key_t first_key, last_key;
static of_tlskey_t first_key, last_key;
#else
static DWORD first_key, last_key;
#endif

#ifndef _WIN32 /* Not used on Win32 yet */
static void
release_all(void *list)
release_all(id obj)
{
#ifndef _WIN32
	void *first = pthread_getspecific(first_key);
#else
	void *first = TlsGetValue(first_key);
#endif

	[of_tlskey_get(first_key) release];
	if (first != NULL)
		[(OFAutoreleasePool*)first release];
}
#endif

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

#ifndef _WIN32
	if (pthread_key_create(&first_key, release_all) ||
	    pthread_key_create(&last_key, NULL))
	if (!of_tlskey_new(&first_key, release_all) ||
	    !of_tlskey_new(&last_key, NULL))
#else
	/* FIXME: Call destructor */
	if ((first_key = TlsAlloc()) == TLS_OUT_OF_INDEXES ||
	    (last_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
#endif
		@throw [OFInitializationFailedException newWithClass: self];
}

+ (void)addObjectToTopmostPool: (OFObject*)obj
{
#ifndef _WIN32
	void *last = pthread_getspecific(last_key);
#else
	void *last = TlsGetValue(last_key);
	id last = of_tlskey_get(last_key);
#endif

	if (last == NULL) {
	if (last == nil) {
		@try {
			[[self alloc] init];
		} @catch (OFException *e) {
			[obj release];
			@throw e;
		}

#ifndef _WIN32
		last = pthread_getspecific(last_key);
#else
		last = TlsGetValue(last_key);
		last = of_tlskey_get(last_key);
#endif
	}

	if (last == NULL) {
	if (last == nil) {
		[obj release];
		@throw [OFInitializationFailedException newWithClass: self];
	}

	@try {
		[(OFAutoreleasePool*)last addObject: obj];
		[last addObject: obj];
	} @catch (OFException *e) {
		[obj release];
		@throw e;
	}
}

- init
{
	id first;

	self = [super init];

#ifndef _WIN32
	void *first = pthread_getspecific(first_key);
	void *last = pthread_getspecific(last_key);
#else
	void *first = TlsGetValue(first_key);
	void *last = TlsGetValue(last_key);
	first = of_tlskey_get(first_key);
	prev = of_tlskey_get(last_key);
#endif

#ifndef _WIN32
	if (pthread_setspecific(last_key, self)) {
#else
	if (!TlsSetValue(last_key, self)) {
	if (!of_tlskey_set(last_key, self)) {
#endif
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	if (first == NULL) {
	if (first == nil) {
#ifndef _WIN32
		if (pthread_setspecific(first_key, self)) {
#else
		if (!TlsSetValue(first_key, self)) {
		if (!of_tlskey_set(first_key, self)) {
#endif
			Class c = isa;

#ifndef _WIN32
			pthread_setspecific(last_key, last);
#else
			TlsSetValue(last_key, last);
			of_tlskey_set(last_key, prev);
#endif

			[super dealloc];
			@throw [OFInitializationFailedException
			    newWithClass: c];
		}
	}

	if (last != NULL) {
	if (prev != nil)
		prev = (OFAutoreleasePool*)last;
		prev->next = self;
	}

	return self;
}

- (void)dealloc
{
	[next dealloc];

	if (prev != nil)
		prev->next = nil;
#ifndef _WIN32
	pthread_setspecific(last_key, (prev != nil ? prev : NULL));

	/* FIXME: Add exception? */
	if (pthread_getspecific(first_key) == self)
		pthread_setspecific(first_key, NULL);
#else
	TlsSetValue(last_key, (prev != nil ? prev : NULL));
	if (TlsGetValue(first_key) == self)
		TlsSetValue(first_key, NULL);
	of_tlskey_set(last_key, prev);
	if (of_tlskey_get(first_key) == self)
		of_tlskey_set(first_key, nil);
#endif

	[objects release];

	[super dealloc];
}

- addObject: (OFObject*)obj