initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / fbtl / pthread_once.c
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include "pthreadP.h"
20 #include <lowlevellock.h>
21 #include <atomic.h>
22
23
24 unsigned long int __fork_generation attribute_hidden;
25
26
27 static void
28 clear_once_control (void *arg)
29 {
30 pthread_once_t *once_control = (pthread_once_t *) arg;
31
32 /* Reset to the uninitialized state here. We don't need a stronger memory
33 order because we do not need to make any other of our writes visible to
34 other threads that see this value: This function will be called if we
35 get interrupted (see __pthread_once), so all we need to relay to other
36 threads is the state being reset again. */
37 *once_control = 0;
38 lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
39 }
40
41
42 /* This is similar to a lock implementation, but we distinguish between three
43 states: not yet initialized (0), initialization finished (2), and
44 initialization in progress (__fork_generation | 1). If in the first state,
45 threads will try to run the initialization by moving to the second state;
46 the first thread to do so via a CAS on once_control runs init_routine,
47 other threads block.
48 When forking the process, some threads can be interrupted during the second
49 state; they won't be present in the forked child, so we need to restart
50 initialization in the child. To distinguish an in-progress initialization
51 from an interrupted initialization (in which case we need to reclaim the
52 lock), we look at the fork generation that's part of the second state: We
53 can reclaim iff it differs from the current fork generation.
54 XXX: This algorithm has an ABA issue on the fork generation: If an
55 initialization is interrupted, we then fork 2^30 times (30 bits of
56 once_control are used for the fork generation), and try to initialize
57 again, we can deadlock because we can't distinguish the in-progress and
58 interrupted cases anymore. */
59 int
60 __pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
61 {
62 while (1)
63 {
64 int oldval, val, newval;
65
66 /* We need acquire memory order for this load because if the value
67 signals that initialization has finished, we need to be see any
68 data modifications done during initialization. */
69 val = *once_control;
70 atomic_read_barrier();
71 do
72 {
73 /* Check if the initialization has already been done. */
74 if (__glibc_likely ((val & 2) != 0))
75 return 0;
76
77 oldval = val;
78 /* We try to set the state to in-progress and having the current
79 fork generation. We don't need atomic accesses for the fork
80 generation because it's immutable in a particular process, and
81 forked child processes start with a single thread that modified
82 the generation. */
83 newval = __fork_generation | 1;
84 /* We need acquire memory order here for the same reason as for the
85 load from once_control above. */
86 val = atomic_compare_and_exchange_val_acq (once_control, newval,
87 oldval);
88 }
89 while (__glibc_unlikely (val != oldval));
90
91 /* Check if another thread already runs the initializer. */
92 if ((oldval & 1) != 0)
93 {
94 /* Check whether the initializer execution was interrupted by a
95 fork. We know that for both values, bit 0 is set and bit 1 is
96 not. */
97 if (oldval == newval)
98 {
99 /* Same generation, some other thread was faster. Wait. */
100 lll_futex_wait (once_control, newval, LLL_PRIVATE);
101 continue;
102 }
103 }
104
105 /* This thread is the first here. Do the initialization.
106 Register a cleanup handler so that in case the thread gets
107 interrupted the initialization can be restarted. */
108 pthread_cleanup_push (clear_once_control, once_control);
109
110 init_routine ();
111
112 pthread_cleanup_pop (0);
113
114
115 /* Mark *once_control as having finished the initialization. We need
116 release memory order here because we need to synchronize with other
117 threads that want to use the initialized data. */
118 atomic_write_barrier();
119 *once_control = 2;
120
121 /* Wake up all other threads. */
122 lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
123 break;
124 }
125
126 return 0;
127 }
128 weak_alias (__pthread_once, pthread_once)
129 hidden_def (__pthread_once)