1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
//===-- tsan_posix.cc -----------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_interface.h"
#include "tsan_test_util.h"
#include "gtest/gtest.h"
#include <pthread.h>
struct thread_key {
pthread_key_t key;
pthread_mutex_t *mtx;
int val;
int *cnt;
thread_key(pthread_key_t key, pthread_mutex_t *mtx, int val, int *cnt)
: key(key)
, mtx(mtx)
, val(val)
, cnt(cnt) {
}
};
static void thread_secific_dtor(void *v) {
thread_key *k = (thread_key *)v;
EXPECT_EQ(pthread_mutex_lock(k->mtx), 0);
(*k->cnt)++;
__tsan_write4(&k->cnt);
EXPECT_EQ(pthread_mutex_unlock(k->mtx), 0);
if (k->val == 42) {
delete k;
} else if (k->val == 43 || k->val == 44) {
k->val--;
EXPECT_EQ(pthread_setspecific(k->key, k), 0);
} else {
ASSERT_TRUE(false);
}
}
static void *dtors_thread(void *p) {
thread_key *k = (thread_key *)p;
EXPECT_EQ(pthread_setspecific(k->key, k), 0);
return 0;
}
TEST(Posix, ThreadSpecificDtors) {
int cnt = 0;
pthread_key_t key;
EXPECT_EQ(pthread_key_create(&key, thread_secific_dtor), 0);
pthread_mutex_t mtx;
EXPECT_EQ(pthread_mutex_init(&mtx, 0), 0);
pthread_t th[3];
thread_key *k[3];
k[0] = new thread_key(key, &mtx, 42, &cnt);
k[1] = new thread_key(key, &mtx, 43, &cnt);
k[2] = new thread_key(key, &mtx, 44, &cnt);
EXPECT_EQ(pthread_create(&th[0], 0, dtors_thread, k[0]), 0);
EXPECT_EQ(pthread_create(&th[1], 0, dtors_thread, k[1]), 0);
EXPECT_EQ(pthread_join(th[0], 0), 0);
EXPECT_EQ(pthread_create(&th[2], 0, dtors_thread, k[2]), 0);
EXPECT_EQ(pthread_join(th[1], 0), 0);
EXPECT_EQ(pthread_join(th[2], 0), 0);
EXPECT_EQ(pthread_key_delete(key), 0);
EXPECT_EQ(6, cnt);
}
static __thread int local_var;
static void *local_thread(void *p) {
__tsan_write1(&local_var);
__tsan_write1(&p);
if (p == 0)
return 0;
const int kThreads = 4;
pthread_t th[kThreads];
for (int i = 0; i < kThreads; i++)
EXPECT_EQ(pthread_create(&th[i], 0, local_thread,
(void*)((long)p - 1)), 0); // NOLINT
for (int i = 0; i < kThreads; i++)
EXPECT_EQ(pthread_join(th[i], 0), 0);
return 0;
}
TEST(Posix, ThreadLocalAccesses) {
local_thread((void*)2);
}
struct CondContext {
pthread_mutex_t m;
pthread_cond_t c;
int data;
};
static void *cond_thread(void *p) {
CondContext &ctx = *static_cast<CondContext*>(p);
EXPECT_EQ(pthread_mutex_lock(&ctx.m), 0);
EXPECT_EQ(ctx.data, 0);
ctx.data = 1;
EXPECT_EQ(pthread_cond_signal(&ctx.c), 0);
EXPECT_EQ(pthread_mutex_unlock(&ctx.m), 0);
EXPECT_EQ(pthread_mutex_lock(&ctx.m), 0);
while (ctx.data != 2)
EXPECT_EQ(pthread_cond_wait(&ctx.c, &ctx.m), 0);
EXPECT_EQ(pthread_mutex_unlock(&ctx.m), 0);
EXPECT_EQ(pthread_mutex_lock(&ctx.m), 0);
ctx.data = 3;
EXPECT_EQ(pthread_cond_broadcast(&ctx.c), 0);
EXPECT_EQ(pthread_mutex_unlock(&ctx.m), 0);
return 0;
}
TEST(Posix, CondBasic) {
CondContext ctx;
EXPECT_EQ(pthread_mutex_init(&ctx.m, 0), 0);
EXPECT_EQ(pthread_cond_init(&ctx.c, 0), 0);
ctx.data = 0;
pthread_t th;
EXPECT_EQ(pthread_create(&th, 0, cond_thread, &ctx), 0);
EXPECT_EQ(pthread_mutex_lock(&ctx.m), 0);
while (ctx.data != 1)
EXPECT_EQ(pthread_cond_wait(&ctx.c, &ctx.m), 0);
ctx.data = 2;
EXPECT_EQ(pthread_mutex_unlock(&ctx.m), 0);
EXPECT_EQ(pthread_cond_broadcast(&ctx.c), 0);
EXPECT_EQ(pthread_mutex_lock(&ctx.m), 0);
while (ctx.data != 3)
EXPECT_EQ(pthread_cond_wait(&ctx.c, &ctx.m), 0);
EXPECT_EQ(pthread_mutex_unlock(&ctx.m), 0);
EXPECT_EQ(pthread_join(th, 0), 0);
EXPECT_EQ(pthread_cond_destroy(&ctx.c), 0);
EXPECT_EQ(pthread_mutex_destroy(&ctx.m), 0);
}
|