From aaeb47dc1429c3d1320781434dcd13ada2d2cabf Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 9 Jul 2014 23:40:46 -0400 Subject: [PATCH] create dns loopup threads in detached state & add a few missing error descriptions --- src/dns.c | 14 +++++++++----- src/toxic.c | 4 +--- src/toxic.h | 19 ++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/dns.c b/src/dns.c index 09476f8..5f3c611 100644 --- a/src/dns.c +++ b/src/dns.c @@ -79,6 +79,7 @@ static struct _thread_data { static struct _dns_thread { pthread_t tid; + pthread_attr_t attr; } dns_thread; @@ -100,7 +101,8 @@ static void kill_dns_thread(void *dns_obj) tox_dns3_kill(dns_obj); memset(&t_data, 0, sizeof(struct _thread_data)); - pthread_exit(0); + pthread_attr_destroy(&dns_thread.attr); + pthread_exit(NULL); } /* puts TXT from dns response in buf. Returns length of TXT on success, -1 on fail.*/ @@ -303,10 +305,12 @@ void dns3_lookup(ToxWindow *self, Tox *m, char *id_bin, char *addr, char *msg) t_data.m = m; t_data.busy = 1; - pthread_mutex_unlock(&Winthread.lock); + if (pthread_attr_init(&dns_thread.attr) != 0) + exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_ATTR); - if (pthread_create(&dns_thread.tid, NULL, dns3_lookup_thread, NULL) != 0) + if (pthread_attr_setdetachstate(&dns_thread.attr, PTHREAD_CREATE_DETACHED) != 0) + exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_ATTR); + + if (pthread_create(&dns_thread.tid, &dns_thread.attr, dns3_lookup_thread, NULL) != 0) exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_CREATE); - - pthread_mutex_lock(&Winthread.lock); } diff --git a/src/toxic.c b/src/toxic.c index b3b46fe..7c750c3 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -446,9 +446,7 @@ static void load_data(Tox *m, char *path) free(buf); fclose(fd); } else { - int st; - - if ((st = store_data(m, path)) != 0) + if (store_data(m, path) != 0) exit_toxic_err("failed in load_data", FATALERR_STORE_DATA); } } diff --git a/src/toxic.h b/src/toxic.h index efb6cd4..65476fa 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -64,15 +64,16 @@ #define T_KEY_C_H 0x08 /* ctrl-h */ typedef enum _FATAL_ERRS { - FATALERR_MEMORY = -1, /* malloc() or calloc() failed */ - FATALERR_FREAD = -2, /* fread() failed on critical read */ - FATALERR_THREAD_CREATE = -3, - FATALERR_MUTEX_INIT = -4, - FATALERR_LOCALE_SET = -5, - FATALERR_STORE_DATA = -6, - FATALERR_NETWORKINIT = -7, /* Tox network failed to init */ - FATALERR_INFLOOP = -8, /* infinite loop detected */ - FATALERR_WININIT = -9, /* window init failed */ + FATALERR_MEMORY = -1, /* malloc() or calloc() failed */ + FATALERR_FREAD = -2, /* fread() failed on critical read */ + FATALERR_THREAD_CREATE = -3, /* thread creation failed */ + FATALERR_MUTEX_INIT = -4, /* mutex init failed */ + FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */ + FATALERR_LOCALE_SET = -6, /* system locale not set */ + FATALERR_STORE_DATA = -7, /* store_data failed in critical section */ + FATALERR_NETWORKINIT = -8, /* Tox network failed to init */ + FATALERR_INFLOOP = -9, /* infinite loop detected */ + FATALERR_WININIT = -10, /* window init failed */ } FATAL_ERRS; /* Fixes text color problem on some terminals.