Project

General

Profile

Actions

Misc #11904

closed

Why was Thread.exclusive deprecated?

Added by bascule (Tony Arcieri) over 5 years ago. Updated 2 months ago.

Status:
Rejected
Priority:
Normal
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) over 5 years ago

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) over 5 years ago

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) over 5 years ago

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

Updated by headius (Charles Nutter) over 5 years ago

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

Updated by bascule (Tony Arcieri) over 5 years ago

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) over 5 years ago

  • 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) over 5 years ago

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) over 5 years ago

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) over 4 years ago

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) over 4 years ago

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) over 4 years ago

shyouhei@ruby-lang.org 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) 7 months ago

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

Updated by nobu (Nobuyoshi Nakada) 7 months ago

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

Updated by hsbt (Hiroshi SHIBATA) 7 months ago

  • Description updated (diff)

Updated by JiceEmbelmlew (JiceEmbelmlew JiceEmbelmlew) 7 months ago

  • Description updated (diff)
  • Subject changed from Why was Thread.exclusive deprecated? to urcns
  • Project changed from Ruby master to CommonRuby
  • Tracker changed from Misc to Feature
Actions #16

Updated by hsbt (Hiroshi SHIBATA) 6 months ago

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

Updated by JamesReots (JamesReots JamesReots) 6 months ago

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

Updated by naruse (Yui NARUSE) 6 months ago

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

Updated by HermanLEd (HermanLEd HermanLEd) 6 months ago

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

Updated by hsbt (Hiroshi SHIBATA) 6 months ago

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

Updated by Robertjoise (Robertjoise Robertjoise) 5 months ago

  • File 476.gif added
Actions #22

Updated by hsbt (Hiroshi SHIBATA) 5 months ago

  • File deleted (476.gif)

Updated by Grona (Grona Grona) 5 months ago

  • File 216.gif added

Updated by Grona (Grona Grona) 5 months ago

  • File 256.gif added

Updated by RobertTougs (RobertTougs RobertTougs) 5 months ago

  • File 122.gif added
Actions #26

Updated by hsbt (Hiroshi SHIBATA) 5 months ago

  • File deleted (216.gif)
Actions #27

Updated by hsbt (Hiroshi SHIBATA) 5 months ago

  • File deleted (256.gif)
Actions #28

Updated by hsbt (Hiroshi SHIBATA) 5 months ago

  • File deleted (122.gif)

Updated by gendiamVary (gendiamVary gendiamVary) 4 months ago

  • File 256.gif added
Actions #30

Updated by hsbt (Hiroshi SHIBATA) 4 months ago

  • File deleted (256.gif)

Updated by Walterroowl (Walterroowl Walterroowl) 4 months ago

  • File 359.gif added
Actions #32

Updated by hsbt (Hiroshi SHIBATA) 4 months ago

  • File deleted (359.gif)
Actions #33

Updated by Andrewmuh (Andrewmuh Andrewmuh) 4 months ago

  • File 270.gif added
Actions #34

Updated by hsbt (Hiroshi SHIBATA) 4 months ago

  • File deleted (270.gif)

Updated by atrox (atrox atrox) 3 months ago

  • File 231.gif added

Updated by atrox (atrox atrox) 3 months ago

  • File 228.gif added
Actions #37

Updated by hsbt (Hiroshi SHIBATA) 3 months ago

  • File deleted (231.gif)
Actions #38

Updated by hsbt (Hiroshi SHIBATA) 3 months ago

  • File deleted (228.gif)

Updated by Jameshaivy (Jameshaivy Jameshaivy) 3 months ago

  • File 581.gif added
Actions #40

Updated by hsbt (Hiroshi SHIBATA) 3 months ago

  • File deleted (581.gif)

Updated by hen (hen hen) 3 months ago

  • File 29.gif added
Actions #42

Updated by hsbt (Hiroshi SHIBATA) 3 months ago

  • File deleted (29.gif)
Actions

Also available in: Atom PDF