ObjFW  Diff

Differences From Artifact [1a195479da]:

To Artifact [72efe40c4e]:


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
178
179
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
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
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







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

+
-
-
+
+
+
+



-










-
+

-
-
-
-
+
-




-







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










-
-



-
-
+
-
-
-
-
-
+














-
-
-
-
+
-

-
-
-
-
-
-
-
-
-
-
-

-









-
-
+
-
-
-
-
-
-







-
+




-
+

-
-



-
-
+
-
-
-
-
+






+
+












-
-
+




-
-
-






-

-
+
-
-
-






-

-
+
-
-
-






-

-
+
-
-
-




 */

#include "config.h"

#import "OFThread.h"
#import "OFExceptions.h"

#ifndef _WIN32
static void*
call_main(void *obj)
{
	return [(OFThread*)obj main];
}
#import "threading.h"

#else
static DWORD WINAPI
call_main(LPVOID obj)
static id
call_main(id obj)
{
	/*
	/* Windows does not support returning a pointer. Nasty workaround. */
	((OFThread*)obj)->retval = [(OFThread*)obj main];
	 * Nasty workaround for thread implementations which can't return a
	 * value on join.
	 */
	((OFThread*)obj)->retval = [obj main];

	return 0;
}
#endif

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

+ setObject: (id)obj
  forTLSKey: (OFTLSKey*)key
{
	id old = [self objectForTLSKey: key];
	id old = of_tlskey_get(key->key);

#ifndef _WIN32
	if (pthread_setspecific(key->key, obj))
#else
	if (!TlsSetValue(key->key, obj))
	if (!of_tlskey_set(key->key, [obj retain]))
#endif
		/* FIXME: Maybe another exception would be better */
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	[obj retain];
	[old release];

	return self;
}

+ (id)objectForTLSKey: (OFTLSKey*)key
{
	void *ret;

	return [[of_tlskey_get(key->key) retain] autorelease];
#ifndef _WIN32
	ret = pthread_getspecific(key->key);
#else
	ret = TlsGetValue(key->key);
#endif

	/*
	 * NULL and nil might be different on some platforms. NULL is returned
	 * if the key is missing, nil can be returned if it was explicitly set
	 * to nil to release the old object.
	 */
	if (ret == NULL)
		return nil;

	return (id)ret;
}

- init
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithObject: (id)obj
{
	Class c;

	self = [super init];
	object = [obj copy];

#ifndef _WIN32
	if (pthread_create(&thread, NULL, call_main, self)) {
	if (!of_thread_new(&thread, call_main, self)) {
#else
	if ((thread =
	    CreateThread(NULL, 0, call_main, self, 0, NULL)) == NULL) {
#endif
		c = isa;
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	return self;
}

- main
{
	return nil;
}

- join
{
#ifndef _WIN32
	void *ret;

	if (pthread_join(thread, &ret))
	of_thread_join(thread);
		@throw [OFThreadJoinFailedException newWithClass: isa];

	if (ret == PTHREAD_CANCELED)
		@throw [OFThreadCanceledException newWithClass: isa];

	return (id)ret;
#else
	if (WaitForSingleObject(thread, INFINITE))
		@throw [OFThreadJoinFailedException newWithClass: isa];

	CloseHandle(thread);
	thread = INVALID_HANDLE_VALUE;

	return retval;
#endif
}

- (void)dealloc
{
	/*
	 * No need to handle errors - if canceling the thread fails, we can't
	 * do anything anyway. Most likely, it finished already or was already
	 * canceled.
	 */
#ifndef _WIN32
	pthread_cancel(thread);
	of_thread_cancel(thread);
#else
	if (thread != INVALID_HANDLE_VALUE) {
		TerminateThread(thread, 1);
		CloseHandle(thread);
	}
#endif

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

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

- initWithDestructor: (void(*)(void*))destructor
- initWithDestructor: (void(*)(id))destructor
{
	Class c;

	self = [super init];

	/* FIXME: Call destructor on Win32 */
#ifndef _WIN32
	if (pthread_key_create(&key, destructor)) {
	if (!of_tlskey_new(&key, destructor)) {
#else
	if ((key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
#endif
		c = isa;
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	return self;
}

/* FIXME: Add dealloc! */
@end

@implementation OFMutex
+ mutex
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

#ifndef _WIN32
	if (pthread_mutex_init(&mutex, NULL)) {
	if (!of_mutex_new(&mutex)) {
		Class c = isa;
		[self dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}
#else
	InitializeCriticalSection(&mutex);
#endif

	return self;
}

- lock
{
#ifndef _WIN32
	/* FIXME: Add error-handling */
	pthread_mutex_lock(&mutex);
	of_mutex_lock(&mutex);
#else
	EnterCriticalSection(&mutex);
#endif

	return self;
}

- unlock
{
#ifndef _WIN32
	/* FIXME: Add error-handling */
	pthread_mutex_unlock(&mutex);
	of_mutex_unlock(&mutex);
#else
	LeaveCriticalSection(&mutex);
#endif

	return self;
}

- (void)dealloc
{
#ifndef _WIN32
	/* FIXME: Add error-handling */
	pthread_mutex_destroy(&mutex);
	of_mutex_free(&mutex);
#else
	DeleteCriticalSection(&mutex);
#endif

	[super dealloc];
}
@end