Project

General

Profile

Actions

Misc #11904

closed

Why was Thread.exclusive deprecated?

Misc #11904: Why was Thread.exclusive deprecated?

Added by bascule (Tony Arcieri) almost 10 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
[ruby-core:72543]

Description

initialize a mutex because the mutex must be initialized in a thread-safe context where it's not possible for multiple caller threads to initialize the mutex concurrently.

One use case is here: this is an idempotent native function invoked via FFI. The contract is that it can be called repeatedly, but only by one thread at a time (concurrent calls from multiple threads can potentially corrupt its internal state):

https://github.com/cryptosphere/rbnacl/blob/master/lib/rbnacl.rb#L88

Thread.exclusive is useful because it provides an implicit mutex you can ensure is initialized correctly before any other threads start.

Updated by Eregon (Benoit Daloze) almost 10 years ago Actions #1 [ruby-core:72560]

Why not create your own Mutex and store it in a constant at load time?
Thread.exclusive could cause accidental contention because it is obviously global for all uses.

Updated by Eregon (Benoit Daloze) almost 10 years ago Actions #2 [ruby-core:72562]

Also in this particular case, it seems relying on require being thread-safe would be good enough for ensuring a single initialization.

Updated by bascule (Tony Arcieri) almost 10 years ago Actions #3 [ruby-core:72568]

Is a single-threaded require actually a guarantee of every Ruby VM?

Updated by headius (Charles Nutter) almost 10 years ago Actions #4 [ruby-core:72573]

I agree with Tony that Thread.exclusive still has uses. Was there some discussion on deprecating it?

Updated by bascule (Tony Arcieri) almost 10 years ago Actions #5 [ruby-core:72601]

I received a PR to my project to "add Ruby 2.3 support" (via an inline Mutex) and I think it illustrates the problem:

https://github.com/cryptosphere/rbnacl/pull/127

This doesn't really help, and I guess to make the warning message go away I need to just remove Thread.exclusive

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago Actions #6 [ruby-core:72604]

  • Status changed from Open to Rejected

This use of Thread.exclusive seems nonsense.
https://github.com/cryptosphere/rbnacl/commit/25444979d161fa0f02a61a5003768c2173cfa2ee

While Thread.exclusive stops all other threads in 1.8 and earlier, it just blocks other threads which also calling it.
If no other threads calls it, it is useless.

Updated by Eregon (Benoit Daloze) almost 10 years ago Actions #7 [ruby-core:72619]

Tony Arcieri wrote:

Is a single-threaded require actually a guarantee of every Ruby VM?

Not single-threaded, but each file should be loaded at most once.
Single require per file/path is certainly highly desirable,
otherwise you may load the same file twice.
It's also a lot easier to make it thread-safe than autoload IMHO.

Updated by headius (Charles Nutter) almost 10 years ago Actions #8 [ruby-core:72722]

Nobuyoshi Nakada wrote:

This use of Thread.exclusive seems nonsense.
https://github.com/cryptosphere/rbnacl/commit/25444979d161fa0f02a61a5003768c2173cfa2ee

While Thread.exclusive stops all other threads in 1.8 and earlier, it just blocks other threads which also calling it.
If no other threads calls it, it is useless.

That's the point; no two threads can call init via this code at the same time.

Of course, require's guarantee of "only once" would do the same thing here.

Updated by jrochkind (jonathan rochkind) almost 9 years ago Actions #9 [ruby-core:78261]

I came here cause I was curious why Thread.exclusive was deprecated, and didn't find an answer!

But for anyone else curious about the answer to Tony's original question (Tony has probably long since figured it out), I think you can create the Mutex at 'load time', to make sure you only have one? I think you can safely replace all uses of Thread.exclusive with something like this:

class RbNaCl::NaCl
    @init_mutex = Mutex.new
    class << self
       def init_mutex
          @init_mutex
       end
     end

    # all the rest of the definition....

end

# no more: Thread.exclusive { RbNaCl::NaCl.sodium_init }

RbNaCl::NaCL.init_mutex.synchronize { RbNaCl::NaCl.sodium_init }

# Or of course you could include the mutex.synchronize inside the `sodium_init` implementation,
# which would probably make more sense. 

Updated by shyouhei (Shyouhei Urabe) almost 9 years ago Actions #10 [ruby-core:78298]

I feel that Thread.exclusive is too big a primitive to merely initialize a mutex at a process startup. We could perhaps introduce pthread_once_t analogous more fine-grained light-weight control structure that does the job.

P.S. we have /#{@mutex=Mutex.new}/o, so we already have such thing, to some extent at least.

Updated by normalperson (Eric Wong) almost 9 years ago Actions #11 [ruby-core:78341]

wrote:

P.S. we have /#{@mutex=Mutex.new}/o, so we already have such thing, to some extent at least.

Awesome, ROFL; you almost made me choke on my food!

Yes, //o is awesome, but this really takes the cake.

Carry on :)

Updated by DorisWooky (DorisWooky DorisWooky) almost 5 years ago Actions #12 [ruby-core:101114]

  • Tracker changed from Misc to Feature
  • Project changed from Ruby to 14
  • Subject changed from Why was Thread.exclusive deprecated? to Gain Position cuddy
  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago Actions #13

  • Tracker changed from Feature to Misc
  • Project changed from 14 to Ruby
  • Subject changed from Gain Position cuddy to Why was Thread.exclusive deprecated?

Updated by hsbt (Hiroshi SHIBATA) almost 5 years ago Actions #14

  • Description updated (diff)

Updated by JiceEmbelmlew (JiceEmbelmlew JiceEmbelmlew) almost 5 years ago Actions #15 [ruby-core:101136]

  • Description updated (diff)
  • Tracker changed from Misc to Feature
  • Project changed from Ruby to 14
  • Subject changed from Why was Thread.exclusive deprecated? to urcns

Updated by hsbt (Hiroshi SHIBATA) almost 5 years ago Actions #16

  • Tracker changed from Feature to Misc
  • Project changed from 14 to Ruby
  • Subject changed from urcns to Why was Thread.exclusive deprecated?
  • Description updated (diff)

Updated by JamesReots (JamesReots JamesReots) almost 5 years ago Actions #17 [ruby-core:101322]

  • Tracker changed from Misc to Feature
  • Project changed from Ruby to 14
  • Subject changed from Why was Thread.exclusive deprecated? to Proven traffic exchange traftop
  • Description updated (diff)

Updated by naruse (Yui NARUSE) almost 5 years ago Actions #18

  • Tracker changed from Feature to Misc
  • Project changed from 14 to Ruby
  • Subject changed from Proven traffic exchange traftop to hy was Thread.exclusive deprecated?
  • Description updated (diff)

Updated by HermanLEd (HermanLEd HermanLEd) almost 5 years ago Actions #19 [ruby-core:101420]

  • Tracker changed from Misc to Feature
  • Project changed from Ruby to 14
  • Subject changed from hy was Thread.exclusive deprecated? to opg-vostorg
  • Description updated (diff)

Updated by hsbt (Hiroshi SHIBATA) almost 5 years ago Actions #20

  • Tracker changed from Feature to Misc
  • Project changed from 14 to Ruby
  • Subject changed from opg-vostorg to Why was Thread.exclusive deprecated?
  • Description updated (diff)

Updated by Robertjoise (Robertjoise Robertjoise) almost 5 years ago Actions #21 [ruby-core:101947]

  • File 476.gif added

Updated by hsbt (Hiroshi SHIBATA) almost 5 years ago Actions #22

  • File deleted (476.gif)

Updated by Grona (Grona Grona) over 4 years ago Actions #23 [ruby-core:102171]

  • File 216.gif added

Updated by Grona (Grona Grona) over 4 years ago Actions #24 [ruby-core:102180]

  • File 256.gif added

Updated by RobertTougs (RobertTougs RobertTougs) over 4 years ago Actions #25 [ruby-core:102220]

  • File 122.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #26

  • File deleted (216.gif)

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #27

  • File deleted (256.gif)

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #28

  • File deleted (122.gif)

Updated by gendiamVary (gendiamVary gendiamVary) over 4 years ago Actions #29 [ruby-core:102450]

  • File 256.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #30

  • File deleted (256.gif)

Updated by Walterroowl (Walterroowl Walterroowl) over 4 years ago Actions #31 [ruby-core:102480]

  • File 359.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #32

  • File deleted (359.gif)

Updated by Andrewmuh (Andrewmuh Andrewmuh) over 4 years ago Actions #33

  • File 270.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #34

  • File deleted (270.gif)

Updated by atrox (atrox atrox) over 4 years ago Actions #35 [ruby-core:102717]

  • File 231.gif added

Updated by atrox (atrox atrox) over 4 years ago Actions #36 [ruby-core:102724]

  • File 228.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #37

  • File deleted (231.gif)

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #38

  • File deleted (228.gif)

Updated by Jameshaivy (Jameshaivy Jameshaivy) over 4 years ago Actions #39 [ruby-core:102739]

  • File 581.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #40

  • File deleted (581.gif)

Updated by hen (hen hen) over 4 years ago Actions #41 [ruby-core:102749]

  • File 29.gif added

Updated by hsbt (Hiroshi SHIBATA) over 4 years ago Actions #42

  • File deleted (29.gif)
Actions

Also available in: PDF Atom