mirror of https://github.com/grpc/grpc.git
Reintroduce the EventEngine default factory (#27920)
* Reintroduce the EventEngine default factory An application can provide an EventEngine factory function that allows gRPC internals to create EventEngines as needed. This factory would be used when no EventEngine is provided for some given channel or server, and where an EventEngine otherwise could not be provided by the application. Note that there currently is no API to provide an EventEngine per channel or per server. I've also deleted some previous iterations on global EventEngine and EventEngine factory ideas. This new code lives in a public API, and coexists with iomgr instead of being isolated to an EventEngine-specific iomgr implementation. * add proper namespaces, and fix description * put factory functions in their own file (for replaceability) * add synchronization * generate_projects.sh * extract event_engine_base and event_engine_factory targets Also separate iomgr/event_engine files in the BUILD, with comments * gpr_platform * move all EE factory declarations to event_engine_base Makes internal hackery easier. * add missing deps * reorder dep alphabetically * comment style changepull/27972/head
parent
4d5cbfb6c2
commit
39adc01a57
25 changed files with 201 additions and 118 deletions
@ -0,0 +1,49 @@ |
||||
// Copyright 2021 The gRPC 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.
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/event_engine/event_engine.h> |
||||
|
||||
#include "src/core/lib/gprpp/sync.h" |
||||
|
||||
namespace grpc_event_engine { |
||||
namespace experimental { |
||||
|
||||
namespace { |
||||
std::function<std::unique_ptr<EventEngine>()>* g_event_engine_factory = nullptr; |
||||
grpc_core::Mutex* g_mu = new grpc_core::Mutex(); |
||||
} // namespace
|
||||
|
||||
EventEngine* GetDefaultEventEngine() { |
||||
static EventEngine* default_event_engine = CreateEventEngine().release(); |
||||
return default_event_engine; |
||||
} |
||||
|
||||
void SetDefaultEventEngineFactory( |
||||
std::function<std::unique_ptr<EventEngine>()>* factory) { |
||||
grpc_core::MutexLock lock(g_mu); |
||||
g_event_engine_factory = factory; |
||||
} |
||||
|
||||
std::unique_ptr<EventEngine> CreateEventEngine() { |
||||
grpc_core::MutexLock lock(g_mu); |
||||
if (g_event_engine_factory == nullptr) { |
||||
// TODO(hork): call LibuvEventEngineFactory
|
||||
abort(); |
||||
} |
||||
return (*g_event_engine_factory)(); |
||||
} |
||||
|
||||
} // namespace experimental
|
||||
} // namespace grpc_event_engine
|
@ -0,0 +1,33 @@ |
||||
// Copyright 2021 The gRPC 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.
|
||||
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H |
||||
#define GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/event_engine/event_engine.h> |
||||
|
||||
namespace grpc_event_engine { |
||||
namespace experimental { |
||||
|
||||
/// Access the shared global EventEngine instance.
|
||||
///
|
||||
/// The concept of a global EventEngine may go away in a post-iomgr world.
|
||||
/// Strongly consider whether you could use \a CreateEventEngine instead.
|
||||
EventEngine* GetDefaultEventEngine(); |
||||
|
||||
} // namespace experimental
|
||||
} // namespace grpc_event_engine
|
||||
|
||||
#endif // GRPC_CORE_LIB_EVENT_ENGINE_EVENT_ENGINE_FACTORY_H
|
@ -1,42 +0,0 @@ |
||||
// Copyright 2021 The gRPC 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.
|
||||
#ifndef GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H |
||||
#define GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H |
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/event_engine/event_engine.h> |
||||
|
||||
/// This can be called anywhere in the EventEngine-based iomgr impl where we
|
||||
/// need to access the global EventEngine instance.
|
||||
grpc_event_engine::experimental::EventEngine* grpc_iomgr_event_engine(); |
||||
|
||||
namespace grpc_core { |
||||
|
||||
/// Set the default \a EventEngine.
|
||||
///
|
||||
/// The iomgr interfaces conceptually expose a global singleton iomgr instance
|
||||
/// that is shared throughout gRPC. To accomodate an EventEngine-based iomgr
|
||||
/// implementation, this method sets the default EventEngine that will be used.
|
||||
/// The default EventEngine can only be set once during the lifetime of gRPC.
|
||||
/// This method must be called before \a grpc_init() (truly, before
|
||||
/// \a grpc_iomgr_init()). This engine is shut down along with iomgr.
|
||||
///
|
||||
/// This is an internal method, not intended for public use. Public APIs are
|
||||
/// being planned.
|
||||
void SetDefaultEventEngine( |
||||
std::unique_ptr<grpc_event_engine::experimental::EventEngine> event_engine); |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_IOMGR_EVENT_ENGINE_IOMGR_H
|
Loading…
Reference in new issue