]> git.meshlink.io Git - meshlink/commitdiff
Properly initialize mutexes and condition variables.
authorGuus Sliepen <guus@meshlink.io>
Wed, 10 Jun 2020 20:25:12 +0000 (22:25 +0200)
committerGuus Sliepen <guus@meshlink.io>
Wed, 10 Jun 2020 20:25:12 +0000 (22:25 +0200)
On Linux, zeroing a pthread_mutex_t or pthread_cond_t variable ensures
the mutex/cond is properly initialized, however this is not the case om
some other platforms. Ensure we always call pthread_mutex/cond_init().

src/adns.c
src/discovery.c
src/meshlink.c

index 01989ac5c71bdeb24602358841cfb5d5555dece6..cb9611e1594b246ab71f8364ef624d33d2ddf3b4 100644 (file)
@@ -184,6 +184,8 @@ struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char
        info->host = host;
        info->serv = serv;
        info->socktype = socktype;
+       pthread_mutex_init(&info->mutex, NULL);
+       pthread_cond_init(&info->cond, NULL);
 
        struct timespec deadline;
        clock_gettime(CLOCK_REALTIME, &deadline);
index df88b17919178a5075a769cf5f191256911c3810..d4790c43f8292020d352606fa23e2cfef78566a5 100644 (file)
@@ -343,6 +343,8 @@ static void *discovery_loop(void *userdata) {
        meshlink_handle_t *mesh = userdata;
        assert(mesh);
 
+       pthread_mutex_lock(&mesh->discovery_mutex);
+
        // handle catta logs
        catta_set_log_function(discovery_log_cb);
 
@@ -414,7 +416,6 @@ static void *discovery_loop(void *userdata) {
 
 fail:
 
-       pthread_mutex_lock(&mesh->discovery_mutex);
        pthread_cond_broadcast(&mesh->discovery_cond);
        pthread_mutex_unlock(&mesh->discovery_mutex);
 
@@ -461,14 +462,16 @@ bool discovery_start(meshlink_handle_t *mesh) {
        assert(!mesh->discovery_threadstarted);
        assert(!mesh->catta_servicetype);
 
+       pthread_mutex_lock(&mesh->discovery_mutex);
+
        // Start the discovery thread
        if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) {
+               pthread_mutex_unlock(&mesh->discovery_mutex);
                logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno));
                memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread);
                return false;
        }
 
-       pthread_mutex_lock(&mesh->discovery_mutex);
        pthread_cond_wait(&mesh->discovery_cond, &mesh->discovery_mutex);
        pthread_mutex_unlock(&mesh->discovery_mutex);
 
index 9aca06779a699e17bc667b233571861f507c3b36..8ad72c15573d0f0cf4da9a25e2cbe10cd6391782 100644 (file)
@@ -1456,12 +1456,17 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
                }
        }
 
-       // initialize mutex
+       // initialize mutexes and conds
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&mesh->mutex, &attr);
 
+       pthread_mutex_init(&mesh->discovery_mutex, NULL);
+       pthread_cond_init(&mesh->discovery_cond, NULL);
+
+       pthread_cond_init(&mesh->adns_cond, NULL);
+
        mesh->threadstarted = false;
        event_loop_init(&mesh->loop);
        mesh->loop.data = mesh;