2021-04-14 04:21:04 +08:00
|
|
|
/*
|
|
|
|
* ThreadHelper.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2021-04-22 05:13:00 +08:00
|
|
|
* Copyright 2013-2021 Apple Inc. and the FoundationDB project authors
|
2021-04-14 04:21:04 +08:00
|
|
|
*
|
|
|
|
* 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 "flow/ThreadHelper.actor.h"
|
|
|
|
#include "flow/Error.h"
|
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2021-04-23 10:34:24 +08:00
|
|
|
#include "flow/flow.h"
|
2021-04-24 01:47:56 +08:00
|
|
|
#include "flow/network.h"
|
2021-04-14 04:21:04 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
ThreadCallback* ThreadCallback::addCallback(ThreadCallback* cb) {
|
|
|
|
return (new ThreadMultiCallback())->addCallback(this)->addCallback(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple thread object that sends the result
|
|
|
|
struct ThreadFutureSendObj {
|
|
|
|
void operator()() { tsav->send(Void()); }
|
|
|
|
ThreadSingleAssignmentVar<Void>* tsav;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A simple thread object that cancels the threadFuture
|
|
|
|
struct ThreadFutureCancelObj {
|
2021-04-23 10:34:24 +08:00
|
|
|
ThreadFutureCancelObj(ThreadFuture<Void> f) : f(f) {}
|
2021-04-14 04:21:04 +08:00
|
|
|
void operator()() { f.cancel(); }
|
|
|
|
ThreadFuture<Void> f;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This unit test should be running with TSAN enabled binary
|
|
|
|
TEST_CASE("/safeThreadFutureSend") {
|
2021-04-24 01:47:56 +08:00
|
|
|
// std::thread is not working in simulation at present, disable this in simulation
|
|
|
|
if (g_network->isSimulated())
|
|
|
|
return Void();
|
2021-04-14 04:21:04 +08:00
|
|
|
auto* tsav = new ThreadSingleAssignmentVar<Void>;
|
|
|
|
state std::thread thread = std::thread{ ThreadFutureSendObj{ tsav } };
|
|
|
|
ThreadFuture<Void> f(tsav);
|
|
|
|
// change this to unsafeThreadFutureToFuture will get a data-race failure
|
|
|
|
wait(safeThreadFutureToFuture(f));
|
|
|
|
thread.join();
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:34:24 +08:00
|
|
|
// Test the case where the underlying threadFuture is cancelled
|
2021-04-14 04:21:04 +08:00
|
|
|
TEST_CASE("/safeThreadFutureCancel") {
|
2021-04-24 01:47:56 +08:00
|
|
|
// std::thread is not working in simulation at present, disable this in simulation
|
|
|
|
if (g_network->isSimulated())
|
|
|
|
return Void();
|
2021-04-23 10:34:24 +08:00
|
|
|
ThreadFuture<Void> f = onMainThread([]() -> Future<Void> { return Never(); });
|
|
|
|
state std::thread thread = std::thread{ ThreadFutureCancelObj(f) };
|
2021-04-14 04:21:04 +08:00
|
|
|
try {
|
2021-04-23 10:34:24 +08:00
|
|
|
wait(safeThreadFutureToFuture(f)); // this actor should get actor_cancelled
|
2021-04-14 04:21:04 +08:00
|
|
|
ASSERT(false);
|
|
|
|
} catch (Error& e) {
|
|
|
|
ASSERT(e.code() == error_code_actor_cancelled);
|
|
|
|
}
|
|
|
|
thread.join();
|
|
|
|
return Void();
|
2021-04-14 04:42:19 +08:00
|
|
|
}
|