77#include <filesystem>
88#include <memory>
99#include <vector>
10+ #include <shared_mutex>
1011#include <string>
1112
1213#include "core/common/common.h"
2021#include "core/platform/threadpool.h"
2122
2223#include "core/session/abi_devices.h"
24+ #include "core/session/abi_key_value_pairs.h"
2325#include "core/session/plugin_ep/ep_library.h"
2426#include "core/session/onnxruntime_c_api.h"
2527
@@ -51,11 +53,13 @@ class Environment {
5153 @param tp_options optional set of parameters controlling the number of intra and inter op threads for the global
5254 threadpools.
5355 @param create_global_thread_pools determine if this function will create the global threadpools or not.
56+ @param config_entries Application-specified configuration entries.
5457 */
5558 static Status Create(std::unique_ptr<logging::LoggingManager> logging_manager,
5659 std::unique_ptr<Environment>& environment,
5760 const OrtThreadingOptions* tp_options = nullptr,
58- bool create_global_thread_pools = false);
61+ bool create_global_thread_pools = false,
62+ const OrtKeyValuePairs* config_entries = nullptr);
5963
6064 /**
6165 * Set the global threading options for the environment, if no global thread pools have been created yet.
@@ -170,14 +174,26 @@ class Environment {
170174 // return a shared allocator from a plugin EP or custom allocator added with RegisterAllocator
171175 Status GetSharedAllocator(const OrtMemoryInfo& mem_info, OrtAllocator*& allocator);
172176
177+ /// <summary>
178+ /// Returns a copy of the configuration entries set by the application on environment creation.
179+ ///
180+ /// Primarily used by EP libraries to retrieve environment-level configurations, but could be used
181+ /// more generally to specify global settings.
182+ ///
183+ /// Refer to OrtApi::CreateEnvWithOptions().
184+ /// </summary>
185+ /// <returns></returns>
186+ OrtKeyValuePairs GetConfigEntries() const;
187+
173188 ~Environment();
174189
175190 private:
176191 ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Environment);
177192
178193 Status Initialize(std::unique_ptr<logging::LoggingManager> logging_manager,
179194 const OrtThreadingOptions* tp_options = nullptr,
180- bool create_global_thread_pools = false);
195+ bool create_global_thread_pools = false,
196+ const OrtKeyValuePairs* config_entries = nullptr);
181197
182198 Status RegisterAllocatorImpl(AllocatorPtr allocator);
183199 Status UnregisterAllocatorImpl(const OrtMemoryInfo& mem_info, bool error_if_not_found = true);
@@ -186,6 +202,13 @@ class Environment {
186202 const OrtKeyValuePairs* allocator_options, OrtAllocator** allocator,
187203 bool replace_existing);
188204
205+ // Inserts (or assigns) a config entry into `config_entries_`. Locks `config_entries_mutex_`.
206+ void InsertOrAssignConfigEntry(std::string key, std::string value);
207+
208+ // Removes a config entry from `config_entries_`. Does nothing if the key does not exist.
209+ // Locks `config_entries_mutex_`.
210+ void RemoveConfigEntry(const std::string& key);
211+
189212 std::unique_ptr<logging::LoggingManager> logging_manager_;
190213 std::unique_ptr<onnxruntime::concurrency::ThreadPool> intra_op_thread_pool_;
191214 std::unique_ptr<onnxruntime::concurrency::ThreadPool> inter_op_thread_pool_;
@@ -254,6 +277,20 @@ class Environment {
254277 DataTransferManager data_transfer_mgr_; // plugin EP IDataTransfer instances
255278
256279#endif // !defined(ORT_MINIMAL_BUILD)
280+
281+ // Application-specified environment configuration entries
282+ // The environment may add or remove an entry on EP library registration and unregistration, respectively.
283+ OrtKeyValuePairs config_entries_;
284+ mutable std::shared_mutex config_entries_mutex_; // Should be locked when accessing config_entries_
285+
286+ // Tracks the number of registered EP libraries that can create virtual devices.
287+ // It is incremented when an EP library is registered with a name that ends in ".virtual".
288+ // It is decremented when that EP library is unregistered.
289+ // If it reaches 0, the config entry "allow_virtual_devices" is removed.
290+ //
291+ // This starts at 1 if user created an OrtEnv with the config "allow_virtual_devices" set to "1"
292+ // to prevent removal of the config entry in that case.
293+ size_t num_allow_virtual_device_uses_{};
257294};
258295
259296} // namespace onnxruntime
0 commit comments