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