2017-08-31 18:35:18 +00:00
|
|
|
#ifndef __XREF_H__
|
|
|
|
#define __XREF_H__
|
|
|
|
|
2017-09-01 12:17:32 +00:00
|
|
|
#include <common/hathreads.h>
|
|
|
|
|
2017-08-31 18:35:18 +00:00
|
|
|
/* xref is used to create relation between two elements.
|
|
|
|
* Once an element is released, it breaks the relation. If the
|
|
|
|
* relation is already broken, it frees the xref struct.
|
|
|
|
* The pointer between two elements is sort of a refcount with
|
|
|
|
* max value 1. The relation is only between two elements.
|
|
|
|
* The pointer and the type of elements a and b are conventional.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct xref {
|
|
|
|
struct xref *peer;
|
|
|
|
};
|
|
|
|
|
2017-09-01 12:17:32 +00:00
|
|
|
#define XREF_BUSY ((struct xref *)1)
|
|
|
|
|
2017-08-31 18:35:18 +00:00
|
|
|
static inline void xref_create(struct xref *xref_a, struct xref *xref_b)
|
|
|
|
{
|
|
|
|
xref_a->peer = xref_b;
|
|
|
|
xref_b->peer = xref_a;
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:17:32 +00:00
|
|
|
static inline struct xref *xref_get_peer_and_lock(struct xref *xref)
|
2017-08-31 18:35:18 +00:00
|
|
|
{
|
2017-09-01 12:17:32 +00:00
|
|
|
struct xref *local;
|
|
|
|
struct xref *remote;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
/* Get the local pointer to the peer. */
|
2019-03-08 17:47:29 +00:00
|
|
|
local = _HA_ATOMIC_XCHG(&xref->peer, XREF_BUSY);
|
2019-03-08 12:47:21 +00:00
|
|
|
__ha_barrier_atomic_store();
|
2017-09-01 12:17:32 +00:00
|
|
|
|
|
|
|
/* If the local pointer is NULL, the peer no longer exists. */
|
|
|
|
if (local == NULL) {
|
|
|
|
xref->peer = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the local pointeru is BUSY, the peer try to acquire the
|
|
|
|
* lock. We retry the process.
|
|
|
|
*/
|
|
|
|
if (local == XREF_BUSY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We are locked, the peer cant disapear, try to acquire
|
|
|
|
* the pper's lock. Note that remote can't be NULL.
|
|
|
|
*/
|
2019-03-08 17:47:29 +00:00
|
|
|
remote = _HA_ATOMIC_XCHG(&local->peer, XREF_BUSY);
|
2017-09-01 12:17:32 +00:00
|
|
|
|
|
|
|
/* The remote lock is BUSY, We retry the process. */
|
|
|
|
if (remote == XREF_BUSY) {
|
|
|
|
xref->peer = local;
|
2019-01-18 16:21:32 +00:00
|
|
|
__ha_barrier_store();
|
2017-09-01 12:17:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have the lock, we return the value of the xref. */
|
|
|
|
return local;
|
|
|
|
}
|
2017-08-31 18:35:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 12:17:32 +00:00
|
|
|
static inline void xref_unlock(struct xref *xref, struct xref *peer)
|
2017-08-31 18:35:18 +00:00
|
|
|
{
|
2017-09-01 12:17:32 +00:00
|
|
|
/* Release the peer. */
|
|
|
|
peer->peer = xref;
|
2017-08-31 18:35:18 +00:00
|
|
|
|
2019-01-18 16:21:32 +00:00
|
|
|
__ha_barrier_store();
|
|
|
|
|
2017-09-01 12:17:32 +00:00
|
|
|
/* Release myself. */
|
|
|
|
xref->peer = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void xref_disconnect(struct xref *xref, struct xref *peer)
|
|
|
|
{
|
|
|
|
peer->peer = NULL;
|
2019-01-18 16:21:32 +00:00
|
|
|
__ha_barrier_store();
|
2017-08-31 18:35:18 +00:00
|
|
|
xref->peer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __XREF_H__ */
|