android - what is purpose of 'or' a value with -1? -
i came across following code in android framework , wondering purpose of android_atomic_or(-1, &mfd)
? -1
0xffffffff 32 bit int, or -1 value should -1. if understanding correctly, code below doesn't make sense me.
https://android.googlesource.com/platform/frameworks/native/+/6eabaa3/libs/utils/memoryheapbase.cpp
line:150
void memoryheapbase::dispose() { int fd = android_atomic_or(-1, &mfd); //mfd file descriptor if (fd >= 0) { if (mneedunmap) { //logd("munmap(fd=%d, base=%p, size=%lu)", fd, mbase, msize); munmap(mbase, msize); } mbase = 0; msize = 0; close(fd); } }
and, here (one of) implementation of android_atomic_or
http://androidxref.com/5.0.0_r2/xref/system/core/include/cutils/atomic-x86.h#135
134extern android_atomic_inline int32_t 135android_atomic_or(int32_t value, volatile int32_t *ptr) 136{ 137 int32_t prev, status; 138 { 139 prev = *ptr; 140 status = android_atomic_cas(prev, prev | value, ptr); 141 } while (__builtin_expect(status != 0, 0)); 142 return prev; 143} 73 android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr) 74{ 75 int32_t prev; 76 __asm__ __volatile__ ("lock; cmpxchgl %1, %2" 77 : "=a" (prev) 78 : "q" (new_value), "m" (*ptr), "0" (old_value) 79 : "memory"); 80 return prev != old_value; 81} 82
i assume android_atomic_or atomic version of (value|*ptr) i'm not sure since can't read assembly. must misunderstand here. help.
that similar standard atomic_fetch_or()
(i suspect exists c11 not available when android started).
that function returns original value before modified. previous value , set bits in value.
see lines 139 , 142, assume loop executes once. compare and set 1 of basic operations required atomics (the other compare , exchange bit more universal).
Comments
Post a Comment