[ANN] fastthread 0.6.2

Discussion in 'Ruby' started by MenTaLguY, Jan 19, 2007.

  1. MenTaLguY

    MenTaLguY Guest

    --=-1/uxTqj3vQ7r47rLW4Hd
    Content-Type: text/plain
    Content-Transfer-Encoding: quoted-printable

    It looks like I got too creative in 0.6.1 and consequently ran afoul of
    a bug in the Ruby interpreter. 0.6.2 works around the bug and should be
    entirely stable at this point.

    Thanks to Young Hyun for his help in coming up with test cases.

    =3D=3D what?

    fastthread is a Ruby library which provides a faster (and
    non-memory-leaking) C implementation of the concurrency primitives from
    stdlib's thread.rb. It aims to be 100% compatible with thread.rb's
    public API.

    So, how much faster? In the single-threaded case, fastthread's version
    of Mutex#lock and Mutex#synchronize are comparable in performance to
    Thread.critical=3D and Thread.exclusive. With multiple threads, it has an
    additional advantage over Thread.critical in that entering a critical
    section doesn't suspend any other threads unless they're competing for
    the same lock. (Compare that to Thread.critical, which stops all other
    threads dead!)

    I know a lot of folks have been avoiding stdlib's Mutex because all the
    method calls killed performance. But no more, with fastthread! Why use
    Thread.critical when you can use the real thing?

    =3D=3D how?

    Simply require 'fastthread' in addition to 'thread'. If you want to
    make fastthread optional (recommended!), do it this way:

    require 'thread'
    begin
    require 'fastthread'
    rescue LoadError
    end

    This way, your program will still work on systems that don't have (or
    don't need -- e.g. JRuby) fastthread, but you still get a performance
    boost on systems where it's available.

    =3D=3D where?

    Gem:
    http://moonbase.rydia.net/software/optimized-locking/fastthread-0.6.2.gem

    Tarball:
    http://moonbase.rydia.net/software/optimized-locking/fastthread-0.6.2.tgz

    fastthread is also available on Rubyforge (and therefore the main gems
    repository), courtesy of the Mongrel project:

    https://rubyforge.org/frs/?group_id=3D1306

    -mental

    --=-1/uxTqj3vQ7r47rLW4Hd
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (GNU/Linux)

    iD8DBQBFsBYLSuZBmZzm14ERApnXAKC6noxT9WIHOfZQR3H09qGu/k/0IQCaAgEe
    wP41XprxuvmKMEsAZWNvvnM=
    =WIgG
    -----END PGP SIGNATURE-----

    --=-1/uxTqj3vQ7r47rLW4Hd--
     
    MenTaLguY, Jan 19, 2007
    #1
    1. Advertising

  2. MenTaLguY

    Tim Pease Guest

    On 1/18/07, MenTaLguY <> wrote:
    > It looks like I got too creative in 0.6.1 and consequently ran afoul of
    > a bug in the Ruby interpreter. 0.6.2 works around the bug and should be
    > entirely stable at this point.
    >
    > Thanks to Young Hyun for his help in coming up with test cases.
    >
    > == what?
    >
    > fastthread is a Ruby library which provides a faster (and
    > non-memory-leaking) C implementation of the concurrency primitives from
    > stdlib's thread.rb. It aims to be 100% compatible with thread.rb's
    > public API.
    >
    > So, how much faster? In the single-threaded case, fastthread's version
    > of Mutex#lock and Mutex#synchronize are comparable in performance to
    > Thread.critical= and Thread.exclusive. With multiple threads, it has an
    > additional advantage over Thread.critical in that entering a critical
    > section doesn't suspend any other threads unless they're competing for
    > the same lock. (Compare that to Thread.critical, which stops all other
    > threads dead!)
    >
    > I know a lot of folks have been avoiding stdlib's Mutex because all the
    > method calls killed performance. But no more, with fastthread! Why use
    > Thread.critical when you can use the real thing?
    >
    > == how?
    >
    > Simply require 'fastthread' in addition to 'thread'. If you want to
    > make fastthread optional (recommended!), do it this way:
    >
    > require 'thread'
    > begin
    > require 'fastthread'
    > rescue LoadError
    > end
    >
    > This way, your program will still work on systems that don't have (or
    > don't need -- e.g. JRuby) fastthread, but you still get a performance
    > boost on systems where it's available.
    >


    Does fastthread allow you to see who has the lock on the mutex? Sync
    allows you to do this, and it allows me to write re-entrant methods
    much more easily.

    Blessings,
    TwP
     
    Tim Pease, Jan 19, 2007
    #2
    1. Advertising

  3. MenTaLguY

    Vlad GALU Guest

    On 1/19/07, MenTaLguY <> wrote:
    > It looks like I got too creative in 0.6.1 and consequently ran afoul of
    > a bug in the Ruby interpreter. 0.6.2 works around the bug and should be
    > entirely stable at this point.
    >
    > Thanks to Young Hyun for his help in coming up with test cases.
    >
    > == what?
    >
    > fastthread is a Ruby library which provides a faster (and
    > non-memory-leaking) C implementation of the concurrency primitives from
    > stdlib's thread.rb. It aims to be 100% compatible with thread.rb's
    > public API.


    Any reasons for not including it in the standard ruby library?

    > So, how much faster? In the single-threaded case, fastthread's version
    > of Mutex#lock and Mutex#synchronize are comparable in performance to
    > Thread.critical= and Thread.exclusive. With multiple threads, it has an
    > additional advantage over Thread.critical in that entering a critical
    > section doesn't suspend any other threads unless they're competing for
    > the same lock. (Compare that to Thread.critical, which stops all other
    > threads dead!)
    >
    > I know a lot of folks have been avoiding stdlib's Mutex because all the
    > method calls killed performance. But no more, with fastthread! Why use
    > Thread.critical when you can use the real thing?
    >
    > == how?
    >
    > Simply require 'fastthread' in addition to 'thread'. If you want to
    > make fastthread optional (recommended!), do it this way:
    >
    > require 'thread'
    > begin
    > require 'fastthread'
    > rescue LoadError
    > end
    >
    > This way, your program will still work on systems that don't have (or
    > don't need -- e.g. JRuby) fastthread, but you still get a performance
    > boost on systems where it's available.
    >
    > == where?
    >
    > Gem:
    > http://moonbase.rydia.net/software/optimized-locking/fastthread-0.6.2.gem
    >
    > Tarball:
    > http://moonbase.rydia.net/software/optimized-locking/fastthread-0.6.2.tgz
    >
    > fastthread is also available on Rubyforge (and therefore the main gems
    > repository), courtesy of the Mongrel project:
    >
    > https://rubyforge.org/frs/?group_id=1306
    >
    > -mental
    >
    >
    >



    --
    If it's there, and you can see it, it's real.
    If it's not there, and you can see it, it's virtual.
    If it's there, and you can't see it, it's transparent.
    If it's not there, and you can't see it, you erased it.
     
    Vlad GALU, Jan 19, 2007
    #3
  4. MenTaLguY

    MenTaLguY Guest

    --=-DkwCmhlCRteRhsiNFf6h
    Content-Type: text/plain
    Content-Transfer-Encoding: quoted-printable

    On Fri, 2007-01-19 at 16:35 +0900, Vlad GALU wrote:
    > > fastthread is a Ruby library which provides a faster (and
    > > non-memory-leaking) C implementation of the concurrency primitives from
    > > stdlib's thread.rb. It aims to be 100% compatible with thread.rb's
    > > public API.

    >=20
    > Any reasons for not including it in the standard ruby library?


    I want to make sure it's sufficiently polished, first. Then I will
    indeed submit it as a patch.

    -mental

    --=-DkwCmhlCRteRhsiNFf6h
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (GNU/Linux)

    iD8DBQBFsPmXSuZBmZzm14ERAh4NAKCqRIzxQx3WXcRtT8JZ1xMJ4XTLSACeKkr6
    dKdKBOptkSZqbrvdPw/zYqk=
    =JKcK
    -----END PGP SIGNATURE-----

    --=-DkwCmhlCRteRhsiNFf6h--
     
    MenTaLguY, Jan 19, 2007
    #4
  5. MenTaLguY

    MenTaLguY Guest

    --=-Ps3/XlrAYAbPOl+U0ZKV
    Content-Type: text/plain
    Content-Transfer-Encoding: quoted-printable

    On Fri, 2007-01-19 at 13:03 +0900, Tim Pease wrote:

    > Does fastthread allow you to see who has the lock on the mutex?


    fastthread is just a re-implementation of the standard API offered by
    Mutex, ConditionVariable, Queue and SizedQueue in thread.rb. That isn't
    an ability offered by stdlib's Mutex.

    > Sync allows you to do this,


    Not safely, actually. Why do you need to do that?

    > and it allows me to write re-entrant methods much more easily.


    Yes, since Sync locks are reentrant, but personally I would avoid Sync
    because it has some bugs, memory leaks, and the implementation isn't
    very nice generally.

    If do you need both a reentrant lock and the ability to see who's
    currently holding it, what I'd suggest instead is writing your own lock
    class along these lines:

    class MyLock
    def initialize
    @lock =3D Mutex.new
    @ready =3D ConditionVariable.new
    @count =3D 0
    end

    def lock
    @lock.synchronize do
    @ready.wait @lock while @owner and @owner !=3D Thread.current
    @owner =3D Thread.current
    @count +=3D 1
    end
    self
    end

    def unlock
    @lock.synchronize do
    return self unless @owner
    @count -=3D 1
    if @count.zero?
    @owner =3D nil
    @ready.signal
    end
    end
    self
    end

    # safely pass the current holder of the lock to a block
    def with_locker
    @lock.synchronize { yield @owner }
    end
    end

    The above code should work fine with or without fastthread. fastthread
    would just make it a little faster (I'd recommend making it an optional
    require, as described in the announcement).

    I'd strongly recommend against writing code which checks the current
    owner of a lock, by the way, but if you really need to do it (I don't
    know your specific requirements), something like MyLock#with_locker is
    probably the safest way to do so.

    -mental

    --=-Ps3/XlrAYAbPOl+U0ZKV
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (GNU/Linux)

    iD8DBQBFsQRzSuZBmZzm14ERAleZAJ91BDiWaMB7vSNAlqGQTX1EMuz+9wCfUQwZ
    4mbffeLrz4egSu1G4FSjbhw=
    =ke22
    -----END PGP SIGNATURE-----

    --=-Ps3/XlrAYAbPOl+U0ZKV--
     
    MenTaLguY, Jan 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MenTaLguY

    [ANN] fastthread 0.4

    MenTaLguY, Nov 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    129
    M. Edward (Ed) Borasky
    Nov 24, 2006
  2. MenTaLguY

    [ANN] fastthread-0.6.3

    MenTaLguY, Jan 26, 2007, in forum: Ruby
    Replies:
    1
    Views:
    99
    MenTaLguY
    Jan 31, 2007
  3. Akinori MUSHA
    Replies:
    0
    Views:
    76
    Akinori MUSHA
    Feb 10, 2007
  4. MenTaLguY

    [ANN] fastthread-0.6.4.1 released

    MenTaLguY, Feb 14, 2007, in forum: Ruby
    Replies:
    0
    Views:
    89
    MenTaLguY
    Feb 14, 2007
  5. MenTaLguY

    [ANN] fastthread 1.0

    MenTaLguY, Mar 20, 2007, in forum: Ruby
    Replies:
    12
    Views:
    264
    Han Holl
    Jun 4, 2007
Loading...

Share This Page