Skip to content

Extend TVar/MVar to N capacity / Add primitive channel type

The current concurrent primitives TVar and MVar have a fixed single capacity for holding the value to be synchronized. This pose great limitations on implementing many higher level concurrent structures efficiently, e.g. bound channel, semaphore, resource pool, etc. For example current semaphore's implementation is not ideal due to the extensive usage of multiple MVar s. If we have a good BoundedChan a type, semaphores can be easily implemented with BoundedChan ().

Currently we have various bound channel implementations, but these implementations are often not very efficient or too complex. For example the BoundedChan from BoundedChan is based on an array of MVar s, thus consumes much more memory than a primitve channel which only have to record parked StgTSO s.

I'm thinking adding something like:

typedef struct {
  StgHeader                  header;
  struct StgMVarTSOQueue_   *head;
  struct StgMVarTSOQueue_   *tail;
  StgInt                     capacity;       // the channel's capacity
  StgInt                     read_index;     // the reading end's index
  StgInt                     write_index;    // the writing end's index
  StgClosure                *payload[];      // payload array in continuous memory
} StgMChan;

I still can't work out all the details, but I'm confident something similar to this will work.

Edited by winter
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information