Replaces FAM file monitoring with inotify on Linux, based on patch by noKid. Fixes #1316, refs #1687.

Removes FAM dependency.

This was SVN commit r12726.
This commit is contained in:
historic_bruno 2012-10-02 00:03:14 +00:00
parent 222256bd59
commit 5cff74f4e7
3 changed files with 99 additions and 112 deletions

View File

@ -5,7 +5,6 @@ newoption { trigger = "coverage", description = "Enable code coverage data colle
newoption { trigger = "gles", description = "Use non-working OpenGL ES 2.0 mode" }
newoption { trigger = "icc", description = "Use Intel C++ Compiler (Linux only; should use either \"--cc icc\" or --without-pch too, and then set CXX=icpc before calling make)" }
newoption { trigger = "outpath", description = "Location for generated project files" }
newoption { trigger = "without-fam", description = "Disable use of FAM API on Linux" }
newoption { trigger = "without-audio", description = "Disable use of OpenAL/Ogg/Vorbis APIs" }
newoption { trigger = "minimal-flags", description = "Only set compiler/linker flags that are really needed. Has no effect on Windows builds" }
newoption { trigger = "without-nvtt", description = "Disable use of NVTT" }
@ -156,10 +155,6 @@ function project_set_build_flags()
defines { "CONFIG2_GLES=1" }
end
if _OPTIONS["without-fam"] then
defines { "CONFIG2_FAM=0" }
end
if _OPTIONS["without-audio"] then
defines { "CONFIG2_AUDIO=0" }
end
@ -780,10 +775,6 @@ function setup_main_exe ()
elseif os.is("linux") or os.is("bsd") then
if not _OPTIONS["without-fam"] then
links { "fam" }
end
if not _OPTIONS["android"] and not (os.getversion().description == "OpenBSD") then
links { "rt" }
end
@ -1205,10 +1196,6 @@ function setup_tests()
elseif os.is("linux") or os.is("bsd") then
if not _OPTIONS["without-fam"] then
links { "fam" }
end
if not _OPTIONS["android"] and not (os.getversion().description == "OpenBSD") then
links { "rt" }
end

View File

@ -101,11 +101,6 @@
# define CONFIG2_GLES 0
#endif
// allow use of FAM API on Linux
#ifndef CONFIG2_FAM
# define CONFIG2_FAM 0
#endif
// allow use of OpenAL/Ogg/Vorbis APIs
#ifndef CONFIG2_AUDIO
# define CONFIG2_AUDIO 1

View File

@ -22,41 +22,24 @@
#include "precompiled.h"
#include <string>
#include "lib/config2.h"
#include "lib/sysdep/sysdep.h"
#include "lib/sysdep/dir_watch.h"
#include "lib/sysdep/sysdep.h"
#include "ps/CLogger.h"
#if !CONFIG2_FAM
#include <map>
#include <string>
#include <sys/inotify.h>
// stub implementations
Status dir_watch_Add(const OsPath& UNUSED(path), PDirWatch& UNUSED(dirWatch))
{
return INFO::OK;
}
Status dir_watch_Poll(DirWatchNotifications& UNUSED(notifications))
{
return INFO::OK;
}
#else
#include <fam.h>
// FAMEvent is large (~4KB), so define a smaller structure to store events
struct NotificationEvent
{
std::string filename;
void *userdata;
FAMCodes code;
uint32_t code;
int wd;
};
// To avoid deadlocks and slow synchronous reads, it's necessary to use a
// separate thread for reading events from FAM.
// separate thread for reading events from inotify.
// So we just spawn a thread to push events into this list, then swap it out
// when someone calls dir_watch_Poll.
// (We assume STL memory allocation is thread-safe.)
@ -69,10 +52,15 @@ static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
// trool; -1 = init failed and all operations will be aborted silently.
// this is so that each dir_* call doesn't complain if the system's
// FAM is broken or unavailable.
// inotify is broken or unavailable.
static int initialized = 0;
static FAMConnection fc;
// Inotify file descriptor
static int inotifyfd;
// With inotify, using a map seems to be a good alternative to FAM's userdata
typedef std::map<int, PDirWatch> DirWatchMap;
static DirWatchMap g_paths;
struct DirWatch
{
@ -84,142 +72,151 @@ struct DirWatch
~DirWatch()
{
ENSURE(initialized > 0);
FAMRequest req;
req.reqnum = reqnum;
FAMCancelMonitor(&fc, &req);
inotify_rm_watch(inotifyfd, reqnum);
}
OsPath path;
int reqnum;
};
// for atexit
static void fam_deinit()
static void inotify_deinit()
{
FAMClose(&fc);
close(inotifyfd);
pthread_cancel(g_event_loop_thread);
// NOTE: POSIX threads are (by default) only cancellable inside particular
// functions (like 'select'), so this should safely terminate while it's
// in select/FAMNextEvent/etc (and won't e.g. cancel while it's holding the
// in select/etc (and won't e.g. cancel while it's holding the
// mutex)
// Wait for the thread to finish
pthread_join(g_event_loop_thread, NULL);
}
static void fam_event_loop_process_events()
static void inotify_event_loop_process_events()
{
while(FAMPending(&fc) > 0)
// Buffer for reading the events.
// Need to be careful about overflow here.
char buffer[65535];
// Event iterator
ssize_t buffer_i = 0;
// Total size of all the events
ssize_t r;
// Size & struct for the current event
size_t event_size;
struct inotify_event *pevent;
r = read(inotifyfd, buffer, 65535);
if(r <= 0)
return;
while(buffer_i < r)
{
FAMEvent e;
if(FAMNextEvent(&fc, &e) < 0)
{
debug_printf(L"FAMNextEvent error");
return;
}
NotificationEvent ne;
ne.filename = e.filename;
ne.userdata = e.userdata;
ne.code = e.code;
pevent = (struct inotify_event *) &buffer[buffer_i];
pthread_mutex_lock(&g_mutex);
g_notifications.push_back(ne);
pthread_mutex_unlock(&g_mutex);
event_size = offsetof(struct inotify_event, name) + pevent->len;
ne.wd = pevent->wd;
ne.filename = pevent->name;
ne.code = pevent->mask;
pthread_mutex_lock(&g_mutex);
g_notifications.push_back(ne);
pthread_mutex_unlock(&g_mutex);
buffer_i += event_size;
}
}
static void* fam_event_loop(void*)
static void* inotify_event_loop(void*)
{
int famfd = FAMCONNECTION_GETFD(&fc);
while(true)
{
fd_set fdrset;
FD_ZERO(&fdrset);
FD_SET(famfd, &fdrset);
FD_SET(inotifyfd, &fdrset);
errno = 0;
// Block with select until there's events waiting
// (Mustn't just block inside FAMNextEvent since fam will deadlock)
while(select(famfd+1, &fdrset, NULL, NULL, NULL) < 0)
while(select(inotifyfd+1, &fdrset, NULL, NULL, NULL) < 0)
{
if(errno == EINTR)
{
// interrupted - try again
FD_ZERO(&fdrset);
FD_SET(famfd, &fdrset);
FD_SET(inotifyfd, &fdrset);
}
else if(errno == EBADF)
{
// probably just lost the connection to FAM - kill the thread
debug_printf(L"lost connection to FAM");
// probably just lost the connection to inotify - kill the thread
debug_printf(L"inotify_event_loop: Invalid file descriptor inotifyfd=%d\n", inotifyfd);
return NULL;
}
else
{
// oops
debug_printf(L"select error %d", errno);
debug_printf(L"inotify_event_loop: select error errno=%d\n", errno);
return NULL;
}
errno = 0;
}
if(FD_ISSET(famfd, &fdrset))
fam_event_loop_process_events();
if(FD_ISSET(inotifyfd, &fdrset))
inotify_event_loop_process_events();
}
}
Status dir_watch_Add(const OsPath& path, PDirWatch& dirWatch)
{
// init already failed; don't try again or complain
char resolved[PATH_MAX + 1];
// init already failed; don't try again or complain
if(initialized == -1)
return ERR::FAIL; // NOWARN
if(!initialized)
{
if(FAMOpen2(&fc, "lib_res"))
errno = 0;
if((inotifyfd = inotify_init()) < 0)
{
// Check for error ?
int err = errno;
initialized = -1;
LOGERROR(L"Error initializing FAM; hotloading will be disabled");
return ERR::FAIL; // NOWARN
LOGERROR(L"Error initializing inotify file descriptor; hotloading will be disabled, errno=%d", err);
errno = err;
return StatusFromErrno(); // NOWARN
}
if (pthread_create(&g_event_loop_thread, NULL, &fam_event_loop, NULL))
errno = 0;
int ret = pthread_create(&g_event_loop_thread, NULL, &inotify_event_loop, NULL);
if (ret != 0)
{
initialized = -1;
LOGERROR(L"Error creating FAM event loop thread; hotloading will be disabled");
return ERR::FAIL; // NOWARN
LOGERROR(L"Error creating inotify event loop thread; hotloading will be disabled, err=%d", ret);
errno = ret;
return StatusFromErrno(); // NOWARN
}
initialized = 1;
atexit(fam_deinit);
atexit(inotify_deinit);
}
PDirWatch tmpDirWatch(new DirWatch);
// NOTE: It would be possible to use FAMNoExists iff we're building with Gamin
// (not FAM), to avoid a load of boring notifications when we add a directory,
// but it would only save tens of milliseconds of CPU time, so it's probably
// not worthwhile
FAMRequest req;
if(FAMMonitorDirectory(&fc, OsString(path).c_str(), &req, tmpDirWatch.get()) < 0)
{
debug_warn(L"res_watch_dir failed!");
WARN_RETURN(ERR::FAIL); // no way of getting error code?
}
errno = 0;
int wd = inotify_add_watch(inotifyfd, realpath(OsString(path).c_str(), resolved), IN_CREATE | IN_DELETE | IN_CLOSE_WRITE);
if (wd < 0)
WARN_RETURN(StatusFromErrno());
dirWatch.swap(tmpDirWatch);
dirWatch->path = path;
dirWatch->reqnum = req.reqnum;
dirWatch->reqnum = wd;
g_paths.insert(std::make_pair(wd, dirWatch));
return INFO::OK;
}
Status dir_watch_Poll(DirWatchNotifications& notifications)
{
if(initialized == -1)
@ -236,27 +233,35 @@ Status dir_watch_Poll(DirWatchNotifications& notifications)
for(size_t i = 0; i < polled_notifications.size(); ++i)
{
DirWatchNotification::EType type;
// TODO: code is actually a bitmask, so this is slightly incorrect
switch(polled_notifications[i].code)
{
case FAMChanged:
case IN_CLOSE_WRITE:
type = DirWatchNotification::Changed;
break;
case FAMCreated:
case IN_CREATE:
type = DirWatchNotification::Created;
break;
case FAMDeleted:
case IN_DELETE:
type = DirWatchNotification::Deleted;
break;
default:
continue;
}
DirWatch* dirWatch = (DirWatch*)polled_notifications[i].userdata;
OsPath pathname = dirWatch->path / polled_notifications[i].filename;
notifications.push_back(DirWatchNotification(pathname, type));
DirWatchMap::iterator it = g_paths.find(polled_notifications[i].wd);
if(it != g_paths.end())
{
OsPath filename = Path(OsString(it->second->path).append(polled_notifications[i].filename));
notifications.push_back(DirWatchNotification(filename, type));
}
else
{
debug_printf(L"dir_watch_Poll: Notification with invalid watch descriptor wd=%d\n", polled_notifications[i].wd);
}
}
// nothing new; try again later
return INFO::OK;
}
#endif // CONFIG2_FAM