onFdEvent :: EventManager -> Fd -> Event -> IO ()
onFdEvent mgr fd evs =
  if fd == controlReadFd (emControl mgr) || fd == wakeupReadFd (emControl mgr)
  then handleControlEvent mgr fd evs
  else
    if emOneShot mgr
    then
      do fdds <- withMVar (callbackTableVar mgr fd) $ \tbl ->
           IT.delete fd' tbl >>=
           maybe (return []) (selectCallbacks tbl)
         forM_ fdds $ \(FdData reg _ cb) -> cb reg evs
    else
      do found <- IT.lookup fd' =<< readMVar (callbackTableVar mgr fd)
         case found of
           Just cbs -> forM_ cbs $ \(FdData reg ev cb) -> do
             when (evs `I.eventIs` ev) $ cb reg evs
           Nothing  -> return ()
  where
    fd' :: Int
    fd' = fromIntegral fd

    selectCallbacks :: IntTable [FdData] -> [FdData] -> IO [FdData]
    selectCallbacks tbl cbs = aux cbs [] []
      where
        -- nothing to rearm.
        aux [] _    []          =
          if haveOneShot
          then return cbs
          else do _ <- I.modifyFd (emBackend mgr) fd (eventsOf cbs) mempty
                  return cbs

        -- reinsert and rearm; note that we already have the lock on the
        -- callback table for this fd, and we deleted above, so we know there
        -- is no entry in the table for this fd.
        aux [] fdds saved@(_:_) = do
          _ <- if haveOneShot
               then I.modifyFdOnce (emBackend mgr) fd $ eventsOf saved
               else I.modifyFd (emBackend mgr) fd (eventsOf cbs) $ eventsOf saved
          _ <- IT.insertWith (\_ _ -> saved) fd' saved tbl
          return fdds

        -- continue, saving those callbacks that don't match the event
        aux (fdd@(FdData _ evs' _) : cbs') fdds saved
          | evs `I.eventIs` evs' = aux cbs' (fdd:fdds) saved
          | otherwise            = aux cbs' fdds (fdd:saved)
onFdEvent :: EventManager -> Fd -> Event -> IO ()
onFdEvent mgr fd evs =
  if fd == controlReadFd (emControl mgr) || fd == wakeupReadFd (emControl mgr)
  then handleControlEvent mgr fd evs
  else
    if emOneShot mgr
    then
      do fdds <- withMVar (callbackTableVar mgr fd) $ \tbl ->
           IT.delete fd' tbl >>=
           maybe (return []) (selectCallbacks tbl)
         forM_ fdds $ \(FdData reg _ cb) -> cb reg evs
    else
      do found <- IT.lookup fd' =<< readMVar (callbackTableVar mgr fd)
         case found of
           Just cbs -> forM_ cbs $ \(FdData reg ev cb) -> do
             when (evs `I.eventIs` ev) $ cb reg evs
           Nothing  -> return ()
  where
    fd' :: Int
    fd' = fromIntegral fd

    selectCallbacks :: IntTable [FdData] -> [FdData] -> IO [FdData]
    selectCallbacks tbl cbs = aux cbs [] []
      where
        -- nothing to rearm.
        aux [] _    []          =
          if haveOneShot
          then return cbs
          else do _ <- I.modifyFd (emBackend mgr) fd (eventsOf cbs) mempty
                  return cbs

        -- reinsert and rearm; note that we already have the lock on the
        -- callback table for this fd, and we deleted above, so we know there
        -- is no entry in the table for this fd.
        aux [] fdds saved@(_:_) = do
          _ <- if haveOneShot
               then I.modifyFdOnce (emBackend mgr) fd $ eventsOf saved
               else I.modifyFd (emBackend mgr) fd (eventsOf cbs) $ eventsOf saved
          _ <- IT.insertWith (\_ _ -> saved) fd' saved tbl
          return fdds

        -- continue, saving those callbacks that don't match the event
        aux (fdd@(FdData _ evs' _) : cbs') fdds saved
          | evs `I.eventIs` evs' = aux cbs' (fdd:fdds) saved
          | otherwise            = aux cbs' fdds (fdd:saved)
onFdEvent mgr fd evs =
  if fd == controlReadFd (emControl mgr) || fd == wakeupReadFd (emControl mgr)
  then handleControlEvent mgr fd evs
  else
    if emOneShot mgr
    then
      do fdds <- withMVar (callbackTableVar mgr fd) $ \tbl ->
           IT.delete fd' tbl >>=
           maybe (return []) (selectCallbacks tbl)
         forM_ fdds $ \(FdData reg _ cb) -> cb reg evs
    else
      do found <- IT.lookup fd' =<< readMVar (callbackTableVar mgr fd)
         case found of
           Just cbs -> forM_ cbs $ \(FdData reg ev cb) -> do
             when (evs `I.eventIs` ev) $ cb reg evs
           Nothing  -> return ()
  where
    fd' :: Int
    fd' = fromIntegral fd

    selectCallbacks :: IntTable [FdData] -> [FdData] -> IO [FdData]
    selectCallbacks tbl cbs = aux cbs [] []
      where
        -- nothing to rearm.
        aux [] _    []          =
          if haveOneShot
          then return cbs
          else do _ <- I.modifyFd (emBackend mgr) fd (eventsOf cbs) mempty
                  return cbs

        -- reinsert and rearm; note that we already have the lock on the
        -- callback table for this fd, and we deleted above, so we know there
        -- is no entry in the table for this fd.
        aux [] fdds saved@(_:_) = do
          _ <- if haveOneShot
               then I.modifyFdOnce (emBackend mgr) fd $ eventsOf saved
               else I.modifyFd (emBackend mgr) fd (eventsOf cbs) $ eventsOf saved
          _ <- IT.insertWith (\_ _ -> saved) fd' saved tbl
          return fdds

        -- continue, saving those callbacks that don't match the event
        aux (fdd@(FdData _ evs' _) : cbs') fdds saved
          | evs `I.eventIs` evs' = aux cbs' (fdd:fdds) saved
          | otherwise            = aux cbs' fdds (fdd:saved)