2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
30 #include "namespace.h"
32 #include "un-namespace.h"
34 #include "thr_private.h"
36 __weak_reference(_pthread_once, pthread_once);
38 #define ONCE_NEVER_DONE PTHREAD_NEEDS_INIT
39 #define ONCE_DONE PTHREAD_DONE_INIT
40 #define ONCE_IN_PROGRESS 0x02
41 #define ONCE_MASK 0x03
43 static pthread_mutex_t _thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
44 static pthread_cond_t _thr_once_cv = PTHREAD_COND_INITIALIZER;
48 * The pthread_once() function is not a cancellation point. However,
49 * if init_routine is a cancellation point and is canceled, the effect
50 * on once_control shall be as if pthread_once() was never called.
54 once_cancel_handler(void *arg)
56 pthread_once_t *once_control = arg;
58 _pthread_mutex_lock(&_thr_once_lock);
59 once_control->state = ONCE_NEVER_DONE;
60 _pthread_mutex_unlock(&_thr_once_lock);
61 _pthread_cond_broadcast(&_thr_once_cv);
65 _pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
69 if (once_control->state == ONCE_DONE)
71 _pthread_mutex_lock(&_thr_once_lock);
72 while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS)
73 _pthread_cond_wait(&_thr_once_cv, &_thr_once_lock);
75 * If previous thread was canceled, then the state still
76 * could be ONCE_NEVER_DONE, we need to check it again.
78 if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) {
79 once_control->state = ONCE_IN_PROGRESS;
80 _pthread_mutex_unlock(&_thr_once_lock);
81 _pthread_cleanup_push(once_cancel_handler, once_control);
83 _pthread_cleanup_pop(0);
84 _pthread_mutex_lock(&_thr_once_lock);
85 once_control->state = ONCE_DONE;
88 _pthread_mutex_unlock(&_thr_once_lock);
90 _pthread_cond_broadcast(&_thr_once_cv);
97 _thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
98 _thr_once_cv = PTHREAD_COND_INITIALIZER;