1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PerProcess_h
27#define PerProcess_h
28
29#include "Inline.h"
30#include "Sizes.h"
31#include "StaticMutex.h"
32#include <mutex>
33
34namespace bmalloc {
35
36// Usage:
37//     Object* object = PerProcess<Object>::get();
38//     x = object->field->field;
39//
40// Object will be instantiated only once, even in the face of concurrency.
41//
42// NOTE: If you observe global side-effects of the Object constructor, be
43// sure to lock the Object mutex. For example:
44//
45// Object() : m_field(...) { globalFlag = true }
46//
47// Object* object = PerProcess<Object>::get();
48// x = object->m_field; // OK
49// if (gobalFlag) { ... } // Undefined behavior.
50//
51// std::lock_guard<StaticMutex> lock(PerProcess<Object>::mutex());
52// Object* object = PerProcess<Object>::get(lock);
53// if (gobalFlag) { ... } // OK.
54
55template<typename T>
56class PerProcess {
57public:
58    static T* get();
59    static T* getFastCase();
60
61    static StaticMutex& mutex() { return s_mutex; }
62
63private:
64    static T* getSlowCase();
65
66    static std::atomic<T*> s_object;
67    static StaticMutex s_mutex;
68
69    typedef typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type Memory;
70    static Memory s_memory;
71};
72
73template<typename T>
74INLINE T* PerProcess<T>::getFastCase()
75{
76    return s_object.load(std::memory_order_consume);
77}
78
79template<typename T>
80INLINE T* PerProcess<T>::get()
81{
82    T* object = getFastCase();
83    if (!object)
84        return getSlowCase();
85    return object;
86}
87
88template<typename T>
89NO_INLINE T* PerProcess<T>::getSlowCase()
90{
91    std::lock_guard<StaticMutex> lock(s_mutex);
92    if (!s_object.load(std::memory_order_consume)) {
93        T* t = new (&s_memory) T(lock);
94        s_object.store(t, std::memory_order_release);
95    }
96    return s_object.load(std::memory_order_consume);
97}
98
99template<typename T>
100std::atomic<T*> PerProcess<T>::s_object;
101
102template<typename T>
103StaticMutex PerProcess<T>::s_mutex;
104
105template<typename T>
106typename PerProcess<T>::Memory PerProcess<T>::s_memory;
107
108} // namespace bmalloc
109
110#endif // PerProcess_h
111