memcheck/tests/sh-mem-random.c: Set huge_addr to 240GB
[valgrind.git] / drd / tests / std_list.cpp
blobd266bba9cb709634e4f8f8133b9dd704c615d3f1
1 /*
2 * Test program that triggers strcpy() from one thread and a memory allocation
3 * immediately after the region read by strcpy() from another thread. Without
4 * strcpy() intercept there is about 50% chance that this test program triggers
5 * a false positive race report on Ubuntu 12.10 amd64.
7 * See also https://bugs.kde.org/show_bug.cgi?id=326436.
8 */
10 #include <locale.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <pthread.h>
14 #include <string.h>
15 #include <string>
16 #include <sstream>
17 #include <list>
18 #if defined(__FreeBSD__)
19 #include <mutex>
20 #endif
22 using namespace std;
24 #if defined(__FreeBSD__)
25 std::mutex g_mutex;
27 // according to this
28 // https://stackoverflow.com/questions/4057319/is-setlocale-thread-safe-function
29 // setlocale is not thread safe, and indeed on FreeBSD
30 // a load of errors are generated if this is not guarded
31 void setlocale_wrapper()
33 const std::lock_guard<std::mutex> lock(g_mutex);
34 setlocale(LC_ALL, "English");
37 #else
39 void setlocale_wrapper()
41 setlocale(LC_ALL, "English");
44 #endif
46 class SubTest {
47 public:
48 SubTest() {
49 list<int *> ffList;
50 ffList.push_back((int *) NULL);
51 for (list<int*>::iterator ff = ffList.begin(); ff != ffList.end(); ff++) {
52 usleep(1000);
55 void subTest() {
56 list<int *> ffList;
57 ffList.push_back((int *) NULL);
58 for (list<int*>::const_iterator ff = ffList.begin(); ff != ffList.end(); ff++) {
59 usleep(1000);
64 class Test {
65 SubTest *subTest;
66 public:
67 void setUp() {
68 subTest = new SubTest();
69 setlocale_wrapper();
71 void tearDown() {
72 delete subTest; }
73 void func1() {
74 for (size_t i = 0; i < 10000; i++) {
75 subTest->subTest();
76 usleep(1000);
79 void func2() {
80 usleep(1000);
84 void *func1(void *instance)
86 Test *casted = reinterpret_cast<Test*>(instance);
87 casted->setUp();
88 casted->func1();
89 casted->tearDown();
90 return NULL;
93 void *func2(void *instance)
95 Test *casted = reinterpret_cast<Test*>(instance);
96 casted->setUp();
97 casted->func2();
98 casted->tearDown();
99 return NULL;
102 int main(int argc, char* argv[])
104 int err;
105 pthread_t thread1;
106 pthread_t thread2;
107 Test instance1;
108 Test instance2;
110 // create
111 err = pthread_create(&thread1, NULL, &func1, &instance1);
112 if (err != 0)
113 throw string("failed to create a thread.");
114 err = pthread_create(&thread2, NULL, &func2, &instance2);
115 if (err != 0)
116 throw string("failed to create a thread.");
117 // join
118 err = pthread_join(thread1, NULL);
119 if (err != 0)
120 throw string("Thread::join(): failed to join.");
121 err = pthread_join(thread2, NULL);
122 if (err != 0)
123 throw string("Thread::join(): failed to join.");