{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UnliftedFFITypes #-}

module Posix.Poll
  ( uninterruptiblePoll
  , uninterruptiblePollMutablePrimArray
  , PollFd (..)
  , Exchange (..)
  , PT.input
  , PT.output
  , PT.error
  , PT.hangup
  , PT.invalid
  , PT.isSubeventOf
  ) where

import Data.Primitive (MutablePrimArray (..))
import Foreign.C.Error (Errno, getErrno)
import Foreign.C.Types (CInt (..))
import GHC.Exts (MutableByteArray#, RealWorld)
import GHC.Ptr (Ptr)
import Posix.Poll.Types (Exchange (..), PollFd (..))
import Posix.Types (CNfds (..))

import qualified Posix.Poll.Types as PT

foreign import ccall unsafe "poll.h poll"
  c_poll_ptr :: Ptr PollFd -> CNfds -> CInt -> IO CInt

foreign import ccall unsafe "poll.h poll"
  c_poll_prim_array :: MutableByteArray# RealWorld -> CNfds -> CInt -> IO CInt

{- | The @timeout@ argument is omitted since it is nonsense to choose
  anything other than 0 when using the unsafe FFI.
-}
uninterruptiblePoll ::
  Ptr PollFd ->
  CNfds ->
  IO (Either Errno CInt)
uninterruptiblePoll :: Ptr PollFd -> CNfds -> IO (Either Errno CInt)
uninterruptiblePoll Ptr PollFd
pfds CNfds
n =
  Ptr PollFd -> CNfds -> CInt -> IO CInt
c_poll_ptr Ptr PollFd
pfds CNfds
n CInt
0 IO CInt
-> (CInt -> IO (Either Errno CInt)) -> IO (Either Errno CInt)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO (Either Errno CInt)
errorsFromInt

uninterruptiblePollMutablePrimArray ::
  MutablePrimArray RealWorld PollFd ->
  CNfds ->
  IO (Either Errno CInt)
uninterruptiblePollMutablePrimArray :: MutablePrimArray RealWorld PollFd
-> CNfds -> IO (Either Errno CInt)
uninterruptiblePollMutablePrimArray (MutablePrimArray MutableByteArray# RealWorld
pfds) CNfds
n =
  MutableByteArray# RealWorld -> CNfds -> CInt -> IO CInt
c_poll_prim_array MutableByteArray# RealWorld
pfds CNfds
n CInt
0 IO CInt
-> (CInt -> IO (Either Errno CInt)) -> IO (Either Errno CInt)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO (Either Errno CInt)
errorsFromInt

errorsFromInt :: CInt -> IO (Either Errno CInt)
errorsFromInt :: CInt -> IO (Either Errno CInt)
errorsFromInt CInt
r =
  if CInt
r CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
>= CInt
0
    then Either Errno CInt -> IO (Either Errno CInt)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CInt -> Either Errno CInt
forall a b. b -> Either a b
Right CInt
r)
    else (Errno -> Either Errno CInt) -> IO Errno -> IO (Either Errno CInt)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Errno -> Either Errno CInt
forall a b. a -> Either a b
Left IO Errno
getErrno