fix scheduling
Handle kevent interruptions in the haskell code, so it can yield to other threads
This commit is contained in:
parent
a5cceb7d4f
commit
03b9341356
2 changed files with 16 additions and 15 deletions
|
@ -24,11 +24,13 @@ import Utility.Types.DirWatcher
|
||||||
|
|
||||||
import System.Posix.Types
|
import System.Posix.Types
|
||||||
import Foreign.C.Types
|
import Foreign.C.Types
|
||||||
|
import Foreign.C.Error
|
||||||
import Foreign.Ptr
|
import Foreign.Ptr
|
||||||
import Foreign.Marshal
|
import Foreign.Marshal
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import qualified Data.Set as S
|
import qualified Data.Set as S
|
||||||
import qualified System.Posix.Files as Files
|
import qualified System.Posix.Files as Files
|
||||||
|
import Control.Concurrent
|
||||||
|
|
||||||
data Change
|
data Change
|
||||||
= Deleted FilePath
|
= Deleted FilePath
|
||||||
|
@ -146,9 +148,14 @@ stopKqueue (Kqueue h _ _) = closeFd h
|
||||||
waitChange :: Kqueue -> IO (Kqueue, [Change])
|
waitChange :: Kqueue -> IO (Kqueue, [Change])
|
||||||
waitChange kq@(Kqueue h dirmap _) = do
|
waitChange kq@(Kqueue h dirmap _) = do
|
||||||
changedfd <- c_waitchange_kqueue h
|
changedfd <- c_waitchange_kqueue h
|
||||||
case M.lookup changedfd dirmap of
|
if changedfd == -1
|
||||||
Nothing -> return (kq, [])
|
then ifM ((==) eINTR <$> getErrno)
|
||||||
Just info -> handleChange kq changedfd info
|
(yield >> waitChange kq, nochange)
|
||||||
|
else case M.lookup changedfd dirmap of
|
||||||
|
Nothing -> nochange
|
||||||
|
Just info -> handleChange kq changedfd info
|
||||||
|
where
|
||||||
|
nochange = return (kq, [])
|
||||||
|
|
||||||
{- The kqueue interface does not tell what type of change took place in
|
{- The kqueue interface does not tell what type of change took place in
|
||||||
- the directory; it could be an added file, a deleted file, a renamed
|
- the directory; it could be an added file, a deleted file, a renamed
|
||||||
|
@ -212,9 +219,9 @@ runHooks kq hooks = do
|
||||||
| Files.isSymbolicLink s = callhook addSymlinkHook (Just s) change
|
| Files.isSymbolicLink s = callhook addSymlinkHook (Just s) change
|
||||||
| Files.isDirectory s = print $ "TODO: recursive directory add: " ++ show change
|
| Files.isDirectory s = print $ "TODO: recursive directory add: " ++ show change
|
||||||
| Files.isRegularFile s = callhook addHook (Just s) change
|
| Files.isRegularFile s = callhook addHook (Just s) change
|
||||||
| otherwise = noop
|
| otherwise = print "not a file??"
|
||||||
callhook h s change = case h hooks of
|
callhook h s change = case h hooks of
|
||||||
Nothing -> noop
|
Nothing -> print "missing hook??"
|
||||||
Just a -> a (changedFile change) s
|
Just a -> a (changedFile change) s
|
||||||
withstatus change a = maybe noop (a change) =<<
|
withstatus change a = maybe noop (a change) =<<
|
||||||
(catchMaybeIO (getSymbolicLinkStatus (changedFile change)))
|
(catchMaybeIO (getSymbolicLinkStatus (changedFile change)))
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
/* The specified fds are added to the set of fds being watched for changes.
|
/* The specified fds are added to the set of fds being watched for changes.
|
||||||
* Fds passed to prior calls still take effect, so it's most efficient to
|
* Fds passed to prior calls still take effect, so it's most efficient to
|
||||||
* not pass the same fds repeatedly.
|
* not pass the same fds repeatedly.
|
||||||
|
*
|
||||||
|
* Returns the fd that changed, or -1 on error.
|
||||||
*/
|
*/
|
||||||
signed int helper(const int kq, const int fdcnt, const int *fdlist, int nodelay) {
|
signed int helper(const int kq, const int fdcnt, const int *fdlist, int nodelay) {
|
||||||
int i, nev;
|
int i, nev;
|
||||||
|
@ -32,12 +34,7 @@ signed int helper(const int kq, const int fdcnt, const int *fdlist, int nodelay)
|
||||||
0, 0);
|
0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((nev = kevent(kq, chlist, fdcnt, evlist, 1, timeout))) {
|
nev = kevent(kq, chlist, fdcnt, evlist, 1, timeout);
|
||||||
if (!(nev == -1 && errno == EINTR)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nev == 1)
|
if (nev == 1)
|
||||||
return evlist[0].ident;
|
return evlist[0].ident;
|
||||||
else
|
else
|
||||||
|
@ -59,10 +56,7 @@ void addfds_kqueue(const int kq, const int fdcnt, const int *fdlist) {
|
||||||
helper(kq, fdcnt, fdlist, 1);
|
helper(kq, fdcnt, fdlist, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Waits for a change event on a kqueue.
|
/* Waits for a change event on a kqueue. */
|
||||||
*
|
|
||||||
* Returns the fd that changed, or -1 on error.
|
|
||||||
*/
|
|
||||||
signed int waitchange_kqueue(const int kq) {
|
signed int waitchange_kqueue(const int kq) {
|
||||||
return helper(kq, 0, NULL, 0);
|
return helper(kq, 0, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue