etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libc++ / dist / libcxx / src / memory.cpp
blob5b81f26e3dc004d9ae475e9a2cc04e4c5d327b7f
1 //===------------------------ memory.cpp ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_BUILDING_MEMORY
11 #include "memory"
12 #ifndef _LIBCPP_HAS_NO_THREADS
13 #include "mutex"
14 #include "thread"
15 #endif
16 #include "include/atomic_support.h"
18 _LIBCPP_BEGIN_NAMESPACE_STD
20 namespace
23 // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
24 // should be sufficient for thread safety.
25 // See https://llvm.org/bugs/show_bug.cgi?id=22803
26 template <class T>
27 inline T
28 increment(T& t) _NOEXCEPT
30 return __libcpp_atomic_add(&t, 1, _AO_Relaxed);
33 template <class T>
34 inline T
35 decrement(T& t) _NOEXCEPT
37 return __libcpp_atomic_add(&t, -1, _AO_Acq_Rel);
40 } // namespace
42 const allocator_arg_t allocator_arg = allocator_arg_t();
44 bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {}
46 const char*
47 bad_weak_ptr::what() const _NOEXCEPT
49 return "bad_weak_ptr";
52 __shared_count::~__shared_count()
56 void
57 __shared_count::__add_shared() _NOEXCEPT
59 increment(__shared_owners_);
62 bool
63 __shared_count::__release_shared() _NOEXCEPT
65 if (decrement(__shared_owners_) == -1)
67 __on_zero_shared();
68 return true;
70 return false;
73 __shared_weak_count::~__shared_weak_count()
77 void
78 __shared_weak_count::__add_shared() _NOEXCEPT
80 __shared_count::__add_shared();
83 void
84 __shared_weak_count::__add_weak() _NOEXCEPT
86 increment(__shared_weak_owners_);
89 void
90 __shared_weak_count::__release_shared() _NOEXCEPT
92 if (__shared_count::__release_shared())
93 __release_weak();
96 void
97 __shared_weak_count::__release_weak() _NOEXCEPT
99 if (decrement(__shared_weak_owners_) == -1)
100 __on_zero_shared_weak();
103 __shared_weak_count*
104 __shared_weak_count::lock() _NOEXCEPT
106 long object_owners = __libcpp_atomic_load(&__shared_owners_);
107 while (object_owners != -1)
109 if (__libcpp_atomic_compare_exchange(&__shared_owners_,
110 &object_owners,
111 object_owners+1))
112 return this;
114 return 0;
117 #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
119 const void*
120 __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
122 return 0;
125 #endif // _LIBCPP_NO_RTTI
127 #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
129 static const std::size_t __sp_mut_count = 16;
130 static pthread_mutex_t mut_back_imp[__sp_mut_count] =
132 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
133 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
134 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
135 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER
138 static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
140 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
141 : __lx(p)
145 void
146 __sp_mut::lock() _NOEXCEPT
148 mutex& m = *static_cast<mutex*>(__lx);
149 unsigned count = 0;
150 while (!m.try_lock())
152 if (++count > 16)
154 m.lock();
155 break;
157 this_thread::yield();
161 void
162 __sp_mut::unlock() _NOEXCEPT
164 static_cast<mutex*>(__lx)->unlock();
167 __sp_mut&
168 __get_sp_mut(const void* p)
170 static __sp_mut muts[__sp_mut_count]
172 &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
173 &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
174 &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
175 &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15]
177 return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
180 #endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
182 void
183 declare_reachable(void*)
187 void
188 declare_no_pointers(char*, size_t)
192 void
193 undeclare_no_pointers(char*, size_t)
197 pointer_safety
198 get_pointer_safety() _NOEXCEPT
200 return pointer_safety::relaxed;
203 void*
204 __undeclare_reachable(void* p)
206 return p;
209 void*
210 align(size_t alignment, size_t size, void*& ptr, size_t& space)
212 void* r = nullptr;
213 if (size <= space)
215 char* p1 = static_cast<char*>(ptr);
216 char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
217 size_t d = static_cast<size_t>(p2 - p1);
218 if (d <= space - size)
220 r = p2;
221 ptr = r;
222 space -= d;
225 return r;
228 _LIBCPP_END_NAMESPACE_STD