Skip to content
Snippets Groups Projects
Commit f066e8d5 authored by Berenger Bramas's avatar Berenger Bramas
Browse files

Second bug solved (update the lock free bool array)

parent 73ed13b4
No related branches found
No related tags found
No related merge requests found
Pipeline #101776 failed
......@@ -27,31 +27,61 @@
#define LOCK_FREE_BOOL_ARRAY_HPP
#include <vector>
#include <memory>
#include <atomic>
#include <unistd.h>
#include <cstdio>
#include <omp.h>
class lock_free_bool_array{
std::vector<std::unique_ptr<long int>> keys;
static const int Available = 0;
static const int Busy = 1;
static const int NoOwner = -1;
struct Locker {
Locker() : lock(Available), ownerId(NoOwner), counter(0) {}
std::atomic_int lock;
std::atomic_int ownerId;
int counter;
};
std::vector<std::unique_ptr<Locker>> keys;
public:
explicit lock_free_bool_array(const long int inNbKeys = 512){
explicit lock_free_bool_array(const long int inNbKeys = 1024){
keys.resize(inNbKeys);
for(std::unique_ptr<long int>& k : keys){
k.reset(new long int(0));
for(auto& k : keys){
k.reset(new Locker());
}
}
void lock(const long int inKey){
volatile long int* k = keys[inKey%keys.size()].get();
long int res = 1;
while(res == 1){
res = __sync_val_compare_and_swap(k, 0, res);
Locker* k = keys[inKey%keys.size()].get();
if(k->ownerId.load() != omp_get_thread_num()){
int expected = Available;
while(!std::atomic_compare_exchange_strong(&k->lock, &expected, Busy)){
usleep(1);
}
k->ownerId.store(omp_get_thread_num());
k->counter = 0; // must remain
}
k->counter += 1;
assert(k->lock.load() == Busy);
assert(k->counter >= 1);
assert(k->ownerId.load() == omp_get_thread_num());
}
void unlock(const long int inKey){
volatile long int* k = keys[inKey%keys.size()].get();
assert(k && *k);
(*k) = 0;
Locker* k = keys[inKey%keys.size()].get();
assert(k->lock.load() == Busy);
assert(k->counter >= 1);
assert(k->ownerId.load() == omp_get_thread_num());
k->counter -= 1;
if(k->counter == 0){
k->ownerId.store(NoOwner);
k->lock.store(Available);
}
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment