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 }
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; }