summaryrefslogtreecommitdiff
path: root/lib/scudo/standalone/tests/mutex_test.cc
blob: ce33db58b450b8d1085a30f4dbe7cff52172e978 (plain)
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
//===-- mutex_test.cc -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mutex.h"

#include "gtest/gtest.h"

#include <string.h>

template <typename MutexType> class TestData {
public:
  explicit TestData(MutexType *M) : Mutex(M) {
    for (scudo::u32 I = 0; I < Size; I++)
      Data[I] = 0;
  }

  void write() {
    Lock L(Mutex);
    T V0 = Data[0];
    for (scudo::u32 I = 0; I < Size; I++) {
      EXPECT_EQ(Data[I], V0);
      Data[I]++;
    }
  }

  void tryWrite() {
    if (!Mutex->tryLock())
      return;
    T V0 = Data[0];
    for (scudo::u32 I = 0; I < Size; I++) {
      EXPECT_EQ(Data[I], V0);
      Data[I]++;
    }
    Mutex->unlock();
  }

  void backoff() {
    volatile T LocalData[Size] = {};
    for (scudo::u32 I = 0; I < Size; I++) {
      LocalData[I]++;
      EXPECT_EQ(LocalData[I], 1U);
    }
  }

private:
  typedef scudo::GenericScopedLock<MutexType> Lock;
  static const scudo::u32 Size = 64U;
  typedef scudo::u64 T;
  MutexType *Mutex;
  ALIGNED(SCUDO_CACHE_LINE_SIZE) T Data[Size];
};

const scudo::u32 NumberOfThreads = 8;
#if SCUDO_DEBUG
const scudo::u32 NumberOfIterations = 4 * 1024;
#else
const scudo::u32 NumberOfIterations = 16 * 1024;
#endif

template <typename MutexType> static void *lockThread(void *Param) {
  TestData<MutexType> *Data = reinterpret_cast<TestData<MutexType> *>(Param);
  for (scudo::u32 I = 0; I < NumberOfIterations; I++) {
    Data->write();
    Data->backoff();
  }
  return 0;
}

template <typename MutexType> static void *tryThread(void *Param) {
  TestData<MutexType> *Data = reinterpret_cast<TestData<MutexType> *>(Param);
  for (scudo::u32 I = 0; I < NumberOfIterations; I++) {
    Data->tryWrite();
    Data->backoff();
  }
  return 0;
}

template <typename MutexType> static void checkLocked(MutexType *M) {
  scudo::GenericScopedLock<MutexType> L(M);
  M->checkLocked();
}

TEST(ScudoMutexTest, SpinMutex) {
  scudo::SpinMutex M;
  M.init();
  TestData<scudo::SpinMutex> Data(&M);
  pthread_t Threads[NumberOfThreads];
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_create(&Threads[I], 0, lockThread<scudo::SpinMutex>, &Data);
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_join(Threads[I], 0);
}

TEST(ScudoMutexTest, SpinMutexTry) {
  scudo::SpinMutex M;
  M.init();
  TestData<scudo::SpinMutex> Data(&M);
  pthread_t Threads[NumberOfThreads];
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_create(&Threads[I], 0, tryThread<scudo::SpinMutex>, &Data);
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_join(Threads[I], 0);
}

TEST(ScudoMutexTest, BlockingMutex) {
  scudo::u64 MutexMemory[1024] = {};
  scudo::BlockingMutex *M =
      new (MutexMemory) scudo::BlockingMutex(scudo::LINKER_INITIALIZED);
  TestData<scudo::BlockingMutex> Data(M);
  pthread_t Threads[NumberOfThreads];
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_create(&Threads[I], 0, lockThread<scudo::BlockingMutex>, &Data);
  for (scudo::u32 I = 0; I < NumberOfThreads; I++)
    pthread_join(Threads[I], 0);
  checkLocked(M);
}