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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include "namespace.h"
28 #include <pthread.h>
29 #include "un-namespace.h"
30 
31 #include "thr_private.h"
32 
33 #define ONCE_NEVER_DONE		PTHREAD_NEEDS_INIT
34 #define ONCE_DONE		PTHREAD_DONE_INIT
35 #define ONCE_IN_PROGRESS	0x02
36 #define ONCE_MASK		0x03
37 
38 static pthread_mutex_t		once_lock = PTHREAD_MUTEX_INITIALIZER;
39 static pthread_cond_t		once_cv = PTHREAD_COND_INITIALIZER;
40 
41 /*
42  * POSIX:
43  * The pthread_once() function is not a cancellation point. However,
44  * if init_routine is a cancellation point and is canceled, the effect
45  * on once_control shall be as if pthread_once() was never called.
46  */
47 
48 static void
49 once_cancel_handler(void *arg)
50 {
51 	pthread_once_t *once_control = arg;
52 
53 	_pthread_mutex_lock(&once_lock);
54 	once_control->__state = ONCE_NEVER_DONE;
55 	_pthread_mutex_unlock(&once_lock);
56 	_pthread_cond_broadcast(&once_cv);
57 }
58 
59 int
60 _pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
61 {
62 	int wakeup = 0;
63 
64 	_thr_check_init();
65 
66 	if (once_control->__state == ONCE_DONE)
67 		return (0);
68 	_pthread_mutex_lock(&once_lock);
69 	while (*(volatile int *)&(once_control->__state) == ONCE_IN_PROGRESS)
70 		_pthread_cond_wait(&once_cv, &once_lock);
71 	/*
72 	 * If previous thread was canceled, then the state still
73 	 * could be ONCE_NEVER_DONE, we need to check it again.
74 	 */
75 	if (*(volatile int *)&(once_control->__state) == ONCE_NEVER_DONE) {
76 		once_control->__state = ONCE_IN_PROGRESS;
77 		_pthread_mutex_unlock(&once_lock);
78 		_pthread_cleanup_push(once_cancel_handler, once_control);
79 		init_routine();
80 		_pthread_cleanup_pop(0);
81 		_pthread_mutex_lock(&once_lock);
82 		once_control->__state = ONCE_DONE;
83 		wakeup = 1;
84 	}
85 	_pthread_mutex_unlock(&once_lock);
86 	if (wakeup)
87 		_pthread_cond_broadcast(&once_cv);
88 	return (0);
89 }
90 
91 __strong_reference(_pthread_once, pthread_once);
92