Skip to content

Commit

Permalink
[CodeClean] internal function to release svc handle
Browse files Browse the repository at this point in the history
Prevent error case if nns-edge dependency does not exists.
Add internal function to release ml-svc handle.

Signed-off-by: Jaeyun Jung <[email protected]>
  • Loading branch information
jaeyun-jung committed Oct 5, 2023
1 parent e2ce8b4 commit 6b6db57
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 67 deletions.
23 changes: 23 additions & 0 deletions c/src/ml-api-service-agent-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,29 @@ ml_service_get_pipeline_state (ml_service_h h, ml_pipeline_state_e * state)
return ret;
}

/**
* @brief Internal function to release ml-service pipeline data.
*/
int
ml_service_pipeline_release_internal (void *priv)
{
_ml_service_server_s *server = (_ml_service_server_s *) priv;
g_autoptr (GError) err = NULL;
int ret;

ret = ml_agent_pipeline_destroy (server->id, &err);
if (ret < 0) {
_ml_error_report_return (ret,
"Failed to invoke the method destroy_pipeline (%s).",
err ? err->message : "Unknown error");
}

g_free (server->service_name);
g_free (server);

return ML_ERROR_NONE;
}

/**
* @brief Registers new information of a neural network model.
*/
Expand Down
67 changes: 17 additions & 50 deletions c/src/ml-api-service-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
* @bug No known bugs except for NYI items
*/

#include "ml-api-internal.h"
#include "ml-api-service.h"
#include "ml-api-service-private.h"
#include "ml-agent-interface.h"

/**
* @brief Destroy the pipeline of given ml_service_h
* @brief Destroy the service handle.
*/
int
ml_service_destroy (ml_service_h h)
Expand All @@ -30,53 +28,22 @@ ml_service_destroy (ml_service_h h)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'h' is NULL. It should be a valid ml_service_h.");

if (ML_SERVICE_TYPE_SERVER_PIPELINE == mls->type) {
_ml_service_server_s *server = (_ml_service_server_s *) mls->priv;
GError *err = NULL;

ret = ml_agent_pipeline_destroy (server->id, &err);
if (ret < 0) {
_ml_error_report ("Failed to invoke the method destroy_pipeline (%s).",
err ? err->message : "Unknown error");
}
g_clear_error (&err);

if (ML_ERROR_NONE != ret)
_ml_error_report_return (ret,
"The data of given handle is corrupted. Please check it.");

g_free (server->service_name);
g_free (server);
} else if (ML_SERVICE_TYPE_CLIENT_QUERY == mls->type) {
_ml_service_query_s *query = (_ml_service_query_s *) mls->priv;
ml_tensors_data_h data_h;

if (ml_pipeline_src_release_handle (query->src_h))
_ml_error_report ("Failed to release src handle");

if (ml_pipeline_sink_unregister (query->sink_h))
_ml_error_report ("Failed to unregister sink handle");

if (ml_pipeline_destroy (query->pipe_h))
_ml_error_report ("Failed to destroy pipeline");

while ((data_h = g_async_queue_try_pop (query->out_data_queue))) {
ml_tensors_data_destroy (data_h);
}

g_async_queue_unref (query->out_data_queue);
g_free (query);
} else if (ML_SERVICE_TYPE_REMOTE == mls->type) {
_ml_remote_service_s *mlrs = (_ml_remote_service_s *) mls->priv;
nns_edge_release_handle (mlrs->edge_h);
/** Wait some time until release the edge handle. */
g_usleep (1000000);
g_free (mlrs);
} else {
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"Invalid type of ml_service_h.");
switch (mls->type) {
case ML_SERVICE_TYPE_SERVER_PIPELINE:
ret = ml_service_pipeline_release_internal (mls->priv);
break;
case ML_SERVICE_TYPE_CLIENT_QUERY:
ret = ml_service_query_release_internal (mls->priv);
break;
case ML_SERVICE_TYPE_REMOTE:
ret = ml_service_remote_release_internal (mls->priv);
break;
default:
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"Invalid type of ml_service_h.");
}

g_free (mls);
return ML_ERROR_NONE;
if (ret == ML_ERROR_NONE)
g_free (mls);
return ret;
}
29 changes: 12 additions & 17 deletions c/src/ml-api-service-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "pipeline-dbus.h"
#include "model-dbus.h"
#include "resource-dbus.h"
#include "nnstreamer-edge.h"
#include "nnstreamer-tizen-internal.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -60,27 +59,23 @@ typedef struct
} _ml_service_server_s;

