GDB (xrefs)
Loading...
Searching...
No Matches
main-thread-selftests.c
Go to the documentation of this file.
1/* Self tests for run_on_main_thread
2
3 Copyright (C) 2019-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (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, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "gdbsupport/selftest.h"
22#include "gdbsupport/block-signals.h"
23#include "gdbsupport/scope-exit.h"
24#include "run-on-main-thread.h"
25#include "gdbsupport/event-loop.h"
26#if CXX_STD_THREAD
27#include <thread>
28#endif
29
30namespace selftests {
31namespace main_thread_tests {
32
33#if CXX_STD_THREAD
34
35static bool done;
36
37static void
38set_done ()
39{
41 {
42 done = true;
43 });
44}
45
46static void
47run_tests ()
48{
49 std::thread thread;
50
51 done = false;
52
53 {
54 gdb::block_signals blocker;
55
56 SCOPE_EXIT
57 {
58 if (thread.joinable ())
59 thread.join ();
60 };
61 thread = std::thread (set_done);
62 }
63
64 while (!done && gdb_do_one_event () >= 0)
65 ;
66
67 /* Actually the test will just hang, but we want to test
68 something. */
69 SELF_CHECK (done);
70}
71
72#endif
73
74}
75}
76
78void
80{
81#if CXX_STD_THREAD
82 selftests::register_test ("run_on_main_thread",
83 selftests::main_thread_tests::run_tests);
84#endif
85}
void _initialize_main_thread_selftests()
void run_on_main_thread(std::function< void()> &&func)