[CAPI/New-Single] Code Clean
authorMyungJoo Ham <myungjoo.ham@samsung.com>
Thu, 19 Sep 2019 10:16:48 +0000 (19:16 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Fri, 20 Sep 2019 01:52:49 +0000 (10:52 +0900)
For the readability and to prevent mistakes in the future,
- Apply symmetricity to lock/unlock, hide magic lock from programmers
- Apply common ops into macro

Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
api/capi/src/nnstreamer-capi-single-new.c
api/capi/src/nnstreamer-capi-single.c

index 08ca956..a2ee3cb 100644 (file)
@@ -56,9 +56,12 @@ G_LOCK_DEFINE_STATIC (magic);
 
 /**
  * @brief Get valid handle after magic verification
- * @note Magic lock is acquired after this
+ * @note handle's mutex (single_h->mutex) is acquired after this
+ * @param[out] single_h The handle properly casted: (ml_single *).
+ * @param[in] single The handle to be validated: (void *).
+ * @param[in] reset Set TRUE if the handle is to be reset (magic = 0).
  */
-#define ML_SINGLE_GET_VALID_HANDLE(single_h, single) do { \
+#define ML_SINGLE_GET_VALID_HANDLE_LOCKED(single_h, single, reset) do { \
   G_LOCK (magic); \
   single_h = (ml_single *) single; \
   if (single_h->magic != ML_SINGLE_MAGIC) { \
@@ -66,8 +69,19 @@ G_LOCK_DEFINE_STATIC (magic);
     G_UNLOCK (magic); \
     return ML_ERROR_INVALID_PARAMETER; \
   } \
+  if (reset) \
+    single_h->magic = 0; \
+  pthread_mutex_lock (&single_h->mutex); \
+  G_UNLOCK (magic); \
 } while (0)
 
+/**
+ * @brief This is for the symmetricity with ML_SINGLE_GET_VALID_HANDLE_LOCKED
+ * @param[in] single_h The casted handle (ml_single *).
+ */
+#define ML_SINGLE_HANDLE_UNLOCK(single_h) \
+  pthread_mutex_unlock (&single_h->mutex);
+
 /* ML single api data structure for handle */
 typedef struct
 {
@@ -461,15 +475,12 @@ ml_single_close (ml_single_h single)
     return ML_ERROR_INVALID_PARAMETER;
   }
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  single_h->magic = 0;
-
-  pthread_mutex_lock (&single_h->mutex);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 1);
 
   single_h->join = TRUE;
   pthread_cond_broadcast (&single_h->cond);
-  pthread_mutex_unlock (&single_h->mutex);
+
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
   pthread_join (single_h->thread, NULL);
 
   /** locking ensures correctness with parallel calls on close */
@@ -509,9 +520,7 @@ ml_single_invoke (ml_single_h single,
     return ML_ERROR_INVALID_PARAMETER;
   }
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  pthread_mutex_lock (&single_h->mutex);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   in_data = (ml_tensors_data_s *) input;
   *output = NULL;
@@ -569,7 +578,7 @@ ml_single_invoke (ml_single_h single,
     status = ML_ERROR_INVALID_PARAMETER;
 
 exit:
-  pthread_mutex_unlock (&single_h->mutex);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
   return status;
 }
 
@@ -591,9 +600,7 @@ ml_single_get_input_info (ml_single_h single, ml_tensors_info_h * info)
   if (!single || !info)
     return ML_ERROR_INVALID_PARAMETER;
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  pthread_mutex_lock (&single_h->mutex);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   /* allocate handle for tensors info */
   ml_tensors_info_create (info);
@@ -624,7 +631,7 @@ ml_single_get_input_info (ml_single_h single, ml_tensors_info_h * info)
     ml_logw ("Invalid state, input tensor name is mismatched in filter.");
   }
 
-  pthread_mutex_unlock (&single_h->mutex);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
 
   ml_tensors_info_copy_from_gst (input_info, &gst_info);
   gst_tensors_info_free (&gst_info);
@@ -649,9 +656,7 @@ ml_single_get_output_info (ml_single_h single, ml_tensors_info_h * info)
   if (!single || !info)
     return ML_ERROR_INVALID_PARAMETER;
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  pthread_mutex_lock (&single_h->mutex);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   /* allocate handle for tensors info */
   ml_tensors_info_create (info);
@@ -682,7 +687,7 @@ ml_single_get_output_info (ml_single_h single, ml_tensors_info_h * info)
     ml_logw ("Invalid state, output tensor name is mismatched in filter.");
   }
 
