Ticket #4001 (new task)

Opened 3 years ago

Last modified 2 months ago

Implement an atomic readMVar

Reported by: simonmar Owned by:
Priority: low Milestone: 7.6.2
Component: Runtime System Version: 6.12.2
Keywords: Cc: ezyang@…, leon.p.smith@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Moderate (less than a day)
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Requested by various people, most recently by John Launchbury.

The idea would be that readMVar is atomic, and never blocks if the MVar is full. A concurrent putMVar operation cannot intervene while readMVar is reading the value.

To implement this, we would need two kinds of blocked threads in the MVar queue, and putMVar would need to wake up zero or more readMVar operations followed by at most one takeMVar.

Change History

Changed 3 years ago by igloo

  • milestone changed from 6.14 branch to 6.14.1

Changed 3 years ago by ezyang

  • cc ezyang@… added

Changed 2 years ago by igloo

  • milestone changed from 7.0.1 to 7.0.2

Changed 2 years ago by igloo

  • milestone changed from 7.0.2 to 7.2.1

Changed 20 months ago by igloo

  • milestone changed from 7.2.1 to 7.4.1

Changed 15 months ago by igloo

  • priority changed from normal to low
  • milestone changed from 7.4.1 to 7.6.1

Changed 8 months ago by igloo

  • milestone changed from 7.6.1 to 7.6.2

Changed 2 months ago by lpsmith

  • cc leon.p.smith@… added

Changed 2 months ago by lpsmith

There is another possible use case here; if GHC implemented both an atomic readMVar and tryReadMVar, then Control.Concurrent.Chan.isEmptyChan could be implemented in a thread-safe way.

Admittedly, this would require the receiving end of a Chan a to use an external semaphore by changing the MVar (Stream a) to a (IORef (Stream a), MVar ()) pair, so that the current read hole could be obtained without blocking. But the problem is that without atomic readMVars, there is a chance that isEmptyChan would observe the stream when another thread is in the process of reading from the thread, and thus say that the channel is empty when in fact it is not. Similarly, without an atomic tryReadMVar, there is the chance that isEmptyChan would observe the stream when another thread is in the process of calling isEmptyChan, and get a faulty empty result.

Note: See TracTickets for help on using tickets.