ObjFW  Diff

Differences From Artifact [cdd7c510a1]:

To Artifact [08a22e6008]:


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

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}

bool

of_thread_attr_init(of_thread_attr_t *attr)
{

	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {
		attr->priority = 0;

		if (pthread_attr_getstacksize(&pattr, &attr->stackSize) != 0)
			return false;
	} @finally {
		pthread_attr_destroy(&pattr);
	}

	return true;
}

bool

of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
    id object, const of_thread_attr_t *attr)
{
	bool ret;

	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {
		struct thread_ctx *ctx;

		if (attr != NULL) {
			struct sched_param param;

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

#ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
			if (pthread_attr_setinheritsched(&pattr,
			    PTHREAD_EXPLICIT_SCHED) != 0)
				return false;
#endif

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

			if (pthread_attr_setschedparam(&pattr, &param) != 0)

				return false;

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

		if ((ctx = malloc(sizeof(*ctx))) == NULL) {
			errno = ENOMEM;
			return false;
		}

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

		ret = (pthread_create(thread, &pattr,
		    functionWrapper, ctx) == 0);
	} @finally {
		pthread_attr_destroy(&pattr);
	}

	return ret;
}

bool

of_thread_join(of_thread_t thread)
{
	void *ret;

	return (pthread_join(thread, &ret) == 0);
}

bool

of_thread_detach(of_thread_t thread)
{
	return (pthread_detach(thread) == 0);
}

void
of_thread_set_name(const char *name)
{
#if defined(OF_HAIKU)
	rename_thread(find_thread(NULL), name);







<
>


>


|
|

<
|
<
|
|
<
|
|
<
|


<
>



<
>


|
|







|
<
|
|
<

|
|
|










|
>
|


|
|
|



|
<
|
<





|
<




|


<
>




|


<
>


|







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

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}


int
of_thread_attr_init(of_thread_attr_t *attr)
{
	int error;
	pthread_attr_t pattr;

	if ((error = pthread_attr_init(&pattr)) != 0)
		return error;


	attr->priority = 0;

	error = pthread_attr_getstacksize(&pattr, &attr->stackSize);


	pthread_attr_destroy(&pattr);


	return error;
}


int
of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
    id object, const of_thread_attr_t *attr)
{

	int error = 0;
	pthread_attr_t pattr;

	if ((error = pthread_attr_init(&pattr)) != 0)
		return error;

	@try {
		struct thread_ctx *ctx;

		if (attr != NULL) {
			struct sched_param param;

			if (attr->priority < -1 || attr->priority > 1)

				return EINVAL;


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

			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(&pattr,
			    &param)) != 0)
				return error;

			if (attr->stackSize > 0) {
				if ((error = pthread_attr_setstacksize(&pattr,
				    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, &pattr, functionWrapper, ctx);

	} @finally {
		pthread_attr_destroy(&pattr);
	}

	return error;
}


int
of_thread_join(of_thread_t thread)
{
	void *ret;

	return pthread_join(thread, &ret);
}


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

void
of_thread_set_name(const char *name)
{
#if defined(OF_HAIKU)
	rename_thread(find_thread(NULL), name);