Skip to content

Commit

Permalink
minor formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jbanaszczyk committed Oct 24, 2018
1 parent a10afe8 commit 297b493
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/mini_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,35 @@

namespace jb {
namespace threads {

using namespace jb::logic;

class mutex {
public:
mutex(const mutex &) = delete;
mutex & operator = (const mutex &) = delete;
mutex() : unlocked(true) {};

bool try_lock() {
static_assert (sizeof(unlocked) == 1, "locked : expected single byte");
bool result;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
result = unlocked;
unlocked = false;
}
return result;
}

void unlock() {
unlocked = true;
}

public:
volatile bool unlocked;
};

template <typename T = uint8_t, T LOCKED = T{ 0xC3 }, T UNLOCKED = T{ 0x3C } >
class safe_mutex {

public:
safe_mutex(const safe_mutex&) = delete;
safe_mutex& operator = (const safe_mutex &) = delete;
Expand Down Expand Up @@ -53,33 +77,6 @@ namespace jb {
return (!lhs) ^ (!rhs);
}
};

class mutex {

public:
mutex(const mutex &) = delete;
mutex & operator = (const mutex &) = delete;
mutex() : unlocked(true) {};

bool try_lock() {
static_assert (sizeof(unlocked) == 1, "locked : expected single byte");
bool result;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
result = unlocked;
unlocked = false;
}
return result;
}

void unlock() {
unlocked = true;
}


public:
volatile bool unlocked;
};

}
}

Expand Down

0 comments on commit 297b493

Please sign in to comment.