xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/unittests/main-thread-selftests.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
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 
30 namespace selftests {
31 namespace main_thread_tests {
32 
33 #if CXX_STD_THREAD
34 
35 static bool done;
36 
37 static void
38 set_done ()
39 {
40   run_on_main_thread ([] ()
41     {
42       done = true;
43     });
44 }
45 
46 static void
47 run_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 
77 void _initialize_main_thread_selftests ();
78 void
79 _initialize_main_thread_selftests ()
80 {
81 #if CXX_STD_THREAD
82   selftests::register_test ("run_on_main_thread",
83 			    selftests::main_thread_tests::run_tests);
84 #endif
85 }
86