Deleted Added
full compact
thr_once.c (172695) thr_once.c (179417)
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 unchanged lines hidden (view full) ---

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 *
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 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/lib/libthr/thread/thr_once.c 172695 2007-10-16 07:46:15Z davidxu $
26 * $FreeBSD: head/lib/libthr/thread/thr_once.c 179417 2008-05-30 00:02:59Z davidxu $
27 *
28 */
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
27 *
28 */
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_MASK 0x03
41#define ONCE_WAIT 0x03
42
42
43static pthread_mutex_t _thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
44static pthread_cond_t _thr_once_cv = PTHREAD_COND_INITIALIZER;
45
46/*
47 * POSIX:
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.
51 */
52
53static void
54once_cancel_handler(void *arg)
55{
56 pthread_once_t *once_control = arg;
57
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
50static void
51once_cancel_handler(void *arg)
52{
53 pthread_once_t *once_control = arg;
54
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);
55 if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS, ONCE_NEVER_DONE))
56 return;
57 atomic_store_rel_int(&once_control->state, ONCE_NEVER_DONE);
58 _thr_umtx_wake(&once_control->state, INT_MAX, 0);
62}
63
64int
65_pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
66{
67 struct pthread *curthread;
59}
60
61int
62_pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
63{
64 struct pthread *curthread;
68 int wakeup = 0;
65 int state;
69
66
70 if (once_control->state == ONCE_DONE)
67 for (;;) {
68 state = once_control->state;
69 if (state == ONCE_DONE)
70 return (0);
71 if (state == ONCE_NEVER_DONE) {
72 if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_IN_PROGRESS))
73 break;
74 } else if (state == ONCE_IN_PROGRESS) {
75 if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_WAIT))
76 _thr_umtx_wait_uint(&once_control->state, ONCE_WAIT, NULL, 0);
77 } else if (state == ONCE_WAIT) {
78 _thr_umtx_wait_uint(&once_control->state, state, NULL, 0);
79 } else
80 return (EINVAL);
81 }
82
83 curthread = _get_curthread();
84 THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
85 init_routine();
86 THR_CLEANUP_POP(curthread, 0);
87 if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS, ONCE_DONE))
71 return (0);
88 return (0);
72 _pthread_mutex_lock(&_thr_once_lock);
73 while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS)
74 _pthread_cond_wait(&_thr_once_cv, &_thr_once_lock);
75 /*
76 * If previous thread was canceled, then the state still
77 * could be ONCE_NEVER_DONE, we need to check it again.
78 */
79 if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) {
80 once_control->state = ONCE_IN_PROGRESS;
81 _pthread_mutex_unlock(&_thr_once_lock);
82 curthread = _get_curthread();
83 THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control);
84 init_routine();
85 THR_CLEANUP_POP(curthread, 0);
86 _pthread_mutex_lock(&_thr_once_lock);
87 once_control->state = ONCE_DONE;
88 wakeup = 1;
89 }
90 _pthread_mutex_unlock(&_thr_once_lock);
91 if (wakeup)
92 _pthread_cond_broadcast(&_thr_once_cv);
89 atomic_store_rel_int(&once_control->state, ONCE_DONE);
90 _thr_umtx_wake(&once_control->state, INT_MAX, 0);
93 return (0);
94}
95
96void
97_thr_once_init()
98{
91 return (0);
92}
93
94void
95_thr_once_init()
96{
99 _thr_once_lock = PTHREAD_MUTEX_INITIALIZER;
100 _thr_once_cv = PTHREAD_COND_INITIALIZER;
101}
97}