2013-08-12 21:10:21 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-08-12 21:50:48 +00:00
|
|
|
#include <cstdlib>
|
2013-08-12 21:10:21 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2013-12-09 22:56:48 +00:00
|
|
|
#include "util/thread.h"
|
2013-11-18 22:03:18 +00:00
|
|
|
#include "util/debug.h"
|
2013-11-12 04:00:21 +00:00
|
|
|
#include "util/shared_mutex.h"
|
2013-11-22 19:32:09 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-11-18 22:03:18 +00:00
|
|
|
using namespace lean;
|
2013-08-12 21:10:21 +00:00
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
#if !defined(__APPLE__) && defined(LEAN_MULTI_THREAD)
|
2013-09-10 20:50:35 +00:00
|
|
|
void foo() {
|
2013-12-09 22:56:48 +00:00
|
|
|
static LEAN_THREAD_LOCAL std::vector<int> v(1024);
|
2013-08-12 21:10:21 +00:00
|
|
|
if (v.size() != 1024) {
|
|
|
|
std::cerr << "Error\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst1() {
|
|
|
|
unsigned n = 5;
|
|
|
|
for (unsigned i = 0; i < n; i++) {
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t([](){ foo(); });
|
2013-08-12 21:10:21 +00:00
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 04:00:21 +00:00
|
|
|
static void tst2() {
|
|
|
|
unsigned N = 10;
|
|
|
|
unsigned n = 1;
|
2013-12-09 22:56:48 +00:00
|
|
|
shared_mutex mut;
|
|
|
|
std::vector<thread> threads;
|
2013-11-12 04:00:21 +00:00
|
|
|
for (unsigned i = 0; i < N; i++) {
|
|
|
|
threads.emplace_back([&]() {
|
|
|
|
unsigned sum = 0;
|
|
|
|
{
|
2013-12-09 22:56:48 +00:00
|
|
|
shared_lock lock(mut);
|
2013-11-12 04:00:21 +00:00
|
|
|
for (unsigned i = 0; i < 1000000; i++)
|
|
|
|
sum += n;
|
|
|
|
}
|
|
|
|
{
|
2013-12-09 22:56:48 +00:00
|
|
|
exclusive_lock lock(mut);
|
|
|
|
std::cout << sum << "\n";
|
2013-11-12 04:00:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < N; i++)
|
|
|
|
threads[i].join();
|
|
|
|
}
|
|
|
|
|
2013-11-18 22:03:18 +00:00
|
|
|
static void tst3() {
|
|
|
|
shared_mutex mutex;
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic<bool> t2_started(false);
|
|
|
|
atomic<bool> t2_done(false);
|
2013-12-10 00:55:13 +00:00
|
|
|
chrono::milliseconds small_delay(10);
|
2013-11-18 22:03:18 +00:00
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t1([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
while (!t2_started) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
while (!mutex.try_lock()) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
2013-11-20 19:28:19 +00:00
|
|
|
// test recursive try_lock
|
|
|
|
lean_verify(mutex.try_lock());
|
|
|
|
mutex.unlock();
|
2013-11-18 22:03:18 +00:00
|
|
|
// we can only succeed getting the lock if t2 is done
|
|
|
|
lean_assert(t2_done);
|
|
|
|
mutex.unlock();
|
|
|
|
});
|
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t2([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
{
|
2013-12-09 22:56:48 +00:00
|
|
|
exclusive_lock lock(mutex);
|
2013-11-18 22:03:18 +00:00
|
|
|
t2_started = true;
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
t2_done = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
lean_assert(t2_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst4() {
|
|
|
|
shared_mutex mutex;
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic<bool> t2_started(false);
|
|
|
|
atomic<bool> t2_done(false);
|
2013-12-10 00:55:13 +00:00
|
|
|
chrono::milliseconds small_delay(10);
|
2013-11-18 22:03:18 +00:00
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t1([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
while (!t2_started) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
while (!mutex.try_lock_shared()) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
2013-11-20 19:28:19 +00:00
|
|
|
// test recursive try_lock_shared
|
|
|
|
lean_verify(mutex.try_lock_shared());
|
|
|
|
mutex.unlock_shared();
|
2013-11-18 22:03:18 +00:00
|
|
|
// we can only succeed getting the lock if t2 is done
|
|
|
|
lean_assert(t2_done);
|
|
|
|
mutex.unlock_shared();
|
|
|
|
});
|
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t2([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
{
|
2013-12-09 22:56:48 +00:00
|
|
|
exclusive_lock lock(mutex);
|
2013-11-18 22:03:18 +00:00
|
|
|
t2_started = true;
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
t2_done = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
lean_assert(t2_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst5() {
|
2013-12-09 22:56:48 +00:00
|
|
|
shared_mutex mutex;
|
|
|
|
atomic<bool> t2_started(false);
|
|
|
|
atomic<bool> t1_done(false);
|
2013-12-10 00:55:13 +00:00
|
|
|
chrono::milliseconds small_delay(10);
|
2013-11-18 22:03:18 +00:00
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t1([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
while (!t2_started) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
lean_verify(mutex.try_lock_shared()); // t2 is also using a shared lock
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
lean_verify(mutex.try_lock_shared());
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
t1_done = true;
|
|
|
|
mutex.unlock_shared();
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
mutex.unlock_shared();
|
|
|
|
});
|
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
thread t2([&]() {
|
2013-11-18 22:03:18 +00:00
|
|
|
{
|
|
|
|
shared_lock lock(mutex);
|
|
|
|
t2_started = true;
|
|
|
|
while (!t1_done) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(small_delay);
|
2013-11-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
}
|
2013-11-22 19:32:09 +00:00
|
|
|
|
|
|
|
static void tst6() {
|
|
|
|
interruptible_thread t1([]() {
|
|
|
|
try {
|
2013-12-09 22:56:48 +00:00
|
|
|
// Remark: this_thread::sleep_for does not check whether the thread has been interrupted or not.
|
2013-12-10 00:55:13 +00:00
|
|
|
// this_thread::sleep_for(chrono::milliseconds(1000000));
|
2013-11-22 19:32:09 +00:00
|
|
|
sleep_for(1000000);
|
|
|
|
} catch (interrupted &) {
|
|
|
|
std::cout << "interrupted...\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sleep_for(20);
|
|
|
|
t1.request_interrupt();
|
|
|
|
t1.join();
|
|
|
|
}
|
2013-11-18 22:03:18 +00:00
|
|
|
#else
|
2013-12-09 22:56:48 +00:00
|
|
|
static void tst1() {}
|
|
|
|
static void tst2() {}
|
2013-11-18 22:03:18 +00:00
|
|
|
static void tst3() {}
|
|
|
|
static void tst4() {}
|
|
|
|
static void tst5() {}
|
2013-11-22 19:32:09 +00:00
|
|
|
static void tst6() {}
|
2013-11-18 22:03:18 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-12 21:10:21 +00:00
|
|
|
int main() {
|
2013-12-01 20:42:32 +00:00
|
|
|
save_stack_info();
|
2013-08-12 21:10:21 +00:00
|
|
|
tst1();
|
2013-11-18 22:03:18 +00:00
|
|
|
tst2();
|
|
|
|
tst3();
|
|
|
|
tst4();
|
|
|
|
tst5();
|
2013-11-22 19:32:09 +00:00
|
|
|
tst6();
|
2013-11-18 22:03:18 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
2013-08-12 21:10:21 +00:00
|
|
|
}
|