forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy
BackgroundThreads
from google::cloud::spanner
, planning to
later remove the class from `g-c-cpp-spanner`.
- Loading branch information
Showing
7 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BACKGROUND_THREADS_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BACKGROUND_THREADS_H | ||
|
||
#include "google/cloud/completion_queue.h" | ||
#include "google/cloud/version.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
/** | ||
* A object representing the background threads available to a Client. | ||
*/ | ||
class BackgroundThreads { | ||
public: | ||
virtual ~BackgroundThreads() = default; | ||
|
||
/// The completion queue used for the background operations. | ||
virtual CompletionQueue cq() const = 0; | ||
}; | ||
|
||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BACKGROUND_THREADS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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 "google/cloud/internal/background_threads_impl.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace internal { | ||
|
||
AutomaticallyCreatedBackgroundThreads::AutomaticallyCreatedBackgroundThreads() | ||
: runner_([](CompletionQueue cq) { cq.Run(); }, cq_) {} | ||
|
||
AutomaticallyCreatedBackgroundThreads:: | ||
~AutomaticallyCreatedBackgroundThreads() { | ||
Shutdown(); | ||
} | ||
|
||
void AutomaticallyCreatedBackgroundThreads::Shutdown() { | ||
cq_.Shutdown(); | ||
if (runner_.joinable()) runner_.join(); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_BACKGROUND_THREADS_IMPL_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_BACKGROUND_THREADS_IMPL_H | ||
|
||
#include "google/cloud/background_threads.h" | ||
#include "google/cloud/completion_queue.h" | ||
#include <thread> | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace internal { | ||
|
||
/// Assume the user has provided the background threads and use them. | ||
class CustomerSuppliedBackgroundThreads : public BackgroundThreads { | ||
public: | ||
explicit CustomerSuppliedBackgroundThreads(CompletionQueue cq) | ||
: cq_(std::move(cq)) {} | ||
~CustomerSuppliedBackgroundThreads() override = default; | ||
|
||
CompletionQueue cq() const override { return cq_; } | ||
|
||
private: | ||
CompletionQueue cq_; | ||
}; | ||
|
||
/// Create a background thread to perform background operations. | ||
class AutomaticallyCreatedBackgroundThreads : public BackgroundThreads { | ||
public: | ||
AutomaticallyCreatedBackgroundThreads(); | ||
~AutomaticallyCreatedBackgroundThreads() override; | ||
|
||
CompletionQueue cq() const override { return cq_; } | ||
void Shutdown(); | ||
|
||
private: | ||
CompletionQueue cq_; | ||
std::thread runner_; | ||
}; | ||
|
||
} // namespace internal | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_BACKGROUND_THREADS_IMPL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// 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 "google/cloud/internal/background_threads_impl.h" | ||
#include <gmock/gmock.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
inline namespace GOOGLE_CLOUD_CPP_NS { | ||
namespace internal { | ||
namespace { | ||
|
||
/// @test Verify we can create and use a CustomerSuppliedBackgroundThreads | ||
/// without impacting the completion queue | ||
TEST(CustomerSuppliedBackgroundThreads, LifecycleNoShutdown) { | ||
CompletionQueue cq; | ||
promise<void> p; | ||
std::thread t([&cq, &p] { | ||
cq.Run(); | ||
p.set_value(); | ||
}); | ||
|
||
{ CustomerSuppliedBackgroundThreads actual(cq); } | ||
|
||
using ms = std::chrono::milliseconds; | ||
|
||
auto has_shutdown = p.get_future(); | ||
EXPECT_NE(std::future_status::ready, has_shutdown.wait_for(ms(2))); | ||
|
||
auto expired = cq.MakeRelativeTimer(ms(0)); | ||
EXPECT_EQ(std::future_status::ready, expired.wait_for(ms(100))); | ||
|
||
cq.Shutdown(); | ||
EXPECT_EQ(std::future_status::ready, has_shutdown.wait_for(ms(100))); | ||
|
||
t.join(); | ||
} | ||
|
||
/// @test Verify that users can supply their own queue and threads. | ||
TEST(CustomerSuppliedBackgroundThreads, SharesCompletionQueue) { | ||
CompletionQueue cq; | ||
|
||
CustomerSuppliedBackgroundThreads actual(cq); | ||
|
||
using ms = std::chrono::milliseconds; | ||
// Verify the completion queue is shared. Scheduling work in actual.cq() works | ||
// once a thread is blocked in cq.Run(). Start that thread after scheduling | ||
// the work to avoid flaky failures where the timer expires immediately. | ||
future<std::thread::id> id = actual.cq().MakeRelativeTimer(ms(1)).then( | ||
[](future<StatusOr<std::chrono::system_clock::time_point>>) { | ||
return std::this_thread::get_id(); | ||
}); | ||
std::thread t([&cq] { cq.Run(); }); | ||
EXPECT_EQ(std::future_status::ready, id.wait_for(ms(100))); | ||
EXPECT_EQ(t.get_id(), id.get()); | ||
|
||
cq.Shutdown(); | ||
t.join(); | ||
} | ||
|
||
/// @test Verify that automatically created completion queues are usable. | ||
TEST(AutomaticallyCreatedBackgroundThreads, IsActive) { | ||
AutomaticallyCreatedBackgroundThreads actual; | ||
|
||
using ms = std::chrono::milliseconds; | ||
|
||
auto expired = actual.cq().MakeRelativeTimer(ms(0)); | ||
EXPECT_EQ(std::future_status::ready, expired.wait_for(ms(100))); | ||
} | ||
|
||
} // namespace | ||
} // namespace internal | ||
} // namespace GOOGLE_CLOUD_CPP_NS | ||
} // namespace cloud | ||
} // namespace google |