{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Massiv.Array.Mutable.Atomic
(
atomicReadIntArray
, atomicWriteIntArray
, atomicModifyIntArray
, atomicAddIntArray
, atomicSubIntArray
, atomicAndIntArray
, atomicNandIntArray
, atomicOrIntArray
, atomicXorIntArray
, casIntArray
) where
import Control.Monad.Primitive
import Data.Massiv.Array.Manifest.Primitive
import Data.Massiv.Core.Common
atomicReadIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> m (Maybe Int)
atomicReadIntArray marr ix
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicReadIntArray marr ix
| otherwise = pure Nothing
{-# INLINE atomicReadIntArray #-}
atomicWriteIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m Bool
atomicWriteIntArray marr ix f
| isSafeIndex (msize marr) ix = unsafeAtomicWriteIntArray marr ix f >> pure True
| otherwise = pure False
{-# INLINE atomicWriteIntArray #-}
casIntArray ::
(Index ix, PrimMonad m)
=> MArray (PrimState m) P ix Int
-> ix
-> Int
-> Int
-> m (Maybe Int)
casIntArray marr ix e n
| isSafeIndex (msize marr) ix = Just <$> unsafeCasIntArray marr ix e n
| otherwise = pure Nothing
{-# INLINE casIntArray #-}
atomicModifyIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> (Int -> Int) -> m (Maybe Int)
atomicModifyIntArray marr ix f
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicModifyIntArray marr ix f
| otherwise = pure Nothing
{-# INLINE atomicModifyIntArray #-}
atomicAddIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAddIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicAddIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicAddIntArray #-}
atomicSubIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicSubIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicSubIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicSubIntArray #-}
atomicAndIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAndIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicAndIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicAndIntArray #-}
atomicNandIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicNandIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicNandIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicNandIntArray #-}
atomicOrIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicOrIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicOrIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicOrIntArray #-}
atomicXorIntArray ::
(Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicXorIntArray marr ix e
| isSafeIndex (msize marr) ix = Just <$> unsafeAtomicXorIntArray marr ix e
| otherwise = pure Nothing
{-# INLINE atomicXorIntArray #-}