media_format: Use g_atomic_ref_count for reference counting 76/276376/8 accepted/tizen_7.0_unified_hotfix tizen_7.0_hotfix accepted/tizen/7.0/unified/20221110.061140 accepted/tizen/7.0/unified/hotfix/20221116.104731 accepted/tizen/unified/20220623.032507 submit/tizen/20220622.070116 submit/unittc/20220622.065806 tizen_7.0_m2_release
authordesperadok <heechul.jeon@samsung.com>
Wed, 15 Jun 2022 09:36:38 +0000 (18:36 +0900)
committerdesperadok <heechul.jeon@samsung.com>
Wed, 22 Jun 2022 06:49:59 +0000 (15:49 +0900)
[Version] 0.1.55
[Issue Type] Improvement

Change-Id: I50df2f92f8b1dfed6740014b13517fdb32bf434c
Signed-off-by: desperadok <heechul.jeon@samsung.com>
include/media_format_private.h
packaging/capi-media-tool.spec
src/media_format.c
unittest/unittest.cpp

index c70ba35e5387aa8f7567dd5cfbed19ed32133d41..b76ce478f87e5204ee2742de5bedd135fc65c337 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <media_format.h>
 #include <glib.h>
+#include <glib/gi18n.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -108,25 +109,27 @@ do { \
  * @brief Gets the number of media format ref_count
  * @since_tizen 2.3
  */
-#define MEDIA_FORMAT_GET_REFCOUNT(x_fmt) (g_atomic_int_get(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
+#define MEDIA_FORMAT_GET_REFCOUNT(x_fmt) (MEDIA_FORMAT_CAST(x_fmt)->ref_count)
 
 /**
  * @brief Check whether given media format is writable or not
  * @since_tizen 2.3
  */
-#define MEDIA_FORMAT_IS_WRITABLE(x_fmt) (MEDIA_FORMAT_GET_REFCOUNT(x_fmt) == 1)
+#define MEDIA_FORMAT_IS_WRITABLE(x_fmt) (g_atomic_ref_count_compare(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count, 1))
 
 /**
  * @brief increase the media format's ref_count
  * @since_tizen 2.3
  */
-#define MEDIA_FORMAT_INC_REFCOUNT(x_fmt) (g_atomic_int_inc(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
+#define MEDIA_FORMAT_INC_REFCOUNT(x_fmt) (g_atomic_ref_count_inc(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
 
 /**
  * @brief decrease the media format's ref_count and return true if ref_count become zero.
  * @since_tizen 2.3
  */
-#define MEDIA_FORMAT_DEC_REFCOUNT_TEST(x_fmt) (g_atomic_int_dec_and_test(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
+#define MEDIA_FORMAT_DEC_REFCOUNT_TEST(x_fmt) (g_atomic_ref_count_dec(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
+
+#define MEDIA_FORMAT_INIT_REFCOUNT(x_fmt) (g_atomic_ref_count_init(&(MEDIA_FORMAT_CAST(x_fmt))->ref_count))
 
 #define CHECK_BIT(x, y) (((x) >> (y)) & 0x01)
 
@@ -180,7 +183,7 @@ typedef struct _media_fomat_text_spec_s {
  * @since_tizen 2.3
  */
 typedef struct _media_format_s {
-       int ref_count;                  /**< media format struct reference count */
+       gatomicrefcount ref_count;                      /**< media format struct reference count */
        media_format_mimetype_e mimetype;                  /**< media format struct mimetype of media_format_mimetype_e */
 
        void *extradata;
@@ -196,4 +199,5 @@ typedef struct _media_format_s {
 }
 #endif
 
+
 #endif                                                 /* __TIZEN_MEDIA_FORMAT_PRIVATE_H__ */
index 03d15f0c87f892925f95f01725abdb3dc9d186b6..418bfdfc1b6d76bb765641ae67164c077b989259 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-tool
 Summary:    A Core API media tool library in Tizen Native API
-Version:    0.1.54
+Version:    0.1.55
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index d15ab164f32436750be4a744de70d00aad96a973..842c5a7e47b187e2327c3154039c164bc7608151 100644 (file)
@@ -101,7 +101,7 @@ int media_format_create(media_format_h *fmt)
        else
                return MEDIA_FORMAT_ERROR_OUT_OF_MEMORY;
 
-       fmt_handle->ref_count = 1;
+       MEDIA_FORMAT_INIT_REFCOUNT(fmt_handle);
 
        *fmt = (media_format_h) fmt_handle;
 
@@ -698,9 +698,9 @@ int media_format_ref(media_format_h fmt)
        if (MEDIA_FORMAT_GET_REFCOUNT(fmt) <= 0)
                return MEDIA_FORMAT_ERROR_INVALID_OPERATION;
 
-       g_atomic_int_inc(&fmt_handle->ref_count);
+       MEDIA_FORMAT_INC_REFCOUNT(fmt_handle);
 
-       LOGI("format[%p] ref_count[%d]", fmt, fmt_handle->ref_count);
+       LOGI("format[%p] ref_count[%d]", fmt, MEDIA_FORMAT_GET_REFCOUNT(fmt_handle));
 
        return MEDIA_FORMAT_ERROR_NONE;
 }
@@ -708,7 +708,6 @@ int media_format_ref(media_format_h fmt)
 int media_format_unref(media_format_h fmt)
 {
        MEDIA_FORMAT_INSTANCE_CHECK(fmt);
-       bool is_zero;
 
        media_format_s *fmt_handle;
        fmt_handle = (media_format_s *)fmt;
@@ -718,13 +717,12 @@ int media_format_unref(media_format_h fmt)
                return MEDIA_FORMAT_ERROR_INVALID_OPERATION;
        }
 
-       is_zero = g_atomic_int_dec_and_test(&fmt_handle->ref_count);
-       if (is_zero) {
+       if (MEDIA_FORMAT_DEC_REFCOUNT_TEST(fmt_handle)) {
                LOGI("The format handle(%p) will be destroyed..", fmt);
                /* if reference count become 0 , free fmt. */
                __media_format_destroy(fmt_handle);
        } else {
-               LOGI("format[%p] ref_count[%d]", fmt, fmt_handle->ref_count);
+               LOGI("format[%p] ref_count[%d]", fmt, MEDIA_FORMAT_GET_REFCOUNT(fmt_handle));
        }
 
        return MEDIA_FORMAT_ERROR_NONE;
@@ -743,12 +741,7 @@ int media_format_is_writable(media_format_h fmt, bool *is_writable)
        media_format_s *fmt_handle;
        fmt_handle = (media_format_s *)fmt;
 
-       if (g_atomic_int_get(&fmt_handle->ref_count) == 1) {
-               /* if reference count is 1, the caller must be owner. */
-               *is_writable = true;
-       } else {
-               *is_writable = false;
-       }
+       *is_writable = MEDIA_FORMAT_IS_WRITABLE(fmt_handle);
 
        LOGI("format[%p] is_writable[%d]", fmt, *is_writable);
 
@@ -777,7 +770,7 @@ int media_format_make_writable(media_format_h fmt, media_format_h *out_fmt)
                        return MEDIA_FORMAT_ERROR_OUT_OF_MEMORY;
 
                memcpy(copy, fmt_handle, sizeof(media_format_s));
-               copy->ref_count = 1;
+               MEDIA_FORMAT_INIT_REFCOUNT(copy);
 
                if (media_format_unref(fmt) != MEDIA_FORMAT_ERROR_NONE) {
                        free(copy);
index 94434761921bd1f0a7868582db4f9d81e33ef8bb..2a0288c078fe6b2497cf38e62beb5226d3f72484 100644 (file)
@@ -84,15 +84,38 @@ TEST(MediaToolTest, media_tool_gtest_refcount_general)
        ASSERT_EQ(ret, MEDIA_FORMAT_ERROR_INVALID_OPERATION);
 }
 
+TEST(MediaToolTest, media_tool_gtest_writable)
+{
+       media_format_h fmt, copy;
+       bool is_writable;
+       int ret = media_format_create(&fmt);
+       ASSERT_EQ(ret, MEDIA_FORMAT_ERROR_NONE);
+
+       media_format_is_writable(fmt, &is_writable);
+       ASSERT_EQ(is_writable, TRUE);
+
+       media_format_ref(fmt);
+       media_format_is_writable(fmt, &is_writable);
+       ASSERT_EQ(is_writable, FALSE);
+
+       media_format_make_writable(fmt, &copy);
+       media_format_is_writable(copy, &is_writable);
+       ASSERT_EQ(is_writable, TRUE);
+
+       media_format_unref(fmt);
+       media_format_unref(fmt);
+       media_format_unref(copy);
+}
+
 static void *refcount_monkey_thread_func(gpointer data, gpointer param)
 {
        media_format_h fmt = (media_format_h)param;
        unsigned int refcount_limit = GPOINTER_TO_UINT(data);
 
-       for (int i = 0; i < refcount_limit; i++)
+       for (unsigned int i = 0; i < refcount_limit; i++)
                media_format_ref(fmt);
 
-       for (int i = 0; i < refcount_limit; i++)
+       for (unsigned int i = 0; i < refcount_limit; i++)
                media_format_unref(fmt);
 
        return NULL;
@@ -112,7 +135,7 @@ TEST(MediaToolTest, media_tool_gtest_refcount_thread_safty)
                (GFunc)refcount_monkey_thread_func,
                (gpointer)fmt, num_threads, TRUE, NULL);
 
-       for (int i = 0; i < num_threads; i++)
+       for (unsigned int i = 0; i < num_threads; i++)
                g_thread_pool_push(thread_pool, GUINT_TO_POINTER(refcount_limit_per_thread), NULL);
 
        g_thread_pool_free(thread_pool, FALSE, TRUE);