[Tizen/Feature] invalid condition to check feature status
authorJaeyun <jy1210.jung@samsung.com>
Thu, 1 Aug 2019 12:10:47 +0000 (21:10 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Fri, 2 Aug 2019 04:24:27 +0000 (13:24 +0900)
1. Fix invalid condition when mutex-lock of element handle locked.
2. Add new struct to set mutex locked.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
api/capi/src/nnstreamer-capi-pipeline.c
api/capi/src/nnstreamer-capi-util.c

index b4e1d8f..d9eade2 100644 (file)
@@ -39,6 +39,7 @@
   ml_pipeline *p; \
   ml_pipeline_element *elem; \
   int ret = ML_ERROR_NONE; \
+  check_feature_state (); \
   if ((h) == NULL) { \
     ml_loge ("The given handle is invalid"); \
     return ML_ERROR_INVALID_PARAMETER; \
@@ -733,8 +734,6 @@ ml_pipeline_sink_unregister (ml_pipeline_sink_h h)
 {
   handle_init (sink, sink, h);
 
-  check_feature_state ();
-
   if (elem->handle_id > 0) {
     g_signal_handler_disconnect (elem->element, elem->handle_id);
     elem->handle_id = 0;
@@ -897,8 +896,6 @@ ml_pipeline_src_release_handle (ml_pipeline_src_h h)
 {
   handle_init (src, src, h);
 
-  check_feature_state ();
-
   elem->handles = g_list_remove (elem->handles, src);
   g_free (src);
 
@@ -921,8 +918,6 @@ ml_pipeline_src_input_data (ml_pipeline_src_h h, ml_tensors_data_h data,
 
   handle_init (src, src, h);
 
-  check_feature_state ();
-
   _data = (ml_tensors_data_s *) data;
   if (!_data) {
     ml_loge ("The given param data is invalid.");
@@ -1012,8 +1007,6 @@ ml_pipeline_src_get_tensors_info (ml_pipeline_src_h h,
 {
   handle_init (src, src, h);
 
-  check_feature_state ();
-
   if (info == NULL) {
     ret = ML_ERROR_INVALID_PARAMETER;
     goto unlock_return;
@@ -1117,8 +1110,6 @@ ml_pipeline_switch_release_handle (ml_pipeline_switch_h h)
 {
   handle_init (switch, swtc, h);
 
-  check_feature_state ();
-
   elem->handles = g_list_remove (elem->handles, swtc);
   g_free (swtc);
 
@@ -1136,8 +1127,6 @@ ml_pipeline_switch_select (ml_pipeline_switch_h h, const char *pad_name)
 
   handle_init (switch, swtc, h);
 
-  check_feature_state ();
-
   if (pad_name == NULL) {
     ml_loge ("The second argument, pad name, is not valid.");
     ret = ML_ERROR_INVALID_PARAMETER;
@@ -1191,8 +1180,6 @@ ml_pipeline_switch_get_pad_list (ml_pipeline_switch_h h, char ***list)
 
   handle_init (switch, swtc, h);
 
-  check_feature_state ();
-
   if (list == NULL) {
     ml_loge ("The second argument, list, is not valid.");
     ret = ML_ERROR_INVALID_PARAMETER;
@@ -1344,8 +1331,6 @@ ml_pipeline_valve_release_handle (ml_pipeline_valve_h h)
 {
   handle_init (valve, valve, h);
 
-  check_feature_state ();
-
   elem->handles = g_list_remove (elem->handles, valve);
   g_free (valve);
 
@@ -1361,8 +1346,6 @@ ml_pipeline_valve_set_open (ml_pipeline_valve_h h, bool open)
   gboolean drop = FALSE;
   handle_init (valve, valve, h);
 
-  check_feature_state ();
-
   g_object_get (G_OBJECT (elem->element), "drop", &drop, NULL);
 
   if ((open != false) != (drop != FALSE)) {
index 8e7396d..850008c 100644 (file)
   #include <system_info.h>
 #endif
 
-/** -1: Not checked yet, 0: Not supported, 1: Supported */
-static int feature_enabled = -1;
+/**
+ * @brief Internal struct to control tizen feature support (machine_learning.inference).
+ * -1: Not checked yet, 0: Not supported, 1: Supported
+ */
+typedef struct
+{
+  GMutex mutex;
+  int feature_status;
+} feature_info_s;
+
+static feature_info_s *feature_info = NULL;
+
+/**
+ * @brief Internal function to initialize feature status.
+ */
+static void
+ml_initialize_feature_status (void)
+{
+  if (feature_info == NULL) {
+    feature_info = g_new0 (feature_info_s, 1);
+    g_assert (feature_info);
+
+    g_mutex_init (&feature_info->mutex);
+    feature_info->feature_status = -1;
+  }
+}
 
 /**
  * @brief Allocates a tensors information handle with default value.
@@ -402,6 +426,9 @@ ml_tensors_info_get_size (const ml_tensors_info_h info)
   size_t tensor_size;
   gint i;
 
+  if (ML_ERROR_NONE != ml_get_feature_enabled())
+    return 0;
+
   tensors_info = (ml_tensors_info_s *) info;
 
   if (!tensors_info)
@@ -822,25 +849,36 @@ done:
 /**
  * @brief Checks whether machine_learning.inference feature is enabled or not.
  */
-int ml_get_feature_enabled (void)
+int
+ml_get_feature_enabled (void)
 {
+  ml_initialize_feature_status ();
+
 #if defined(__TIZEN__)
-  if (0 == feature_enabled) {
-    ml_loge ("machine_learning.inference NOT supported");
-    return ML_ERROR_NOT_SUPPORTED;
-  } else if (-1 == feature_enabled) {
-    bool ml_inf_supported = false;
-    if (0 == system_info_get_platform_bool(ML_INF_FEATURE_PATH, &ml_inf_supported)) {
-      if (false == ml_inf_supported) {
-        ml_loge ("machine_learning.inference NOT supported");
-        ml_set_feature_status (0);
-        return ML_ERROR_NOT_SUPPORTED;
-      }
+  {
+    int feature_enabled;
 
-      ml_set_feature_status (1);
-    } else {
-      ml_loge ("failed to get feature value of machine_learning.inference");
+    g_mutex_lock (&feature_info->mutex);
+    feature_enabled = feature_info->feature_status;
+    g_mutex_unlock (&feature_info->mutex);
+
+    if (0 == feature_enabled) {
+      ml_loge ("machine_learning.inference NOT supported");
       return ML_ERROR_NOT_SUPPORTED;
+    } else if (-1 == feature_enabled) {
+      bool ml_inf_supported = false;
+      if (0 == system_info_get_platform_bool(ML_INF_FEATURE_PATH, &ml_inf_supported)) {
+        if (false == ml_inf_supported) {
+          ml_loge ("machine_learning.inference NOT supported");
+          ml_set_feature_status (0);
+          return ML_ERROR_NOT_SUPPORTED;
+        }
+
+        ml_set_feature_status (1);
+      } else {
+        ml_loge ("failed to get feature value of machine_learning.inference");
+        return ML_ERROR_NOT_SUPPORTED;
+      }
     }
   }
 #endif
@@ -850,18 +888,18 @@ int ml_get_feature_enabled (void)
 /**
  * @brief Set the feature status of machine_learning.inference.
  */
-int ml_set_feature_status (int status)
+int
+ml_set_feature_status (int status)
 {
-  GMutex mutex;
-  g_mutex_init (&mutex);
-  g_mutex_lock (&mutex);
+  ml_initialize_feature_status ();
+  g_mutex_lock (&feature_info->mutex);
 
-  /** Update feature status
-   *   -1: Not checked yet, 0: Not supported, 1: Supported
-  */
-  feature_enabled = status;
+  /**
+   * Update feature status
+   * -1: Not checked yet, 0: Not supported, 1: Supported
+   */
+  feature_info->feature_status = status;
 
-  g_mutex_unlock (&mutex);
-  g_mutex_clear (&mutex);
+  g_mutex_unlock (&feature_info->mutex);
   return ML_ERROR_NONE;
 }