ObjFW  Diff

Differences From Artifact [1416763f86]:

To Artifact [020e13328a]:


32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
47
48

49
50
51
52
53
54
55
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47

48
49
50
51
52
53
54
55







-
+








-
+







#ifndef OF_MORPHOS
extern void of_tlskey_thread_exited(void);
#endif
static of_tlskey_t threadKey;

OF_CONSTRUCTOR()
{
	OF_ENSURE(of_tlskey_new(&threadKey));
	OF_ENSURE(of_tlskey_new(&threadKey) == 0);
}

static void
functionWrapper(void)
{
	bool detached = false;
	of_thread_t thread =
	    (of_thread_t)((struct Process *)FindTask(NULL))->pr_ExitData;
	OF_ENSURE(of_tlskey_set(threadKey, thread));
	OF_ENSURE(of_tlskey_set(threadKey, thread) == 0);

	thread->function(thread->object);

	ObtainSemaphore(&thread->semaphore);
	@try {
		thread->done = true;

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







-
+





-
+


-
+





-
+
-
-
+
-







		ReleaseSemaphore(&thread->semaphore);
	}

	if (detached)
		free(thread);
}

bool
int
of_thread_attr_init(of_thread_attr_t *attr)
{
	attr->priority = 0;
	attr->stackSize = 0;

	return true;
	return 0;
}

bool
int
of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
    id object, const of_thread_attr_t *attr)
{
	OFMutableData *tags = nil;

	if ((*thread = calloc(1, sizeof(**thread))) == NULL) {
	if ((*thread = calloc(1, sizeof(**thread))) == NULL)
		errno = ENOMEM;
		return false;
		return ENOMEM;
	}

	@try {
		(*thread)->function = function;
		(*thread)->object = object;
		InitSemaphore(&(*thread)->semaphore);

		tags = [[OFMutableData alloc]
120
121
122
123
124
125
126
127

128
129

130
131
132
133
134
135
136
137
118
119
120
121
122
123
124

125


126

127
128
129
130
131
132
133







-
+
-
-
+
-







		ADD_TAG(NP_Output, ((struct Process *)FindTask(NULL))->pr_COS)
		ADD_TAG(NP_Error, ((struct Process *)FindTask(NULL))->pr_CES)
		ADD_TAG(NP_CloseInput, FALSE)
		ADD_TAG(NP_CloseOutput, FALSE)
		ADD_TAG(NP_CloseError, FALSE)

		if (attr != NULL && attr->priority != 0) {
			if (attr->priority < 1 || attr->priority > 1) {
			if (attr->priority < 1 || attr->priority > 1)
				errno = EINVAL;
				return false;
				return EINVAL;
			}

			/*
			 * -1 should be -128 (lowest possible priority) while
			 * +1 should be +127 (highest possible priority).
			 */
			ADD_TAG(NP_Priority, (attr->priority > 0
			    ? attr->priority * 127 : attr->priority * 128))
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
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







-
-
+








-
+








-
+








-
+



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












-
+


-
+











-
+







		ADD_TAG(TAG_DONE, 0)
#undef ADD_TAG

		(*thread)->task = (struct Task *)CreateNewProc(tags.items);
		if ((*thread)->task == NULL) {
			free(*thread);
			errno = EAGAIN;
			return false;
			return EAGAIN;
		}
	} @catch (id e) {
		free(*thread);
		@throw e;
	} @finally {
		[tags release];
	}

	return true;
	return 0;
}

of_thread_t
of_thread_current(void)
{
	return of_tlskey_get(threadKey);
}

bool
int
of_thread_join(of_thread_t thread)
{
	ObtainSemaphore(&thread->semaphore);

	if (thread->done) {
		ReleaseSemaphore(&thread->semaphore);

		free(thread);
		return true;
		return 0;
	}

	@try {
		if (thread->detached || thread->joinTask != NULL) {
		if (thread->detached || thread->joinTask != NULL)
			errno = EINVAL;
			return false;
		}
			return EINVAL;


		if ((thread->joinSigBit = AllocSignal(-1)) == -1) {
		if ((thread->joinSigBit = AllocSignal(-1)) == -1)
			errno = EAGAIN;
			return false;
			return EAGAIN;
		}

		thread->joinTask = FindTask(NULL);
	} @finally {
		ReleaseSemaphore(&thread->semaphore);
	}

	Wait(1ul << thread->joinSigBit);
	FreeSignal(thread->joinSigBit);

	assert(thread->done);
	free(thread);

	return true;
	return 0;
}

bool
int
of_thread_detach(of_thread_t thread)
{
	ObtainSemaphore(&thread->semaphore);

	if (thread->done)
		free(thread);
	else
		thread->detached = true;

	ReleaseSemaphore(&thread->semaphore);

	return true;
	return 0;
}

void
of_thread_set_name(const char *name)
{
}