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 char *g_char_ptr = NULL;
22
23 void
do_bad_thing_with_location(unsigned index,char * char_ptr,char new_val)24 do_bad_thing_with_location(unsigned index, char *char_ptr, char new_val)
25 {
26 unsigned what = new_val;
27 printf("new value written to array(%p) and index(%u) = %u\n", char_ptr, index, what);
28 char_ptr[index] = new_val;
29 }
30
31 uint32_t access_pool (uint32_t flag = 0);
32
33 uint32_t
access_pool(uint32_t flag)34 access_pool (uint32_t flag)
35 {
36 static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER;
37 static unsigned idx = 0; // Well-behaving thread only writes into indexs from 0..6.
38 if (flag == 0)
39 ::pthread_mutex_lock (&g_access_mutex);
40
41 // idx valid range is [0, 6].
42 if (idx > 6)
43 idx = 0;
44
45 if (flag != 0) {
46 // Write into a forbidden area.
47 do_bad_thing_with_location(7, g_char_ptr, 99);
48 }
49
50 unsigned index = idx++;
51
52 if (flag == 0)
53 ::pthread_mutex_unlock (&g_access_mutex);
54 return g_char_ptr[index];
55 }
56
57 void *
thread_func(void * arg)58 thread_func (void *arg)
59 {
60 uint32_t thread_index = *((uint32_t *)arg);
61 printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
62
63 uint32_t count = 0;
64 uint32_t val;
65 while (count++ < 15)
66 {
67 // random micro second sleep from zero to 3 seconds
68 int usec = ::rand() % 3000000;
69 printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
70 ::usleep (usec);
71
72 if (count < 7)
73 val = access_pool ();
74 else
75 val = access_pool (1);
76
77 printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
78 }
79 printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
80 return NULL;
81 }
82
83
main(int argc,char const * argv[])84 int main (int argc, char const *argv[])
85 {
86 int err;
87 void *thread_result = NULL;
88 uint32_t thread_index_1 = 1;
89 uint32_t thread_index_2 = 2;
90 uint32_t thread_index_3 = 3;
91
92 g_char_ptr = (char *)malloc (10);
93 for (int i = 0; i < 10; ++i)
94 *g_char_ptr = 0;
95
96 // Create 3 threads
97 err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1);
98 err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2);
99 err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3);
100
101 struct {
102 int a;
103 int b;
104 int c;
105 } MyAggregateDataType;
106
107 printf ("Before turning all three threads loose...\n"); // Set break point at this line.
108
109 // Join all of our threads
110 err = ::pthread_join (g_thread_1, &thread_result);
111 err = ::pthread_join (g_thread_2, &thread_result);
112 err = ::pthread_join (g_thread_3, &thread_result);
113
114 return 0;
115 }
116