summaryrefslogtreecommitdiff
path: root/scripts/atomic/fallbacks/dec_if_positive
blob: 3459075d8674b57fba88af9889b814f72d0f53f5 (plain)
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
28
29
30
31
if /bin/sh ${ATOMICDIR}/chkdup.sh arch_${atomic}_dec_if_positive dec_if_positive
then
cat <<EOF
/**
 * arch_${atomic}_dec_if_positive - Atomic decrement if old value is positive
 * @v: pointer of type ${atomic}_t
 *
 * Atomically decrement @v with full ordering, but only if the original
 * value is greater than zero, returning new value.  Note that the desired
 * new value will be returned even if the decrement did not occur, so that
 * if the old value is -3, then there @v will not be decremented, but -4
 * will be returned.  As a result, if the return value is non-negative,
 * then the value was in fact decremented.
 */
EOF
fi
cat <<EOF
static __always_inline ${ret}
arch_${atomic}_dec_if_positive(${atomic}_t *v)
{
	${int} dec, c = arch_${atomic}_read(v);

	do {
		dec = c - 1;
		if (unlikely(dec < 0))
			break;
	} while (!arch_${atomic}_try_cmpxchg(v, &c, dec));

	return dec;
}
EOF