1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _ATOMIC_BITSET_H_ 18 #define _ATOMIC_BITSET_H_ 19 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include <cpu/atomicBitset.h> 23 24 struct AtomicBitset; 25 26 //static size calc: 27 // ATOMIC_BITSET_SZ(numbits) 28 //static alloc 29 // ATOMIC_BITSET_DECL(nam, numbits, [static]); 30 //dynamic init: 31 // uint32_t sz = atomicBitsetSize(uint32_t numBits); 32 // struct AtomicBitset *set = (struct AtomicBitset*)heapAlloc(sz); 33 // atomicBitsetInit(set, numBits); 34 35 36 void atomicBitsetInit(struct AtomicBitset *set, uint32_t numBits); //inited state is all zeroes 37 uint32_t atomicBitsetGetNumBits(const struct AtomicBitset *set); 38 bool atomicBitsetGetBit(const struct AtomicBitset *set, uint32_t num); 39 void atomicBitsetClearBit(struct AtomicBitset *set, uint32_t num); 40 void atomicBitsetSetBit(struct AtomicBitset *set, uint32_t num); 41 42 //find a clear bit and set it atomically. 43 // returns bit number or negative if none. 44 // only one pass is attempted so if index 0 is cleared after we've looked at it, too bad 45 int32_t atomicBitsetFindClearAndSet(struct AtomicBitset *set); 46 47 //swap the bitsets in atomicallyAccessedSet and otherSet 48 // returns false if the size of the bitsets are different. 49 // otherwise atomically copies the bitset in otherSet into atomicallyAccessedSet 50 // and returns the previous value of atomicallyAccessedSet in otherSet 51 // NOTE: the copy back to otherSet is not atomic 52 bool atomicBitsetXchg(struct AtomicBitset *atomicallyAccessedSet, struct AtomicBitset *otherSet); 53 54 //read the bits in set into an array of uint32_t's 55 // returns false if the number of bits in set is not numBits 56 // otherwise atomically read a uint32_t at a time from the bitset in set into 57 // dest 58 // NOTE: if bitset is not a multiple of 32, the remaining bits up to 32 in the 59 // last uint32_t will contain undefined values 60 bool atomicBitsetBulkRead(struct AtomicBitset *set, uint32_t *dest, uint32_t numBits); 61 62 #endif 63 64