wrflock.h 4.42 KB
#ifndef _WRFLOCK_H
#define _WRFLOCK_H	1

#include <atomic>
#include <linux/futex.h>

#define WRFLOCK_WWAITBLOCK 1
#define WRFLOCK_WWAITYIELD 2
#define WRFLOCK_RWAITBLOCK 4
#define WRFLOCK_RWAITYIELD 8
#define WRFLOCK_FWAITBLOCK 16
#define WRFLOCK_FWAITYIELD 32
struct wrflock_t
{
# if __BYTE_ORDER == __LITTLE_ENDIAN
#  define WRFLOCK_COUNTERS_OFFSET 0
#  define WRFLOCK_STATE_OFFSET 1
#  define MAKEFLAGS64(flags, le) (le?((((uint64_t)(flags))<<32)):(((uint64_t)(flags))))
#  define MAKESHIFT64(shift, le) (le?(shift+32):(shift))
# elif __BYTE_ORDER == __BIG_ENDIAN
#  define WRFLOCK_COUNTERS_OFFSET 1
#  define WRFLOCK_STATE_OFFSET 0
#  define MAKEFLAGS64(flags, le) (le?((((uint64_t)(flags)))):((((uint64_t)(flags))<<32)))
#  define MAKESHIFT64(shift, le) (le?(shift):(shift+32))
# else
# error Unsupported byte order.
# endif

# define WRFLOCK_PRIVATE_MASK_32		((uint32_t)0x04000000)
# define WRFLOCK_PRIVATE_MASK_64		(MAKEFLAGS64(WRFLOCK_PRIVATE_MASK_32,0) | MAKEFLAGS64(WRFLOCK_PRIVATE_MASK_32,1))
# define WRFLOCK_WWAITYIELD_MASK_32		((uint32_t)0x00010000)
# define WRFLOCK_WWAITYIELD_MASK_64		MAKEFLAGS64(WRFLOCK_WWAITYIELD_MASK_32,1)
# define WRFLOCK_RWAITYIELD_MASK_32		((uint32_t)0x00020000)
# define WRFLOCK_RWAITYIELD_MASK_64		MAKEFLAGS64(WRFLOCK_RWAITYIELD_MASK_32,1)
# define WRFLOCK_FWAITYIELD_MASK_32		((uint32_t)0x00040000)
# define WRFLOCK_FWAITYIELD_MASK_64		MAKEFLAGS64(WRFLOCK_FWAITYIELD_MASK_32,1)

# define WRACQUIRE_VALUE_SHIFT_32		28
# define RDACQUIRE_VALUE_SHIFT_32		29
# define FRACQUIRE_VALUE_SHIFT_32		30
# define RDACQUIRE_COUNTER_SHIFT_32		0
# define WRACQUIRE_VALUE_SHIFT_64		MAKESHIFT64(WRACQUIRE_VALUE_SHIFT_32,1)
# define RDACQUIRE_VALUE_SHIFT_64		MAKESHIFT64(RDACQUIRE_VALUE_SHIFT_32,1)
# define FRACQUIRE_VALUE_SHIFT_64		MAKESHIFT64(FRACQUIRE_VALUE_SHIFT_32,1)
# define RDACQUIRE_COUNTER_SHIFT_64		MAKESHIFT64(RDACQUIRE_COUNTER_SHIFT_32,0)

# define WRACQUIRE_VALUE_MASK_32		((uint32_t)0x10000000)
# define RDACQUIRE_VALUE_MASK_32		((uint32_t)0x20000000)
# define FRACQUIRE_VALUE_MASK_32		((uint32_t)0x40000000)
# define RDACQUIRE_COUNTER_MASK_32		((uint32_t)0x0000FFFF)
# define RDNXTLOOP_FLAG_MASK_32			((uint32_t)0x02000000)
# define WRACQUIRE_VALUE_MASK_64		MAKEFLAGS64(WRACQUIRE_VALUE_MASK_32,1)
# define RDACQUIRE_VALUE_MASK_64		MAKEFLAGS64(RDACQUIRE_VALUE_MASK_32,1)
# define FRACQUIRE_VALUE_MASK_64		MAKEFLAGS64(FRACQUIRE_VALUE_MASK_32,1)
# define RDACQUIRE_COUNTER_MASK_64		MAKEFLAGS64(RDACQUIRE_COUNTER_MASK_32,0)
# define RDNXTLOOP_FLAG_MASK_64			MAKEFLAGS64(RDNXTLOOP_FLAG_MASK_32,1)


# define NEXTSTATE_WRITE_MASK_32		((uint32_t)0x00000010)
# define NEXTSTATE_READFREE_MASK_32		((uint32_t)0x00000020)
# define NEXTSTATE_VALUE_MASK_32		(NEXTSTATE_WRITE_MASK_32|NEXTSTATE_READFREE_MASK_32|NEXTSTATE_FREE_MASK_32)
# define NEXTSTATE_WRITE_MASK_64		MAKEFLAGS64(NEXTSTATE_WRITE_MASK_32,1)
# define NEXTSTATE_READFREE_MASK_64		MAKEFLAGS64(NEXTSTATE_READFREE_MASK_32,1)
# define NEXTSTATE_VALUE_MASK_64		(NEXTSTATE_WRITE_MASK_64|NEXTSTATE_READFREE_MASK_64|NEXTSTATE_FREE_MASK_64)

# define CURRSTATE_WRITE_MASK_32		((uint32_t)0x00000001)
# define CURRSTATE_READ_MASK_32			((uint32_t)0x00000002)
# define CURRSTATE_FREE_MASK_32			((uint32_t)0x00000004)
# define CURRSTATE_VALUE_MASK_32		(CURRSTATE_WRITE_MASK_32|CURRSTATE_READ_MASK_32|CURRSTATE_FREE_MASK_32)
# define CURRSTATE_WRITE_MASK_64		MAKEFLAGS64(CURRSTATE_WRITE_MASK_32,1)
# define CURRSTATE_READ_MASK_64			MAKEFLAGS64(CURRSTATE_READ_MASK_32,1)
# define CURRSTATE_FREE_MASK_64			MAKEFLAGS64(CURRSTATE_FREE_MASK_32,1)
# define CURRSTATE_VALUE_MASK_64		(CURRSTATE_WRITE_MASK_64|CURRSTATE_READ_MASK_64|CURRSTATE_FREE_MASK_64)


	union {
		std::atomic<uint64_t> data64;
		std::atomic<uint32_t> data32[2];
	};
};

int wrflock_init (wrflock_t *lock, int waittype, int pshared);
int wrflock_destroy (wrflock_t *lock);
int wrflock_wacquire (wrflock_t *lock);
int wrflock_racquire (wrflock_t *lock);
int wrflock_facquire (wrflock_t *lock);
int wrflock_wrelease (wrflock_t *lock);
int wrflock_rrelease (wrflock_t *lock);
int wrflock_frelease (wrflock_t *lock);
int wrflock_wwait (wrflock_t *lock);
int wrflock_rwait (wrflock_t *lock);
int wrflock_fwait (wrflock_t *lock);
int wrflock_wtrywait (wrflock_t *lock);
int wrflock_rtrywait (wrflock_t *lock);
int wrflock_ftrywait (wrflock_t *lock);
int wrflock_wtimewait (wrflock_t *lock, const struct timespec *abstime);
int wrflock_rtimewait (wrflock_t *lock, const struct timespec *abstime);
int wrflock_ftimewait (wrflock_t *lock, const struct timespec *abstime);


#endif	/* rmwlock.h */