diff options
author | SooChan Lim <sc1.lim@samsung.com> | 2016-09-23 19:08:15 +0900 |
---|---|---|
committer | SooChan Lim <sc1.lim@samsung.com> | 2016-09-23 20:18:16 +0900 |
commit | 612025e3f1d6c0f0ce4ac9208cb46314dfa976bd (patch) | |
tree | 77e66666cf90df84db953e84437869fecdfe4b1f | |
parent | bf6eea642541c8f82b416e7a2acd72256a0ac109 (diff) | |
download | libtbm-612025e3f1d6c0f0ce4ac9208cb46314dfa976bd.tar.gz libtbm-612025e3f1d6c0f0ce4ac9208cb46314dfa976bd.tar.bz2 libtbm-612025e3f1d6c0f0ce4ac9208cb46314dfa976bd.zip |
enforce the thread safety of the bufmgr->lock
Change-Id: I96484ef27ccb9bf7e6ca6f3802b85a1ee5e1ee31
-rw-r--r-- | src/tbm_bufmgr.c | 256 |
1 files changed, 200 insertions, 56 deletions
diff --git a/src/tbm_bufmgr.c b/src/tbm_bufmgr.c index 54f1626..bd66449 100644 --- a/src/tbm_bufmgr.c +++ b/src/tbm_bufmgr.c @@ -744,13 +744,19 @@ tbm_bufmgr_deinit(tbm_bufmgr bufmgr) int tbm_bo_size(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - tbm_bufmgr bufmgr = bo->bufmgr; + tbm_bufmgr bufmgr = gBufMgr; int size; pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + size = bufmgr->backend->bo_size(bo); TBM_TRACE("bo(%p) size(%d)\n", bo, size); @@ -763,12 +769,18 @@ tbm_bo_size(tbm_bo bo) tbm_bo tbm_bo_ref(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), NULL); - tbm_bufmgr bufmgr = bo->bufmgr; + tbm_bufmgr bufmgr = gBufMgr; pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return NULL; + } + _tbm_bo_ref(bo); TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt); @@ -781,12 +793,18 @@ tbm_bo_ref(tbm_bo bo) void tbm_bo_unref(tbm_bo bo) { - TBM_RETURN_IF_FAIL(_tbm_bo_is_valid(bo)); + TBM_RETURN_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr)); - tbm_bufmgr bufmgr = bo->bufmgr; + tbm_bufmgr bufmgr = gBufMgr; pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return; + } + TBM_TRACE("bo(%p) ref_cnt(%d)\n", bo, bo->ref_cnt - 1); _tbm_bo_unref(bo); @@ -797,7 +815,9 @@ tbm_bo_unref(tbm_bo bo) tbm_bo tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags) { - TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr) && (size > 0), NULL); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL); + TBM_RETURN_VAL_IF_FAIL(bufmgr != gBufMgr, NULL); + TBM_RETURN_VAL_IF_FAIL(size > 0, NULL); tbm_bo bo = NULL; void *bo_priv = NULL; @@ -809,13 +829,13 @@ tbm_bo_alloc(tbm_bufmgr bufmgr, int size, int flags) return NULL; } + pthread_mutex_lock(&bufmgr->lock); + _tbm_util_check_bo_cnt(bufmgr); bufmgr->bo_cnt++; bo->bufmgr = bufmgr; - pthread_mutex_lock(&bufmgr->lock); - bo_priv = bufmgr->backend->bo_alloc(bo, size, flags); if (!bo_priv) { TBM_TRACE("error: fail to create of tbm_bo size(%d) flag(%s)\n", size, tbm_flag_to_str(flags)); @@ -844,18 +864,21 @@ tbm_bo tbm_bo_import(tbm_bufmgr bufmgr, unsigned int key) { TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL); + TBM_RETURN_VAL_IF_FAIL(bufmgr != gBufMgr, NULL); tbm_bo bo = NULL; tbm_bo bo2 = NULL; tbm_bo tmp = NULL; void *bo_priv = NULL; - _tbm_util_check_bo_cnt(bufmgr); + pthread_mutex_lock(&bufmgr->lock); - if (!bufmgr->backend->bo_import) + if (!bufmgr->backend->bo_import) { + pthread_mutex_unlock(&bufmgr->lock); return NULL; + } - pthread_mutex_lock(&bufmgr->lock); + _tbm_util_check_bo_cnt(bufmgr); bo = calloc(1, sizeof(struct _tbm_bo)); if (!bo) { @@ -914,18 +937,21 @@ tbm_bo tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd) { TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL); + TBM_RETURN_VAL_IF_FAIL(bufmgr != gBufMgr, NULL); tbm_bo bo = NULL; tbm_bo bo2 = NULL; tbm_bo tmp = NULL; void *bo_priv = NULL; - _tbm_util_check_bo_cnt(bufmgr); + pthread_mutex_lock(&bufmgr->lock); - if (!bufmgr->backend->bo_import_fd) + if (!bufmgr->backend->bo_import_fd) { + pthread_mutex_unlock(&bufmgr->lock); return NULL; + } - pthread_mutex_lock(&bufmgr->lock); + _tbm_util_check_bo_cnt(bufmgr); bo = calloc(1, sizeof(struct _tbm_bo)); if (!bo) { @@ -983,17 +1009,23 @@ tbm_bo_import_fd(tbm_bufmgr bufmgr, tbm_fd fd) tbm_key tbm_bo_export(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; tbm_key ret; - bufmgr = bo->bufmgr; + pthread_mutex_lock(&bufmgr->lock); - if (!bufmgr->backend->bo_export) + if (!bufmgr->backend->bo_export) { + pthread_mutex_unlock(&bufmgr->lock); return 0; + } - pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } ret = bufmgr->backend->bo_export(bo); if (!ret) { @@ -1013,17 +1045,23 @@ tbm_bo_export(tbm_bo bo) tbm_fd tbm_bo_export_fd(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), -1); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; int ret; - bufmgr = bo->bufmgr; + pthread_mutex_lock(&bufmgr->lock); - if (!bufmgr->backend->bo_export_fd) - return -1; + if (!bufmgr->backend->bo_export_fd) { + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } - pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } ret = bufmgr->backend->bo_export_fd(bo); if (ret < 0) { @@ -1043,15 +1081,19 @@ tbm_bo_export_fd(tbm_bo bo) tbm_bo_handle tbm_bo_get_handle(tbm_bo bo, int device) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; tbm_bo_handle bo_handle; - bufmgr = bo->bufmgr; - pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return (tbm_bo_handle) NULL; + } + bo_handle = bufmgr->backend->bo_get_handle(bo, device); if (bo_handle.ptr == NULL) { _tbm_set_last_result(TBM_BO_ERROR_GET_HANDLE_FAILED); @@ -1070,15 +1112,19 @@ tbm_bo_get_handle(tbm_bo bo, int device) tbm_bo_handle tbm_bo_map(tbm_bo bo, int device, int opt) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), (tbm_bo_handle) 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), (tbm_bo_handle) NULL); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; tbm_bo_handle bo_handle; - bufmgr = bo->bufmgr; - pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return (tbm_bo_handle) NULL; + } + if (!_tbm_bo_lock(bo, device, opt)) { _tbm_set_last_result(TBM_BO_ERROR_LOCK_FAILED); TBM_TRACE("error: fail to lock bo:%p)\n", bo); @@ -1108,15 +1154,19 @@ tbm_bo_map(tbm_bo bo, int device, int opt) int tbm_bo_unmap(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; int ret; - bufmgr = bo->bufmgr; - pthread_mutex_lock(&bufmgr->lock); + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + ret = bufmgr->backend->bo_unmap(bo); if (!ret) { TBM_TRACE("error: bo(%p) map_cnt(%d)\n", bo, bo->map_cnt); @@ -1140,19 +1190,31 @@ tbm_bo_unmap(tbm_bo bo) int tbm_bo_swap(tbm_bo bo1, tbm_bo bo2) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo1), 0); - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo2), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + tbm_bufmgr bufmgr = gBufMgr; void *temp; - pthread_mutex_lock(&bo1->bufmgr->lock); + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo1)) { + TBM_LOG_E("_tbm_bo_is_valid(bo1) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + + if (!_tbm_bo_is_valid(bo2)) { + TBM_LOG_E("_tbm_bo_is_valid(bo2) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } TBM_TRACE("before: bo1(%p) bo2(%p)\n", bo1, bo2); - if (bo1->bufmgr->backend->bo_size(bo1) != bo2->bufmgr->backend->bo_size(bo2)) { + if (bufmgr->backend->bo_size(bo1) != bufmgr->backend->bo_size(bo2)) { _tbm_set_last_result(TBM_BO_ERROR_SWAP_FAILED); TBM_TRACE("error: bo1(%p) bo2(%p)\n", bo1, bo2); - pthread_mutex_unlock(&bo1->bufmgr->lock); + pthread_mutex_unlock(&bufmgr->lock); return 0; } @@ -1170,19 +1232,23 @@ tbm_bo_swap(tbm_bo bo1, tbm_bo bo2) int tbm_bo_locked(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); - tbm_bufmgr bufmgr; + tbm_bufmgr bufmgr = gBufMgr; - bufmgr = bo->bufmgr; + pthread_mutex_lock(&bufmgr->lock); if (bufmgr->lock_type == LOCK_TRY_NEVER) { TBM_TRACE("bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt); + pthread_mutex_unlock(&bufmgr->lock); return 0; } - pthread_mutex_lock(&bufmgr->lock); - + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } if (bo->lock_cnt > 0) { TBM_TRACE("error: bo(%p) lock_cnt(%d)\n", bo, bo->lock_cnt); @@ -1200,20 +1266,31 @@ int tbm_bo_add_user_data(tbm_bo bo, unsigned long key, tbm_data_free data_free_func) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + tbm_bufmgr bufmgr = gBufMgr; tbm_user_data *data; + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + /* check if the data according to the key exist if so, return false. */ data = user_data_lookup(&bo->user_data_list, key); if (data) { TBM_TRACE("warning: user data already exist key(%ld)\n", key); + pthread_mutex_unlock(&bufmgr->lock); return 0; } data = user_data_create(key, data_free_func); if (!data) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_unlock(&bufmgr->lock); return 0; } @@ -1221,24 +1298,37 @@ tbm_bo_add_user_data(tbm_bo bo, unsigned long key, LIST_ADD(&data->item_link, &bo->user_data_list); + pthread_mutex_unlock(&bufmgr->lock); + return 1; } int tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + tbm_bufmgr bufmgr = gBufMgr; tbm_user_data *old_data; + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + if (LIST_IS_EMPTY(&bo->user_data_list)) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_unlock(&bufmgr->lock); return 0; } old_data = user_data_lookup(&bo->user_data_list, key); if (!old_data) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_unlock(&bufmgr->lock); return 0; } @@ -1249,18 +1339,30 @@ tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data) TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data); + pthread_mutex_unlock(&bufmgr->lock); + return 1; } int tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + tbm_bufmgr bufmgr = gBufMgr; tbm_user_data *old_data; + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + if (!data || LIST_IS_EMPTY(&bo->user_data_list)) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_unlock(&bufmgr->lock); return 0; } @@ -1268,6 +1370,7 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data) if (!old_data) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); *data = NULL; + pthread_mutex_unlock(&bufmgr->lock); return 0; } @@ -1275,24 +1378,37 @@ tbm_bo_get_user_data(tbm_bo bo, unsigned long key, void **data) TBM_TRACE("bo(%p) key(%lu) data(%p)\n", bo, key, old_data->data); + pthread_mutex_unlock(&bufmgr->lock); + return 1; } int tbm_bo_delete_user_data(tbm_bo bo, unsigned long key) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + tbm_bufmgr bufmgr = gBufMgr; tbm_user_data *old_data = (void *)0; + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + if (LIST_IS_EMPTY(&bo->user_data_list)) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_lock(&bufmgr->lock); return 0; } old_data = user_data_lookup(&bo->user_data_list, key); if (!old_data) { TBM_TRACE("error: bo(%p) key(%lu)\n", bo, key); + pthread_mutex_lock(&bufmgr->lock); return 0; } @@ -1300,6 +1416,8 @@ tbm_bo_delete_user_data(tbm_bo bo, unsigned long key) user_data_delete(old_data); + pthread_mutex_lock(&bufmgr->lock); + return 1; } @@ -1316,11 +1434,26 @@ tbm_bufmgr_get_capability(tbm_bufmgr bufmgr) int tbm_bo_get_flags(tbm_bo bo) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + + tbm_bufmgr bufmgr = gBufMgr; + int flags; + + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } + + flags = bo->flags TBM_TRACE("bo(%p)\n", bo); - return bo->flags; + pthread_mutex_unlock(&bufmgr->lock); + + return flags; } /* LCOV_EXCL_START */ @@ -1522,17 +1655,28 @@ _tbm_bufmgr_get_bufmgr(void) int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface) { - TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + + pthread_mutex_lock(&bufmgr->lock); + + if (!_tbm_bo_is_valid(bo)) { + TBM_LOG_E("_tbm_bo_is_valid(bo) failed.\n"); + pthread_mutex_unlock(&bufmgr->lock); + return 0; + } bo->surface = surface; + pthread_mutex_unlock(&bufmgr->lock); + return 1; } int tbm_bufmgr_bind_native_display(tbm_bufmgr bufmgr, void *NativeDisplay) { - TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), 0); + TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(gBufMgr), 0); + TBM_RETURN_VAL_IF_FAIL(bufmgr != gBufMgr, 0); int ret; |