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