From b8d53d17d53f93e2f67340a75a2372141e0816ab Mon Sep 17 00:00:00 2001 From: Tomas Winkler Date: Mon, 15 Oct 2018 21:41:56 +0300 Subject: [PATCH] dm: mei: add reference counter functions mei handles objects on the list, hence reference counting infrastructure is required for easier multithreading. Tracked-On: #1536 Signed-off-by: Tomas Winkler Acked-by: Wang, Yu1 --- devicemodel/hw/pci/virtio/virtio_mei.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/devicemodel/hw/pci/virtio/virtio_mei.c b/devicemodel/hw/pci/virtio/virtio_mei.c index f66d88263..d30ed24a0 100644 --- a/devicemodel/hw/pci/virtio/virtio_mei.c +++ b/devicemodel/hw/pci/virtio/virtio_mei.c @@ -129,3 +129,20 @@ static int guid_unparse(const guid_t *guid, char *str, size_t len) return 0; } +struct refcnt { + void (*destroy)(const struct refcnt *ref); + int count; +}; + +static inline void +refcnt_get(const struct refcnt *ref) +{ + __sync_add_and_fetch((int *)&ref->count, 1); +} + +static inline void +refcnt_put(const struct refcnt *ref) +{ + if (__sync_sub_and_fetch((int *)&ref->count, 1) == 0) + ref->destroy(ref); +}