chiark / gitweb /
0d5b3f4e1b533f7399fff6d92eca841e5cbb5da9
[elogind.git] / src / basic / refcnt.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2013 Lennart Poettering
8 ***/
9
10 /* A type-safe atomic refcounter.
11  *
12  * DO NOT USE THIS UNLESS YOU ACTUALLY CARE ABOUT THREAD SAFETY! */
13
14 typedef struct {
15         volatile unsigned _value;
16 } RefCount;
17
18 #define REFCNT_GET(r) ((r)._value)
19 #define REFCNT_INC(r) (__sync_add_and_fetch(&(r)._value, 1))
20 #define REFCNT_DEC(r) (__sync_sub_and_fetch(&(r)._value, 1))
21
22 #define REFCNT_INIT ((RefCount) { ._value = 1 })