xref: /freebsd/lib/libthr/thread/thr_once.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*
2  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "namespace.h"
31 #include <pthread.h>
32 #include "un-namespace.h"
33 
34 #include "thr_private.h"
35 
36 __weak_reference(_pthread_once, pthread_once);
37 
38 #define ONCE_NEVER_DONE		PTHREAD_NEEDS_INIT
39 #define ONCE_DONE		PTHREAD_DONE_INIT
40 #define	ONCE_IN_PROGRESS	0x02
41 #define ONCE_WAIT		0x03
42 
43 /*
44  * POSIX:
45  * The pthread_once() function is not a cancellation point. However,
46  * if init_routine is a cancellation point and is canceled, the effect
47  * on once_control shall be as if pthread_once() was never called.
48  */
49 
50 static void
51 once_cancel_handler(void *arg)
52 {
53 	pthread_once_t *once_control;
54 
55 	once_control = arg;
56 	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
57 	    ONCE_NEVER_DONE))
58 		return;
59 	atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
60 	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
61 }
62 
63 int
64 _pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
65 {
66 	struct pthread *curthread;
67 	int state;
68 
69 	_thr_check_init();
70 
71 	for (;;) {
72 		state = once_control->state;
73 		if (state == ONCE_DONE) {
74 			atomic_thread_fence_acq();
75 			return (0);
76 		}
77 		if (state == ONCE_NEVER_DONE) {
78 			if (atomic_cmpset_int(&once_control->state, state,
79 			    ONCE_IN_PROGRESS))
80 				break;
81 		} else if (state == ONCE_IN_PROGRESS) {
82 			if (atomic_cmpset_int(&once_control->state, state,
83 			    ONCE_WAIT))
84 				_thr_umtx_wait_uint(&once_control->state,
85 				    ONCE_WAIT, NULL, 0);
86 		} else if (state == ONCE_WAIT) {
87 			_thr_umtx_wait_uint(&once_control->state, state,
88 			    NULL, 0);
89 		} else
90 			return (EINVAL);
91         }
92 
93 	curthread = _get_curthread();
94 	THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
95 	init_routine();
96 	THR_CLEANUP_POP(curthread, 0);
97 	if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS,
98 	    ONCE_DONE))
99 		return (0);
100 	atomic_store_rel_int(&once_control->state, ONCE_DONE);
101 	_thr_umtx_wake(&once_control->state, INT_MAX, 0);
102 	return (0);
103 }
104