2017-11-29 21:05:31 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-03-02 21:16:49 +08:00
|
|
|
#ifndef CARTOGRAPHER_GRPC_INTERNAL_FRAMEWORK_EXECUTION_CONTEXT_H
|
|
|
|
#define CARTOGRAPHER_GRPC_INTERNAL_FRAMEWORK_EXECUTION_CONTEXT_H
|
2017-11-29 21:05:31 +08:00
|
|
|
|
|
|
|
#include "cartographer/common/mutex.h"
|
|
|
|
#include "glog/logging.h"
|
|
|
|
|
2018-03-03 06:21:28 +08:00
|
|
|
namespace cartographer {
|
|
|
|
namespace cloud {
|
2017-11-29 21:05:31 +08:00
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
// Implementations of this class allow RPC handlers to share state among one
|
|
|
|
// another. Using Server::SetExecutionContext(...) a server-wide
|
|
|
|
// 'ExecutionContext' can be specified. This 'ExecutionContext' can be retrieved
|
|
|
|
// by all implementations of 'RpcHandler' by calling
|
|
|
|
// 'RpcHandler::GetContext<MyContext>()'.
|
|
|
|
class ExecutionContext {
|
|
|
|
public:
|
2017-12-13 05:36:44 +08:00
|
|
|
// Automatically locks an ExecutionContext for shared use by RPC handlers.
|
2017-11-29 21:05:31 +08:00
|
|
|
// This non-movable, non-copyable class is used to broker access from various
|
2017-12-13 05:36:44 +08:00
|
|
|
// RPC handlers to the shared 'ExecutionContext'.
|
2017-11-29 21:05:31 +08:00
|
|
|
template <typename ContextType>
|
|
|
|
class Synchronized {
|
|
|
|
public:
|
|
|
|
ContextType* operator->() {
|
|
|
|
return static_cast<ContextType*>(execution_context_);
|
|
|
|
}
|
2018-03-03 06:21:28 +08:00
|
|
|
Synchronized(common::Mutex* lock, ExecutionContext* execution_context)
|
2017-11-29 21:05:31 +08:00
|
|
|
: locker_(lock), execution_context_(execution_context) {}
|
|
|
|
Synchronized(const Synchronized&) = delete;
|
|
|
|
Synchronized(Synchronized&&) = delete;
|
|
|
|
|
|
|
|
private:
|
2018-03-03 06:21:28 +08:00
|
|
|
common::MutexLocker locker_;
|
2017-11-29 21:05:31 +08:00
|
|
|
ExecutionContext* execution_context_;
|
|
|
|
};
|
2018-02-01 02:22:08 +08:00
|
|
|
ExecutionContext() = default;
|
2017-12-13 05:36:44 +08:00
|
|
|
virtual ~ExecutionContext() = default;
|
2018-02-01 02:22:08 +08:00
|
|
|
ExecutionContext(const ExecutionContext&) = delete;
|
|
|
|
ExecutionContext& operator=(const ExecutionContext&) = delete;
|
2018-03-03 06:21:28 +08:00
|
|
|
common::Mutex* lock() { return &lock_; }
|
2017-11-29 21:05:31 +08:00
|
|
|
|
|
|
|
private:
|
2018-03-03 06:21:28 +08:00
|
|
|
common::Mutex lock_;
|
2017-11-29 21:05:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
2018-03-03 06:21:28 +08:00
|
|
|
} // namespace cloud
|
|
|
|
} // namespace cartographer
|
2017-11-29 21:05:31 +08:00
|
|
|
|
2018-03-02 21:16:49 +08:00
|
|
|
#endif // CARTOGRAPHER_GRPC_INTERNAL_FRAMEWORK_EXECUTION_CONTEXT_H
|