ObjFW  Diff

Differences From Artifact [5a946f82c1]:

To Artifact [59ba7d304a]:


21
22
23
24
25
26
27
28


29
30
31
32
33

34
35
36
37
38
39
40
21
22
23
24
25
26
27

28
29
30
31
32
33

34
35
36
37
38
39
40
41







-
+
+




-
+







# include <pthread_np.h>
#endif

#ifdef OF_HAIKU
# include <kernel/OS.h>
#endif

#import "thread.h"
#import "OFPlainThread.h"

#import "macros.h"

static int minPrio = 0, maxPrio = 0, normalPrio = 0;

struct thread_ctx {
struct ThreadContext {
	void (*function)(id object);
	id object;
	const char *name;
};

/*
 * This is done here to make sure this is done as early as possible in the main
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
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







-
+


-
+










-
+







		pthread_attr_destroy(&attr);
	}
}

static void *
functionWrapper(void *data)
{
	struct thread_ctx *ctx = data;
	struct ThreadContext *ctx = data;

	if (ctx->name != NULL)
		of_thread_set_name(ctx->name);
		OFSetThreadName(ctx->name);

	pthread_cleanup_push(free, data);

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}

int
of_thread_attr_init(of_thread_attr_t *attr)
OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr)
{
	int error;
	pthread_attr_t POSIXAttr;

	attr->priority = 0;
	attr->stackSize = 0;

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







-
-
+
+













-
+


+

+




+
-
+



-
+
+
+
+
+












+















-
+
+










-
+







-
+





-
+








	pthread_attr_destroy(&POSIXAttr);

	return error;
}

int
of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
    id object, const of_thread_attr_t *attr)
OFPlainThreadNew(OFPlainThread *thread, const char *name, void (*function)(id),
    id object, const OFPlainThreadAttributes *attr)
{
	int error = 0;
	pthread_attr_t POSIXAttr;
	bool POSIXAttrAvailable = true;

	if ((error = pthread_attr_init(&POSIXAttr)) != 0) {
		if (error == ENOSYS)
			POSIXAttrAvailable = false;
		else
			return error;
	}

	@try {
		struct thread_ctx *ctx;
		struct ThreadContext *ctx;

		if (attr != NULL && POSIXAttrAvailable) {
#ifndef OF_HPUX
			struct sched_param param;
#endif

			if (attr->priority < -1 || attr->priority > 1)
				return EINVAL;

#ifndef OF_HPUX
#ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
# ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
			if ((error = pthread_attr_setinheritsched(&POSIXAttr,
			    PTHREAD_EXPLICIT_SCHED)) != 0)
				return error;
#endif
# endif

			if ((error = pthread_attr_getschedparam(&POSIXAttr,
			    &param)) != 0)
				return error;

			if (attr->priority < 0) {
				param.sched_priority = minPrio +
				    (1.0f + attr->priority) *
				    (normalPrio - minPrio);
			} else
				param.sched_priority = normalPrio +
				    attr->priority * (maxPrio - normalPrio);

			if ((error = pthread_attr_setschedparam(&POSIXAttr,
			    &param)) != 0)
				return error;
#endif

			if (attr->stackSize > 0) {
				if ((error = pthread_attr_setstacksize(
				    &POSIXAttr, attr->stackSize)) != 0)
					return error;
			}
		}

		if ((ctx = malloc(sizeof(*ctx))) == NULL)
			return ENOMEM;

		ctx->function = function;
		ctx->object = object;
		ctx->name = name;

		error = pthread_create(thread, &POSIXAttr, functionWrapper,
		error = pthread_create(thread,
		    (POSIXAttrAvailable ? &POSIXAttr : NULL), functionWrapper,
		    ctx);
	} @finally {
		if (POSIXAttrAvailable)
			pthread_attr_destroy(&POSIXAttr);
	}

	return error;
}

int
of_thread_join(of_thread_t thread)
OFPlainThreadJoin(OFPlainThread thread)
{
	void *ret;

	return pthread_join(thread, &ret);
}

int
of_thread_detach(of_thread_t thread)
OFPlainThreadDetach(OFPlainThread thread)
{
	return pthread_detach(thread);
}

void
of_thread_set_name(const char *name)
OFSetThreadName(const char *name)
{
#if defined(OF_HAIKU)
	rename_thread(find_thread(NULL), name);
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
	pthread_set_name_np(pthread_self(), name);
#elif defined(HAVE_PTHREAD_SETNAME_NP)
# if defined(OF_MACOS) || defined(OF_IOS)