Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / osl / doublecheckedlocking.h
blob8e7f4b24e0e8c9da5b117e742e2e8d2a20a47244
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 .
20 #ifndef INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
21 #define INCLUDED_OSL_DOUBLECHECKEDLOCKING_H
23 #if defined __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
27 /** A platform specific macro needed to make double-checked locking work.
29 See
30 <http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>
31 for a description of double-checked locking, why it is broken, and how it
32 can be fixed. On platforms where it is necessary, this macro will expand
33 to some memory barrier instruction. On many platforms, double-checked
34 locking works as it is, though, so on those platforms this macro will be
35 empty. This is a macro instead of a (C++ inline) function to allow for
36 maximum performance in both C and C++.
38 If possible, use the rtl_Instance template instead of explicitly spelling
39 out the double-checked locking pattern. There are few cases where you
40 will have to spell it out explicitly (e.g., the logic of a certain
41 instance of the pattern is too complex to be mapped to the template, or
42 some compiler refuses to compile a template instantiation due to internal
43 compiler errors), though, and you should always call this macro at the
44 right places then:
46 @code{.cpp}
47 static T * pInstance = 0;
49 T * p = pInstance;
50 if (!p)
52 Guard aGuard(aMutex);
53 p = pInstance;
54 if (!p)
56 p = ...;
57 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
58 pInstance = p;
61 else
62 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
63 return p;
64 @endcode
66 One extra advantage of this macro is that it makes it easier to find all
67 places where double-checked locking is used.
69 #define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER() /* empty */
71 #if defined __cplusplus
73 #endif /* __cplusplus */
75 #endif /* INCLUDED_OSL_DOUBLECHECKEDLOCKING_H */
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */