aboutsummaryrefslogtreecommitdiff
path: root/src/tracker.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tracker.cpp')
-rw-r--r--src/tracker.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/tracker.cpp b/src/tracker.cpp
new file mode 100644
index 0000000..e6affb2
--- /dev/null
+++ b/src/tracker.cpp
@@ -0,0 +1,108 @@
1/*
2===============================================================================
3
4Copyright (C) 2024 Benoit Germain <bnt.germain@gmail.com>
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22THE SOFTWARE.
23
24===============================================================================
25*/
26#include "tracker.h"
27#include "lanes_private.h"
28
29// #################################################################################################
30
31#if HAVE_LANE_TRACKING()
32
33/*
34 * Add the lane to tracking chain; the ones still running at the end of the
35 * whole process will be cancelled.
36 */
37void LaneTracker::tracking_add(Lane* lane_)
38{
39 if (!isActive()) {
40 return;
41 }
42 std::lock_guard<std::mutex> _guard{ trackingMutex };
43 assert(lane_->tracking_next == nullptr);
44
45 lane_->tracking_next = trackingFirst;
46 trackingFirst = lane_;
47}
48
49// #################################################################################################
50
51/*
52 * A free-running lane has ended; remove it from tracking chain
53 */
54[[nodiscard]] bool LaneTracker::tracking_remove(Lane* lane_)
55{
56 if (!isActive()) {
57 return false;
58 }
59
60 bool _found{ false };
61 std::lock_guard<std::mutex> _guard{ trackingMutex };
62 // Make sure (within the MUTEX) that we actually are in the chain
63 // still (at process exit they will remove us from chain and then
64 // cancel/kill).
65 //
66 if (lane_->tracking_next != nullptr) {
67 Lane** _ref = (Lane**) &trackingFirst;
68
69 while (*_ref != TRACKING_END) {
70 if (*_ref == lane_) {
71 *_ref = lane_->tracking_next;
72 lane_->tracking_next = nullptr;
73 _found = true;
74 break;
75 }
76 _ref = (Lane**) &((*_ref)->tracking_next);
77 }
78 assert(_found);
79 }
80 return _found;
81}
82
83// ################################################################################################
84
85[[nodiscard]] int LaneTracker::pushThreadsTable(lua_State* L_) const
86{
87 int const _top{ lua_gettop(L_) };
88 // List _all_ still running threads
89 std::lock_guard<std::mutex> _guard{ trackingMutex };
90 if (trackingFirst && trackingFirst != TRACKING_END) {
91 Lane* _lane{ trackingFirst };
92 int _index{ 0 };
93 lua_newtable(L_); // L_: {}
94 while (_lane != TRACKING_END) {
95 // insert a { name='<name>', status='<status>' } tuple, so that several lanes with the same name can't clobber each other
96 lua_createtable(L_, 0, 2); // L_: {} {}
97 lua_pushstring(L_, _lane->debugName); // L_: {} {} "name"
98 lua_setfield(L_, -2, "name"); // L_: {} {}
99 _lane->pushThreadStatus(L_); // L_: {} {} "status"
100 lua_setfield(L_, -2, "status"); // L_: {} {}
101 lua_rawseti(L_, -2, ++_index); // L_: {}
102 _lane = _lane->tracking_next;
103 }
104 }
105 return lua_gettop(L_) - _top; // L_: 0 or 1
106}
107
108#endif // HAVE_LANE_TRACKING()