From: desperadok Date: Wed, 15 Jun 2022 09:36:38 +0000 (+0900) Subject: media_format: Use g_atomic_ref_count for reference counting X-Git-Tag: submit/unittc/20220622.065806^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F76%2F276376%2F8;p=platform%2Fcore%2Fapi%2Fmediatool.git media_format: Use g_atomic_ref_count for reference counting [Version] 0.1.55 [Issue Type] Improvement Change-Id: I50df2f92f8b1dfed6740014b13517fdb32bf434c Signed-off-by: desperadok --- diff --git a/include/media_format_private.h b/include/media_format_private.h index c70ba35..b76ce47 100644 --- a/include/media_format_private.h +++ b/include/media_format_private.h @@ -19,6 +19,7 @@ #include #include +#include #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__ */ diff --git a/packaging/capi-media-tool.spec b/packaging/capi-media-tool.spec index 03d15f0..418bfdf 100644 --- a/packaging/capi-media-tool.spec +++ b/packaging/capi-media-tool.spec @@ -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 diff --git a/src/media_format.c b/src/media_format.c index d15ab16..842c5a7 100644 --- a/src/media_format.c +++ b/src/media_format.c @@ -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); diff --git a/unittest/unittest.cpp b/unittest/unittest.cpp index 9443476..2a0288c 100644 --- a/unittest/unittest.cpp +++ b/unittest/unittest.cpp @@ -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, ©); + 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);