Add 469782 to NEWS
[valgrind.git] / helgrind / tests / bug322621.cpp
blob9e80b5331733287800b4e9cd00aa7a1f8ad9d258
1 // See also https://bugs.kde.org/show_bug.cgi?id=322621
3 #include <pthread.h>
4 #include <iostream>
5 #include "../../helgrind/helgrind.h"
7 static int verbose;
8 static pthread_t ls_thread;
9 static pthread_barrier_t ls_barrier;
11 char* ls_buf[2];
13 #define LS_BUF_SIZE (1024*1024)
14 #define NR_RUNS 2
16 void fill_buffer(int buf)
18 if (verbose)
19 std::cerr << "Fill " << buf << "\n";
20 for (int i = 0; i < LS_BUF_SIZE; i++)
21 ls_buf[buf][i] = 1;
22 if (verbose)
23 std::cerr << "Fill " << buf << " done\n";
26 int read_buffer(int buf)
28 if (verbose)
29 std::cerr << "Read " << buf << "\n";
30 int res = 0;
31 for (int i = 0; i < LS_BUF_SIZE; i++)
32 res += ls_buf[buf][i];
33 if (verbose)
34 std::cerr << "Read " << buf << " done\n";
35 return res;
38 void *the_thread(void *ptr)
40 int buf = 1;
42 for (int i = 0; i < NR_RUNS; i++) {
44 fill_buffer(buf);
46 if (verbose)
47 std::cerr << "Aux at barrier " << i << "\n";
48 pthread_barrier_wait(&ls_barrier);
49 if (verbose)
50 std::cerr << "Aux after barrier " << i << "\n";
52 buf = buf ^ 1;
54 return ptr;
58 int main()
60 VALGRIND_HG_DISABLE_CHECKING(&std::cerr, sizeof(std::cerr));
62 ls_buf[0] = new char[LS_BUF_SIZE];
63 ls_buf[1] = new char[LS_BUF_SIZE]; // second buffer only when multithreaded
65 pthread_barrier_init(&ls_barrier, NULL, 2);
67 pthread_attr_t attr;
68 pthread_attr_init(&attr);
69 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
70 pthread_create(&ls_thread, &attr, the_thread, NULL);
71 pthread_attr_destroy(&attr);
73 int buf = 0;
74 for (int i = 0; i < NR_RUNS; i++) {
75 std::cerr << "Main at barrier " << i << "\n";
76 pthread_barrier_wait(&ls_barrier);
77 std::cerr << "Main after barrier " << i << "\n";
78 buf = buf ^ 1;
79 (void)read_buffer(buf);
82 pthread_join(ls_thread,NULL);
83 pthread_barrier_destroy(&ls_barrier);
84 delete[] ls_buf[1]; // second buffer only when multithreaded
85 delete[] ls_buf[0];
87 return 0;