Opened 9 years ago

Closed 9 years ago

#4457 closed feature request (fixed)

Way to determine if code is running in thread fixed on capability

Reported by: mitar Owned by: simonmar
Priority: high Milestone: 7.4.1
Component: Compiler Version: 6.12.3
Keywords: Cc: mmitar@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


I would request a way to determine if code is running in thread fixed on capability, that is in thread created by forkOnIO. Furthermore I would like to request a way to get number of this capability. Something like:

wasForkedOnIO :: IO (Maybe Int)

The reason for this is that I would like to know in my code if it is running on fixed capability so that I would also further threads spawn on this same capability.

This is useful for FFI libraries which require that all code runs in the same (OS) thread, while still allowing Haskell threads to work with that library. There will be not much of parallelism boost because of that but code can be much nicer: you can spawn few Haskell threads each doing its own thing while RTS interweave all this together. So code can be more modular while still running on one OS thread. Something like easy coroutines.

An example of this is X11 library. It supports threading but by enabling it performance of whole Haskell program really drops a lot (probably locking it does is not really nice for Haskell itself). By fixing all Haskell threads which work with X11 library to one OS thread you do not need to enable threading in X11 library and performance and responsiveness is great. And an example of when code is nicer for X11 if using threads is when you want to process key events independently from drawing process. So one threads needs fast response to key events, and another threads draws things to the screen. Trying to write this together means coupling key events with drawing code.

So, to be able to write code in modular fashion and without hard-coded capability numbers such primitive mentioned above would be really useful.

Change History (3)

comment:1 Changed 9 years ago by simonmar

Architecture: x86_64 (amd64)Unknown/Multiple
Milestone: 7.2.1
Operating System: LinuxUnknown/Multiple
Owner: set to simonmar
Priority: normalhigh

Should be easy enough, I'll take a look.

comment:2 Changed 9 years ago by simonmar

See #4859

comment:3 Changed 9 years ago by simonmar

Resolution: fixed
Status: newclosed

GHC HEAD has this in GHC.Conc:

-- | returns the number of the capability on which the thread is currently
-- running, and a boolean indicating whether the thread is locked to
-- that capability or not.  A thread is locked to a capability if it
-- was created with @forkOnIO@.
threadCapability :: ThreadId -> IO (Int, Bool)

and as part of the proposal to add forkOnIO and friends, this will be exported via Control.Concurrent too (perhaps with a different name, I haven't decided yet).

Note: See TracTickets for help on using tickets.