summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/threads/pthread.c
blob: d8c324a7d91bf83bcbef3a30c2ba41e376246a3e (plain)
1
2
3
4
5
6
7
8
9
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "lint.h"
#include "thr_uberdata.h"

/*
 * pthread_once related data
 * This structure is exported as pthread_once_t in pthread.h.
 * We export only the size of this structure. so check
 * pthread_once_t in pthread.h before making a change here.
 */
typedef struct  __once {
	mutex_t	mlock;
	union {
		uint32_t	pad32_flag[2];
		uint64_t	pad64_flag;
	} oflag;
} __once_t;

#define	once_flag	oflag.pad32_flag[1]

/*
 * pthread_create: creates a thread in the current process.
 * calls common _thrp_create() after copying the attributes.
 */
#pragma weak	pthread_create			= _pthread_create
int
_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
	void * (*start_routine)(void *), void *arg)
{
	ulwp_t		*self = curthread;
	uberdata_t	*udp = self->ul_uberdata;
	const thrattr_t	*ap = attr? attr->__pthread_attrp : def_thrattr();
	long		flag;
	pthread_t	tid;
	int		policy;
	pri_t		priority;
	int		error;
	int		mapped = 0;
	int		mappedpri;
	int		rt = 0;

	if (ap == NULL)
		return (EINVAL);

	if (ap->inherit == PTHREAD_INHERIT_SCHED) {
		policy = self->ul_policy;
		priority = self->ul_pri;
		mapped = self->ul_pri_mapped;
		mappedpri = self->ul_mappedpri;
	} else {
		policy = ap->policy;
		priority = ap->prio;
		if (policy == SCHED_OTHER) {
			if (priority < THREAD_MIN_PRIORITY ||
			    priority > THREAD_MAX_PRIORITY) {
				if (_validate_rt_prio(policy, priority))
					return (EINVAL);
				mapped = 1;
				mappedpri = priority;
				priority = map_rtpri_to_gp(priority);
				ASSERT(priority >= THREAD_MIN_PRIORITY &&
				    priority <= THREAD_MAX_PRIORITY);
			}
		} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
			if (_validate_rt_prio(policy, priority))
				return (EINVAL);
			if (_private_geteuid() == 0)
				rt = 1;
		} else {
			return (EINVAL);
		}
	}

	flag = ap->scope | ap->detachstate | ap->daemonstate | THR_SUSPENDED;
	error = _thrp_create(ap->stkaddr, ap->stksize, start_routine, arg,
		flag, &tid, priority, policy, ap->guardsize);
	if (error == 0) {
		int prio_err;

		if (mapped) {
			ulwp_t *ulwp = find_lwp(tid);
			ulwp->ul_pri_mapped = 1;
			ulwp->ul_mappedpri = mappedpri;
			ulwp_unlock(ulwp, udp);
		}

		if (rt && (prio_err = _thrp_setlwpprio(tid, policy, priority)))
			return (prio_err);

		if (thread)
			*thread = tid;
		(void) _thr_continue(tid);
	}

	/* posix version expects EAGAIN for lack of memory */
	if (error == ENOMEM)
		error = EAGAIN;
	return (error);
}

/*
 * pthread_once: calls given function only once.
 * it synchronizes via mutex in pthread_once_t structure
 */
#pragma weak	pthread_once			= _pthread_once
int
_pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
	__once_t *once = (__once_t *)once_control;

	if (once == NULL || init_routine == NULL)
		return (EINVAL);

	if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
		(void) _private_mutex_lock(&once->mlock);
		if (once->once_flag == PTHREAD_ONCE_NOTDONE) {
			pthread_cleanup_push(_private_mutex_unlock,
			    &once->mlock);
			(*init_routine)();
			pthread_cleanup_pop(0);
			once->once_flag = PTHREAD_ONCE_DONE;
		}
		(void) _private_mutex_unlock(&once->mlock);
	}

	return (0);
}

/*
 * pthread_equal: equates two thread ids.
 */
#pragma weak	pthread_equal			= _pthread_equal
int
_pthread_equal(pthread_t t1, pthread_t t2)
{
	return (t1 == t2);
}

/*
 * pthread_getschedparam: gets the sched parameters in a struct.
 */
