/src/yoga/yoga/event/event.cpp
Line | Count | Source |
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 | | #include "event.h" |
9 | | #include <atomic> |
10 | | #include <memory> |
11 | | |
12 | | namespace facebook::yoga { |
13 | | |
14 | 0 | const char* LayoutPassReasonToString(const LayoutPassReason value) { |
15 | 0 | switch (value) { |
16 | 0 | case LayoutPassReason::kInitial: |
17 | 0 | return "initial"; |
18 | 0 | case LayoutPassReason::kAbsLayout: |
19 | 0 | return "abs_layout"; |
20 | 0 | case LayoutPassReason::kStretch: |
21 | 0 | return "stretch"; |
22 | 0 | case LayoutPassReason::kMultilineStretch: |
23 | 0 | return "multiline_stretch"; |
24 | 0 | case LayoutPassReason::kFlexLayout: |
25 | 0 | return "flex_layout"; |
26 | 0 | case LayoutPassReason::kMeasureChild: |
27 | 0 | return "measure"; |
28 | 0 | case LayoutPassReason::kAbsMeasureChild: |
29 | 0 | return "abs_measure"; |
30 | 0 | case LayoutPassReason::kFlexMeasure: |
31 | 0 | return "flex_measure"; |
32 | 0 | case LayoutPassReason::kGridLayout: |
33 | 0 | return "grid_layout"; |
34 | 0 | default: |
35 | 0 | return "unknown"; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | | namespace { |
40 | | |
41 | | struct Node { |
42 | | std::function<Event::Subscriber> subscriber = nullptr; |
43 | | Node* next = nullptr; |
44 | | |
45 | | explicit Node(std::function<Event::Subscriber>&& subscriber) |
46 | 0 | : subscriber{std::move(subscriber)} {} |
47 | | }; |
48 | | |
49 | | std::atomic<Node*> subscribers{nullptr}; |
50 | | |
51 | 0 | Node* push(Node* newHead) { |
52 | 0 | Node* oldHead = nullptr; |
53 | 0 | do { |
54 | 0 | oldHead = subscribers.load(std::memory_order_relaxed); |
55 | 0 | if (newHead != nullptr) { |
56 | 0 | newHead->next = oldHead; |
57 | 0 | } |
58 | 0 | } while (!subscribers.compare_exchange_weak( |
59 | 0 | oldHead, newHead, std::memory_order_release, std::memory_order_relaxed)); |
60 | 0 | return oldHead; |
61 | 0 | } |
62 | | |
63 | | } // namespace |
64 | | |
65 | 0 | void Event::reset() { |
66 | 0 | auto head = push(nullptr); |
67 | 0 | while (head != nullptr) { |
68 | 0 | auto current = head; |
69 | 0 | head = head->next; |
70 | 0 | delete current; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | 0 | void Event::subscribe(std::function<Subscriber>&& subscriber) { |
75 | 0 | push(new Node{std::move(subscriber)}); |
76 | 0 | } |
77 | | |
78 | | void Event::publish( |
79 | | YGNodeConstRef node, |
80 | | Type eventType, |
81 | 33.3M | const Data& eventData) { |
82 | 33.3M | for (auto subscriber = subscribers.load(std::memory_order_relaxed); |
83 | 33.3M | subscriber != nullptr; |
84 | 33.3M | subscriber = subscriber->next) { |
85 | 0 | subscriber->subscriber(node, eventType, eventData); |
86 | 0 | } |
87 | 33.3M | } |
88 | | |
89 | | } // namespace facebook::yoga |