| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 1 | //===-- 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 Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdlib.h> |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 14 | |
| Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 15 | // C++ includes |
| 16 | #include <chrono> |
| 17 | #include <mutex> |
| 18 | #include <random> |
| 19 | #include <thread> |
| 20 | |
| 21 | std::thread g_thread_1; |
| 22 | std::thread g_thread_2; |
| 23 | std::thread g_thread_3; |
| 24 | std::mutex g_mask_mutex; |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 25 | |
| 26 | typedef enum { |
| 27 | eGet, |
| 28 | eAssign, |
| 29 | eClearBits |
| 30 | } MaskAction; |
| 31 | |
| 32 | uint32_t mask_access (MaskAction action, uint32_t mask = 0); |
| 33 | |
| 34 | uint32_t |
| 35 | mask_access (MaskAction action, uint32_t mask) |
| 36 | { |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 37 | static uint32_t g_mask = 0; |
| Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 38 | |
| 39 | std::lock_guard<std::mutex> lock(g_mask_mutex); |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 40 | 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 Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 53 | return g_mask; |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void * |
| 57 | thread_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 Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 63 | std::default_random_engine generator; |
| 64 | std::uniform_int_distribution<int> distribution(0, 3000000); |
| 65 | |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 66 | while (mask_access(eGet) & thread_mask) |
| 67 | { |
| 68 | // random micro second sleep from zero to 3 seconds |
| Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 69 | int usec = distribution(generator); |
| 70 | |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 71 | printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); |
| Zachary Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 72 | std::chrono::microseconds duration(usec); |
| 73 | std::this_thread::sleep_for(duration); |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 74 | 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 | |
| 81 | int 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 Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 96 | 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 Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 99 | |
| 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 Turner | c782652 | 2014-08-13 17:44:53 +0000 | [diff] [blame] | 131 | g_thread_1.join(); |
| 132 | g_thread_2.join(); |
| 133 | g_thread_3.join(); |
| Johnny Chen | b2c7825 | 2011-09-27 23:15:58 +0000 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |