Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^6: OT: Locking and syncing mechanisms available on *nix.

by BrowserUk (Patriarch)
on Mar 27, 2011 at 15:10 UTC ( [id://895758]=note: print w/replies, xml ) Need Help??


in reply to Re^5: OT: Locking and syncing mechanisms available on *nix.
in thread OT: Locking and syncing mechanisms available on *nix.

Sorry, I guess I should have mentioned I'm operating in XS with no access to threads::shared. Indeed, I'm specifically trying to replace it.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^6: OT: Locking and syncing mechanisms available on *nix.

Replies are listed 'Best First'.
Re^7: OT: Locking and syncing mechanisms available on *nix.
by educated_foo (Vicar) on Mar 27, 2011 at 15:25 UTC
    The idea's basically the same, though it's of course uglier using libpthread in C. You have to have both a flag to keep track of readiness, and a condition variable to queue up waiting threads. Here's a rough sketch:
    #include <pthread.h> pthread_mutex_t lock; pthread_cond_t cond; int ready; void consumer() { pthread_mutex_lock(&lock); while (!ready) pthread_cond_wait(&cond, &lock); /* lock is locked, ready is true */ ready = 0; pthread_mutex_unlock(&lock); } void producer() { /* lock is unlocked */ pthread_mutex_lock(&lock); ready = 1; pthread_cond_signal(&cond, &lock); /* lock is still locked */ pthread_mutex_unlock(&lock); }
    libpthread is POSIX, so it should be available anywhere.

      Thanks, but the trouble with that is we now need 3 elements. The flag, the cond_var and the mutex.

      And the only 'reason' for using the mutex appears to be to serialise access to the flag. But mutexes always require a user/kernel/user space transition which is horribly expensive, when the same job can be done on modern processors using memory barriers and atomic instructions.

      For example, the ready flag state changes can be safely done entirely in user space using gcc's Atomic builtins.

      But that still doesn't allow us to ditch the mutex, because the only signalled wait construct is the cond_wait, which requires a bloody mutex.

      Many of the issues, and solution to them, are discussed in Mutex .v. futex. What I've yet been unable to find is the documentation (and discussion of availablity) of futexes?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        It looks like there are futexes down there in the hairball that is glibc. AFAICT either they or atomic test-and-set are used to implement pthread mutexes, rather than an OS call (but I just glanced at the code, so don't take my word for it).

        If thread-switching performance is really critical, and pthreads are too slow, would some form of cooperative threading work?

Re^7: OT: Locking and syncing mechanisms available on *nix.
by ikegami (Patriarch) on Mar 27, 2011 at 17:31 UTC
    hum, XS has access to the entire Perl interpreter. That can't be true.
      XS has access to the entire Perl interpreter. That can't be true.

      Yes. But what is the point in coding in XS if you have effectively callback into Perl (even if you only simulate doing so by mocking up the perl calling conventions), for a fundamental part of the process?

      And, what would be the point in using the very thing you are trying to replace, to implement it's replacement?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I figured that's what you were doing, but I got doubts when I read your first sentence. I seem to not have made it to your second sentence. Nevermind.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://895758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-18 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found