Compare commits
2 Commits
5d6734be4a
...
5809a89eae
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5809a89eae | ||
![]() |
65af565668 |
10
atomicity.lua
Normal file
10
atomicity.lua
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
t = {}
|
||||||
|
|
||||||
|
threads.parallel(8, function(no)
|
||||||
|
for i = 1, 1250 do
|
||||||
|
table.insert(t, i)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
print("Expected: 10000")
|
||||||
|
print(#t)
|
63
lrwl.h
Normal file
63
lrwl.h
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#pragma omp
|
||||||
|
|
||||||
|
#define LRWL_WRITE_ACTIVE (1UL<<31)
|
||||||
|
#define LRWL_MASK (LRWL_WRITE_ACTIVE - 1)
|
||||||
|
#define LRWL_MAX_READERS 1024
|
||||||
|
|
||||||
|
typedef struct LRWL {
|
||||||
|
_Atomic uint32_t data;
|
||||||
|
} LRWL;
|
||||||
|
|
||||||
|
static inline void lrwl_read_lock(LRWL *self) {
|
||||||
|
while(1) {
|
||||||
|
uint32_t data = atomic_load(&self->data);
|
||||||
|
|
||||||
|
if((data & LRWL_WRITE_ACTIVE) || (data & LRWL_MASK) >= LRWL_MAX_READERS) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(atomic_compare_exchange_weak(&self->data, &data, data + 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lrwl_read_unlock(LRWL *self) {
|
||||||
|
while(1) {
|
||||||
|
uint32_t data = atomic_load(&self->data);
|
||||||
|
|
||||||
|
assert((data & LRWL_MASK) > 0);
|
||||||
|
|
||||||
|
if(atomic_compare_exchange_weak(&self->data, &data, data - 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lrwl_write_lock(LRWL *self) {
|
||||||
|
while(1) {
|
||||||
|
uint32_t data = atomic_load(&self->data);
|
||||||
|
|
||||||
|
if(data & LRWL_WRITE_ACTIVE) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(atomic_compare_exchange_weak(&self->data, &data, data | LRWL_WRITE_ACTIVE)) {
|
||||||
|
while((atomic_load(&self->data) & LRWL_MASK) > 0);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void lrwl_write_unlock(LRWL *self) {
|
||||||
|
while(1) {
|
||||||
|
uint32_t data = atomic_load(&self->data);
|
||||||
|
|
||||||
|
assert(data & LRWL_WRITE_ACTIVE);
|
||||||
|
|
||||||
|
if(atomic_compare_exchange_weak(&self->data, &data, data & ~LRWL_WRITE_ACTIVE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
multifizz.lua
Normal file
11
multifizz.lua
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
threads.parallel(1, function(no)
|
||||||
|
for i = 1, 100000 do
|
||||||
|
print(i)
|
||||||
|
if i % 3 == 0 then
|
||||||
|
print("Fizz")
|
||||||
|
end
|
||||||
|
if i % 5 == 0 then
|
||||||
|
print("Buzz")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
Loading…
Reference in New Issue
Block a user