Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

apply changes from #4 to utils.h#12

Merged
vmg merged 4 commits into
github:masterfrom
haneefmubarak:patch-2
Jun 18, 2015
Merged

apply changes from #4 to utils.h#12
vmg merged 4 commits into
github:masterfrom
haneefmubarak:patch-2

Conversation

@haneefmubarak

Copy link
Copy Markdown
Contributor

and I even signed the CLA this time!

@haneefmubarak

Copy link
Copy Markdown
Contributor Author

Good point. I think I'll have the thread yield in case of contention then. Also, while we're at it, you should use -pthread instead of -lpthread for compiler options.

If a platform does not support bus yielding, yield the threads' cpu timeslice early.
@haneefmubarak

Copy link
Copy Markdown
Contributor Author

Okay so on any platform where explicit bus yielding support is unavailable, we call pthread_yield(), which yields the remainder of the thread's timeslice to the scheduler, who may then switch to another thread or resume the calling thread. Either way, this should hopefully be long enough for contention to clear (and it's non-deterministic).

The only minus to this is that in cases of contention this is effectively going to be at least two context-switches long, if not longer.


One possible solution might be to just switch to using pthread locks entirely, especially considering that on normal, reasonably updated linux systems, glibc (the default libc) is already pretty ingenious with locks:

Simplified Pseudocode:

some_lock () {

    ...

    int x = 0;
    while (CANNOT_LOCK) {
        if (x < a_few_times) {
            bus_yield ();
            spin ();
        } else {
            yield_to_kernel ();
        }
    }

    set (lock);
    return;
}

This makes it decently fast for most purposes and allows the kernel to time switch some threads if a lock is held for a while so that other threads can make progress.

@haneefmubarak

Copy link
Copy Markdown
Contributor Author

@vmg anyways, that's more of a long term thing that needs benchmarking and such; for right now, I think it's ready to merge.

vmg pushed a commit that referenced this pull request Jun 18, 2015
apply changes from #4 to utils.h
@vmg vmg merged commit 07ace16 into github:master Jun 18, 2015
@vmg

vmg commented Jun 18, 2015

Copy link
Copy Markdown
Contributor

Thanks for the PR. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants