| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef threading_ProtectedData_h |
| 8 |
#define threading_ProtectedData_h |
| 9 |
|
| 10 |
#include "mozilla/Atomics.h" |
| 11 |
#include "jstypes.h" |
| 12 |
#include "threading/ThreadId.h" |
| 13 |
|
| 14 |
struct JS_PUBLIC_API JSContext; |
| 15 |
|
| 16 |
namespace js { |
| 17 |
|
| 18 |
class Mutex; |
| 19 |
|
| 20 |
// This file provides classes for encapsulating pieces of data with a check |
| 21 |
// that ensures the data is only accessed if certain conditions are met. |
| 22 |
// Checking is only done in debug builds; in release builds these classes |
| 23 |
// have no space or time overhead. These classes are mainly used for ensuring |
| 24 |
// that data is used in threadsafe ways. |
| 25 |
// |
| 26 |
// ProtectedData does not by itself ensure that data is threadsafe: it only |
| 27 |
// documents and checks synchronization constraints that need to be established |
| 28 |
// by the code using the data. If a mutex can be created and directly |
| 29 |
// associated with the data, consider using the ExclusiveData class instead. |
| 30 |
// Otherwise, ProtectedData should be used to document whatever synchronization |
| 31 |
// method is used. |
| 32 |
|
| 33 |
// Protected data checks are enabled in debug builds, except on android where |
| 34 |
// they cause some permatimeouts in automation. |
| 35 |
#if defined(DEBUG) && !defined(ANDROID) |
| 36 |
# define JS_HAS_PROTECTED_DATA_CHECKS |
| 37 |
#endif |
| 38 |
|
| 39 |
#define DECLARE_ONE_BOOL_OPERATOR(OP, T) \ |
| 40 |
template <typename U> \ |
| 41 |
bool operator OP(const U& other) const { \ |
| 42 |
if constexpr (std::is_integral_v<T>) { \ |
| 43 |
return ref() OP static_cast<T>(other); \ |
| 44 |
} else { \ |
| 45 |
return ref() OP other; \ |
| 46 |
} \ |
| 47 |
} |
| 48 |
|
| 49 |
#define DECLARE_BOOL_OPERATORS(T) \ |
| 50 |
DECLARE_ONE_BOOL_OPERATOR(==, T) \ |
| 51 |
DECLARE_ONE_BOOL_OPERATOR(!=, T) \ |
| 52 |
DECLARE_ONE_BOOL_OPERATOR(<=, T) \ |
| 53 |
DECLARE_ONE_BOOL_OPERATOR(>=, T) \ |
| 54 |
DECLARE_ONE_BOOL_OPERATOR(<, T) \ |
| 55 |
DECLARE_ONE_BOOL_OPERATOR(>, T) |
| 56 |
|
| 57 |
// Mark a region of code that should be treated as single threaded and suppress |
| 58 |
// any ProtectedData checks. |
| 59 |
// |
| 60 |
// Note that in practice there may be multiple threads running when this class |
| 61 |
// is used, due to the presence of multiple runtimes in the process. When each |
| 62 |
// process has only a single runtime this will no longer be a concern. |
| 63 |
class MOZ_RAII AutoNoteSingleThreadedRegion { |
| 64 |
public: |
| 65 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 66 |
static mozilla::Atomic<size_t, mozilla::SequentiallyConsistent> count; |
| 67 |
AutoNoteSingleThreadedRegion() { count++; } |
| 68 |
~AutoNoteSingleThreadedRegion() { count--; } |
| 69 |
#else |
| 70 |
AutoNoteSingleThreadedRegion() {} |
| 71 |
#endif |
| 72 |
}; |
| 73 |
|
| 74 |
// Class for protected data that may be written to any number of times. Checks |
| 75 |
// occur when the data is both read from and written to. |
| 76 |
template <typename Check, typename T> |
| 77 |
class ProtectedData { |
| 78 |
using ThisType = ProtectedData<Check, T>; |
| 79 |
|
| 80 |
public: |
| 81 |
template <typename... Args> |
| 82 |
explicit ProtectedData(const Check& check, Args&&... args) |
| 83 |
: value(std::forward<Args>(args)...) |
| 84 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 85 |
, |
| 86 |
check(check) |
| 87 |
#endif |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
DECLARE_BOOL_OPERATORS(T) |
| 92 |
|
| 93 |
operator const T&() const { return ref(); } |
| 94 |
const T& operator->() const { return ref(); } |
| 95 |
|
| 96 |
template <typename U> |
| 97 |
ThisType& operator=(const U& p) { |
| 98 |
this->ref() = p; |
| 99 |
return *this; |
| 100 |
} |
| 101 |
|
| 102 |
template <typename U> |
| 103 |
ThisType& operator=(U&& p) { |
| 104 |
this->ref() = std::move(p); |
| 105 |
return *this; |
| 106 |
} |
| 107 |
|
| 108 |
template <typename U> |
| 109 |
T& operator+=(const U& rhs) { |
| 110 |
return ref() += rhs; |
| 111 |
} |
| 112 |
template <typename U> |
| 113 |
T& operator-=(const U& rhs) { |
| 114 |
return ref() -= rhs; |
| 115 |
} |
| 116 |
template <typename U> |
| 117 |
T& operator*=(const U& rhs) { |
| 118 |
return ref() *= rhs; |
| 119 |
} |
| 120 |
template <typename U> |
| 121 |
T& operator/=(const U& rhs) { |
| 122 |
return ref() /= rhs; |
| 123 |
} |
| 124 |
template <typename U> |
| 125 |
T& operator&=(const U& rhs) { |
| 126 |
return ref() &= rhs; |
| 127 |
} |
| 128 |
template <typename U> |
| 129 |
T& operator|=(const U& rhs) { |
| 130 |
return ref() |= rhs; |
| 131 |
} |
| 132 |
T& operator++() { return ++ref(); } |
| 133 |
T& operator--() { return --ref(); } |
| 134 |
T operator++(int) { return ref()++; } |
| 135 |
T operator--(int) { return ref()--; } |
| 136 |
|
| 137 |
T& ref() { |
| 138 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 139 |
if (!AutoNoteSingleThreadedRegion::count) { |
| 140 |
check.check(); |
| 141 |
} |
| 142 |
#endif |
| 143 |
return value; |
| 144 |
} |
| 145 |
|
| 146 |
const T& ref() const { |
| 147 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 148 |
if (!AutoNoteSingleThreadedRegion::count) { |
| 149 |
check.check(); |
| 150 |
} |
| 151 |
#endif |
| 152 |
return value; |
| 153 |
} |
| 154 |
|
| 155 |
T& refNoCheck() { return value; } |
| 156 |
const T& refNoCheck() const { return value; } |
| 157 |
|
| 158 |
static size_t offsetOfValue() { return offsetof(ThisType, value); } |
| 159 |
|
| 160 |
private: |
| 161 |
T value; |
| 162 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 163 |
Check check; |
| 164 |
#endif |
| 165 |
}; |
| 166 |
|
| 167 |
// Intermediate class for protected data whose checks take no constructor |
| 168 |
// arguments. |
| 169 |
template <typename Check, typename T> |
| 170 |
class ProtectedDataNoCheckArgs : public ProtectedData<Check, T> { |
| 171 |
using Base = ProtectedData<Check, T>; |
| 172 |
|
| 173 |
public: |
| 174 |
template <typename... Args> |
| 175 |
explicit ProtectedDataNoCheckArgs(Args&&... args) |
| 176 |
: ProtectedData<Check, T>(Check(), std::forward<Args>(args)...) {} |
| 177 |
|
| 178 |
using Base::operator=; |
| 179 |
}; |
| 180 |
|
| 181 |
// Intermediate class for protected data whose checks take a single argument. |
| 182 |
template <typename CheckArg, typename Check, typename T> |
| 183 |
class ProtectedDataWithArg : public ProtectedData<Check, T> { |
| 184 |
using Base = ProtectedData<Check, T>; |
| 185 |
|
| 186 |
public: |
| 187 |
template <typename... Args> |
| 188 |
explicit ProtectedDataWithArg(CheckArg checkArg, Args&&... args) |
| 189 |
: ProtectedData<Check, T>(Check(checkArg), std::forward<Args>(args)...) {} |
| 190 |
|
| 191 |
using Base::operator=; |
| 192 |
}; |
| 193 |
|
| 194 |
template <typename Check, typename T> |
| 195 |
using ProtectedDataContextArg = ProtectedDataWithArg<JSContext*, Check, T>; |
| 196 |
|
| 197 |
template <typename Check, typename T> |
| 198 |
using ProtectedDataMutexArg = ProtectedDataWithArg<const Mutex&, Check, T>; |
| 199 |
|
| 200 |
class CheckUnprotected { |
| 201 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 202 |
public: |
| 203 |
inline void check() const {} |
| 204 |
#endif |
| 205 |
}; |
| 206 |
|
| 207 |
// Data with a no-op check that permits all accesses. This is tantamount to not |
| 208 |
// using ProtectedData at all, but is in place to document points which need |
| 209 |
// to be fixed in order for runtimes to be multithreaded (see bug 1323066). |
| 210 |
template <typename T> |
| 211 |
using UnprotectedData = ProtectedDataNoCheckArgs<CheckUnprotected, T>; |
| 212 |
|
| 213 |
class CheckThreadLocal { |
| 214 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 215 |
ThreadId id; |
| 216 |
|
| 217 |
public: |
| 218 |
CheckThreadLocal() : id(ThreadId::ThisThreadId()) {} |
| 219 |
|
| 220 |
void check() const; |
| 221 |
#endif |
| 222 |
}; |
| 223 |
|
| 224 |
class CheckContextLocal { |
| 225 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 226 |
JSContext* cx_; |
| 227 |
|
| 228 |
public: |
| 229 |
explicit CheckContextLocal(JSContext* cx) : cx_(cx) {} |
| 230 |
|
| 231 |
void check() const; |
| 232 |
#else |
| 233 |
public: |
| 234 |
explicit CheckContextLocal(JSContext* cx) {} |
| 235 |
#endif |
| 236 |
}; |
| 237 |
|
| 238 |
class CheckMutexHeld { |
| 239 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 240 |
const Mutex& mutex_; |
| 241 |
|
| 242 |
public: |
| 243 |
explicit CheckMutexHeld(const Mutex& mutex) : mutex_(mutex) {} |
| 244 |
|
| 245 |
void check() const; |
| 246 |
#else |
| 247 |
public: |
| 248 |
explicit CheckMutexHeld(const Mutex& mutex) {} |
| 249 |
#endif |
| 250 |
}; |
| 251 |
|
| 252 |
// Data which may only be accessed by the thread on which it is created. |
| 253 |
template <typename T> |
| 254 |
using ThreadData = ProtectedDataNoCheckArgs<CheckThreadLocal, T>; |
| 255 |
|
| 256 |
// Data which belongs to a JSContext and should only be accessed from that |
| 257 |
// JSContext's thread. Note that a JSContext may not have a thread currently |
| 258 |
// associated with it and any associated thread may change over time. |
| 259 |
template <typename T> |
| 260 |
using ContextData = ProtectedDataContextArg<CheckContextLocal, T>; |
| 261 |
|
| 262 |
template <typename T> |
| 263 |
using MutexData = ProtectedDataMutexArg<CheckMutexHeld, T>; |
| 264 |
|
| 265 |
// Enum describing which helper threads (GC tasks or Ion compilations) may |
| 266 |
// access data. |
| 267 |
enum class AllowedHelperThread { |
| 268 |
None, |
| 269 |
GCTask, |
| 270 |
IonCompile, |
| 271 |
GCTaskOrIonCompile, |
| 272 |
}; |
| 273 |
|
| 274 |
template <AllowedHelperThread Helper> |
| 275 |
class CheckMainThread { |
| 276 |
public: |
| 277 |
void check() const; |
| 278 |
}; |
| 279 |
|
| 280 |
// Data which may only be accessed by the runtime's main thread. |
| 281 |
template <typename T> |
| 282 |
using MainThreadData = |
| 283 |
ProtectedDataNoCheckArgs<CheckMainThread<AllowedHelperThread::None>, T>; |
| 284 |
|
| 285 |
// Data which may only be accessed by the runtime's main thread or by various |
| 286 |
// helper thread tasks. |
| 287 |
template <typename T> |
| 288 |
using MainThreadOrGCTaskData = |
| 289 |
ProtectedDataNoCheckArgs<CheckMainThread<AllowedHelperThread::GCTask>, T>; |
| 290 |
template <typename T> |
| 291 |
using MainThreadOrIonCompileData = |
| 292 |
ProtectedDataNoCheckArgs<CheckMainThread<AllowedHelperThread::IonCompile>, |
| 293 |
T>; |
| 294 |
template <typename T> |
| 295 |
using MainThreadOrGCTaskOrIonCompileData = ProtectedDataNoCheckArgs< |
| 296 |
CheckMainThread<AllowedHelperThread::GCTaskOrIonCompile>, T>; |
| 297 |
|
| 298 |
// Runtime wide locks which might protect some data. |
| 299 |
enum class GlobalLock { GCLock, HelperThreadLock }; |
| 300 |
|
| 301 |
template <GlobalLock Lock, AllowedHelperThread Helper> |
| 302 |
class CheckGlobalLock { |
| 303 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 304 |
public: |
| 305 |
void check() const; |
| 306 |
#endif |
| 307 |
}; |
| 308 |
|
| 309 |
// Data which may only be accessed while holding the GC lock. |
| 310 |
template <typename T> |
| 311 |
using GCLockData = ProtectedDataNoCheckArgs< |
| 312 |
CheckGlobalLock<GlobalLock::GCLock, AllowedHelperThread::None>, T>; |
| 313 |
|
| 314 |
// Data which may only be accessed while holding the helper thread lock. |
| 315 |
template <typename T> |
| 316 |
using HelperThreadLockData = ProtectedDataNoCheckArgs< |
| 317 |
CheckGlobalLock<GlobalLock::HelperThreadLock, AllowedHelperThread::None>, |
| 318 |
T>; |
| 319 |
|
| 320 |
// Class for protected data that is only written to once. 'const' may sometimes |
| 321 |
// be usable instead of this class, but in cases where the data cannot be set |
| 322 |
// to its final value in its constructor this class is helpful. Protected data |
| 323 |
// checking only occurs when writes are performed, not reads. Steps may need to |
| 324 |
// be taken to ensure that reads do not occur until the written value is fully |
| 325 |
// initialized, as such guarantees are not provided by this class. |
| 326 |
template <typename Check, typename T> |
| 327 |
class ProtectedDataWriteOnce { |
| 328 |
using ThisType = ProtectedDataWriteOnce<Check, T>; |
| 329 |
|
| 330 |
public: |
| 331 |
template <typename... Args> |
| 332 |
explicit ProtectedDataWriteOnce(Args&&... args) |
| 333 |
: value(std::forward<Args>(args)...) |
| 334 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 335 |
, |
| 336 |
nwrites(0) |
| 337 |
#endif |
| 338 |
{ |
| 339 |
} |
| 340 |
|
| 341 |
DECLARE_BOOL_OPERATORS(T) |
| 342 |
|
| 343 |
operator const T&() const { return ref(); } |
| 344 |
const T& operator->() const { return ref(); } |
| 345 |
|
| 346 |
template <typename U> |
| 347 |
ThisType& operator=(const U& p) { |
| 348 |
if (ref() != p) { |
| 349 |
this->writeRef() = p; |
| 350 |
} |
| 351 |
return *this; |
| 352 |
} |
| 353 |
|
| 354 |
const T& ref() const { return value; } |
| 355 |
|
| 356 |
T& writeRef() { |
| 357 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 358 |
if (!AutoNoteSingleThreadedRegion::count) { |
| 359 |
check.check(); |
| 360 |
} |
| 361 |
// Despite the WriteOnce name, actually allow two writes to accommodate |
| 362 |
// data that is cleared during teardown. |
| 363 |
MOZ_ASSERT(++nwrites <= 2); |
| 364 |
#endif |
| 365 |
return value; |
| 366 |
} |
| 367 |
|
| 368 |
private: |
| 369 |
T value; |
| 370 |
#ifdef JS_HAS_PROTECTED_DATA_CHECKS |
| 371 |
Check check; |
| 372 |
size_t nwrites; |
| 373 |
#endif |
| 374 |
}; |
| 375 |
|
| 376 |
// Data that is written once with no requirements for exclusive access when |
| 377 |
// that write occurs. |
| 378 |
template <typename T> |
| 379 |
using WriteOnceData = ProtectedDataWriteOnce<CheckUnprotected, T>; |
| 380 |
|
| 381 |
#undef DECLARE_ASSIGNMENT_OPERATOR |
| 382 |
#undef DECLARE_ONE_BOOL_OPERATOR |
| 383 |
#undef DECLARE_BOOL_OPERATORS |
| 384 |
|
| 385 |
} // namespace js |
| 386 |
|
| 387 |
#endif // threading_ProtectedData_h |