/**
* @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) { \
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
{
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 */
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;
status = ML_ERROR_INVALID_PARAMETER;
exit:
- pthread_mutex_unlock (&single_h->mutex);
+ ML_SINGLE_HANDLE_UNLOCK (single_h);
return status;
}
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);
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);
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);
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);
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;
}
/**
* @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) { \
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
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);
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);
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;
if (sample)
gst_sample_unref (sample);
- g_mutex_unlock (&single_h->lock);
+ ML_SINGLE_HANDLE_UNLOCK (single_h);
return status;
}
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;
}
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;