{-|
Module      : Control.Concurrent.Bag.TaskBuffer
Description : Task buffers for the IO monad
Copyright   : (c) Bastian Holst, 2014
License     : BSD3
Maintainer  : bastianholst@gmx.de
Stability   : experimental
Portability : POSIX

This module contains the definition of a task buffer in the 'IO' monad,
'TaskBuffer', and the functions to create a 'Stack' and a 'Queue' buffer.
-}
module Control.Concurrent.Bag.TaskBuffer
 ( TaskBuffer (..)
 , BufferType (..)
 , newChanBuffer
 , newStackBuffer )
where

import Control.Concurrent.Chan
import Control.Concurrent.Stack
import Control.Concurrent.Bag.BufferType

-- | A buffer holding tasks.
--
--   For this type, all access functions use the 'IO' monad.
data TaskBuffer a = TaskBuffer {
    writeBuffer :: a -> IO ()
  , readBuffer  :: IO a }

-- | Create a new Queue buffer from a 'Chan'.
newChanBuffer :: IO (TaskBuffer r)
newChanBuffer = do
  c <- newChan
  return $ TaskBuffer (writeChan c) (readChan c)

-- | Create a new Stack buffer from a 'Stack'.
newStackBuffer :: IO (TaskBuffer r)
newStackBuffer = do
  s <- newStack
  return $ TaskBuffer (writeStack s) (readStack s)