mirror of
https://github.com/yuzu-emu/unicorn
synced 2024-11-24 16:48:30 +00:00
bitmap: add atomic test and clear
The new bitmap_test_and_clear_atomic() function clears a range and returns whether or not the bits were set. Backports commit 36546e5b803f6e363906607307f27c489441fd15 from qemu
This commit is contained in:
parent
7ff5f05c82
commit
6172e3dc29
2 changed files with 47 additions and 0 deletions
|
@ -29,6 +29,7 @@
|
|||
* bitmap_set(dst, pos, nbits) Set specified bit area
|
||||
* bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops
|
||||
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
||||
* bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area
|
||||
*/
|
||||
|
||||
/*
|
||||
|
@ -50,6 +51,7 @@
|
|||
void bitmap_set(unsigned long *map, long i, long len);
|
||||
void bitmap_set_atomic(unsigned long *map, long i, long len);
|
||||
void bitmap_clear(unsigned long *map, long start, long nr);
|
||||
bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr);
|
||||
|
||||
static inline unsigned long *bitmap_zero_extend(unsigned long *old,
|
||||
long old_nbits, long new_nbits)
|
||||
|
|
|
@ -91,3 +91,48 @@ void bitmap_clear(unsigned long *map, long start, long nr)
|
|||
*p &= ~mask_to_clear;
|
||||
}
|
||||
}
|
||||
|
||||
bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
|
||||
{
|
||||
unsigned long *p = map + BIT_WORD(start);
|
||||
const long size = start + nr;
|
||||
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
||||
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
||||
unsigned long dirty = 0;
|
||||
unsigned long old_bits;
|
||||
|
||||
/* First word */
|
||||
if (nr - bits_to_clear > 0) {
|
||||
old_bits = atomic_fetch_and(p, ~mask_to_clear);
|
||||
dirty |= old_bits & mask_to_clear;
|
||||
nr -= bits_to_clear;
|
||||
bits_to_clear = BITS_PER_LONG;
|
||||
mask_to_clear = ~0UL;
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Full words */
|
||||
if (bits_to_clear == BITS_PER_LONG) {
|
||||
while (nr >= BITS_PER_LONG) {
|
||||
if (*p) {
|
||||
old_bits = atomic_xchg(p, 0);
|
||||
dirty |= old_bits;
|
||||
}
|
||||
nr -= BITS_PER_LONG;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Last word */
|
||||
if (nr) {
|
||||
mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
|
||||
old_bits = atomic_fetch_and(p, ~mask_to_clear);
|
||||
dirty |= old_bits & mask_to_clear;
|
||||
} else {
|
||||
if (!dirty) {
|
||||
smp_mb();
|
||||
}
|
||||
}
|
||||
|
||||
return dirty != 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue