qrwlock.h 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Queue read/write lock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
 *
 * Authors: Waiman Long <waiman.long@hp.com>
 */
#ifndef __ASM_GENERIC_QRWLOCK_H
#define __ASM_GENERIC_QRWLOCK_H

#include <linux/atomic.h>
#include <asm/barrier.h>
#include <asm/processor.h>

#include <asm-generic/qrwlock_types.h>

/*
28
 * Writer states & reader shift and bias.
29
 */
30 31 32 33
#define	_QW_WAITING	0x100		/* A writer is waiting	   */
#define	_QW_LOCKED	0x0ff		/* A writer holds the lock */
#define	_QW_WMASK	0x1ff		/* Writer mask		   */
#define	_QR_SHIFT	9		/* Reader count shift	   */
34 35 36 37 38
#define _QR_BIAS	(1U << _QR_SHIFT)

/*
 * External function declarations
 */
39
extern void queued_read_lock_slowpath(struct qrwlock *lock);
40
extern void queued_write_lock_slowpath(struct qrwlock *lock);
41 42

/**
43
 * queued_read_trylock - try to acquire read lock of a queue rwlock
44 45 46
 * @lock : Pointer to queue rwlock structure
 * Return: 1 if lock acquired, 0 if failed
 */
47
static inline int queued_read_trylock(struct qrwlock *lock)
48 49 50 51 52
{
	u32 cnts;

	cnts = atomic_read(&lock->cnts);
	if (likely(!(cnts & _QW_WMASK))) {
53
		cnts = (u32)atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
54 55 56 57 58 59 60 61
		if (likely(!(cnts & _QW_WMASK)))
			return 1;
		atomic_sub(_QR_BIAS, &lock->cnts);
	}
	return 0;
}

/**
62
 * queued_write_trylock - try to acquire write lock of a queue rwlock
63 64 65
 * @lock : Pointer to queue rwlock structure
 * Return: 1 if lock acquired, 0 if failed
 */
66
static inline int queued_write_trylock(struct qrwlock *lock)
67 68 69 70 71 72 73
{
	u32 cnts;

	cnts = atomic_read(&lock->cnts);
	if (unlikely(cnts))
		return 0;

74 75
	return likely(atomic_cmpxchg_acquire(&lock->cnts,
					     cnts, cnts | _QW_LOCKED) == cnts);
76 77
}
/**
78
 * queued_read_lock - acquire read lock of a queue rwlock
79 80
 * @lock: Pointer to queue rwlock structure
 */
81
static inline void queued_read_lock(struct qrwlock *lock)
82 83 84
{
	u32 cnts;

85
	cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
86 87 88 89
	if (likely(!(cnts & _QW_WMASK)))
		return;

	/* The slowpath will decrement the reader count, if necessary. */
90
	queued_read_lock_slowpath(lock);
91 92 93
}

/**
94
 * queued_write_lock - acquire write lock of a queue rwlock
95 96
 * @lock : Pointer to queue rwlock structure
 */
97
static inline void queued_write_lock(struct qrwlock *lock)
98 99
{
	/* Optimize for the unfair lock case where the fair flag is 0. */
100
	if (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0)
101 102
		return;

103
	queued_write_lock_slowpath(lock);
104 105 106
}

/**
107
 * queued_read_unlock - release read lock of a queue rwlock
108 109
 * @lock : Pointer to queue rwlock structure
 */
110
static inline void queued_read_unlock(struct qrwlock *lock)
111 112 113 114
{
	/*
	 * Atomically decrement the reader count
	 */
115
	(void)atomic_sub_return_release(_QR_BIAS, &lock->cnts);
116 117 118
}

/**
119
 * queued_write_unlock - release write lock of a queue rwlock
120 121
 * @lock : Pointer to queue rwlock structure
 */
122
static inline void queued_write_unlock(struct qrwlock *lock)
123
{
124
	smp_store_release(&lock->wlocked, 0);
125 126 127 128 129 130
}

/*
 * Remapping rwlock architecture specific functions to the corresponding
 * queue rwlock functions.
 */
131 132 133 134 135 136
#define arch_read_lock(l)	queued_read_lock(l)
#define arch_write_lock(l)	queued_write_lock(l)
#define arch_read_trylock(l)	queued_read_trylock(l)
#define arch_write_trylock(l)	queued_write_trylock(l)
#define arch_read_unlock(l)	queued_read_unlock(l)
#define arch_write_unlock(l)	queued_write_unlock(l)
137 138

#endif /* __ASM_GENERIC_QRWLOCK_H */