Skip to content

Commit

Permalink
Simplified macOS implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Jul 25, 2023
1 parent ac1a2fc commit a2a0e47
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 87 deletions.
61 changes: 2 additions & 59 deletions src/efsw/FileWatcherFSEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void FileWatcherFSEvents::FSEventCallback( ConstFSEventStreamRef streamRef, void
}

FileWatcherFSEvents::FileWatcherFSEvents( FileWatcher* parent ) :
FileWatcherImpl( parent ), mRunLoopRef( NULL ), mLastWatchID( 0 ), mThread( NULL ) {
FileWatcherImpl( parent ), mLastWatchID( 0 ) {
mInitOK = true;

watch();
Expand All @@ -76,11 +76,6 @@ FileWatcherFSEvents::~FileWatcherFSEvents() {
mWatchCond.notify_all();
#endif

if ( mRunLoopRef.load() )
CFRunLoopStop( mRunLoopRef.load() );

efSAFE_DELETE( mThread );

WatchMap::iterator iter = mWatches.begin();

for ( ; iter != mWatches.end(); ++iter ) {
Expand All @@ -92,11 +87,6 @@ FileWatcherFSEvents::~FileWatcherFSEvents() {

WatchID FileWatcherFSEvents::addWatch( const std::string& directory, FileWatchListener* watcher,
bool recursive ) {
/// Wait to the RunLoopRef to be ready
while ( NULL == mRunLoopRef.load() ) {
System::sleep( 1 );
}

std::string dir( directory );

FileInfo fi( dir );
Expand Down Expand Up @@ -181,54 +171,7 @@ void FileWatcherFSEvents::removeWatch( WatchID watchid ) {
efSAFE_DELETE( watch );
}

void FileWatcherFSEvents::watch() {
if ( NULL == mThread ) {
mThread = new Thread( &FileWatcherFSEvents::run, this );
mThread->launch();
}
}

void FileWatcherFSEvents::run() {
mRunLoopRef = CFRunLoopGetCurrent();

while ( mInitOK ) {
mNeedInitMutex.lock();

if ( !mNeedInit.empty() ) {
for ( std::vector<WatcherFSEvents*>::iterator it = mNeedInit.begin();
it != mNeedInit.end(); ++it ) {
( *it )->initAsync();
}

mNeedInit.clear();
}

mNeedInitMutex.unlock();

bool isEmpty = true;

{
Lock lock( mWatchesLock );
isEmpty = mWatches.empty();
}

if ( isEmpty ) {
#ifndef EFSW_LEGACY_CPP
std::unique_lock<std::mutex> lk( mWatchesMutex );
mWatchCond.wait( lk, [this] {
Lock lock( mWatchesLock );
return !mInitOK || !mWatches.empty();
} );
#else
System::sleep( 1 );
#endif
} else {
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0.5, kCFRunLoopRunTimedOut );
}
}

mRunLoopRef = NULL;
}
void FileWatcherFSEvents::watch() {}

void FileWatcherFSEvents::handleAction( Watcher* watch, const std::string& filename,
unsigned long action, std::string oldFilename ) {
Expand Down
9 changes: 0 additions & 9 deletions src/efsw/FileWatcherFSEvents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,21 @@ class FileWatcherFSEvents : public FileWatcherImpl {
void* eventPaths, const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[] );

Atomic<CFRunLoopRef> mRunLoopRef;

/// Vector of WatcherWin32 pointers
WatchMap mWatches;

/// The last watchid
WatchID mLastWatchID;

Thread* mThread;

Mutex mWatchesLock;

bool pathInWatches( const std::string& path );

std::vector<WatcherFSEvents*> mNeedInit;
Mutex mNeedInitMutex;

#ifndef EFSW_LEGACY_CPP
std::mutex mWatchesMutex;
std::condition_variable mWatchCond;
#endif

private:
void run();
};

} // namespace efsw
Expand Down
19 changes: 4 additions & 15 deletions src/efsw/WatcherFSEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
namespace efsw {

WatcherFSEvents::WatcherFSEvents() :
Watcher(), FWatcher( NULL ), FSStream( NULL ), WatcherGen( NULL ), initializedAsync( false ) {}
Watcher(), FWatcher( NULL ), FSStream( NULL ), WatcherGen( NULL ) {}

WatcherFSEvents::WatcherFSEvents( WatchID id, std::string directory, FileWatchListener* listener,
bool recursive, WatcherFSEvents* parent ) :
Watcher( id, directory, listener, recursive ),
FWatcher( NULL ),
FSStream( NULL ),
WatcherGen( NULL ),
initializedAsync( false ) {}
WatcherGen( NULL ) {}

WatcherFSEvents::~WatcherFSEvents() {
if ( NULL != FSStream ) {
if ( initializedAsync ) {
FSEventStreamStop( FSStream );
}

FSEventStreamStop( FSStream );
FSEventStreamInvalidate( FSStream );
FSEventStreamRelease( FSStream );
}
Expand Down Expand Up @@ -60,19 +56,12 @@ void WatcherFSEvents::init() {

FSEventStreamSetDispatchQueue(FSStream, queue);

FWatcher.load()->mNeedInitMutex.lock();
FWatcher.load()->mNeedInit.push_back( this );
FWatcher.load()->mNeedInitMutex.unlock();
FSEventStreamStart( FSStream );

CFRelease( CFDirectoryArray );
CFRelease( CFDirectory );
}

void WatcherFSEvents::initAsync() {
FSEventStreamStart( FSStream );
initializedAsync = true;
}

void WatcherFSEvents::sendFileAction( WatchID watchid, const std::string& dir,
const std::string& filename, Action action,
std::string oldFilename ) {
Expand Down
4 changes: 0 additions & 4 deletions src/efsw/WatcherFSEvents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class WatcherFSEvents : public Watcher {

void init();

void initAsync();

void handleActions( std::vector<FSEvent>& events );

void process();
Expand All @@ -51,8 +49,6 @@ class WatcherFSEvents : public Watcher {

WatcherGeneric* WatcherGen;

Atomic<bool> initializedAsync;

std::set<std::string> DirsChanged;

void sendFileAction( WatchID watchid, const std::string& dir, const std::string& filename,
Expand Down

0 comments on commit a2a0e47

Please sign in to comment.