Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 1 | #ifndef __XREF_H__ |
| 2 | #define __XREF_H__ |
| 3 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 4 | #include <common/hathreads.h> |
| 5 | |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 6 | /* xref is used to create relation between two elements. |
| 7 | * Once an element is released, it breaks the relation. If the |
| 8 | * relation is already broken, it frees the xref struct. |
| 9 | * The pointer between two elements is sort of a refcount with |
| 10 | * max value 1. The relation is only between two elements. |
| 11 | * The pointer and the type of elements a and b are conventional. |
| 12 | */ |
| 13 | |
| 14 | struct xref { |
| 15 | struct xref *peer; |
| 16 | }; |
| 17 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 18 | #define XREF_BUSY ((struct xref *)1) |
| 19 | |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 20 | static inline void xref_create(struct xref *xref_a, struct xref *xref_b) |
| 21 | { |
| 22 | xref_a->peer = xref_b; |
| 23 | xref_b->peer = xref_a; |
| 24 | } |
| 25 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 26 | static inline struct xref *xref_get_peer_and_lock(struct xref *xref) |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 27 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 28 | struct xref *local; |
| 29 | struct xref *remote; |
| 30 | |
| 31 | while (1) { |
| 32 | |
| 33 | /* Get the local pointer to the peer. */ |
Olivier Houchard | 8beb27e | 2019-03-08 18:47:29 +0100 | [diff] [blame] | 34 | local = _HA_ATOMIC_XCHG(&xref->peer, XREF_BUSY); |
Olivier Houchard | d2b5d16 | 2019-03-08 13:47:21 +0100 | [diff] [blame] | 35 | __ha_barrier_atomic_store(); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 36 | |
| 37 | /* If the local pointer is NULL, the peer no longer exists. */ |
| 38 | if (local == NULL) { |
| 39 | xref->peer = NULL; |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | /* If the local pointeru is BUSY, the peer try to acquire the |
| 44 | * lock. We retry the process. |
| 45 | */ |
| 46 | if (local == XREF_BUSY) |
| 47 | continue; |
| 48 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 49 | /* We are locked, the peer can't disappear, try to acquire |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 50 | * the pper's lock. Note that remote can't be NULL. |
| 51 | */ |
Olivier Houchard | 8beb27e | 2019-03-08 18:47:29 +0100 | [diff] [blame] | 52 | remote = _HA_ATOMIC_XCHG(&local->peer, XREF_BUSY); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 53 | |
| 54 | /* The remote lock is BUSY, We retry the process. */ |
| 55 | if (remote == XREF_BUSY) { |
| 56 | xref->peer = local; |
Olivier Houchard | ff5dd74 | 2019-01-18 17:21:32 +0100 | [diff] [blame] | 57 | __ha_barrier_store(); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 58 | continue; |
| 59 | } |
| 60 | |
| 61 | /* We have the lock, we return the value of the xref. */ |
| 62 | return local; |
| 63 | } |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 66 | static inline void xref_unlock(struct xref *xref, struct xref *peer) |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 67 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 68 | /* Release the peer. */ |
| 69 | peer->peer = xref; |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 70 | |
Olivier Houchard | ff5dd74 | 2019-01-18 17:21:32 +0100 | [diff] [blame] | 71 | __ha_barrier_store(); |
| 72 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 73 | /* Release myself. */ |
| 74 | xref->peer = peer; |
| 75 | } |
| 76 | |
| 77 | static inline void xref_disconnect(struct xref *xref, struct xref *peer) |
| 78 | { |
| 79 | peer->peer = NULL; |
Olivier Houchard | ff5dd74 | 2019-01-18 17:21:32 +0100 | [diff] [blame] | 80 | __ha_barrier_store(); |
Thierry FOURNIER | 3c65b7a | 2017-08-31 20:35:18 +0200 | [diff] [blame] | 81 | xref->peer = NULL; |
| 82 | } |
| 83 | |
| 84 | #endif /* __XREF_H__ */ |