Compare commits
No commits in common. "5809a89eaedc651bd30150e5d8b86840e1f44bb6" and "5d6734be4af088ecb52051729dfdc4eb0933a60b" have entirely different histories.
5809a89eae
...
5d6734be4a
@ -1,10 +0,0 @@
|
||||
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
63
lrwl.h
@ -1,63 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
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