| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_OBJECT_STATE_ID_H |
| 4 | #define NETDATA_OBJECT_STATE_ID_H |
| 5 | |
| 6 | #include "libnetdata/libnetdata.h" |
| 7 | |
| 8 | typedef uint32_t OBJECT_STATE_ID; |
| 9 | |
| 10 | typedef struct { |
| 11 | OBJECT_STATE_ID state_id; |
| 12 | REFCOUNT state_refcount; |
| 13 | } OBJECT_STATE; |
| 14 | |
| 15 | #define OBJECT_STATE_DEACTIVATED (-INT32_MAX) |
| 16 | |
| 17 | #define OBJECT_STATE_INIT_ACTIVATED (OBJECT_STATE){ .state_id = 0, .state_refcount = 0, }; |
| 18 | #define OBJECT_STATE_INIT_DEACTIVATED (OBJECT_STATE){ .state_id = 0, .state_refcount = OBJECT_STATE_DEACTIVATED, }; |
| 19 | |
| 20 | // get the current state id of the object |
| 21 | OBJECT_STATE_ID object_state_id(OBJECT_STATE *os); |
| 22 | |
| 23 | // increments the object's state id |
| 24 | // enables using the object - users may acquire and release the object |
| 25 | void object_state_activate(OBJECT_STATE *os); |
| 26 | void object_state_activate_if_not_activated(OBJECT_STATE *os); |
| 27 | |
| 28 | // increments the object's state id |
| 29 | // prevents users from acquiring it, and waits until all of its holders have released it |
| 30 | void object_state_deactivate(OBJECT_STATE *os); |
| 31 | |
| 32 | bool object_state_acquire(OBJECT_STATE *os, OBJECT_STATE_ID wanted_state_id); |
| 33 | void object_state_release(OBJECT_STATE *os); |
| 34 | |
| 35 | #endif //NETDATA_OBJECT_STATE_ID_H |