1"""Test function call thread safety."""
2
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestSafeFuncCalls(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    @skipUnlessDarwin
16    @add_test_categories(['pyapi'])
17    def test_with_python_api(self):
18        """Test function call thread safety."""
19        self.build()
20        exe = self.getBuildArtifact("a.out")
21
22        target = self.dbg.CreateTarget(exe)
23        self.assertTrue(target, VALID_TARGET)
24        self.main_source_spec = lldb.SBFileSpec("main.c")
25        break1 = target.BreakpointCreateByName("stopper", 'a.out')
26        self.assertTrue(break1, VALID_BREAKPOINT)
27        process = target.LaunchSimple(
28            None, None, self.get_process_working_directory())
29        self.assertTrue(process, PROCESS_IS_VALID)
30        threads = lldbutil.get_threads_stopped_at_breakpoint(process, break1)
31        self.assertEqual(len(threads), 1, "Failed to stop at breakpoint 1.")
32
33        self.assertEqual(
34            process.GetNumThreads(), 2,
35            "Check that the process has two threads when sitting at the stopper() breakpoint")
36
37        main_thread = lldb.SBThread()
38        select_thread = lldb.SBThread()
39        for idx in range(0, process.GetNumThreads()):
40            t = process.GetThreadAtIndex(idx)
41            if t.GetName() == "main thread":
42                main_thread = t
43            if t.GetName() == "select thread":
44                select_thread = t
45
46        self.assertTrue(
47            main_thread.IsValid() and select_thread.IsValid(),
48            "Got both expected threads")
49
50        self.assertTrue(main_thread.SafeToCallFunctions(),
51                        "It is safe to call functions on the main thread")
52        self.assertEqual(
53            select_thread.SafeToCallFunctions(), False,
54            "It is not safe to call functions on the select thread")
55