Max OS 0.3
Loading...
Searching...
No Matches
resource.cpp
Go to the documentation of this file.
1
10
12
13using namespace MaxOS;
14using namespace MaxOS::processes;
15using namespace MaxOS::processes::resources;
16
27
35
45
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}
69
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}
92
93
100void BaseResourceRegistry::close_resource(Resource* resource, size_t flags) {
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}
126
134Resource* BaseResourceRegistry::create_resource(string const& name, size_t flags, uintptr_t data) {
135 return nullptr;
136}
137
142 s_current = this;
143
144}
145
150 if(s_current == this)
151 s_current = nullptr;
152
153}
154
162
163 auto registry = s_current->m_registries.find(type);
164 if(registry == s_current->m_registries.end())
165 return nullptr;
166
167 return registry->second;
168}
169
177
178 // Does it already exist?
179 if(s_current->m_registries.find(type) != s_current->m_registries.end())
180 return;
181
182 s_current->m_registries.insert(type, registry);
183}
184
191
192 // Does it already exist?
193 if(s_current->m_registries.find(registry->type()) == s_current->m_registries.end())
194 return;
195
196 s_current->m_registries.erase(registry->type());
197
198}
199
200ResourceManager::ResourceManager() = default;
201
206
207 // Collect all resources (as closing will break iteration)
208 common::Vector<uint64_t> handles;
209 for (const auto& kv : m_resources)
210 handles.push_back(kv.first);
211
212 // Close the resources
213 for (auto h : handles)
214 close_resource(h, 0);
215
216}
217
223common::Map<uint64_t, Resource*> ResourceManager::resources() {
224
225 return m_resources;
226}
227
236uint64_t ResourceManager::open_resource(resource_type_t type, string const& name, size_t flags) {
237
238 // Make sure there is a registry of the requested type
239 auto registry = GlobalResourceRegistry::get_registry(type);
240 if(!registry)
241 return 0;
242
243 // Make sure the registry holds the requested resource
244 auto resource = registry -> get_resource(name);
245 if(!resource)
246 return 0;
247
248 // Store it
249 m_resources.insert(m_next_handle, resource);
250
251 // Open it
252 resource->open(flags);
253 return m_next_handle++;
254}
255
262void ResourceManager::close_resource(uint64_t handle, size_t flags) {
263
264 auto it = m_resources.find(handle);
265 if(it == m_resources.end())
266 return;
267
268 // Remove it
269 Resource* resource = it->second;
270 m_resources.erase(handle);
271
272 // Close it
273 GlobalResourceRegistry::get_registry(resource->type()) -> close_resource(resource, flags);
274}
275
283
284 if(m_resources.find(handle) == m_resources.end())
285 return nullptr;
286
287 return m_resources[handle];
288}
289
297
298 for(const auto& resource : m_resources)
299 if(resource.second->name() == name)
300 return resource.second;
301
302 return nullptr;
303}
304
305
Manages the creation, retention and destruction of resources of a certain type. Should be subclassed ...
Definition resource.h:32
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
common::Spinlock m_lock
The lock to make the registry threadsafe.
Definition resource.h:38
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:52
void close_resource(Resource *resource, size_t flags)
Close the resource provided, if it exists in this registry.
Definition resource.cpp:100
~BaseResourceRegistry()
Destructor for BaseResourceRegistry, removes it from the global list.
Definition resource.cpp:31
common::Map< string, uint64_t > m_resource_uses
The map of resource names to how many processes are using them.
Definition resource.h:36
BaseResourceRegistry(resource_type_t type)
Constructs a new BaseResourceRegistry object and registers it globally.
Definition resource.cpp:22
common::Map< string, Resource * > m_resources
The map of resource names to resource instances.
Definition resource.h:35
bool register_resource(Resource *resource)
Registers a resource in the registry.
Definition resource.cpp:76
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.
Definition resource.cpp:134
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
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:161
~GlobalResourceRegistry()
Destructor for GlobalResourceRegistry, unsets it as the current global registry if it is.
Definition resource.cpp:149
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
GlobalResourceRegistry()
Constructs a new GlobalResourceRegistry and sets it as the current global registry.
Definition resource.cpp:141
Resource * get_resource(uint64_t handle)
Gets a resource based on the handle id.
Definition resource.cpp:282
void close_resource(uint64_t handle, size_t flags)
Un registers the resource and then closes it.
Definition resource.cpp:262
~ResourceManager()
Destructor for ResourceManager, closes all open resources.
Definition resource.cpp:205
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
Definition resource.cpp:223
uint64_t open_resource(resource_type_t type, const string &name, size_t flags)
Registers a resource with the resource manager and then opens it.
Definition resource.cpp:236
Defines classes for managing process resources such as files and devices.
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
Definition resource.h:26
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:25
Defines scheduler resource classes for wrapping Processe and Thread objects as Resources in the Globa...