Skip to content

add futex semaphore support derived from hutchinson port #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2016

Conversation

frankeh
Copy link
Contributor

@frankeh frankeh commented Feb 15, 2016

No description provided.

#elif USE_FUTEX_SEM
#include <sys/syscall.h>
#include <linux/futex.h>
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this block is duplicated (249-254)

#define DISPATCH_FUTEX_NUM_SPINS 100
#define DISPATCH_FUTEX_VALUE_MAX INT_MAX
#define DISPATCH_FUTEX_NWAITERS_SHIFT 32
#define DISPATCH_FUTEX_VALUE_MASK ((1ull << DISPATCH_FUTEX_NWAITERS_SHIFT) - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also these last 3 should be next to the structure definition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please still move them next to the definition of dispatch_futex_t

@MadCoder
Copy link
Contributor

ok I know this is a lot of comments, but despite that I love where this is going.

I have not completely checked that the futex() based semaphore implementation is correct, and only made remarks for the barriers (which aren't important on intel but could be for other architectures).

Note that dispatch assumes little endian in a LOT of places so I'm not entirely sure it's useful to test, but why not.

Last, as far as atomic barriers go, for a semaphore, the semantics is that:

  • signal() must have a release barrier
  • wait() must have at least one acquire barrier as late as possible.

This is why there is no need for a barrier at all right before the call to futex() in the wait_slow because the sub2o done on the way out has one and that is enough (of course on Intel it has no impact anyway)

I'm done with the first round of reviews

if (dispatch_atomic_cmpxchg2o(dfx, dfx_data, orig, orig - 1, acquire)) {
return true;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code should be:

    uint64_t orig = dispatch_atomic_load2o(dfx, dfx_data, relaxed);
    while (orig & DISPATCH_FUTEX_VALUE_MASK) {
        if (dispatch_atomic_cmpxchgv2o(dfx, dfx_data, orig, orig - 1, &orig, acquire)) {
            return true;
        }
    }
    return false;

@MadCoder
Copy link
Contributor

sorry I had missed the extra commit, wasn't notified, I start to believe either some notification go to my spam box or github has a problem.

anyway made extra comments but that is more like what I had in mind indeed!

@@ -716,6 +785,10 @@ _dispatch_thread_semaphore_create(void)
int ret = sem_init(s4, 0, 0);
DISPATCH_SEMAPHORE_VERIFY_RET(ret);
return (_dispatch_thread_semaphore_t)s4;
#elif USE_FUTEX_SEM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not reall related to that very code but:

_dispatch_get_thread_semaphore and _dispatch_put_thread_semaphore are meant to amortize the cost of the syscall to create a kernel semaphore.

on linux where a futex is used, caching the malloc() is not useful, as we allocate a continuation which has its own cache.

_dispatch_put_thread_semaphore and _dispatch_get_thread_semaphore should be the same as dispose and create when USE_FUTEX_SEM is in use like this:

#if USE_FUTEX_SEM
#define _dispatch_get_thread_semaphore() _dispatch_thread_semaphore_create()
#define _dispatch_put_thread_semaphore() _dispatch_thread_semaphore_dispose()
#else
DISPATCH_ALWAYS_INLINE
static inline _dispatch_thread_semaphore_t
_dispatch_get_thread_semaphore(void)
{
...
}
#endif

@frankeh
Copy link
Contributor Author

frankeh commented Feb 25, 2016

OK .. in case the commit got lost ..

@MadCoder
Copy link
Contributor

oooh that's it, I don't get commit notifications. doh...

I'm ok with this, can you please squash this in a single commit & force push and then I'll merge

@frankeh
Copy link
Contributor Author

frankeh commented Feb 25, 2016

done .. can you see it ?

@MadCoder
Copy link
Contributor

nope I still see 3 commits. you have to update the pull request one way or the other, I assumed that a force push on your repository should do it naturally but maybe not.

@frankeh
Copy link
Contributor Author

frankeh commented Feb 25, 2016

Damned ..

2023 git checkout master
2024 git merge --squash futexes
2025 git commit -m "complete futex patch for semaphore implementation"
2033 git push -f origin appledispatch/master

what's wrong with this ?

@MadCoder
Copy link
Contributor

2033 git push -f origin appledispatch/master shouldn't that be "futexes" ? your pull request is about "futexes"

@frankeh
Copy link
Contributor Author

frankeh commented Feb 25, 2016

Hmm none of that works .. all tell me its upto date.
Will have to consult somebody else.

MadCoder added a commit that referenced this pull request Feb 25, 2016
add futex semaphore support derived from hutchinson port
@MadCoder MadCoder merged commit a650657 into swiftlang:master Feb 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants