react-native-windows 0.80.5 → 0.80.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,79 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include <mutex>
11
+
12
+ #include <jsi/decorator.h>
13
+ #include <jsi/jsi.h>
14
+
15
+ namespace facebook {
16
+ namespace jsi {
17
+
18
+ class ThreadSafeRuntime : public Runtime {
19
+ public:
20
+ virtual void lock() const = 0;
21
+ virtual void unlock() const = 0;
22
+ virtual Runtime& getUnsafeRuntime() = 0;
23
+ };
24
+
25
+ namespace detail {
26
+
27
+ template <typename R, typename L>
28
+ struct WithLock {
29
+ L lock;
30
+ WithLock(R& r) : lock(r) {}
31
+ void before() {
32
+ lock.lock();
33
+ }
34
+ void after() {
35
+ lock.unlock();
36
+ }
37
+ };
38
+
39
+ // The actual implementation of a given ThreadSafeRuntime. It's parameterized
40
+ // by:
41
+ //
42
+ // - R: The actual Runtime type that this wraps
43
+ // - L: A lock type that has three members:
44
+ // - L(R& r) // ctor
45
+ // - void lock()
46
+ // - void unlock()
47
+ template <typename R, typename L>
48
+ class ThreadSafeRuntimeImpl final
49
+ : public WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime> {
50
+ public:
51
+ template <typename... Args>
52
+ ThreadSafeRuntimeImpl(Args&&... args)
53
+ : WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>(
54
+ unsafe_,
55
+ lock_),
56
+ unsafe_(std::forward<Args>(args)...),
57
+ lock_(unsafe_) {}
58
+
59
+ R& getUnsafeRuntime() override {
60
+ return WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>::plain();
61
+ }
62
+
63
+ void lock() const override {
64
+ lock_.before();
65
+ }
66
+
67
+ void unlock() const override {
68
+ lock_.after();
69
+ }
70
+
71
+ private:
72
+ R unsafe_;
73
+ mutable WithLock<R, L> lock_;
74
+ };
75
+
76
+ } // namespace detail
77
+
78
+ } // namespace jsi
79
+ } // namespace facebook