blob: 6b87c3d649e63ddfaeedcbe67b349627ccf6cfda [file] [log] [blame]
Johnny Chenb2c78252011-09-27 23:15:58 +00001//===-- main.cpp ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C includes
Johnny Chenb2c78252011-09-27 23:15:58 +000011#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
Johnny Chenb2c78252011-09-27 23:15:58 +000014
Zachary Turnerc7826522014-08-13 17:44:53 +000015// C++ includes
16#include <chrono>
17#include <mutex>
18#include <random>
19#include <thread>
20
21std::thread g_thread_1;
22std::thread g_thread_2;
23std::thread g_thread_3;
24std::mutex g_mask_mutex;
Johnny Chenb2c78252011-09-27 23:15:58 +000025
26typedef enum {
27 eGet,
28 eAssign,
29 eClearBits
30} MaskAction;
31
32uint32_t mask_access (MaskAction action, uint32_t mask = 0);
33
34uint32_t
35mask_access (MaskAction action, uint32_t mask)
36{
Johnny Chenb2c78252011-09-27 23:15:58 +000037 static uint32_t g_mask = 0;
Zachary Turnerc7826522014-08-13 17:44:53 +000038
39 std::lock_guard<std::mutex> lock(g_mask_mutex);
Johnny Chenb2c78252011-09-27 23:15:58 +000040 switch (action)
41 {
42 case eGet:
43 break;
44
45 case eAssign:
46 g_mask |= mask;
47 break;
48
49 case eClearBits:
50 g_mask &= ~mask;
51 break;
52 }
Zachary Turnerc7826522014-08-13 17:44:53 +000053 return g_mask;
Johnny Chenb2c78252011-09-27 23:15:58 +000054}
55
56void *
57thread_func (void *arg)
58{
59 uint32_t thread_index = *((uint32_t *)arg);
60 uint32_t thread_mask = (1u << (thread_index));
61 printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
62
Zachary Turnerc7826522014-08-13 17:44:53 +000063 std::default_random_engine generator;
64 std::uniform_int_distribution<int> distribution(0, 3000000);
65
Johnny Chenb2c78252011-09-27 23:15:58 +000066 while (mask_access(eGet) & thread_mask)
67 {
68 // random micro second sleep from zero to 3 seconds
Zachary Turnerc7826522014-08-13 17:44:53 +000069 int usec = distribution(generator);
70
Johnny Chenb2c78252011-09-27 23:15:58 +000071 printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
Zachary Turnerc7826522014-08-13 17:44:53 +000072 std::chrono::microseconds duration(usec);
73 std::this_thread::sleep_for(duration);
Johnny Chenb2c78252011-09-27 23:15:58 +000074 printf ("%s (thread = %u) after usleep ...\n", __FUNCTION__, thread_index); // Set break point at this line.
75 }
76 printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
77 return NULL;
78}
79
80
81int main (int argc, char const *argv[])
82{
83 int err;
84 void *thread_result = NULL;
85 uint32_t thread_index_1 = 1;
86 uint32_t thread_index_2 = 2;
87 uint32_t thread_index_3 = 3;
88 uint32_t thread_mask_1 = (1u << thread_index_1);
89 uint32_t thread_mask_2 = (1u << thread_index_2);
90 uint32_t thread_mask_3 = (1u << thread_index_3);
91
92 // Make a mask that will keep all threads alive
93 mask_access (eAssign, thread_mask_1 | thread_mask_2 | thread_mask_3); // And that line.
94
95 // Create 3 threads
Zachary Turnerc7826522014-08-13 17:44:53 +000096 g_thread_1 = std::thread(thread_func, (void*)&thread_index_1);
97 g_thread_2 = std::thread(thread_func, (void*)&thread_index_2);
98 g_thread_3 = std::thread(thread_func, (void*)&thread_index_3);
Johnny Chenb2c78252011-09-27 23:15:58 +000099
100 char line[64];
101 while (mask_access(eGet) != 0)
102 {
103 printf ("Enter thread index to kill or ENTER for all:\n");
104 fflush (stdout);
105 // Kill threads by index, or ENTER for all threads
106
107 if (fgets (line, sizeof(line), stdin))
108 {
109 if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0')
110 {
111 printf ("Exiting all threads...\n");
112 break;
113 }
114 int32_t index = strtoul (line, NULL, 0);
115 switch (index)
116 {
117 case 1: mask_access (eClearBits, thread_mask_1); break;
118 case 2: mask_access (eClearBits, thread_mask_2); break;
119 case 3: mask_access (eClearBits, thread_mask_3); break;
120 }
121 continue;
122 }
123
124 break;
125 }
126
127 // Clear all thread bits to they all exit
128 mask_access (eClearBits, UINT32_MAX);
129
130 // Join all of our threads
Zachary Turnerc7826522014-08-13 17:44:53 +0000131 g_thread_1.join();
132 g_thread_2.join();
133 g_thread_3.join();
Johnny Chenb2c78252011-09-27 23:15:58 +0000134
135 return 0;
136}