• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "osal_spinlock.h"
10 #include <pthread.h>
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 
14 #define HDF_LOG_TAG osal_spinlock
15 
OsalSpinInit(OsalSpinlock * spinlock)16 int32_t OsalSpinInit(OsalSpinlock *spinlock)
17 {
18     pthread_spinlock_t *spinTmp = NULL;
19     int ret;
20 
21     if (spinlock == NULL) {
22         HDF_LOGE("%s invalid param", __func__);
23         return HDF_ERR_INVALID_PARAM;
24     }
25 
26     spinlock->realSpinlock = NULL;
27 
28     spinTmp = (pthread_spinlock_t *)OsalMemCalloc(sizeof(pthread_spinlock_t));
29     if (spinTmp == NULL) {
30         HDF_LOGE("malloc fail");
31         return HDF_ERR_MALLOC_FAIL;
32     }
33 
34     ret = pthread_spin_init(spinTmp, PTHREAD_PROCESS_PRIVATE);
35     if (ret != 0) {
36         HDF_LOGE("pthread_spin_init fail %d %d", ret, __LINE__);
37         OsalMemFree(spinTmp);
38         return HDF_FAILURE;
39     }
40     spinlock->realSpinlock = (void *)spinTmp;
41 
42     return HDF_SUCCESS;
43 }
44 
OsalSpinDestroy(OsalSpinlock * spinlock)45 int32_t OsalSpinDestroy(OsalSpinlock *spinlock)
46 {
47     int ret;
48 
49     if (spinlock == NULL || spinlock->realSpinlock == NULL) {
50         HDF_LOGE("%s invalid param", __func__);
51         return HDF_ERR_INVALID_PARAM;
52     }
53 
54     ret = pthread_spin_destroy((pthread_spinlock_t *)spinlock->realSpinlock);
55     if (ret != 0) {
56         HDF_LOGE("pthread_spin_destroy fail %d %d", ret, __LINE__);
57         return HDF_FAILURE;
58     }
59 
60     OsalMemFree(spinlock->realSpinlock);
61     spinlock->realSpinlock = NULL;
62 
63     return HDF_SUCCESS;
64 }
65 
OsalSpinLock(OsalSpinlock * spinlock)66 int32_t OsalSpinLock(OsalSpinlock *spinlock)
67 {
68     int ret;
69 
70     if (spinlock == NULL || spinlock->realSpinlock == NULL) {
71         HDF_LOGE("%s invalid param", __func__);
72         return HDF_ERR_INVALID_PARAM;
73     }
74 
75     ret = pthread_spin_lock((pthread_spinlock_t *)spinlock->realSpinlock);
76     if (ret != 0) {
77         HDF_LOGE("pthread_spin_lock fail %d %d", ret, __LINE__);
78         return HDF_FAILURE;
79     }
80 
81     return HDF_SUCCESS;
82 }
83 
OsalSpinUnlock(OsalSpinlock * spinlock)84 int32_t OsalSpinUnlock(OsalSpinlock *spinlock)
85 {
86     int ret;
87 
88     if (spinlock == NULL || spinlock->realSpinlock == NULL) {
89         HDF_LOGE("%s invalid param", __func__);
90         return HDF_ERR_INVALID_PARAM;
91     }
92 
93     ret = pthread_spin_unlock((pthread_spinlock_t *)spinlock->realSpinlock);
94     if (ret != 0) {
95         HDF_LOGE("pthread_spin_unlock fail %d %d", ret, __LINE__);
96         return HDF_FAILURE;
97     }
98 
99     return HDF_SUCCESS;
100 }
101 
102