blob: 42eed5808d9b01ef8895bf4115fb3e70c25e35e1 [file] [log] [blame]
Willy Tarreau374b4422020-06-02 17:46:16 +02001/*
2 * include/haproxy/xref.h
3 * Atomic cross-references between two elements - functions
4 *
5 * Copyright (C) 2017 Thierry Fournier <thierry.fournier@ozon.io>
6 * Copyright (C) 2020 Willy Tarreau - w@1wt.eu
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28#ifndef __HAPROXY_XREF_H__
29#define __HAPROXY_XREF_H__
30
Willy Tarreau374b4422020-06-02 17:46:16 +020031#include <haproxy/xref-t.h>
32
33/* xref is used to create relation between two elements.
34 * Once an element is released, it breaks the relation. If the
35 * relation is already broken, it frees the xref struct.
36 * The pointer between two elements is sort of a refcount with
37 * max value 1. The relation is only between two elements.
38 * The pointer and the type of elements a and b are conventional.
39 */
40
41static inline void xref_create(struct xref *xref_a, struct xref *xref_b)
42{
43 xref_a->peer = xref_b;
44 xref_b->peer = xref_a;
45}
46
47static inline struct xref *xref_get_peer_and_lock(struct xref *xref)
48{
49 struct xref *local;
50 struct xref *remote;
51
52 while (1) {
53
54 /* Get the local pointer to the peer. */
55 local = _HA_ATOMIC_XCHG(&xref->peer, XREF_BUSY);
56 __ha_barrier_atomic_store();
57
58 /* If the local pointer is NULL, the peer no longer exists. */
59 if (local == NULL) {
60 xref->peer = NULL;
61 return NULL;
62 }
63
64 /* If the local pointeru is BUSY, the peer try to acquire the
65 * lock. We retry the process.
66 */
67 if (local == XREF_BUSY)
68 continue;
69
70 /* We are locked, the peer can't disappear, try to acquire
71 * the pper's lock. Note that remote can't be NULL.
72 */
73 remote = _HA_ATOMIC_XCHG(&local->peer, XREF_BUSY);
74
75 /* The remote lock is BUSY, We retry the process. */
76 if (remote == XREF_BUSY) {
77 xref->peer = local;
78 __ha_barrier_store();
79 continue;
80 }
81
82 /* We have the lock, we return the value of the xref. */
83 return local;
84 }
85}
86
87static inline void xref_unlock(struct xref *xref, struct xref *peer)
88{
89 /* Release the peer. */
90 peer->peer = xref;
91
92 __ha_barrier_store();
93
94 /* Release myself. */
95 xref->peer = peer;
96}
97
98static inline void xref_disconnect(struct xref *xref, struct xref *peer)
99{
100 peer->peer = NULL;
101 __ha_barrier_store();
102 xref->peer = NULL;
103}
104
105#endif /* __HAPROXY_XREF_H__ */