Skip to content

Should thread IDs be stored weakly? #141

@treeowl

Description

@treeowl

GHC.Conc.Sync currently says

-- ToDo: data ThreadId = ThreadId (Weak ThreadId#)
-- But since ThreadId# is unlifted, the Weak type must use open
-- type variables.

That seems unlikely to happen, especially now that we have threadStatus (which needs to be able to determine how a thread died).

However, the condition that comment waits for has arrived—we can now have values of type Weak# ThreadId#, which are just slightly more efficient than the Weak# ThreadId values we could have before. No, I don't know why that was considered important. Anyway, the question arises as to whether Asyncs should hold their ThreadId#s weakly. That would mean doing something like

data WeakThreadId = WeakThreadId
  { tid_number :: !Word64
  , tid_weak :: Weak# ThreadId#
  }

The thread ID number is needed to support compareAsyncs and the Ord (Async a) instance.

Making this change would necessitate an API change: the asyncThreadId function (defined as a field accessor) would have to be removed, in favor of something like

asyncThreadIdMaybe :: Async a -> IO (Maybe ThreadId)

One big question in my mind: garbage collecting Weak#s can be pretty expensive. Is releasing threads earlier worth that cost?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions