Skip to content

Instantly share code, notes, and snippets.

@markmarch
Created April 13, 2011 03:17
Show Gist options
  • Save markmarch/916900 to your computer and use it in GitHub Desktop.
Save markmarch/916900 to your computer and use it in GitHub Desktop.
int add_count(unsigned long delta)
{
int c;
int cm;
int old;
int new;
do {
split_counterandmax(&counterandmax, &old, &c, &cm);
if (delta > MAX_COUNTERMAX || c + delta > cm)
goto slowpath;
new = merge_counterandmax(c + delta, cm);
} while (atomic_cmpxchg(&counterandmax, old, new) != old);
return 1;
slowpath:
spin_lock(&gblcnt_mutex);
globalize_count();
if (globalcountmax - globalcount - globalreserve < delta) {
flush_local_count();
if (globalcountmax - globalcount - globalreserve < delta) {
spin_unlock(&gblcnt_mutex);
return 0;
}
}
globalcount += delta;
balance_count();
spin_unlock(&gblcnt_mutex);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment