summaryrefslogtreecommitdiff
path: root/scripts/atomic/fallbacks/dec_unless_positive
blob: 0c7a99d26784e1c406eedcaf1647a89d892d6507 (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
if /bin/sh ${ATOMICDIR}/chkdup.sh arch_${atomic}_dec_unless_positive dec_unless_positive
then
cat <<EOF
/**
 * arch_${atomic}_dec_unless_positive - Atomic decrement if old value is non-positive
 * @v: pointer of type ${atomic}_t
 *
 * Atomically decrement @v with full ordering, but only if the original value is less
 * than or equal to zero.  Return @true if the decrement happened and
 * @false otherwise.
 */
EOF
fi
cat <<EOF
static __always_inline bool
arch_${atomic}_dec_unless_positive(${atomic}_t *v)
{
	${int} c = arch_${atomic}_read(v);

	do {
		if (unlikely(c > 0))
			return false;
	} while (!arch_${atomic}_try_cmpxchg(v, &c, c - 1));

	return true;
}
EOF