Skip to content

Instantly share code, notes, and snippets.

@luuhq
Last active August 29, 2015 13:59
Show Gist options
  • Save luuhq/10503518 to your computer and use it in GitHub Desktop.
Save luuhq/10503518 to your computer and use it in GitHub Desktop.
Naive concurrent commits
void Commit(Row[] rows)
{
// yield priority to commits that came before
GetGlobalLock();
ReleaseGlobalLock();
// attempt to lock all rows
int i = 0;
for (i = 0; i < rows.Length; i++)
{
if (!rows[i].Lock())
{
break;
}
}
// check if row locks needs releasing
if (i != rows.Length)
{
while (--i >=0)
{
rows[i].Unlock();
}
}
if (i != rows.Length)
{
GetGlobalLock();
Write(rows);
ReleaseGlobalLock();
}
else
{
Write(rows);
for (i = 0; i < rows.Length; i++)
{
rows[i].Unlock();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment