12ee81decSopenharmony_ci/* 22ee81decSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 32ee81decSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42ee81decSopenharmony_ci * you may not use this file except in compliance with the License. 52ee81decSopenharmony_ci * You may obtain a copy of the License at 62ee81decSopenharmony_ci * 72ee81decSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82ee81decSopenharmony_ci * 92ee81decSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102ee81decSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112ee81decSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122ee81decSopenharmony_ci * See the License for the specific language governing permissions and 132ee81decSopenharmony_ci * limitations under the License. 142ee81decSopenharmony_ci */ 152ee81decSopenharmony_ci 162ee81decSopenharmony_ci#ifndef SEC_UTILS_COMMON_MUTEX_H 172ee81decSopenharmony_ci#define SEC_UTILS_COMMON_MUTEX_H 182ee81decSopenharmony_ci 192ee81decSopenharmony_ci#include <pthread.h> 202ee81decSopenharmony_ci 212ee81decSopenharmony_ci#include "utils_log.h" 222ee81decSopenharmony_ci 232ee81decSopenharmony_ci#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 242ee81decSopenharmony_ci#define RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP 252ee81decSopenharmony_ci 262ee81decSopenharmony_ci#define INITED_MUTEX \ 272ee81decSopenharmony_ci { \ 282ee81decSopenharmony_ci MUTEX_INITIALIZER \ 292ee81decSopenharmony_ci } 302ee81decSopenharmony_ci 312ee81decSopenharmony_ci#define IRECURSIVE_INITED_MUTEX \ 322ee81decSopenharmony_ci { \ 332ee81decSopenharmony_ci PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \ 342ee81decSopenharmony_ci } 352ee81decSopenharmony_ci 362ee81decSopenharmony_ci#ifdef __cplusplus 372ee81decSopenharmony_ciextern "C" { 382ee81decSopenharmony_ci#endif 392ee81decSopenharmony_ci 402ee81decSopenharmony_citypedef struct Mutex { 412ee81decSopenharmony_ci pthread_mutex_t mutex; 422ee81decSopenharmony_ci} Mutex; 432ee81decSopenharmony_ci 442ee81decSopenharmony_ciinline static void InitMutex(Mutex *mutex) 452ee81decSopenharmony_ci{ 462ee81decSopenharmony_ci (void)pthread_mutex_init(&mutex->mutex, NULL); 472ee81decSopenharmony_ci} 482ee81decSopenharmony_ci 492ee81decSopenharmony_ciinline static void InitRecursiveMutex(Mutex *mutex) 502ee81decSopenharmony_ci{ 512ee81decSopenharmony_ci pthread_mutexattr_t attr; 522ee81decSopenharmony_ci pthread_mutexattr_init(&attr); 532ee81decSopenharmony_ci pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 542ee81decSopenharmony_ci (void)pthread_mutex_init(&mutex->mutex, &attr); 552ee81decSopenharmony_ci} 562ee81decSopenharmony_ci 572ee81decSopenharmony_ciinline static void LockMutex(Mutex *mutex) 582ee81decSopenharmony_ci{ 592ee81decSopenharmony_ci int ret = pthread_mutex_lock(&(mutex->mutex)); 602ee81decSopenharmony_ci if (ret != 0) { 612ee81decSopenharmony_ci SECURITY_LOG_ERROR("pthread_mutex_lock error"); 622ee81decSopenharmony_ci } 632ee81decSopenharmony_ci} 642ee81decSopenharmony_ci 652ee81decSopenharmony_ciinline static void UnlockMutex(Mutex *mutex) 662ee81decSopenharmony_ci{ 672ee81decSopenharmony_ci int ret = pthread_mutex_unlock(&(mutex->mutex)); 682ee81decSopenharmony_ci if (ret != 0) { 692ee81decSopenharmony_ci SECURITY_LOG_ERROR("pthread_mutex_unlock error"); 702ee81decSopenharmony_ci } 712ee81decSopenharmony_ci} 722ee81decSopenharmony_ci 732ee81decSopenharmony_ci#ifdef __cplusplus 742ee81decSopenharmony_ci} 752ee81decSopenharmony_ci#endif 762ee81decSopenharmony_ci 772ee81decSopenharmony_ci#endif // SEC_UTILS_COMMON_MUTEX_H 78