summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-11-29 16:09:24 -0500
committerirungentoo <irungentoo@gmail.com>2014-11-29 16:09:24 -0500
commit8deb032b2d9a77465a3c2b65a409787098e387cd (patch)
tree785fc1b1b975b712bb9a375af386f4de18f89663 /toxcore
parenteafe0e6b0b83b4db3d79a9416d8aa33318fb12a7 (diff)
parente62ded3a6dfc1203418e3d7a2c936794c4c9ec1c (diff)
Merge branch 'mutex-1' of https://github.com/mannol/toxcore
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/net_crypto.c14
-rw-r--r--toxcore/util.c19
-rw-r--r--toxcore/util.h3
3 files changed, 24 insertions, 12 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 2f125fdc..87541d9f 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2696,22 +2696,12 @@ Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info)
2696 if (temp == NULL) 2696 if (temp == NULL)
2697 return NULL; 2697 return NULL;
2698 2698
2699 pthread_mutexattr_t attr; 2699 if (create_recursive_mutex(&temp->tcp_mutex) != 0 ||
2700 2700 pthread_mutex_init(&temp->connections_mutex, NULL) != 0) {
2701 if (pthread_mutexattr_init(&attr) == 0) {
2702 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0 || pthread_mutex_init(&temp->tcp_mutex, &attr) != 0
2703 || pthread_mutex_init(&temp->connections_mutex, NULL) != 0) {
2704 pthread_mutexattr_destroy(&attr);
2705 free(temp);
2706 return NULL;
2707 }
2708 } else {
2709 free(temp); 2701 free(temp);
2710 return NULL; 2702 return NULL;
2711 } 2703 }
2712 2704
2713 pthread_mutexattr_destroy(&attr);
2714
2715 temp->dht = dht; 2705 temp->dht = dht;
2716 2706
2717 new_keys(temp); 2707 new_keys(temp);
diff --git a/toxcore/util.c b/toxcore/util.c
index 3d444b07..93e63ac2 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -162,3 +162,22 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
162 162
163 return length == 0 ? 0 : -1; 163 return length == 0 ? 0 : -1;
164}; 164};
165
166int create_recursive_mutex(pthread_mutex_t *mutex)
167{
168 pthread_mutexattr_t attr;
169
170 if (pthread_mutexattr_init(&attr) != 0)
171 return -1;
172
173 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
174 return -1;
175
176 /* Create queue mutex */
177 if (pthread_mutex_init(mutex, &attr) != 0)
178 return -1;
179
180 pthread_mutexattr_destroy(&attr);
181
182 return 0;
183}
diff --git a/toxcore/util.h b/toxcore/util.h
index 007db079..7cd6bb8b 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -27,6 +27,7 @@
27 27
28#include <stdbool.h> 28#include <stdbool.h>
29#include <stdint.h> 29#include <stdint.h>
30#include <pthread.h>
30 31
31#define MIN(a,b) (((a)<(b))?(a):(b)) 32#define MIN(a,b) (((a)<(b))?(a):(b))
32 33
@@ -52,4 +53,6 @@ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32
52int load_state(load_state_callback_func load_state_callback, void *outer, 53int load_state(load_state_callback_func load_state_callback, void *outer,
53 const uint8_t *data, uint32_t length, uint16_t cookie_inner); 54 const uint8_t *data, uint32_t length, uint16_t cookie_inner);
54 55
56int create_recursive_mutex(pthread_mutex_t *mutex);
57
55#endif /* __UTIL_H__ */ 58#endif /* __UTIL_H__ */