Advertisement
Semper_Idem

atomic_fetch_inc_modulo_wrap

Jan 30th, 2021
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1.  
  2. /* Fetch and increment with modulo wrap
  3.  * Although there's theoretically a race condition,
  4.  * it's practically impossible to increment 32^2 times
  5.  * while the number is wrapped */
  6. static inline uint64_t atomic_fetch_inc_modulo_wrap(atomic_uint_fast64_t *val,
  7.                                                     uint32_t wrap) {
  8.   uint64_t fetched = (*val)++;
  9.   uint32_t hi = (fetched & 0xFFFFFFFF00000000) >> 32;
  10.   if (hi == wrap) {
  11.     atomic_fetch_and(val, 0xFFFFFFFF);
  12.   }
  13.   return fetched % wrap;
  14. }
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement