From: Niklas Hofmann Date: Wed, 13 Aug 2014 11:49:30 +0000 (+0200) Subject: locks in discovery code X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=eea70edd68ff954c35f8d26ac8eb01d4bf3921d9;p=meshlink locks in discovery code --- diff --git a/src/discovery.c b/src/discovery.c index d17f962b..fa4e13c3 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -32,6 +32,8 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); + pthread_mutex_lock(&(mesh->mesh_mutex)); + /* Called whenever the entry group state changes */ switch(state) { @@ -55,6 +57,8 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup case AVAHI_ENTRY_GROUP_REGISTERING: ; } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } @@ -71,6 +75,8 @@ static void discovery_create_services(meshlink_handle_t *mesh) assert(mesh->avahi_servicetype != NULL); assert(mesh->self != NULL); + pthread_mutex_lock(&(mesh->mesh_mutex)); + logger(mesh, MESHLINK_DEBUG, "Adding service\n"); /* Ifthis is the first time we're called, let's create a new entry group */ @@ -121,6 +127,8 @@ fail: done: if(txt_name) { free(txt_name); } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } static void discovery_server_callback(AvahiServer *server, AvahiServerState state, void * userdata) @@ -129,6 +137,8 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat // asserts assert(mesh != NULL); + + pthread_mutex_lock(&(mesh->mesh_mutex)); switch(state) { @@ -195,6 +205,8 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat case AVAHI_SERVER_INVALID: break; } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata) @@ -206,6 +218,8 @@ static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfI assert(mesh != NULL); assert(mesh->avahi_server != NULL); + pthread_mutex_lock(&(mesh->mesh_mutex)); + /* Called whenever a service has been resolved successfully or timed out */ switch(event) { @@ -327,6 +341,8 @@ static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfI } avahi_s_service_resolver_free(resolver); + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) @@ -338,6 +354,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); + pthread_mutex_lock(&(mesh->mesh_mutex)); + /* Called whenever a new services becomes available on the LAN or is removed from the LAN */ switch (event) { @@ -385,6 +403,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde } break; } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } static void *discovery_loop(void *userdata)