session BUGFIX lock waiting should be in milliseconds, not microseconds
diff --git a/src/netconf.h b/src/netconf.h
index a411429..01b0331 100644
--- a/src/netconf.h
+++ b/src/netconf.h
@@ -113,6 +113,42 @@
NC_PARAMTYPE_DUP_AND_FREE /**< make a copy of the argument, free afterwards */
} NC_PARAMTYPE;
+#ifdef ENABLE_SSH
+
+/**
+ * @brief Initialize libssh so that libnetconf2 can be safely used in a multi-threaded environment.
+ *
+ * Must be called before using any other SSH functions. Afterwards can libssh be used in the application
+ * as well.
+ */
+void nc_ssh_init(void);
+
+/**
+ * @brief Free all the resources allocated by libssh.
+ *
+ * Must be called before nc_tls_destroy() (if called) as libssh uses libcrypto as well.
+ */
+void nc_ssh_destroy(void);
+
+#endif /* ENABLE_SSH */
+
+#ifdef ENABLE_TLS
+
+/**
+ * @brief Initialize libcrypto so that libnetconf2 can be safely used in a multi-threaded environment.
+ *
+ * Must be called before using any other TLS functions. Afterwards can libcrypto be used in the application
+ * as well.
+ */
+void nc_tls_init(void);
+
+/**
+ * @brief Free all the resources allocated by libcrypto and libssl.
+ */
+void nc_tls_destroy(void);
+
+#endif /* ENABLE_TLS */
+
/**
* @brief Transform given time_t (seconds since the epoch) into the RFC 3339 format
* accepted by NETCONF functions.
diff --git a/src/session.c b/src/session.c
index 87915e1..4b44699 100644
--- a/src/session.c
+++ b/src/session.c
@@ -23,6 +23,7 @@
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
+#include <time.h>
#include <libyang/libyang.h>
#include "libnetconf.h"
@@ -172,16 +173,22 @@
int
nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
{
- int ret, elap = 0;
+ int ret;
+ struct timespec ts_timeout, ts_old, ts_new;
if (timeout > 0) {
- while ((timeout > elap) && ((ret = pthread_mutex_trylock(lock)) == EBUSY)) {
- usleep(NC_TIMEOUT_STEP);
- elap += NC_TIMEOUT_STEP;
- }
+ ts_timeout.tv_sec = timeout / 1000;
+ ts_timeout.tv_nsec = (timeout % 1000) * 1000000;
+
+ clock_gettime(CLOCK_REALTIME, &ts_old);
+
+ ret = pthread_mutex_timedlock(lock, &ts_timeout);
+
+ clock_gettime(CLOCK_REALTIME, &ts_new);
if (elapsed) {
- *elapsed += elap;
+ *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
+ *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
}
} else if (!timeout) {
ret = pthread_mutex_trylock(lock);
@@ -189,7 +196,7 @@
ret = pthread_mutex_lock(lock);
}
- if (ret == EBUSY) {
+ if (ret == ETIMEDOUT) {
/* timeout */
return 0;
} else if (ret) {
diff --git a/src/session.h b/src/session.h
index 89229af..4c1cf70 100644
--- a/src/session.h
+++ b/src/session.h
@@ -157,40 +157,4 @@
*/
void nc_session_free(struct nc_session *session);
-#ifdef ENABLE_SSH
-
-/**
- * @brief Initialize libssh so that libnetconf2 can be safely used in a multi-threaded environment.
- *
- * Must be called before using any other SSH functions. Afterwards can libssh be used in the application
- * as well.
- */
-void nc_ssh_init(void);
-
-/**
- * @brief Free all the resources allocated by libssh.
- *
- * Must be called before nc_tls_destroy() (if called) as libssh uses libcrypto as well.
- */
-void nc_ssh_destroy(void);
-
-#endif /* ENABLE_SSH */
-
-#ifdef ENABLE_TLS
-
-/**
- * @brief Initialize libcrypto so that libnetconf2 can be safely used in a multi-threaded environment.
- *
- * Must be called before using any other TLS functions. Afterwards can libcrypto be used in the application
- * as well.
- */
-void nc_tls_init(void);
-
-/**
- * @brief Free all the resources allocated by libcrypto and libssl.
- */
-void nc_tls_destroy(void);
-
-#endif /* ENABLE_TLS */
-
#endif /* NC_SESSION_H_ */
diff --git a/src/session_server.h b/src/session_server.h
index cafcdb7..77367b9 100644
--- a/src/session_server.h
+++ b/src/session_server.h
@@ -285,9 +285,6 @@
*/
void nc_ssh_server_free_opts(void);
-/* TODO remove */
-struct nc_session *nc_accept_ssh_channel(struct nc_session *session, int timeout);
-
#endif /* ENABLE_SSH */
#ifdef ENABLE_TLS
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index a33405c..02f4c6e 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -823,7 +823,7 @@
}
/* TODO remove */
-API struct nc_session *
+struct nc_session *
nc_accept_ssh_channel(struct nc_session *session, int timeout)
{
struct nc_session *new_session;