Version 24.8.3.2, tag libreoffice-24.8.3.2
[LibreOffice.git] / include / osl / doublecheckedlocking.h
blobf89ab362055b2acd893cb647aa4ae8ece576eb15
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
25 #define INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
27 #if defined __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
31 /** A platform specific macro needed to make double-checked locking work.
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++.
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:
50 @code{.cpp}
51 static T * pInstance = 0;
53 T * p = pInstance;
54 if (!p)
56 Guard aGuard(aMutex);
57 p = pInstance;
58 if (!p)
60 p = ...;
61 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
62 pInstance = p;
65 else
66 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
67 return p;
68 @endcode
70 One extra advantage of this macro is that it makes it easier to find all
71 places where double-checked locking is used.
73 #define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER() /* empty */
75 #if defined __cplusplus
77 #endif /* __cplusplus */
79 #endif /* INCLUDED_OSL_DOUBLECHECKEDLOCKING_H */
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */