xref: /freebsd/lib/libthr/thread/thr_once.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1*5e53a4f9SPedro F. Giffuni /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
4df2cf821SDavid Xu  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5bb535300SJeff Roberson  * All rights reserved.
6bb535300SJeff Roberson  *
7bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
8bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
9bb535300SJeff Roberson  * are met:
10bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
11df2cf821SDavid Xu  *    notice unmodified, this list of conditions, and the following
12df2cf821SDavid Xu  *    disclaimer.
13bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
14bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
15bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
16bb535300SJeff Roberson  *
17df2cf821SDavid Xu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18df2cf821SDavid Xu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19df2cf821SDavid Xu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20df2cf821SDavid Xu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21df2cf821SDavid Xu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22df2cf821SDavid Xu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23df2cf821SDavid Xu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24df2cf821SDavid Xu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25df2cf821SDavid Xu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26df2cf821SDavid Xu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27bb535300SJeff Roberson  */
28a091d823SDavid Xu 
2932793011SKonstantin Belousov #include <sys/cdefs.h>
3032793011SKonstantin Belousov __FBSDID("$FreeBSD$");
3132793011SKonstantin Belousov 
32a091d823SDavid Xu #include "namespace.h"
33bb535300SJeff Roberson #include <pthread.h>
34a091d823SDavid Xu #include "un-namespace.h"
35a091d823SDavid Xu 
36bb535300SJeff Roberson #include "thr_private.h"
37bb535300SJeff Roberson 
38bb535300SJeff Roberson __weak_reference(_pthread_once, pthread_once);
39bb535300SJeff Roberson 
40a091d823SDavid Xu #define ONCE_NEVER_DONE		PTHREAD_NEEDS_INIT
41a091d823SDavid Xu #define ONCE_DONE		PTHREAD_DONE_INIT
42a091d823SDavid Xu #define	ONCE_IN_PROGRESS	0x02
431b3418b2SDavid Xu #define ONCE_WAIT		0x03
44a091d823SDavid Xu 
45a091d823SDavid Xu /*
46a091d823SDavid Xu  * POSIX:
47a091d823SDavid Xu  * The pthread_once() function is not a cancellation point. However,
48a091d823SDavid Xu  * if init_routine is a cancellation point and is canceled, the effect
49a091d823SDavid Xu  * on once_control shall be as if pthread_once() was never called.
50a091d823SDavid Xu  */
51a091d823SDavid Xu 
52a091d823SDavid Xu static void
53a091d823SDavid Xu once_cancel_handler(void *arg)
54a091d823SDavid Xu {
55b684727bSKonstantin Belousov 	pthread_once_t *once_control;
56a091d823SDavid Xu 
57b684727bSKonstantin Belousov 	once_control = arg;
58b684727bSKonstantin Belousov 	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
59b684727bSKonstantin Belousov 	    ONCE_NEVER_DONE))
601b3418b2SDavid Xu 		return;
611b3418b2SDavid Xu 	atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
621b3418b2SDavid Xu 	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
63a091d823SDavid Xu }
64a091d823SDavid Xu 
65bb535300SJeff Roberson int
66bb535300SJeff Roberson _pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
67bb535300SJeff Roberson {
685150e987SDavid Xu 	struct pthread *curthread;
691b3418b2SDavid Xu 	int state;
70a091d823SDavid Xu 
71aad93b04SRyan Stone 	_thr_check_init();
72aad93b04SRyan Stone 
731b3418b2SDavid Xu 	for (;;) {
741b3418b2SDavid Xu 		state = once_control->state;
753e7e67c0SKonstantin Belousov 		if (state == ONCE_DONE) {
763e7e67c0SKonstantin Belousov 			atomic_thread_fence_acq();
77a091d823SDavid Xu 			return (0);
783e7e67c0SKonstantin Belousov 		}
791b3418b2SDavid Xu 		if (state == ONCE_NEVER_DONE) {
80b684727bSKonstantin Belousov 			if (atomic_cmpset_int(&once_control->state, state,
81b684727bSKonstantin Belousov 			    ONCE_IN_PROGRESS))
821b3418b2SDavid Xu 				break;
831b3418b2SDavid Xu 		} else if (state == ONCE_IN_PROGRESS) {
84b684727bSKonstantin Belousov 			if (atomic_cmpset_int(&once_control->state, state,
85b684727bSKonstantin Belousov 			    ONCE_WAIT))
86b684727bSKonstantin Belousov 				_thr_umtx_wait_uint(&once_control->state,
87b684727bSKonstantin Belousov 				    ONCE_WAIT, NULL, 0);
881b3418b2SDavid Xu 		} else if (state == ONCE_WAIT) {
89b684727bSKonstantin Belousov 			_thr_umtx_wait_uint(&once_control->state, state,
90b684727bSKonstantin Belousov 			    NULL, 0);
911b3418b2SDavid Xu 		} else
921b3418b2SDavid Xu 			return (EINVAL);
931b3418b2SDavid Xu         }
941b3418b2SDavid Xu 
955150e987SDavid Xu 	curthread = _get_curthread();
965150e987SDavid Xu 	THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
97bb535300SJeff Roberson 	init_routine();
985150e987SDavid Xu 	THR_CLEANUP_POP(curthread, 0);
99b684727bSKonstantin Belousov 	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
100b684727bSKonstantin Belousov 	    ONCE_DONE))
1011b3418b2SDavid Xu 		return (0);
1021b3418b2SDavid Xu 	atomic_store_rel_int(&once_control->state, ONCE_DONE);
1031b3418b2SDavid Xu 	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
104bb535300SJeff Roberson 	return (0);
105bb535300SJeff Roberson }
106