diff options
| author | wangmenglan <[email protected]> | 2023-05-09 12:07:48 +0800 |
|---|---|---|
| committer | wangmenglan <[email protected]> | 2023-05-09 14:23:39 +0800 |
| commit | ceffc9b16837a592a6cbbe570f9d6fe826eb0686 (patch) | |
| tree | d38f1406bca5bd945623411a9e0d90cf4c82e556 /common/src/tfe_cmsg.cpp | |
| parent | 7c3b77fb2f8de60a93a6ae2ef1c36d1e6646dbce (diff) | |
TSG-14930 TFE支持发送控制报文给SAPP
Diffstat (limited to 'common/src/tfe_cmsg.cpp')
| -rw-r--r-- | common/src/tfe_cmsg.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/common/src/tfe_cmsg.cpp b/common/src/tfe_cmsg.cpp index 8b01030..d16705b 100644 --- a/common/src/tfe_cmsg.cpp +++ b/common/src/tfe_cmsg.cpp @@ -24,6 +24,8 @@ struct tfe_cmsg_tlv struct tfe_cmsg { + uint8_t flag; + uint8_t ref; pthread_rwlock_t rwlock; uint16_t nr_tlvs; struct tfe_cmsg_tlv* tlvs[TFE_CMSG_TLV_NR_MAX]; @@ -43,6 +45,10 @@ struct tfe_cmsg* tfe_cmsg_init() cmsg->size = sizeof(struct tfe_cmsg_serialize_header); pthread_rwlock_init(&(cmsg->rwlock), NULL); + + ATOMIC_ZERO(&cmsg->flag); + ATOMIC_ZERO(&cmsg->ref); + ATOMIC_INC(&cmsg->ref); return cmsg; } @@ -50,6 +56,8 @@ void tfe_cmsg_destroy(struct tfe_cmsg *cmsg) { if(cmsg != NULL) { + if ((__sync_sub_and_fetch(&cmsg->ref, 1) != 0)) + return; pthread_rwlock_wrlock(&cmsg->rwlock); for(int i = 0; i < TFE_CMSG_TLV_NR_MAX; i++) { @@ -57,9 +65,31 @@ void tfe_cmsg_destroy(struct tfe_cmsg *cmsg) } pthread_rwlock_unlock(&cmsg->rwlock); pthread_rwlock_destroy(&cmsg->rwlock); + FREE(&cmsg); } +} + +void tfe_cmsg_dup(struct tfe_cmsg *cmsg) +{ + if (cmsg == NULL) + return; + ATOMIC_INC(&cmsg->ref); +} - FREE(&cmsg); +void tfe_cmsg_set_flag(struct tfe_cmsg *cmsg, uint8_t flag) +{ + if (cmsg == NULL) + return; + ATOMIC_SET(&cmsg->flag, flag); +} + +uint8_t tfe_cmsg_get_flag(struct tfe_cmsg *cmsg) +{ + if (cmsg == NULL) + return 0; + uint8_t flag = 0; + flag = ATOMIC_READ(&cmsg->flag); + return flag; } int tfe_cmsg_set(struct tfe_cmsg * cmsg, enum tfe_cmsg_tlv_type type, const unsigned char * value, uint16_t size) @@ -230,6 +260,7 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms } cmsg = ALLOC(struct tfe_cmsg, 1); + pthread_rwlock_init(&(cmsg->rwlock), NULL); offset = sizeof(struct tfe_cmsg_serialize_header); nr_tlvs = ntohs(header->nr_tlvs); for(int i = 0; i < nr_tlvs; i++) @@ -258,9 +289,7 @@ int tfe_cmsg_deserialize(const unsigned char *data, uint16_t len, struct tfe_cms offset += length; } cmsg->size = offset; - pthread_rwlock_wrlock(&((*pcmsg)->rwlock)); *pcmsg = cmsg; - pthread_rwlock_unlock(&((*pcmsg)->rwlock)); return 0; error_out: |
