1 /* 2 * Copyright (c) 2020-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 "hdf_sref.h" 10 #include "hdf_log.h" 11 12 #define HDF_LOG_TAG hdf_sref 13 HdfSRefAcquire(struct HdfSRef * sref)14void HdfSRefAcquire(struct HdfSRef *sref) 15 { 16 int32_t lockRef; 17 if (sref == NULL) { 18 HDF_LOGE("Acquire input sref is null"); 19 return; 20 } 21 OsalAtomicInc(&sref->refs); 22 lockRef = OsalAtomicRead(&sref->refs); 23 if ((lockRef == 1) && (sref->listener != NULL)) { 24 struct IHdfSRefListener *listener = sref->listener; 25 if (listener->OnFirstAcquire != NULL) { 26 listener->OnFirstAcquire(sref); 27 } 28 } 29 } 30 HdfSRefCount(const struct HdfSRef * sref)31int HdfSRefCount(const struct HdfSRef *sref) 32 { 33 if (sref == NULL) { 34 HDF_LOGE("invalid sref"); 35 return 0; 36 } 37 38 return OsalAtomicRead(&sref->refs); 39 } 40 HdfSRefRelease(struct HdfSRef * sref)41void HdfSRefRelease(struct HdfSRef *sref) 42 { 43 int32_t lockRef; 44 if (sref == NULL) { 45 HDF_LOGE("Release input sref is null"); 46 return; 47 } 48 OsalAtomicDec(&sref->refs); 49 lockRef = OsalAtomicRead(&sref->refs); 50 if ((lockRef == 0) && (sref->listener != NULL)) { 51 struct IHdfSRefListener *listener = sref->listener; 52 if (listener->OnLastRelease != NULL) { 53 listener->OnLastRelease(sref); 54 } 55 } 56 } 57 HdfSRefConstruct(struct HdfSRef * sref,struct IHdfSRefListener * listener)58void HdfSRefConstruct(struct HdfSRef *sref, struct IHdfSRefListener *listener) 59 { 60 if ((sref == NULL) || (listener == NULL)) { 61 HDF_LOGE("Input params is invalid"); 62 return; 63 } 64 OsalAtomicSet(&sref->refs, 0); 65 sref->listener = listener; 66 sref->Acquire = HdfSRefAcquire; 67 sref->Release = HdfSRefRelease; 68 sref->Count = HdfSRefCount; 69 } 70 71