summaryrefslogtreecommitdiff
path: root/packages/Python/lldbsuite/test/python_api/lldbutil
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2019-08-20 18:01:57 +0000
committerDimitry Andric <dim@FreeBSD.org>2019-08-20 18:01:57 +0000
commit88c643b6fec27eec436c8d138fee6346e92337d6 (patch)
tree82cd13b2f3cde1c9e5f79689ba4e6ba67694843f /packages/Python/lldbsuite/test/python_api/lldbutil
parent94994d372d014ce4c8758b9605d63fae651bd8aa (diff)
Notes
Diffstat (limited to 'packages/Python/lldbsuite/test/python_api/lldbutil')
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/frame/Makefile6
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/frame/TestFrameUtils.py64
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/frame/main.c47
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/iter/Makefile8
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestLLDBIterator.py129
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestRegistersIterator.py109
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/iter/main.cpp134
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/process/Makefile8
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/process/TestPrintStackTraces.py63
-rw-r--r--packages/Python/lldbsuite/test/python_api/lldbutil/process/main.cpp136
10 files changed, 0 insertions, 704 deletions
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/Makefile b/packages/Python/lldbsuite/test/python_api/lldbutil/frame/Makefile
deleted file mode 100644
index 69b74b5d7531..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/Makefile
+++ /dev/null
@@ -1,6 +0,0 @@
-LEVEL = ../../../make
-
-C_SOURCES := main.c
-MAKE_DSYM :=NO
-
-include $(LEVEL)/Makefile.rules
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/TestFrameUtils.py b/packages/Python/lldbsuite/test/python_api/lldbutil/frame/TestFrameUtils.py
deleted file mode 100644
index 4db6322e7513..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/TestFrameUtils.py
+++ /dev/null
@@ -1,64 +0,0 @@
-"""
-Test utility functions for the frame object.
-"""
-
-from __future__ import print_function
-
-
-import os
-import lldb
-from lldbsuite.test.decorators import *
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test import lldbutil
-
-
-class FrameUtilsTestCase(TestBase):
-
- mydir = TestBase.compute_mydir(__file__)
-
- def setUp(self):
- # Call super's setUp().
- TestBase.setUp(self)
- # Find the line number to break inside main().
- self.line = line_number('main.c',
- "// Find the line number here.")
-
- @add_test_categories(['pyapi'])
- def test_frame_utils(self):
- """Test utility functions for the frame object."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- # Now launch the process, and do not stop at entry point.
- process = target.LaunchSimple(
- None, None, self.get_process_working_directory())
-
- if not process:
- self.fail("SBTarget.LaunchProcess() failed")
- self.assertTrue(process.GetState() == lldb.eStateStopped,
- PROCESS_STOPPED)
-
- import lldbsuite.test.lldbutil as lldbutil
- thread = lldbutil.get_stopped_thread(
- process, lldb.eStopReasonBreakpoint)
- self.assertTrue(thread)
- frame0 = thread.GetFrameAtIndex(0)
- self.assertTrue(frame0)
- frame1 = thread.GetFrameAtIndex(1)
- self.assertTrue(frame1)
- parent = lldbutil.get_parent_frame(frame0)
- self.assertTrue(parent and parent.GetFrameID() == frame1.GetFrameID())
- frame0_args = lldbutil.get_args_as_string(frame0)
- parent_args = lldbutil.get_args_as_string(parent)
- self.assertTrue(
- frame0_args and parent_args and "(int)val=1" in frame0_args)
- if self.TraceOn():
- lldbutil.print_stacktrace(thread)
- print("Current frame: %s" % frame0_args)
- print("Parent frame: %s" % parent_args)
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/main.c b/packages/Python/lldbsuite/test/python_api/lldbutil/frame/main.c
deleted file mode 100644
index e6eeef5b46d9..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/frame/main.c
+++ /dev/null
@@ -1,47 +0,0 @@
-//===-- main.c --------------------------------------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-#include <stdio.h>
-
-int a(int);
-int b(int);
-int c(int);
-
-int a(int val)
-{
- if (val <= 1)
- return b(val);
- else if (val >= 3)
- return c(val);
-
- return val;
-}
-
-int b(int val)
-{
- return c(val);
-}
-
-int c(int val)
-{
- return val + 3; // Find the line number here.
-}
-
-int main (int argc, char const *argv[])
-{
- int A1 = a(1); // a(1) -> b(1) -> c(1)
- printf("a(1) returns %d\n", A1);
-
- int B2 = b(2); // b(2) -> c(2)
- printf("b(2) returns %d\n", B2);
-
- int A3 = a(3); // a(3) -> c(3)
- printf("a(3) returns %d\n", A3);
-
- return 0;
-}
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/Makefile b/packages/Python/lldbsuite/test/python_api/lldbutil/iter/Makefile
deleted file mode 100644
index 051354123495..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-LEVEL = ../../../make
-
-CFLAGS_EXTRAS += -D__STDC_LIMIT_MACROS
-ENABLE_THREADS := YES
-CXX_SOURCES := main.cpp
-MAKE_DSYM := NO
-
-include $(LEVEL)/Makefile.rules
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestLLDBIterator.py b/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestLLDBIterator.py
deleted file mode 100644
index 6816b0d1ee95..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestLLDBIterator.py
+++ /dev/null
@@ -1,129 +0,0 @@
-"""
-Test the iteration protocol for some lldb container objects.
-"""
-
-from __future__ import print_function
-
-
-import os
-import time
-import re
-import lldb
-from lldbsuite.test.decorators import *
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test import lldbutil
-
-
-class LLDBIteratorTestCase(TestBase):
-
- mydir = TestBase.compute_mydir(__file__)
-
- def setUp(self):
- # Call super's setUp().
- TestBase.setUp(self)
- # Find the line numbers to break inside main().
- self.line1 = line_number(
- 'main.cpp', '// Set break point at this line.')
- self.line2 = line_number('main.cpp', '// And that line.')
-
- @add_test_categories(['pyapi'])
- def test_lldb_iter_module(self):
- """Test module_iter works correctly for SBTarget -> SBModule."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- # Now launch the process, and do not stop at entry point.
- process = target.LaunchSimple(
- None, None, self.get_process_working_directory())
-
- if not process:
- self.fail("SBTarget.LaunchProcess() failed")
-
- from lldbsuite.test.lldbutil import get_description
- yours = []
- for i in range(target.GetNumModules()):
- yours.append(target.GetModuleAtIndex(i))
- mine = []
- for m in target.module_iter():
- mine.append(m)
-
- self.assertTrue(len(yours) == len(mine))
- for i in range(len(yours)):
- if self.TraceOn():
- print("yours[%d]='%s'" % (i, get_description(yours[i])))
- print("mine[%d]='%s'" % (i, get_description(mine[i])))
- self.assertTrue(
- yours[i] == mine[i],
- "UUID+FileSpec of yours[{0}] and mine[{0}] matches".format(i))
-
- @add_test_categories(['pyapi'])
- def test_lldb_iter_breakpoint(self):
- """Test breakpoint_iter works correctly for SBTarget -> SBBreakpoint."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line2)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- self.assertTrue(target.GetNumBreakpoints() == 2)
-
- from lldbsuite.test.lldbutil import get_description
- yours = []
- for i in range(target.GetNumBreakpoints()):
- yours.append(target.GetBreakpointAtIndex(i))
- mine = []
- for b in target.breakpoint_iter():
- mine.append(b)
-
- self.assertTrue(len(yours) == len(mine))
- for i in range(len(yours)):
- if self.TraceOn():
- print("yours[%d]='%s'" % (i, get_description(yours[i])))
- print("mine[%d]='%s'" % (i, get_description(mine[i])))
- self.assertTrue(yours[i] == mine[i],
- "ID of yours[{0}] and mine[{0}] matches".format(i))
-
- @add_test_categories(['pyapi'])
- def test_lldb_iter_frame(self):
- """Test iterator works correctly for SBProcess->SBThread->SBFrame."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- # Now launch the process, and do not stop at entry point.
- process = target.LaunchSimple(
- None, None, self.get_process_working_directory())
-
- if not process:
- self.fail("SBTarget.LaunchProcess() failed")
-
- from lldbsuite.test.lldbutil import print_stacktrace
- stopped_due_to_breakpoint = False
- for thread in process:
- if self.TraceOn():
- print_stacktrace(thread)
- ID = thread.GetThreadID()
- if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
- stopped_due_to_breakpoint = True
- for frame in thread:
- self.assertTrue(frame.GetThread().GetThreadID() == ID)
- if self.TraceOn():
- print(frame)
-
- self.assertTrue(stopped_due_to_breakpoint)
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestRegistersIterator.py b/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestRegistersIterator.py
deleted file mode 100644
index bd46749d6e64..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/TestRegistersIterator.py
+++ /dev/null
@@ -1,109 +0,0 @@
-"""
-Test the iteration protocol for frame registers.
-"""
-
-from __future__ import print_function
-
-
-import os
-import time
-import re
-import lldb
-from lldbsuite.test.decorators import *
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test import lldbutil
-
-
-class RegistersIteratorTestCase(TestBase):
-
- mydir = TestBase.compute_mydir(__file__)
-
- def setUp(self):
- # Call super's setUp().
- TestBase.setUp(self)
- # Find the line number to break inside main().
- self.line1 = line_number(
- 'main.cpp', '// Set break point at this line.')
-
- @add_test_categories(['pyapi'])
- @expectedFailureAll(oslist=["windows"])
- def test_iter_registers(self):
- """Test iterator works correctly for lldbutil.iter_registers()."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- # Now launch the process, and do not stop at entry point.
- process = target.LaunchSimple(
- None, None, self.get_process_working_directory())
-
- if not process:
- self.fail("SBTarget.LaunchProcess() failed")
-
- import lldbsuite.test.lldbutil as lldbutil
- for thread in process:
- if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
- for frame in thread:
- # Dump the registers of this frame using
- # lldbutil.get_GPRs() and friends.
- if self.TraceOn():
- print(frame)
-
- REGs = lldbutil.get_GPRs(frame)
- num = len(REGs)
- if self.TraceOn():
- print(
- "\nNumber of general purpose registers: %d" %
- num)
- for reg in REGs:
- self.assertTrue(reg)
- if self.TraceOn():
- print("%s => %s" % (reg.GetName(), reg.GetValue()))
-
- REGs = lldbutil.get_FPRs(frame)
- num = len(REGs)
- if self.TraceOn():
- print("\nNumber of floating point registers: %d" % num)
- for reg in REGs:
- self.assertTrue(reg)
- if self.TraceOn():
- print("%s => %s" % (reg.GetName(), reg.GetValue()))
-
- REGs = lldbutil.get_ESRs(frame)
- if self.platformIsDarwin():
- if self.getArchitecture() != 'armv7' and self.getArchitecture() != 'armv7k':
- num = len(REGs)
- if self.TraceOn():
- print(
- "\nNumber of exception state registers: %d" %
- num)
- for reg in REGs:
- self.assertTrue(reg)
- if self.TraceOn():
- print(
- "%s => %s" %
- (reg.GetName(), reg.GetValue()))
- else:
- self.assertIsNone(REGs)
-
- # And these should also work.
- for kind in ["General Purpose Registers",
- "Floating Point Registers"]:
- REGs = lldbutil.get_registers(frame, kind)
- self.assertTrue(REGs)
-
- REGs = lldbutil.get_registers(
- frame, "Exception State Registers")
- if self.platformIsDarwin():
- if self.getArchitecture() != 'armv7' and self.getArchitecture() != 'armv7k':
- self.assertIsNotNone(REGs)
- else:
- self.assertIsNone(REGs)
-
- # We've finished dumping the registers for frame #0.
- break
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/main.cpp b/packages/Python/lldbsuite/test/python_api/lldbutil/iter/main.cpp
deleted file mode 100644
index 8fb45f94b1ef..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/iter/main.cpp
+++ /dev/null
@@ -1,134 +0,0 @@
-//===-- main.cpp ------------------------------------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-// C includes
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-
-// C++ includes
-#include <chrono>
-#include <mutex>
-#include <random>
-#include <thread>
-
-std::thread g_thread_1;
-std::thread g_thread_2;
-std::thread g_thread_3;
-std::mutex g_mask_mutex;
-
-typedef enum {
- eGet,
- eAssign,
- eClearBits
-} MaskAction;
-
-uint32_t mask_access (MaskAction action, uint32_t mask = 0);
-
-uint32_t
-mask_access (MaskAction action, uint32_t mask)
-{
- static uint32_t g_mask = 0;
-
- std::lock_guard<std::mutex> lock(g_mask_mutex);
- switch (action)
- {
- case eGet:
- break;
-
- case eAssign:
- g_mask |= mask;
- break;
-
- case eClearBits:
- g_mask &= ~mask;
- break;
- }
- return g_mask;
-}
-
-void *
-thread_func (void *arg)
-{
- uint32_t thread_index = *((uint32_t *)arg);
- uint32_t thread_mask = (1u << (thread_index));
- printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
-
- std::default_random_engine generator;
- std::uniform_int_distribution<int> distribution(0, 3000000);
-
- while (mask_access(eGet) & thread_mask)
- {
- // random micro second sleep from zero to 3 seconds
- int usec = distribution(generator);
- printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
-
- std::chrono::microseconds duration(usec);
- std::this_thread::sleep_for(duration);
- printf ("%s (thread = %u) after usleep ...\n", __FUNCTION__, thread_index); // Set break point at this line.
- }
- printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
- return NULL;
-}
-
-
-int main (int argc, char const *argv[])
-{
- uint32_t thread_index_1 = 1;
- uint32_t thread_index_2 = 2;
- uint32_t thread_index_3 = 3;
- uint32_t thread_mask_1 = (1u << thread_index_1);
- uint32_t thread_mask_2 = (1u << thread_index_2);
- uint32_t thread_mask_3 = (1u << thread_index_3);
-
- // Make a mask that will keep all threads alive
- mask_access (eAssign, thread_mask_1 | thread_mask_2 | thread_mask_3); // And that line.
-
- // Create 3 threads
- g_thread_1 = std::thread(thread_func, (void*)&thread_index_1);
- g_thread_2 = std::thread(thread_func, (void*)&thread_index_2);
- g_thread_3 = std::thread(thread_func, (void*)&thread_index_3);
-
- char line[64];
- while (mask_access(eGet) != 0)
- {
- printf ("Enter thread index to kill or ENTER for all:\n");
- fflush (stdout);
- // Kill threads by index, or ENTER for all threads
-
- if (fgets (line, sizeof(line), stdin))
- {
- if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0')
- {
- printf ("Exiting all threads...\n");
- break;
- }
- int32_t index = strtoul (line, NULL, 0);
- switch (index)
- {
- case 1: mask_access (eClearBits, thread_mask_1); break;
- case 2: mask_access (eClearBits, thread_mask_2); break;
- case 3: mask_access (eClearBits, thread_mask_3); break;
- }
- continue;
- }
-
- break;
- }
-
- // Clear all thread bits to they all exit
- mask_access (eClearBits, UINT32_MAX);
-
- // Join all of our threads
- g_thread_1.join();
- g_thread_2.join();
- g_thread_3.join();
-
- return 0;
-}
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/process/Makefile b/packages/Python/lldbsuite/test/python_api/lldbutil/process/Makefile
deleted file mode 100644
index 93fc28b4ee07..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/process/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-LEVEL = ../../../make
-
-CFLAGS_EXTRAS += -D__STDC_LIMIT_MACROS
-ENABLE_THREADS := YES
-CXX_SOURCES := main.cpp
-MAKE_DSYM :=NO
-
-include $(LEVEL)/Makefile.rules
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/process/TestPrintStackTraces.py b/packages/Python/lldbsuite/test/python_api/lldbutil/process/TestPrintStackTraces.py
deleted file mode 100644
index dcdade25d46f..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/process/TestPrintStackTraces.py
+++ /dev/null
@@ -1,63 +0,0 @@
-"""
-Test SBprocess and SBThread APIs with printing of the stack traces using lldbutil.
-"""
-
-from __future__ import print_function
-
-
-import os
-import time
-import re
-import lldb
-from lldbsuite.test.decorators import *
-from lldbsuite.test.lldbtest import *
-from lldbsuite.test import lldbutil
-
-
-class ThreadsStackTracesTestCase(TestBase):
-
- mydir = TestBase.compute_mydir(__file__)
-
- def setUp(self):
- # Call super's setUp().
- TestBase.setUp(self)
- # Find the line number to break inside main().
- self.line = line_number('main.cpp', '// Set break point at this line.')
-
- # We are unable to produce a backtrace of the main thread when the thread
- # is blocked in fgets
- @expectedFailureAll("llvm.org/pr23043", ["linux"], archs=["i386"])
- # The __thread_start function in libc doesn't contain any epilogue and prologue instructions
- # hence unwinding fail when we are stopped in __thread_start
- @expectedFailureAll(triple='mips*')
- @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
- @expectedFlakeyAndroid("llvm.org/26492", archs=["arm"])
- @expectedFlakeyLinux("llvm.org/pr27687")
- @add_test_categories(['pyapi'])
- def test_stack_traces(self):
- """Test SBprocess and SBThread APIs with printing of the stack traces."""
- self.build()
- exe = self.getBuildArtifact("a.out")
-
- target = self.dbg.CreateTarget(exe)
- self.assertTrue(target, VALID_TARGET)
-
- breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
- self.assertTrue(breakpoint, VALID_BREAKPOINT)
-
- # Now launch the process, and do not stop at entry point.
- process = target.LaunchSimple(
- ["abc", "xyz"], None, self.get_process_working_directory())
-
- if not process:
- self.fail("SBTarget.LaunchProcess() failed")
-
- import lldbsuite.test.lldbutil as lldbutil
- if process.GetState() != lldb.eStateStopped:
- self.fail("Process should be in the 'stopped' state, "
- "instead the actual state is: '%s'" %
- lldbutil.state_type_to_str(process.GetState()))
-
- stacktraces = lldbutil.print_stacktraces(process, string_buffer=True)
- self.expect(stacktraces, exe=False,
- substrs=['(int)argc=3'])
diff --git a/packages/Python/lldbsuite/test/python_api/lldbutil/process/main.cpp b/packages/Python/lldbsuite/test/python_api/lldbutil/process/main.cpp
deleted file mode 100644
index 6b87c3d649e6..000000000000
--- a/packages/Python/lldbsuite/test/python_api/lldbutil/process/main.cpp
+++ /dev/null
@@ -1,136 +0,0 @@
-//===-- main.cpp ------------------------------------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-// C includes
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-
-// C++ includes
-#include <chrono>
-#include <mutex>
-#include <random>
-#include <thread>
-
-std::thread g_thread_1;
-std::thread g_thread_2;
-std::thread g_thread_3;
-std::mutex g_mask_mutex;
-
-typedef enum {
- eGet,
- eAssign,
- eClearBits
-} MaskAction;
-
-uint32_t mask_access (MaskAction action, uint32_t mask = 0);
-
-uint32_t
-mask_access (MaskAction action, uint32_t mask)
-{
- static uint32_t g_mask = 0;
-
- std::lock_guard<std::mutex> lock(g_mask_mutex);
- switch (action)
- {
- case eGet:
- break;
-
- case eAssign:
- g_mask |= mask;
- break;
-
- case eClearBits:
- g_mask &= ~mask;
- break;
- }
- return g_mask;
-}
-
-void *
-thread_func (void *arg)
-{
- uint32_t thread_index = *((uint32_t *)arg);
- uint32_t thread_mask = (1u << (thread_index));
- printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
-
- std::default_random_engine generator;
- std::uniform_int_distribution<int> distribution(0, 3000000);
-
- while (mask_access(eGet) & thread_mask)
- {
- // random micro second sleep from zero to 3 seconds
- int usec = distribution(generator);
-
- printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
- std::chrono::microseconds duration(usec);
- std::this_thread::sleep_for(duration);
- printf ("%s (thread = %u) after usleep ...\n", __FUNCTION__, thread_index); // Set break point at this line.
- }
- printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
- return NULL;
-}
-
-
-int main (int argc, char const *argv[])
-{
- int err;
- void *thread_result = NULL;
- uint32_t thread_index_1 = 1;
- uint32_t thread_index_2 = 2;
- uint32_t thread_index_3 = 3;
- uint32_t thread_mask_1 = (1u << thread_index_1);
- uint32_t thread_mask_2 = (1u << thread_index_2);
- uint32_t thread_mask_3 = (1u << thread_index_3);
-
- // Make a mask that will keep all threads alive
- mask_access (eAssign, thread_mask_1 | thread_mask_2 | thread_mask_3); // And that line.
-
- // Create 3 threads
- g_thread_1 = std::thread(thread_func, (void*)&thread_index_1);
- g_thread_2 = std::thread(thread_func, (void*)&thread_index_2);
- g_thread_3 = std::thread(thread_func, (void*)&thread_index_3);
-
- char line[64];
- while (mask_access(eGet) != 0)
- {
- printf ("Enter thread index to kill or ENTER for all:\n");
- fflush (stdout);
- // Kill threads by index, or ENTER for all threads
-
- if (fgets (line, sizeof(line), stdin))
- {
- if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0')
- {
- printf ("Exiting all threads...\n");
- break;
- }
- int32_t index = strtoul (line, NULL, 0);
- switch (index)
- {
- case 1: mask_access (eClearBits, thread_mask_1); break;
- case 2: mask_access (eClearBits, thread_mask_2); break;
- case 3: mask_access (eClearBits, thread_mask_3); break;
- }
- continue;
- }
-
- break;
- }
-
- // Clear all thread bits to they all exit
- mask_access (eClearBits, UINT32_MAX);
-
- // Join all of our threads
- g_thread_1.join();
- g_thread_2.join();
- g_thread_3.join();
-
- return 0;
-}