summaryrefslogtreecommitdiff
path: root/common/libs/threads/cuttlefish_thread_test.cpp
blob: 5989e9b7e742a4831e968c8a96e76f97bb3892c2 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "common/libs/threads/cuttlefish_thread.h"

#include <gtest/gtest.h>

#include <android-base/logging.h>
#include "common/libs/threads/thunkers.h"
#include "common/libs/time/monotonic_time.h"

using cvd::ConditionVariable;
using cvd::Mutex;
using cvd::ScopedThread;
using cvd::time::MonotonicTimePoint;
using cvd::time::Milliseconds;

static const int FINISHED = 100;

static void SleepUntil(const MonotonicTimePoint& in) {
  struct timespec ts;
  in.ToTimespec(&ts);
#ifdef CLOCK_MONOTONIC_RAW
  // WARNING:
  // While we do have CLOCK_MONOTONIC_RAW, we can't depend on it until:
  // - ALL places relying on MonotonicTimePoint are fixed,
  // - pthread supports pthread_timewait_monotonic.
  // - CLOCK_MONOTONIC_RAW is re-enabled in monotonic_time.h.
  //
  // This is currently observable as a LEGITIMATE problem while running
  // this test. DO NOT revert this to CLOCK_MONOTONIC_RAW until this is
  // fixed everywhere AND this test passes.
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
#else
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
#endif
}

class MutexTest {
 public:
  MutexTest() : busy_(NULL), stage_(0) {}

  void Run() {
    {
      ScopedThread thread_a(cvd::thunk<void, &MutexTest::FastThread>, this);
      ScopedThread thread_b(cvd::thunk<void, &MutexTest::SlowThread>, this);
    }
    LOG(INFO) << "MutexTest: completed at stage "
              << stage_
              << ", result: "
              << ((stage_ == FINISHED) ? "PASSED" : "FAILED");
  }

protected:
  void* FastThread() {
    mutex_.Lock();
    CHECK(busy_ == NULL);
    busy_ = "FastThread";
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    stage_ = 1;
    busy_ = NULL;
    mutex_.Unlock();
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(10));
    mutex_.Lock();
    CHECK(busy_ == NULL);
    busy_ = "FastThread";
    CHECK(stage_ == 2);
    stage_ = FINISHED;
    busy_ = NULL;
    mutex_.Unlock();
    return NULL;
  }

  void* SlowThread() {
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(50));
    mutex_.Lock();
    CHECK(busy_== NULL);
    busy_ = "SlowThread";
    CHECK(stage_ == 1);
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    stage_ = 2;
    busy_ = NULL;
    mutex_.Unlock();
    return NULL;
  }

  Mutex mutex_;
  const char* busy_;
  int stage_;
};

class NotifyOneTest {
 public:
  NotifyOneTest() : cond_(&mutex_), signalled_(0) {}

  void Run() {
    {
      ScopedThread thread_s(
          cvd::thunk<void, &NotifyOneTest::SignalThread>, this);
      ScopedThread thread_w1(
          cvd::thunk<void, &NotifyOneTest::WaitThread>, this);
      ScopedThread thread_w2(
          cvd::thunk<void, &NotifyOneTest::WaitThread>, this);
    }
    LOG(INFO) << "NotifyOneTest: completed, signalled "
              << signalled_
              << ", result: "
              << ((signalled_ == 2) ? "PASSED" : "FAILED");
  }

protected:
  void* SignalThread() {
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    mutex_.Lock();
    cond_.NotifyOne();
    mutex_.Unlock();
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    mutex_.Lock();
    CHECK(signalled_== 1);
    cond_.NotifyOne();
    mutex_.Unlock();
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    mutex_.Lock();
    CHECK(signalled_ == 2);
    mutex_.Unlock();
    return NULL;
  }

  void* WaitThread() {
    mutex_.Lock();
    cond_.Wait();
    signalled_++;
    mutex_.Unlock();
    return NULL;
  }

  Mutex mutex_;
  ConditionVariable cond_;
  int signalled_;
};

class NotifyAllTest {
 public:
  NotifyAllTest() : cond_(&mutex_), signalled_(0) {}

  void Run() {
    {
      ScopedThread thread_s(
          cvd::thunk<void, &NotifyAllTest::SignalThread>, this);
      ScopedThread thread_w1(
          cvd::thunk<void, &NotifyAllTest::WaitThread>, this);
      ScopedThread thread_w2(
          cvd::thunk<void, &NotifyAllTest::WaitThread>, this);
    }
    printf("NotifyAllTest: completed, signalled %d (%s)\n",
           signalled_, (signalled_ == 2) ? "PASSED" : "FAILED");
  }

protected:
  void* SignalThread() {
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    mutex_.Lock();
    cond_.NotifyAll();
    mutex_.Unlock();
    SleepUntil(MonotonicTimePoint::Now() + Milliseconds(100));
    mutex_.Lock();
    CHECK(signalled_ == 2);
    mutex_.Unlock();
    return NULL;
  }

  void* WaitThread() {
    mutex_.Lock();
    cond_.Wait();
    signalled_++;
    mutex_.Unlock();
    return NULL;
  }

  Mutex mutex_;
  ConditionVariable cond_;
  int signalled_;
};

class WaitUntilTest {
 public:
  WaitUntilTest() : cond_(&mutex_), stage_(0) {}

  bool Run() {
    start_ = MonotonicTimePoint::Now();
    {
      ScopedThread thread_s(
          cvd::thunk<void, &WaitUntilTest::SignalThread>, this);
      ScopedThread thread_w2(
          cvd::thunk<void, &WaitUntilTest::WaitThread>, this);
    }
    printf("WaitUntilTest: completed, stage %d (%s)\n",
           stage_, (stage_ == FINISHED) ? "PASSED" : "FAILED");
    return stage_ == FINISHED;
  }

protected:
  void* SignalThread() {
    SleepUntil(start_ + Milliseconds(200));
    mutex_.Lock();
    CHECK(stage_ == 2);
    cond_.NotifyOne();
    stage_ = 3;
    mutex_.Unlock();
    return NULL;
  }

  void* WaitThread() {
    mutex_.Lock();
    CHECK(stage_ == 0);
    stage_ = 1;
    cond_.WaitUntil(start_ + Milliseconds(50));
    MonotonicTimePoint current(MonotonicTimePoint::Now());
    CHECK(Milliseconds(current - start_).count() >= 50);
    CHECK(Milliseconds(current - start_).count() <= 100);
    stage_ = 2;
    cond_.WaitUntil(start_ + Milliseconds(1000));
    current = MonotonicTimePoint::Now();
    CHECK(Milliseconds(current - start_).count() <= 500);
    CHECK(stage_ == 3);
    stage_ = FINISHED;
    mutex_.Unlock();
    return NULL;
  }

  Mutex mutex_;
  ConditionVariable cond_;
  int stage_;
  MonotonicTimePoint start_;
};

TEST(ThreadTest, Mutex) {
  MutexTest mt;
  mt.Run();
  NotifyOneTest nt1;
  nt1.Run();
  NotifyAllTest nta;
  nta.Run();
  WaitUntilTest wu;
  bool success = wu.Run();
  EXPECT_TRUE(success);
}