• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 23669027368

27 Mar 2026 09:50PM UTC coverage: 26.198% (+0.02%) from 26.176%
23669027368

push

github

reinder
Merge remote-tracking branch 'origin/master' into cbus

11 of 144 new or added lines in 34 files covered. (7.64%)

1 existing line in 1 file now uncovered.

8256 of 31514 relevant lines covered (26.2%)

182.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.0
/server/src/core/eventloop.hpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2019-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#ifndef TRAINTASTIC_SERVER_CORE_EVENTLOOP_HPP
23
#define TRAINTASTIC_SERVER_CORE_EVENTLOOP_HPP
24

25
#include <thread>
26
#include <boost/asio/io_context.hpp>
27
#include <boost/asio/post.hpp>
28

29
class EventLoop
30
{
31
  private:
32
    EventLoop() = default;
33
    ~EventLoop() = default;
34

35
    EventLoop(const EventLoop&) = delete;
36
    EventLoop& operator =(const EventLoop&) = delete;
37

38
    inline static std::unique_ptr<boost::asio::io_context> s_ioContext;
39
    inline static std::shared_ptr<boost::asio::executor_work_guard<decltype(s_ioContext->get_executor())>> s_keepAlive;
40

41
  public:
42
#ifdef TRAINTASTIC_TEST
43
    inline static std::thread::id threadId;
44
#else
45
    inline static const std::thread::id threadId = std::this_thread::get_id();
46
#endif
47

48
    static boost::asio::io_context& ioContext()
296✔
49
    {
50
      assert(s_ioContext);
296✔
51
      return *s_ioContext;
296✔
52
    }
53

54
    static void reset()
208✔
55
    {
56
      s_ioContext = std::make_unique<boost::asio::io_context>();
208✔
57
    }
208✔
58

59
    static void exec()
×
60
    {
61
#ifdef TRAINTASTIC_TEST
62
      threadId = std::this_thread::get_id();
×
63
#endif
NEW
64
      s_keepAlive = std::make_shared<boost::asio::executor_work_guard<decltype(ioContext().get_executor())>>(ioContext().get_executor());
×
65
      ioContext().run();
×
66
      s_keepAlive.reset();
×
67
    }
×
68

69
    static void stop()
×
70
    {
71
      s_keepAlive.reset();
×
72
    }
×
73

74
    template<typename _Callable, typename... _Args>
75
    inline static void call(_Callable&& __f, _Args&&... __args)
×
76
    {
NEW
77
      boost::asio::post(ioContext(), std::bind(__f, __args...));
×
78
    }
×
79

80
    template<typename T>
81
    inline static void deleteLater(T* object)
×
82
    {
NEW
83
      boost::asio::post(ioContext(),
×
84
        [object]()
×
85
        {
86
          delete object;
×
87
        });
88
    }
×
89
};
90

91
inline bool isEventLoopThread()
119✔
92
{
93
  return std::this_thread::get_id() == EventLoop::threadId;
119✔
94
}
95

96
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc