Project

General

Profile

Actions

Misc #11904

closed

Why was Thread.exclusive deprecated?

Added by bascule (Tony Arcieri) over 8 years ago. Updated about 3 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) over 8 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 8 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 8 years ago

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

Updated by headius (Charles Nutter) over 8 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 8 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 8 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 8 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 8 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 7 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 7 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 7 years ago

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

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

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

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

Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

  • Description updated (diff)

Updated by JiceEmbelmlew (JiceEmbelmlew JiceEmbelmlew) over 3 years ago

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

Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

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

Updated by JamesReots (JamesReots JamesReots) over 3 years ago

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

Updated by naruse (Yui NARUSE) over 3 years ago

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

Updated by HermanLEd (HermanLEd HermanLEd) over 3 years ago

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

Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

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

Updated by Robertjoise (Robertjoise Robertjoise) over 3 years ago

  • File 476.gif added
Actions #22

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (476.gif)

Updated by Grona (Grona Grona) about 3 years ago

  • File 216.gif added

Updated by Grona (Grona Grona) about 3 years ago

  • File 256.gif added

Updated by RobertTougs (RobertTougs RobertTougs) about 3 years ago

  • File 122.gif added
Actions #26

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (216.gif)
Actions #27

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (256.gif)
Actions #28

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (122.gif)

Updated by gendiamVary (gendiamVary gendiamVary) about 3 years ago

  • File 256.gif added
Actions #30

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (256.gif)

Updated by Walterroowl (Walterroowl Walterroowl) about 3 years ago

  • File 359.gif added
Actions #32

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (359.gif)
Actions #33

Updated by Andrewmuh (Andrewmuh Andrewmuh) about 3 years ago

  • File 270.gif added
Actions #34

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (270.gif)

Updated by atrox (atrox atrox) about 3 years ago

  • File 231.gif added

Updated by atrox (atrox atrox) about 3 years ago

  • File 228.gif added
Actions #37

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (231.gif)
Actions #38

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (228.gif)

Updated by Jameshaivy (Jameshaivy Jameshaivy) about 3 years ago

  • File 581.gif added
Actions #40

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (581.gif)

Updated by hen (hen hen) about 3 years ago

  • File 29.gif added
Actions #42

Updated by hsbt (Hiroshi SHIBATA) about 3 years ago

  • File deleted (29.gif)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0