• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 
17 pthread_t g_thread_1 = NULL;
18 pthread_t g_thread_2 = NULL;
19 pthread_t g_thread_3 = NULL;
20 
21 uint32_t g_val = 0;
22 
23 uint32_t access_pool (uint32_t flag = 0);
24 
25 uint32_t
access_pool(uint32_t flag)26 access_pool (uint32_t flag)
27 {
28     static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER;
29     if (flag == 0)
30         ::pthread_mutex_lock (&g_access_mutex);
31 
32     uint32_t old_val = g_val;
33     if (flag != 0) {
34         printf("changing g_val to %d...\n", (old_val + 1));
35         g_val = old_val + 1;
36     }
37 
38     if (flag == 0)
39         ::pthread_mutex_unlock (&g_access_mutex);
40     return g_val;
41 }
42 
43 void *
thread_func(void * arg)44 thread_func (void *arg)
45 {
46     uint32_t thread_index = *((uint32_t *)arg); // Break here in order to allow the thread
47                                                 // to inherit the global watchpoint state.
48     printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
49 
50     uint32_t count = 0;
51     uint32_t val;
52     while (count++ < 15)
53     {
54         // random micro second sleep from zero to 3 seconds
55         int usec = ::rand() % 3000000;
56         printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
57         ::usleep (usec);
58 
59         if (count < 7)
60             val = access_pool ();
61         else
62             val = access_pool (1);
63 
64         printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
65     }
66     printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
67     return NULL;
68 }
69 
70 
main(int argc,char const * argv[])71 int main (int argc, char const *argv[])
72 {
73     int err;
74     void *thread_result = NULL;
75     uint32_t thread_index_1 = 1;
76     uint32_t thread_index_2 = 2;
77     uint32_t thread_index_3 = 3;
78 
79     printf ("Before turning all three threads loose...\n"); // Set break point at this line,
80                                                             // in order to set our watchpoint.
81     // Create 3 threads
82     err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1);
83     err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2);
84     err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3);
85 
86     // Join all of our threads
87     err = ::pthread_join (g_thread_1, &thread_result);
88     err = ::pthread_join (g_thread_2, &thread_result);
89     err = ::pthread_join (g_thread_3, &thread_result);
90 
91     return 0;
92 }
93