#pragma weak	pthread_getschedparam		= _pthread_getschedparam
int
_pthread_getschedparam(pthread_t tid, int *policy, struct sched_param *param)
{
	uberdata_t *udp = curthread->ul_uberdata;
	ulwp_t *ulwp;
	int error = 0;

	if (param == NULL || policy == NULL)
		error = EINVAL;
	else if ((ulwp = find_lwp(tid)) == NULL)
		error = ESRCH;
	else {
		if (ulwp->ul_pri_mapped)
			param->sched_priority = ulwp->ul_mappedpri;
		else
			param->sched_priority = ulwp->ul_pri;
		*policy = ulwp->ul_policy;
		ulwp_unlock(ulwp, udp);
	}

	return (error);
}

/*
 * Besides the obvious arguments, the inheritflag needs to be explained:
 * If set to PRIO_SET or PRIO_SET_PRIO, it does the normal, expected work
 * of setting thread's assigned scheduling parameters and policy.
 * If set to PRIO_INHERIT, it sets the thread's effective priority values
 * (t_epri, t_empappedpri), and does not update the assigned priority values
 * (t_pri, t_mappedpri).  If set to PRIO_DISINHERIT, it clears the thread's
 * effective priority values, and reverts the thread, if necessary, back
 * to the assigned priority values.
 */
int
_thread_setschedparam_main(pthread_t tid, int policy,
    const struct sched_param *param, int inheritflag)
{
	uberdata_t *udp = curthread->ul_uberdata;
	ulwp_t	*ulwp;
	int	error = 0;
	int	prio;
	int	opolicy;
	int	mappedprio;
	int	mapped = 0;
	pri_t	*mappedprip;

	if (param == NULL)
		return (EINVAL);
	if ((ulwp = find_lwp(tid)) == NULL)
		return (ESRCH);
	prio = param->sched_priority;
	opolicy = ulwp->ul_policy;
	if (inheritflag == PRIO_SET_PRIO) {	/* don't change policy */
		policy = opolicy;
		inheritflag = PRIO_SET;
	}
	ASSERT(inheritflag == PRIO_SET || opolicy == policy);
	if (inheritflag == PRIO_DISINHERIT) {
		ulwp->ul_emappedpri = 0;
		ulwp->ul_epri = 0;
		prio = ulwp->ul_pri;	/* ignore prio in sched_param */
	}
	if (policy == SCHED_OTHER) {
		/*
		 * Set thread's policy to OTHER
		 */
		if (prio < THREAD_MIN_PRIORITY || prio > THREAD_MAX_PRIORITY) {
			if (_validate_rt_prio(policy, prio)) {
				error = EINVAL;
				goto out;
			}
			mapped = 1;
			mappedprio = prio;
			prio = map_rtpri_to_gp(prio);
			ASSERT(prio >= THREAD_MIN_PRIORITY &&
			    prio <= THREAD_MAX_PRIORITY);
		}
		/*
		 * Thread changing from FIFO/RR to OTHER
		 */
		if (opolicy == SCHED_FIFO || opolicy == SCHED_RR) {
			if ((error = _thrp_setlwpprio(tid, policy, prio)) != 0)
				goto out;
		}
		if (inheritflag != PRIO_DISINHERIT) {
			if (inheritflag == PRIO_INHERIT)
				mappedprip = &ulwp->ul_emappedpri;
			else
				mappedprip = &ulwp->ul_mappedpri;
			if (mapped) {
				ulwp->ul_pri_mapped = 1;
				*mappedprip = mappedprio;
			} else {
				ulwp->ul_pri_mapped = 0;
				*mappedprip = 0;
			}
		}
		ulwp->ul_policy = policy;
		if (inheritflag == PRIO_INHERIT)
			ulwp->ul_epri = prio;
		else
			ulwp->ul_pri = prio;
	} else if (policy == SCHED_FIFO || policy == SCHED_RR) {
		if (_validate_rt_prio(policy, prio))
			error = EINVAL;
		else {
			int prio_err;

			if (_private_geteuid() == 0 &&
			    (prio_err = _thrp_setlwpprio(tid, policy, prio))) {
				error = prio_err;
				goto out;
			}

			ulwp->ul_policy = policy;
			if (inheritflag == PRIO_INHERIT)
				ulwp->ul_epri = prio;
			else
				ulwp->ul_pri = prio;
		}
	} else {
		error = EINVAL;
	}

out:
	ulwp_unlock(ulwp, udp);
	return (error);
}

/*
 * pthread_setschedparam: sets the sched parameters for a thread.
 */
#pragma weak	pthread_setschedparam		= _pthread_setschedparam
int
_pthread_setschedparam(pthread_t tid,
	int policy, const struct sched_param *param)
{
	return (_thread_setschedparam_main(tid, policy, param, PRIO_SET));
}