Deleted Added
full compact
guard.cc (227972) guard.cc (232922)
1/*
2 * Copyright 2010-2012 PathScale, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
1/**
2 * guard.cc: Functions for thread-safe static initialisation.
3 *
4 * Static values in C++ can be initialised lazily their first use. This file
5 * contains functions that are used to ensure that two threads attempting to
6 * initialize the same static do not call the constructor twice. This is
7 * important because constructors can have side effects, so calling the
8 * constructor twice may be very bad.

--- 89 unchanged lines hidden (view full) ---

98 if (1 == first_byte) { return 0; }
99 int32_t *lock = low_32_bits(guard_object);
100 // Simple spin lock using the low 32 bits. We assume that concurrent
101 // attempts to initialize statics are very rare, so we don't need to
102 // optimise for the case where we have lots of threads trying to acquire
103 // the lock at the same time.
104 while (!__sync_bool_compare_and_swap_4(lock, 0, 1))
105 {
27/**
28 * guard.cc: Functions for thread-safe static initialisation.
29 *
30 * Static values in C++ can be initialised lazily their first use. This file
31 * contains functions that are used to ensure that two threads attempting to
32 * initialize the same static do not call the constructor twice. This is
33 * important because constructors can have side effects, so calling the
34 * constructor twice may be very bad.

--- 89 unchanged lines hidden (view full) ---

124 if (1 == first_byte) { return 0; }
125 int32_t *lock = low_32_bits(guard_object);
126 // Simple spin lock using the low 32 bits. We assume that concurrent
127 // attempts to initialize statics are very rare, so we don't need to
128 // optimise for the case where we have lots of threads trying to acquire
129 // the lock at the same time.
130 while (!__sync_bool_compare_and_swap_4(lock, 0, 1))
131 {
132 if (1 == ((*guard_object) >> 56))
133 {
134 break;
135 }
106 sched_yield();
107 }
108 // We have to test the guard again, in case another thread has performed
109 // the initialisation while we were trying to acquire the lock.
110 first_byte = (*guard_object) >> 56;
111 return (1 != first_byte);
112}
113

--- 21 unchanged lines hidden ---
136 sched_yield();
137 }
138 // We have to test the guard again, in case another thread has performed
139 // the initialisation while we were trying to acquire the lock.
140 first_byte = (*guard_object) >> 56;
141 return (1 != first_byte);
142}
143

--- 21 unchanged lines hidden ---