-  pthread_mutex_unlock (&single_h->mutex);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
 
   ml_tensors_info_copy_from_gst (output_info, &gst_info);
   gst_tensors_info_free (&gst_info);
@@ -702,12 +707,10 @@ ml_single_set_timeout (ml_single_h single, unsigned int timeout)
   if (!single || timeout == 0)
     return ML_ERROR_INVALID_PARAMETER;
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  pthread_mutex_lock (&single_h->mutex);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   MSEC_TO_TIMESPEC (single_h->timeout, timeout);
-  pthread_mutex_unlock (&single_h->mutex);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
 
   return ML_ERROR_NONE;
 }
index addd1ce..5244606 100644 (file)
@@ -48,9 +48,12 @@ G_LOCK_DEFINE_STATIC (magic);
 
 /**
  * @brief Get valid handle after magic verification
- * @note Magic lock is acquired after this
+ * @note handle's mutex (single_h->mutex) is acquired after this
+ * @param[out] single_h The handle properly casted: (ml_single *).
+ * @param[in] single The handle to be validated: (void *).
+ * @param[in] reset Set TRUE if the handle is to be reset (magic = 0).
  */
-#define ML_SINGLE_GET_VALID_HANDLE(single_h, single) do { \
+#define ML_SINGLE_GET_VALID_HANDLE_LOCKED(single_h, single, reset) do { \
   G_LOCK (magic); \
   single_h = (ml_single *) single; \
   if (single_h->magic != ML_SINGLE_MAGIC) { \
@@ -58,9 +61,20 @@ G_LOCK_DEFINE_STATIC (magic);
     G_UNLOCK (magic); \
     return ML_ERROR_INVALID_PARAMETER; \
   } \
+  if (reset) \
+    single_h->magic = 0; \
+  g_mutex_lock (&single_h->lock); \
+  G_UNLOCK (magic); \
 } while (0)
 
 /**
+ * @brief This is for the symmetricity with ML_SINGLE_GET_VALID_HANDLE_LOCKED
+ * @param[in] single_h The casted handle (ml_single *).
+ */
+#define ML_SINGLE_HANDLE_UNLOCK(single_h) \
+  g_mutex_unlock (&single_h->lock);
+
+/**
  * @brief Default time to wait for an output in appsink (3 seconds).
  */
 #define SINGLE_DEFAULT_TIMEOUT 3000
@@ -405,11 +419,7 @@ ml_single_close (ml_single_h single)
     return ML_ERROR_INVALID_PARAMETER;
   }
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  single_h->magic = 0;
-
-  g_mutex_lock (&single_h->lock);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 1);
 
   if (single_h->src) {
     gst_object_unref (single_h->src);
@@ -431,7 +441,7 @@ ml_single_close (ml_single_h single)
 
   status = ml_pipeline_destroy (single_h->pipe);
 
-  g_mutex_unlock (&single_h->lock);
+  ML_SINGLE_HANDLE_UNLOCK(single_h);
   g_mutex_clear (&single_h->lock);
 
   g_free (single_h);
@@ -461,9 +471,7 @@ ml_single_invoke (ml_single_h single,
     return ML_ERROR_INVALID_PARAMETER;
   }
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  g_mutex_lock (&single_h->lock);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   in_data = (ml_tensors_data_s *) input;
   *output = NULL;
@@ -562,7 +570,7 @@ done:
   if (sample)
     gst_sample_unref (sample);
 
-  g_mutex_unlock (&single_h->lock);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
   return status;
 }
 
@@ -581,13 +589,11 @@ ml_single_get_tensors_info (ml_single_h single, gboolean is_input,
   if (!single || !info)
     return ML_ERROR_INVALID_PARAMETER;
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  g_mutex_lock (&single_h->lock);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   ml_single_get_tensors_info_from_filter (single_h->filter, is_input, info);
 
-  g_mutex_unlock (&single_h->lock);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
   return status;
 }
 
@@ -624,13 +630,11 @@ ml_single_set_timeout (ml_single_h single, unsigned int timeout)
   if (!single || timeout == 0)
     return ML_ERROR_INVALID_PARAMETER;
 
-  ML_SINGLE_GET_VALID_HANDLE (single_h, single);
-  g_mutex_lock (&single_h->lock);
-  G_UNLOCK (magic);
+  ML_SINGLE_GET_VALID_HANDLE_LOCKED (single_h, single, 0);
 
   single_h->timeout = (guint) timeout;
 
-  g_mutex_unlock (&single_h->lock);
+  ML_SINGLE_HANDLE_UNLOCK (single_h);
   return status;
 #else
   return ML_ERROR_NOT_SUPPORTED;