mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-08-21 17:35:06 +00:00
Remove try_get and put functions for services
This commit is contained in:
parent
469cf0d275
commit
e801dcf397
@ -86,19 +86,6 @@ void* service_instance_get_data(struct ServiceInstance* instance);
|
|||||||
*/
|
*/
|
||||||
ServiceState service_instance_get_state(struct ServiceInstance* instance);
|
ServiceState service_instance_get_state(struct ServiceInstance* instance);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Try to claim usage for this service instance. Increases reference count internally.
|
|
||||||
* @param instance non-null service instance pointer
|
|
||||||
* @return true when the instance is started and ref count was increased.
|
|
||||||
*/
|
|
||||||
bool service_instance_try_get(struct ServiceInstance* instance);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Release a claim for usage of this service instance. Decreases reference count internally.
|
|
||||||
* @param instance non-null service instance pointer
|
|
||||||
*/
|
|
||||||
void service_instance_put(struct ServiceInstance* instance);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -31,6 +31,13 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
error_t service_manager_remove(const char* id);
|
error_t service_manager_remove(const char* id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Find a registered manifest by id.
|
||||||
|
* @param[in] id non-null service id
|
||||||
|
* @return the manifest, or NULL if not found
|
||||||
|
*/
|
||||||
|
const struct ServiceManifest* service_manager_find_manifest(const char* id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Start a registered service by id.
|
* @brief Start a registered service by id.
|
||||||
* @param[in] id non-null service id
|
* @param[in] id non-null service id
|
||||||
@ -56,13 +63,6 @@ error_t service_manager_stop(const char* id);
|
|||||||
*/
|
*/
|
||||||
ServiceState service_manager_get_state(const char* id);
|
ServiceState service_manager_get_state(const char* id);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Find a registered manifest by id.
|
|
||||||
* @param[in] id non-null service id
|
|
||||||
* @return the manifest, or NULL if not found
|
|
||||||
*/
|
|
||||||
const struct ServiceManifest* service_manager_find_manifest(const char* id);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Find the context of a running service by id.
|
* @brief Find the context of a running service by id.
|
||||||
* @param[in] id non-null service id
|
* @param[in] id non-null service id
|
||||||
|
|||||||
@ -12,7 +12,6 @@
|
|||||||
struct ServiceInstanceInternal {
|
struct ServiceInstanceInternal {
|
||||||
Mutex mutex {};
|
Mutex mutex {};
|
||||||
ServiceState state = SERVICE_STATE_STOPPED;
|
ServiceState state = SERVICE_STATE_STOPPED;
|
||||||
uint32_t use_count = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -84,22 +83,4 @@ const ServiceManifest* service_context_get_manifest(ServiceContext* context) {
|
|||||||
return service_instance_get_manifest(context);
|
return service_instance_get_manifest(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool service_instance_try_get(struct ServiceInstance* instance) {
|
|
||||||
mutex_lock(&instance->internal->mutex);
|
|
||||||
bool acquired = instance->internal->state == SERVICE_STATE_STARTED;
|
|
||||||
if (acquired) {
|
|
||||||
instance->internal->use_count++;
|
|
||||||
}
|
|
||||||
mutex_unlock(&instance->internal->mutex);
|
|
||||||
return acquired;
|
|
||||||
}
|
|
||||||
|
|
||||||
void service_instance_put(struct ServiceInstance* instance) {
|
|
||||||
mutex_lock(&instance->internal->mutex);
|
|
||||||
if (instance->internal->use_count > 0) {
|
|
||||||
instance->internal->use_count--;
|
|
||||||
}
|
|
||||||
mutex_unlock(&instance->internal->mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|||||||
@ -68,35 +68,6 @@ TEST_CASE("ServiceInstance construction and destruction") {
|
|||||||
CHECK_EQ(last_destroy_context, &context_marker);
|
CHECK_EQ(last_destroy_context, &context_marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("service_instance_try_get/put reference counting") {
|
|
||||||
reset_counters();
|
|
||||||
|
|
||||||
static const ServiceManifest manifest = {
|
|
||||||
.id = "refcount-test",
|
|
||||||
.create_service = test_create_service,
|
|
||||||
.destroy_service = test_destroy_service
|
|
||||||
};
|
|
||||||
|
|
||||||
ServiceInstance instance = { .manifest = nullptr, .data = nullptr, .on_start = nullptr, .on_stop = nullptr, .internal = nullptr };
|
|
||||||
CHECK_EQ(service_instance_construct(&instance, &manifest), ERROR_NONE);
|
|
||||||
|
|
||||||
// Not started yet: claiming usage should fail
|
|
||||||
CHECK_FALSE(service_instance_try_get(&instance));
|
|
||||||
|
|
||||||
service_instance_set_state(&instance, SERVICE_STATE_STARTED);
|
|
||||||
|
|
||||||
CHECK(service_instance_try_get(&instance));
|
|
||||||
CHECK(service_instance_try_get(&instance));
|
|
||||||
service_instance_put(&instance);
|
|
||||||
service_instance_put(&instance);
|
|
||||||
|
|
||||||
// Extra put beyond the claimed count should not underflow
|
|
||||||
service_instance_put(&instance);
|
|
||||||
|
|
||||||
service_instance_set_state(&instance, SERVICE_STATE_STOPPED);
|
|
||||||
CHECK_EQ(service_instance_destruct(&instance), ERROR_NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("service_manager_add rejects duplicate ids") {
|
TEST_CASE("service_manager_add rejects duplicate ids") {
|
||||||
reset_counters();
|
reset_counters();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user