Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use polling as a generic fallback #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions fsnotify.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ library
, unix-compat >=0.2
default-language: Haskell2010
if os(linux)
cpp-options: -DOS_Linux
cpp-options: -DOS_Linux -DHAVE_NATIVE_WATCHER
if os(windows)
cpp-options: -DOS_Win32
cpp-options: -DOS_Win32 -DHAVE_NATIVE_WATCHER
if os(darwin)
cpp-options: -DOS_Mac
if os(freebsd) || os(netbsd) || os(openbsd)
cpp-options: -DOS_BSD
cpp-options: -DOS_Mac -DHAVE_NATIVE_WATCHER
if os(linux)
other-modules:
System.FSNotify.Linux
Expand Down Expand Up @@ -103,13 +101,11 @@ executable example
, unliftio
default-language: Haskell2010
if os(linux)
cpp-options: -DOS_Linux
cpp-options: -DOS_Linux -DHAVE_NATIVE_WATCHER
if os(windows)
cpp-options: -DOS_Win32
cpp-options: -DOS_Win32 -DHAVE_NATIVE_WATCHER
if os(darwin)
cpp-options: -DOS_Mac
if os(freebsd) || os(netbsd) || os(openbsd)
cpp-options: -DOS_BSD
cpp-options: -DOS_Mac -DHAVE_NATIVE_WATCHER

test-suite tests
type: exitcode-stdio-1.0
Expand Down Expand Up @@ -140,13 +136,11 @@ test-suite tests
, unliftio >=0.2.20
default-language: Haskell2010
if os(linux)
cpp-options: -DOS_Linux
cpp-options: -DOS_Linux -DHAVE_NATIVE_WATCHER
if os(windows)
cpp-options: -DOS_Win32
cpp-options: -DOS_Win32 -DHAVE_NATIVE_WATCHER
if os(darwin)
cpp-options: -DOS_Mac
if os(freebsd) || os(netbsd) || os(openbsd)
cpp-options: -DOS_BSD
cpp-options: -DOS_Mac -DHAVE_NATIVE_WATCHER
if os(windows)
build-depends:
Win32
Expand Down
8 changes: 3 additions & 5 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ extra-source-files:

when:
- condition: os(linux)
cpp-options: -DOS_Linux
cpp-options: -DOS_Linux -DHAVE_NATIVE_WATCHER
- condition: os(windows)
cpp-options: -DOS_Win32
cpp-options: -DOS_Win32 -DHAVE_NATIVE_WATCHER
- condition: os(darwin)
cpp-options: -DOS_Mac
- condition: os(freebsd) || os(netbsd) || os(openbsd)
cpp-options: -DOS_BSD
cpp-options: -DOS_Mac -DHAVE_NATIVE_WATCHER

default-extensions:
- ScopedTypeVariables
Expand Down
8 changes: 4 additions & 4 deletions src/System/FSNotify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ data WatchManager = forall manager argType. FileListener manager argType =>

-- | Default configuration
--
-- * Uses OS watch mode and single thread.
-- * Uses OS watch mode (if possible) and single thread.
defaultConfig :: WatchConfig
defaultConfig = WatchConfig {
#ifdef OS_BSD
#ifndef HAVE_NATIVE_WATCHER
confWatchMode = WatchModePoll 500000
#else
confWatchMode = WatchModeOS
Expand Down Expand Up @@ -163,12 +163,12 @@ startManagerConf conf = do

case confWatchMode conf of
WatchModePoll interval -> WatchManager conf <$> liftIO (createPollManager interval) <*> globalWatchChan
#ifndef OS_BSD
#ifdef HAVE_NATIVE_WATCHER
WatchModeOS -> liftIO (initSession ()) >>= createManager
#endif

where
#ifndef OS_BSD
#ifdef HAVE_NATIVE_WATCHER
createManager :: Either T.Text NativeManager -> IO WatchManager
createManager (Right nativeManager) = WatchManager conf nativeManager <$> globalWatchChan
createManager (Left err) = throwIO $ userError $ T.unpack $ "Error: couldn't start native file manager: " <> err
Expand Down
4 changes: 2 additions & 2 deletions src/System/FSNotify/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ data WatchMode =
}
-- ^ Detect changes by polling the filesystem. Less efficient and may miss fast changes. Not recommended
-- unless you're experiencing problems with 'WatchModeOS' (or 'WatchModeOS' is not supported on your platform).
#ifndef OS_BSD
#ifdef HAVE_NATIVE_WATCHER
| WatchModeOS
-- ^ Use OS-specific mechanisms to be notified of changes (inotify on Linux, FSEvents on OSX, etc.).
-- Not currently available on *BSD.
-- Not currently available on e.g. *BSD and WASI.
#endif

data ThreadingMode =
Expand Down
2 changes: 1 addition & 1 deletion test/FSNotify/Test/EventTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ eventTests :: (
MonadUnliftIO m, MonadThrow m
) => ThreadingMode -> SpecFree context m ()
eventTests threadingMode = describe "Tests" $ parallelWithoutDirectory $ do
let pollOptions = if isBSD then [True] else [False, True]
let pollOptions = if haveNativeWatcher then [False, True] else [True]

forM_ pollOptions $ \poll -> describe (if poll then "Polling" else "Native") $ parallelWithoutDirectory $ do
forM_ [False, True] $ \recursive -> describe (if recursive then "Recursive" else "Non-recursive") $ parallelWithoutDirectory $
Expand Down
10 changes: 5 additions & 5 deletions test/FSNotify/Test/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ isLinux = True
isLinux = False
#endif

isBSD :: Bool
#ifdef OS_BSD
isBSD = True
haveNativeWatcher :: Bool
#ifdef HAVE_NATIVE_WATCHER
haveNativeWatcher = True
#else
isBSD = False
haveNativeWatcher = False
#endif

waitUntil :: MonadUnliftIO m => Double -> m a -> m a
Expand Down Expand Up @@ -132,7 +132,7 @@ withTestFolder threadingMode poll recursive nested setup action = do
when (isMac || poll) $ threadDelay (max 1000000 pollInterval)

let conf = defaultConfig {
#ifdef OS_BSD
#ifndef HAVE_NATIVE_WATCHER
confWatchMode = if poll then WatchModePoll pollInterval else error "No native watcher available."
#else
confWatchMode = if poll then WatchModePoll pollInterval else WatchModeOS
Expand Down
Loading