ObjFW  Diff

Differences From Artifact [5063c8614f]:

To Artifact [7cfc6bc1be]:


21
22
23
24
25
26
27

28
29
30
31
32
33
34
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35







+







# include <sched.h>
#else
# include <windows.h>
#endif

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

#import "threading.h"

static OFList *tlskeys;
static of_tlskey_t thread_self;
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
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







+
+
+
+
+






-
-
+
+







-
+




-
+




-
+


-
+

+
+
+

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

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







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

	if (!of_tlskey_new(&thread_self))
		@throw [OFInitializationFailedException newWithClass: self];
}

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

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

+ (id)setObject: (id)obj
      forTLSKey: (OFTLSKey*)key
+ (void)setObject: (id)obj
	forTLSKey: (OFTLSKey*)key
{
	id old = of_tlskey_get(key->key);

	if (!of_tlskey_set(key->key, [obj retain]))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	return [old autorelease];
	[old release];
}

+ (id)objectForTLSKey: (OFTLSKey*)key
{
	return of_tlskey_get(key->key);
	return [[of_tlskey_get(key->key) retain] autorelease];
}

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

+ (void)sleepForNMilliseconds: (unsigned int)msecs;
+ (void)sleepForTimeInterval: (int64_t)sec
{
	if (sec < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	sleep(sec);
#else
	Sleep(sec * 1000);
#endif
}

+ (void)sleepForTimeInterval: (int64_t)sec
		microseconds: (uint32_t)usec
{
	if (sec < 0)
		@throw [OFOutOfRangeException newWithClass: self];

#ifndef _WIN32
	sleep(sec);
	usleep(msecs * 1000);
	usleep(usec);
#else
	Sleep(sec * 1000 + usec / 1000);
#endif
}

+ (void)sleepUntilDate: (OFDate*)date
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDate *now = [OFDate date];
	int64_t sec;
	uint32_t usec;

	if ((sec = [date timeIntervalSinceDate: now]) < 0)
		@throw [OFOutOfRangeException newWithClass: self];

	usec = [date microsecondsOfTimeIntervalSinceDate: now];

	[pool release];

#ifndef _WIN32
	Sleep(msecs);
	sleep(sec);
	usleep(usec);
#else
	Sleep(sec * 1000 + usec / 1000);
#endif
}

+ (void)yield
{
#ifndef _WIN32
	sched_yield();
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
179
180
181
182
183
184
185








186
187
188
189
190
191
192







-
-
-
-
-
-
-
-







	[OFAutoreleasePool releaseAll];

	[thread release];

	of_thread_exit();
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

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

	@try {
		object = [obj retain];
	} @catch (id e) {
212
213
214
215
216
217
218
219

220
221
222
223
224

225
226
227
228
229
230
231
249
250
251
252
253
254
255

256
257
258
259
260

261
262
263
264
265
266
267
268







-
+




-
+







@implementation OFTLSKey
+ (void)initialize
{
	if (self == [OFTLSKey class])
		tlskeys = [[OFList alloc] init];
}

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

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

+ (void)callAllDestructors
{
	of_list_object_t *iter;