forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutex.cc
59 lines (48 loc) · 1.56 KB
/
mutex.cc
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
// Copyright 2022 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#include <chrono>
#include <cstddef>
#include <random>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack.h"
#include "xnnpack/common.h"
#include "xnnpack/mutex.h"
#include "replicable_random_device.h"
TEST(MUTEX, init_lock_unlock_destroy) {
xnn_mutex m;
ASSERT_EQ(xnn_status_success, xnn_mutex_init(&m));
ASSERT_EQ(xnn_status_success, xnn_mutex_lock(&m));
ASSERT_EQ(xnn_status_success, xnn_mutex_unlock(&m));
ASSERT_EQ(xnn_status_success, xnn_mutex_destroy(&m));
}
TEST(MUTEX, counter) {
// Skip if we are not targeting pthread.
#if XNN_PLATFORM_WEB && !defined(__EMSCRIPTEN_PTHREADS__)
GTEST_SKIP();
#endif
xnn_mutex m;
constexpr size_t num_threads = 50;
std::vector<std::thread> threads;
threads.reserve(num_threads);
volatile size_t counter = 0;
xnnpack::ReplicableRandomDevice rng;
auto dist = std::uniform_int_distribution<int>(100, 200);
ASSERT_EQ(xnn_status_success, xnn_mutex_init(&m));
for (size_t i = 0; i < num_threads; i++) {
threads.emplace_back(([&] () {
ASSERT_EQ(xnn_status_success, xnn_mutex_lock(&m));
std::this_thread::sleep_for(std::chrono::milliseconds(dist(rng)));
counter += 1;
ASSERT_EQ(xnn_status_success, xnn_mutex_unlock(&m));
}));
}
for (int i = num_threads - 1; i >= 0; i--) {
threads[i].join();
}
ASSERT_EQ(counter, num_threads);
ASSERT_EQ(xnn_status_success, xnn_mutex_destroy(&m));
}