void remove_watch(const string& path) { int fd = descriptors_by_file_name[path]; descriptors_by_file_name.erase(path); file_names_by_descriptor.erase(fd); file_modes.erase(fd); close(fd); }
void remove_watch(int fd) { string name = file_names_by_descriptor[fd]; file_names_by_descriptor.erase(fd); descriptors_by_file_name.erase(name); file_modes.erase(fd); close(fd); }
FSW_SESSION * get_session(const FSW_HANDLE handle) { if (sessions.find(handle) == sessions.end()) throw int(FSW_ERR_SESSION_UNKNOWN); #ifdef HAVE_CXX_UNIQUE_PTR return sessions[handle].get(); #else return sessions[handle]; #endif }
int fsw_start_monitor(const FSW_HANDLE handle) { try { #ifdef HAVE_CXX_MUTEX unique_lock<mutex> session_lock(session_mutex, defer_lock); session_lock.lock(); #endif FSW_SESSION * session = get_session(handle); #ifdef HAVE_CXX_MUTEX if (session->running.load(memory_order_acquire)) return fsw_set_last_error(int(FSW_ERR_MONITOR_ALREADY_RUNNING)); # ifdef HAVE_CXX_UNIQUE_PTR unique_ptr<mutex> & sm = session_mutexes.at(handle); lock_guard<mutex> lock_sm(*sm.get()); # else mutex * sm = session_mutexes.at(handle); lock_guard<mutex> lock_sm(*sm); # endif session_lock.unlock(); #endif if (!session->monitor) create_monitor(handle, session->type); session->monitor->set_filters(session->filters); session->monitor->set_follow_symlinks(session->follow_symlinks); session->monitor->set_latency(session->latency); session->monitor->set_recursive(session->recursive); #ifdef HAVE_CXX_MUTEX session->running.store(true, memory_order_release); monitor_start_guard<bool> guard(session->running, false); #endif session->monitor->start(); } catch (int error) { return fsw_set_last_error(error); } return fsw_set_last_error(FSW_OK); }
int fsw_destroy_session(const FSW_HANDLE handle) { try { #ifdef HAVE_CXX_MUTEX std::lock_guard<std::mutex> session_lock(session_mutex); #endif FSW_SESSION * session = get_session(handle); #ifdef HAVE_CXX_MUTEX # ifdef HAVE_CXX_UNIQUE_PTR const unique_ptr<mutex> & sm = session_mutexes[handle]; lock_guard<mutex> sm_lock(*sm.get()); # else mutex * sm = session_mutexes[handle]; lock_guard<mutex> sm_lock(*sm); # endif #endif if (session->monitor) { void * context = session->monitor->get_context(); if (!context) { session->monitor->set_context(nullptr); delete static_cast<fsw_callback_context *> (context); } delete session->monitor; } sessions.erase(handle); #ifdef HAVE_CXX_MUTEX session_mutexes.erase(handle); #endif } catch (int error) { return fsw_set_last_error(error); } return fsw_set_last_error(FSW_OK); }
FSW_HANDLE fsw_init_session(const fsw_monitor_type type) { #ifdef HAVE_CXX_MUTEX std::lock_guard<std::mutex> session_lock(session_mutex); #endif if (!srand_initialized) { srand(time(nullptr)); srand_initialized = true; } int handle; do { handle = rand(); } while (sessions.find(handle) != sessions.end()); FSW_SESSION *session = new FSW_SESSION{}; session->handle = handle; session->type = type; // Store the handle and a mutex to guard access to session instances. #ifdef HAVE_CXX_UNIQUE_PTR sessions[handle] = unique_ptr<FSW_SESSION>(session); #else sessions[handle] = session; #endif #ifdef HAVE_CXX_MUTEX # ifdef HAVE_CXX_UNIQUE_PTR session_mutexes[handle] = unique_ptr<mutex>(new mutex); # else session_mutexes[handle] = new mutex; # endif #endif return handle; }