Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::resources::BaseResourceRegistry Class Reference

Manages the creation, retention and destruction of resources of a certain type. Should be subclassed for each resource type. More...

#include <resource.h>

Inheritance diagram for MaxOS::processes::resources::BaseResourceRegistry:
MaxOS::processes::resources::ResourceRegistry< BridgeResource > MaxOS::processes::resources::ResourceRegistry< ProcessResource > MaxOS::processes::resources::ResourceRegistry< ThreadResource > MaxOS::processes::resources::ResourceRegistry< MaxOS::processes::SharedMemory > MaxOS::processes::resources::ResourceRegistry< MaxOS::processes::SharedMessageEndpoint > MaxOS::processes::resources::ResourceRegistry< Type > MaxOS::processes::resources::BridgeResourceRegistry MaxOS::processes::resources::ProcessResourceRegistry MaxOS::processes::resources::ThreadResourceRegistry

Public Member Functions

 BaseResourceRegistry (resource_type_t type)
 Constructs a new BaseResourceRegistry object and registers it globally.
 
 ~BaseResourceRegistry ()
 Destructor for BaseResourceRegistry, removes it from the global list.
 
resource_type_t type ()
 Gets the type of resources this registry handles.
 
bool register_resource (Resource *resource)
 Registers a resource in the registry.
 
void close_resource (Resource *resource, size_t flags)
 Close the resource provided, if it exists in this registry.
 
virtual Resource * get_resource (const string &name)
 Gets a resource from the registry by name.
 
virtual Resource * create_resource (const string &name, size_t flags, uintptr_t data)
 Try to create a new resource with the specified name, will fail if the name is in use.
 

Protected Attributes

common::Map< string, Resource * > m_resources
 The map of resource names to resource instances.
 
common::Map< string, uint64_t > m_resource_uses
 The map of resource names to how many processes are using them.
 
common::Spinlock m_lock
 The lock to make the registry threadsafe.
 
resource_type_t m_type
 The resource type that this registry manages.
 

Detailed Description

Manages the creation, retention and destruction of resources of a certain type. Should be subclassed for each resource type.

Definition at line 32 of file resource.h.

Constructor & Destructor Documentation

◆ BaseResourceRegistry()

BaseResourceRegistry::BaseResourceRegistry ( resource_type_t  type)
explicit

Constructs a new BaseResourceRegistry object and registers it globally.

Parameters
typeThe type of resources this registry will handle

Definition at line 22 of file resource.cpp.

23: m_type(type)
24{
26}
resource_type_t m_type
The resource type that this registry manages.
Definition resource.h:40
resource_type_t type()
Gets the type of resources this registry handles.
Definition resource.cpp:41
static void add_registry(resource_type_t type, BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:176

References MaxOS::processes::resources::GlobalResourceRegistry::add_registry(), and type().

◆ ~BaseResourceRegistry()

BaseResourceRegistry::~BaseResourceRegistry ( )

Destructor for BaseResourceRegistry, removes it from the global list.

Definition at line 31 of file resource.cpp.

31 {
32
34}
static void remove_registry(BaseResourceRegistry *registry)
Adds a registry to the global list if there is not already one for that type.
Definition resource.cpp:190

References MaxOS::processes::resources::GlobalResourceRegistry::remove_registry().

Member Function Documentation

◆ close_resource()

void BaseResourceRegistry::close_resource ( Resource *  resource,
size_t  flags 
)

Close the resource provided, if it exists in this registry.

Parameters
resourceThe resource to close
flagsOptional flags to pass (unused by default but registry type specific)

Definition at line 100 of file resource.cpp.

100 {
101
102 m_lock.lock();
103
104 // Resource isn't stored in this registry
105 if(m_resources.find(resource->name()) == m_resources.end()) {
106 m_lock.unlock();
107 return;
108 }
109
110 m_resource_uses[resource->name()]--;
111
112 // Don't close a resource that has more processes using it
113 if(m_resource_uses[resource->name()]) {
114 m_lock.unlock();
115 return;
116 }
117
118 // Can safely close the resource
119 resource->close(flags);
120 m_resources.erase(resource->name());
121 m_resource_uses.erase(resource->name());
122
123 m_lock.unlock();
124 delete resource;
125}
common::Spinlock m_lock
The lock to make the registry threadsafe.
Definition resource.h:38
common::Map< string, uint64_t > m_resource_uses
The map of resource names to how many processes are using them.
Definition resource.h:36
common::Map< string, Resource * > m_resources
The map of resource names to resource instances.
Definition resource.h:35

References m_lock, m_resource_uses, and m_resources.

◆ create_resource()

Resource * BaseResourceRegistry::create_resource ( const string &  name,
size_t  flags,
uintptr_t  data 
)
virtual

◆ get_resource()

Resource * BaseResourceRegistry::get_resource ( const string &  name)
virtual

Gets a resource from the registry by name.

Parameters
nameThe name of the resource to find
Returns
The resource or nullptr if the resource was not found

Reimplemented in MaxOS::processes::resources::BridgeResourceRegistry, MaxOS::processes::resources::ThreadResourceRegistry, and MaxOS::processes::resources::ProcessResourceRegistry.

Definition at line 52 of file resource.cpp.

52 {
53
54 m_lock.lock();
55
56 // Resource isn't stored in this registry
57 if(m_resources.find(name) == m_resources.end()) {
58 m_lock.unlock();
59 return nullptr;
60 }
61
62 // Increment the use count of the resource and return it
63 m_resource_uses[name]++;
64 auto resource = m_resources[name];
65
66 m_lock.unlock();
67 return resource;
68}

References m_lock, m_resource_uses, and m_resources.

Referenced by MaxOS::processes::resources::BridgeHandler::BridgeHandler(), MaxOS::processes::resources::BridgeResourceRegistry::get_resource(), MaxOS::processes::resources::ThreadResourceRegistry::get_resource(), and MaxOS::processes::resources::ProcessResourceRegistry::get_resource().

◆ register_resource()

bool BaseResourceRegistry::register_resource ( Resource *  resource)

Registers a resource in the registry.

Parameters
resourceThe resource to store
Returns
True if the register was successful, false if not

Definition at line 76 of file resource.cpp.

76 {
77
78 m_lock.lock();
79
80 // Resource name is already stored in this registry
81 if(m_resources.find(resource->name()) != m_resources.end()) {
82 m_lock.unlock();
83 return false;
84 }
85
86 m_resources.insert(resource->name(), resource);
87 m_resource_uses.insert(resource->name(), 0);
88
89 m_lock.unlock();
90 return true;
91}

References m_lock, m_resource_uses, and m_resources.

Referenced by MaxOS::processes::resources::BridgeResourceRegistry::create_resource(), MaxOS::processes::resources::ResourceRegistry< Type >::create_resource(), MaxOS::processes::resources::BridgeResourceRegistry::get_resource(), MaxOS::processes::resources::ThreadResourceRegistry::get_resource(), and MaxOS::processes::resources::ProcessResourceRegistry::get_resource().

◆ type()

resource_type_t BaseResourceRegistry::type ( )

Gets the type of resources this registry handles.

Returns
The type

Definition at line 41 of file resource.cpp.

41 {
42
43 return m_type;
44}

References m_type.

Referenced by BaseResourceRegistry(), MaxOS::processes::resources::ResourceRegistry< Type >::create_resource(), and MaxOS::processes::resources::GlobalResourceRegistry::remove_registry().

Member Data Documentation

◆ m_lock

common::Spinlock MaxOS::processes::resources::BaseResourceRegistry::m_lock
protected

The lock to make the registry threadsafe.

Definition at line 38 of file resource.h.

Referenced by close_resource(), get_resource(), and register_resource().

◆ m_resource_uses

common::Map<string, uint64_t> MaxOS::processes::resources::BaseResourceRegistry::m_resource_uses
protected

The map of resource names to how many processes are using them.

Definition at line 36 of file resource.h.

Referenced by close_resource(), get_resource(), and register_resource().

◆ m_resources

common::Map<string, Resource*> MaxOS::processes::resources::BaseResourceRegistry::m_resources
protected

The map of resource names to resource instances.

Definition at line 35 of file resource.h.

Referenced by close_resource(), get_resource(), and register_resource().

◆ m_type

resource_type_t MaxOS::processes::resources::BaseResourceRegistry::m_type
protected

The documentation for this class was generated from the following files: