1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #if !defined INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
25 #define INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
26 
27 #if defined __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30 
31 /** A platform specific macro needed to make double-checked locking work.
32 
33     See
34     <http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>
35     for a description of double-checked locking, why it is broken, and how it
36     can be fixed.  On platforms where it is necessary, this macro will expand
37     to some memory barrier instruction.  On many platforms, double-checked
38     locking works as it is, though, so on those platforms this macro will be
39     empty.  This is a macro instead of a (C++ inline) function to allow for
40     maximum performance in both C and C++.
41 
42     If possible, use the rtl_Instance template instead of explicitly spelling
43     out the double-checked locking pattern.  There are few cases where you
44     will have to spell it out explicitly (e.g., the logic of a certain
45     instance of the pattern is too complex to be mapped to the template, or
46     some compiler refuses to compile a template instantiation due to internal
47     compiler errors), though, and you should always call this macro at the
48     right places then:
49 
50       static T * pInstance = 0;
51 
52       T * p = pInstance;
53       if (!p)
54       {
55           Guard aGuard(aMutex);
56           p = pInstance;
57           if (!p)
58           {
59               p = ...;
60               OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
61               pInstance = p;
62           }
63       }
64       else
65           OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
66       return p;
67 
68     One extra advantage of this macro is that it makes it easier to find all
69     places where double-checked locking is used.
70  */
71 #define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER() /* empty */
72 
73 #if defined __cplusplus
74 }
75 #endif /* __cplusplus */
76 
77 #endif /* INCLUDED_OSL_DOUBLECHECKEDLOCKING_H */
78