Detailed Description
Resources to be managed by a resource manager.
Terms
Resource : a pointer to anything which needs to be replaced or freed in a thread-safe manor
Manager : manages access to a resource by handing out handles and altering the resource safely
Handle : a reference to a resource which is being managed. This is the only way in which a resource should be accessed
Introduction
A Resource is a structure which can be changed or freed at any time, so is managed by a resource manager to allow safe concurrent access. Any resource which is being used will not be changed or freed until the last reference to it has been released.
Create
A resource manager is created by allocating the memory for the structure and calling the fiftyoneDegreesResourceManagerInit method to give it a resource to manage, and a method to free it when necessary.
Get
A handle to a resource can be fetched from a resource manager using the fiftyoneDegreesResourceHandleIncUse method. This increments the "in use" counter in a thread safe manor and returns an exclusive handle to the resource.
When getting a handle to a resource, if all the available handles are in use then the method will block until one is available.
Release
Once a resource handle is finished with, it must be released back to the resource manager using the fiftyoneDegreesResourceHandleDecUse method. This releases the handle so it is available to other threads.
Free
A resource manager is freed, along with its resource, using the fiftyoneDegreesResourceManagerFree method. This prevents any new handles from being acquired, and frees the resource being managed. If the resource has active handles, then a free method does not block. Instead it prevents new handles from being acquired and sets the manager to free the resource once the last handle has been released.
Replace
A resource can be replaced using the fiftyoneDegreesResourceReplace method. This swaps the resource being managed so that any new requests for a handle return the new resource. The existing resource is freed once the last active handle to it has been released.
Usage Example
typedef struct someResourceType {
void *data;
}
someResourceType *resource;
&manager,
resource,
&resource->handle,
if (resource->handle != NULL) {
someResourceType *localResource = (someResourceType*)
}
Collaboration diagram for Resource Manager:
Structs
Function Documentation
◆ fiftyoneDegreesResourceHandleDecUse()
Decrements the usage counter.
If the count reaches zero then resource will become eligible to be freed either when the manager replaces it or when the manager is freed.
- Parameters
-
- handle - to the resource which should be released by the manager
|
◆ fiftyoneDegreesResourceHandleGetUse()
Return the current usage counter.
WARNING: This call is not thread-safe and is suitable for using in testing only.
- Parameters
-
- handle - to the resource which should be released by the manager
|
- Returns
- the current usage counter
◆ fiftyoneDegreesResourceHandleIncUse()
Increments the usage counter for the resource and returns a handle that can be used to reference it.
The handle MUST be used to decrement the use count using the fiftyoneDegreesResourceHandleDecUse method when the resource is finished with. The resource can be guaranteed not to be freed until after the decrement method has been called.
- Parameters
-
- manager - the resource manager to initialise with the resource
|
◆ fiftyoneDegreesResourceManagerFree()
Frees any data associated with the manager and releases the manager.
All memory is released after this operation.
- Parameters
-
- manager - the resource manager to be freed
|
- Examples
- Hash/MatchMetrics.c, and Hash/StronglyTyped.c.
◆ fiftyoneDegreesResourceManagerInit()
Initialise a preallocated resource manager structure with a resource for it to manage access to.
The resourceHandle parameter must point to the handle within the resource under management so that the handle can be assigned to the resource before the resource is placed under management.
- Parameters
-
- manager - the resource manager to initialise with the resource
|
- resource - pointer to the resource which the manager should manage access to
|
- resourceHandle - points to the location the new handle should be stored
|
- freeResource - method to use when freeing the resource
|
◆ fiftyoneDegreesResourceReplace()
Replaces the resource with the new resource.
If the existing resource is not being used it will be freed. Otherwise it is left to the decrement function to free the resource when the usage count is zero.
- Parameters
-
- manager - the resource manager to initialise with the resource
|
- newResource - pointer to the resource which the manager should manage access to
|
- newResourceHandle - points to the location the new handle should be stored
|