From 946a09f57b223f4b8bf5bd6e65fb23d089b9959e Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 31 Jul 2014 13:11:21 -0400 Subject: [PATCH] Adding a new connection while a lossy packet is being sent could also cause thread related issues. --- toxcore/net_crypto.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 57ee20b8..8b5de632 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1268,13 +1268,26 @@ static int create_crypto_connection(Net_Crypto *c) return i; } - if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1) - return -1; + while (1) { /* TODO: is this really the best way to do this? */ + pthread_mutex_lock(&c->connections_mutex); - memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); - int id = c->crypto_connections_length; - pthread_mutex_init(&c->crypto_connections[id].mutex, NULL); - ++c->crypto_connections_length; + if (!c->connection_use_counter) { + break; + } + + pthread_mutex_unlock(&c->connections_mutex); + } + + int id = -1; + + if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == 0) { + memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); + id = c->crypto_connections_length; + pthread_mutex_init(&c->crypto_connections[id].mutex, NULL); + ++c->crypto_connections_length; + } + + pthread_mutex_unlock(&c->connections_mutex); return id; }