/**
* @brief Structure for ml_service_query
* @brief Internal function to release ml-service pipeline data.
*/
typedef struct
{
ml_pipeline_h pipe_h;
ml_pipeline_src_h src_h;
ml_pipeline_sink_h sink_h;
int ml_service_pipeline_release_internal (void *priv);

gchar *caps;
guint timeout; /**< in ms unit */
GAsyncQueue *out_data_queue;
} _ml_service_query_s;
/**
* @brief Internal function to release ml-service query data.
*/
int ml_service_query_release_internal (void *priv);

#if defined(ENABLE_REMOTE_SERVICE)
/**
* @brief Structure for ml_remote_service
* @brief Internal function to release ml-service remote data.
*/
typedef struct
{
nns_edge_h edge_h;
nns_edge_node_type_e node_type;
} _ml_remote_service_s;
int ml_service_remote_release_internal (void *priv);
#else
#define ml_service_remote_release_internal(...) ML_ERROR_NOT_SUPPORTED
#endif

#ifdef __cplusplus
}
Expand Down
45 changes: 45 additions & 0 deletions c/src/ml-api-service-query-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
#include "ml-api-service.h"
#include "ml-api-service-private.h"

/**
* @brief Structure for ml_service_query
*/
typedef struct
{
ml_pipeline_h pipe_h;
ml_pipeline_src_h src_h;
ml_pipeline_sink_h sink_h;

gchar *caps;
guint timeout; /**< in ms unit */
GAsyncQueue *out_data_queue;
} _ml_service_query_s;

/**
* @brief Sink callback for query_client
*/
Expand Down Expand Up @@ -58,6 +72,37 @@ _sink_callback_for_query_client (const ml_tensors_data_h data,
g_async_queue_push (mls->out_data_queue, copied_data);
}

/**
* @brief Internal function to release ml-service query data.
*/
int
ml_service_query_release_internal (void *priv)
{
_ml_service_query_s *query = (_ml_service_query_s *) priv;
ml_tensors_data_h data_h;

if (!query)
return ML_ERROR_INVALID_PARAMETER;

if (ml_pipeline_src_release_handle (query->src_h))
_ml_error_report ("Failed to release src handle");

if (ml_pipeline_sink_unregister (query->sink_h))
_ml_error_report ("Failed to unregister sink handle");

if (ml_pipeline_destroy (query->pipe_h))
_ml_error_report ("Failed to destroy pipeline");

while ((data_h = g_async_queue_try_pop (query->out_data_queue))) {
ml_tensors_data_destroy (data_h);
}

g_async_queue_unref (query->out_data_queue);
g_free (query);

return ML_ERROR_NONE;
}

/**
* @brief Creates query client service handle with given ml-option handle.
*/
Expand Down
30 changes: 30 additions & 0 deletions c/src/ml-api-service-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <gst/app/app.h>
#include <string.h>
#include <curl/curl.h>
#include <nnstreamer-edge.h>

#include "ml-api-internal.h"
#include "ml-api-service.h"
Expand Down Expand Up @@ -51,6 +52,15 @@ typedef struct
nns_edge_node_type_e node_type;
} edge_info_s;

/**
* @brief Structure for ml_remote_service
*/
typedef struct
{
nns_edge_h edge_h;
nns_edge_node_type_e node_type;
} _ml_remote_service_s;

/**
* @brief Get ml-service node type from ml_option.
*/
Expand Down Expand Up @@ -480,6 +490,26 @@ _mlrs_create_edge_handle (nns_edge_h * edge_h, edge_info_s * edge_info)
return ret;
}

/**
* @brief Internal function to release ml-service remote data.
*/
int
ml_service_remote_release_internal (void *priv)
{
_ml_remote_service_s *mlrs = (_ml_remote_service_s *) priv;

if (!mlrs)
return ML_ERROR_INVALID_PARAMETER;

nns_edge_release_handle (mlrs->edge_h);

/** Wait some time until release the edge handle. */
g_usleep (1000000);
g_free (mlrs);

return ML_ERROR_NONE;
}

/**
* @brief Creates ml-service handle with given ml-option handle.
*/
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if get_option('enable-ml-service')
nnstreamer_edge_dep = dependency('nnstreamer-edge', required: false)
curl_dep = cc.find_library('curl', required: false)
if nnstreamer_edge_dep.found() and curl_dep.found()
add_project_arguments('-DENABLE_REMOTE_SERVICE=1', language: ['c', 'cpp'])
support_remote_service = true
endif
endif
Expand Down

0 comments on commit 6b6db57

Please sign in to comment.