Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / spin_lock.cpp
Created September 20, 2015 08:31
basic spin lock
class Atomic {
std::atomic<int> val = 0;
public:
void Lock()
{
int expected = 0;
while (!val.compare_exchange_weak(expected, 1)) {
expected = 0;
}
}
@yorung
yorung / readers_writer_lock.cpp
Last active September 20, 2015 15:08
Simple readers-writer lock
class Atomic {
std::atomic<int> val = 0;
static const int writeLockBit = 0x40000000;
void LockInternal(int delta, int testBits)
{
int expected;
do {
do {
expected = val;
@yorung
yorung / upgradable_readers_writer_lock.cpp
Last active September 20, 2015 15:07
Upgradable readers-writer lock
class Atomic {
std::atomic<int> val = 0;
static const int writeLockBit = 0x40000000;
static const int upgradingBit = 0x00010000;
static const int writeUpgradeingBits = 0x7fff0000;
static const int readerBits = 0x0000ffff;
void LockInternal(int delta, int testBits)
{
int expected;
@yorung
yorung / upgradable_read_lock.cpp
Last active September 20, 2015 15:05
Upgradable read lock
class Atomic {
std::atomic<int> val = 0;
static const int writeLockBit = 0x40000000;
static const int upgradableBit = 0x00010000;
static const int readerBits = 0x0000ffff;
void LockInternal(int delta, int testBits)
{
int expected;
do {
@yorung
yorung / try_upgrade_spin_lock.cpp
Last active September 21, 2015 14:07
Upgradable read lock. It may fail when conflict occurred.
class Atomic {
std::atomic<int> val = 0;
static const int writeLockBit = 0x40000000;
static const int upgradingBit = 0x20000000;
static const int readerBits = 0x0000ffff;
void LockInternal(int delta, int testBits)
{
int expected;
do {
@yorung
yorung / replace_dofile.cpp
Created November 22, 2015 12:59
[Lua] Replace dofile for own file system
int aflDoFileForReplace(lua_State* L)
{
const char* fileName = lua_tostring(L, -1);
char* img = (char*)LoadFile(fileName);
if (!img) {
luaL_error(L, "aflDoFile: could not load file %s", fileName);
return false;
}
bool ok = true;
if (luaL_loadbuffer(L, img, strlen(img), fileName)) {
@yorung
yorung / callee.lua
Created November 22, 2015 13:55
Return values of dofile
return "A message from callee!"
@yorung
yorung / replace_require.lua
Created December 5, 2015 07:08
Write "require" in Lua
function require(m)
dofile(m..".lua")
end
print("calling with dofile", dofile("random.lua"), dofile("random.lua"), dofile("random.lua"))
print("calling with require", require("random"), require("random"), require("random"))
assert(require("random") == package.loaded["random"])
function require(m)
package.loaded[m] = package.loaded[m] or dofile(m..".lua") or true
return package.loaded[m]
end