compat CHANGE support for systems without pthread_mutex_timedlock()
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dbbba9e..6a2d605 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -105,12 +105,10 @@
 find_package(Threads REQUIRED)
 target_link_libraries(netconf2 ${CMAKE_THREAD_LIBS_INIT})
 
-# check availability of spinlock
+# check availability for some pthread functions
 set(CMAKE_REQUIRED_LIBRARIES pthread)
 check_function_exists(pthread_spin_lock HAVE_SPINLOCK)
-if (HAVE_SPINLOCK)
-	set(DEFINE_HAVE_SPINLOCK "#define HAVE_SPINLOCK")
-endif()
+check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK)
 
 # dependencies - libssh
 if(ENABLE_SSH)
diff --git a/src/config.h.in b/src/config.h.in
index e1d626a..097c235 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -40,6 +40,11 @@
 #endif
 
 /*
+ * support for pthread_mutex_timedlock
+ */
+#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
+
+/*
  * Location of installed basic YIN/YANG schemas
  */
 #define SCHEMAS_DIR "@CMAKE_INSTALL_PREFIX@/@DATA_INSTALL_DIR@"
diff --git a/src/session.c b/src/session.c
index 34dc782..b9dd886 100644
--- a/src/session.c
+++ b/src/session.c
@@ -65,6 +65,40 @@
 #endif
 }
 
+#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
+int
+pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
+{
+    int rc;
+    struct timespec cur, dur;
+
+    /* Try to acquire the lock and, if we fail, sleep for 5ms. */
+    while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
+        nc_gettimespec(&cur);
+
+        if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) {
+            break;
+        }
+
+        dur.tv_sec = abstime->tv_sec - cur.tv_sec;
+        dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec;
+        if (dur.tv_nsec < 0) {
+            dur.tv_sec--;
+            dur.tv_nsec += 1000000000;
+        }
+
+        if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) {
+            dur.tv_sec = 0;
+            dur.tv_nsec = 5000000;
+        }
+
+        nanosleep(&dur, NULL);
+    }
+
+    return rc;
+}
+#endif
+
 /*
  * @return 1 - success
  *         0 - timeout
diff --git a/src/session_p.h b/src/session_p.h
index e9e9eec..06f20de 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -295,6 +295,10 @@
 
 NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op);
 
+#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
+int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
+#endif
+
 int nc_gettimespec(struct timespec *ts);
 int nc_timedlock(pthread_mutex_t *lock, int timeout);