xref: /aoo42x/main/sal/inc/osl/doublecheckedlocking.h (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #if !defined INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
29 #define INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
30 
31 #if defined __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34 
35 /** A platform specific macro needed to make double-checked locking work.
36 
37     See
38     <http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>
39     for a description of double-checked locking, why it is broken, and how it
40     can be fixed.  On platforms where it is necessary, this macro will expand
41     to some memory barrier instruction.  On many platforms, double-checked
42     locking works as it is, though, so on those platforms this macro will be
43     empty.  This is a macro instead of a (C++ inline) function to allow for
44     maximum performance in both C and C++.
45 
46     If possible, use the rtl_Instance template instead of explicitly spelling
47     out the double-checked locking pattern.  There are few cases where you
48     will have to spell it out explicitly (e.g., the logic of a certain
49     instance of the pattern is too complex to be mapped to the template, or
50     some compiler refuses to compile a template instantiation due to internal
51     compiler errors), though, and you should always call this macro at the
52     right places then:
53 
54       static T * pInstance = 0;
55 
56       T * p = pInstance;
57       if (!p)
58       {
59           Guard aGuard(aMutex);
60           p = pInstance;
61           if (!p)
62           {
63               p = ...;
64               OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
65               pInstance = p;
66           }
67       }
68       else
69           OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
70       return p;
71 
72     One extra advantage of this macro is that it makes it easier to find all
73     places where double-checked locking is used.
74  */
75 #define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER() /* empty */
76 
77 #if defined __cplusplus
78 }
79 #endif /* __cplusplus */
80 
81 #endif /* INCLUDED_OSL_DOUBLECHECKEDLOCKING_H */
82