]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
no warnings for thread code (porting), and nicer logging.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 2 Mar 2007 09:03:14 +0000 (09:03 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 2 Mar 2007 09:03:14 +0000 (09:03 +0000)
git-svn-id: file:///svn/unbound/trunk@159 be551aaa-1e26-0410-a405-d3ace91eadb9

daemon/daemon.c
doc/Changelog
doc/TODO
util/locks.c
util/locks.h
util/log.c
util/log.h

index 68aaf2b6fc71e522cc58ea2328d09cb1669f8741..aac6ac6a304b0e913a0ba29bfde1d945d189f4e8 100644 (file)
@@ -179,6 +179,7 @@ thread_start(void* arg)
 {
        struct worker* worker = (struct worker*)arg;
        int num = worker->thread_num;
+       log_thread_set(&worker->thread_num);
        ub_thread_blocksigs();
 #if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS)
        /* close pipe ends used by main */
@@ -283,6 +284,7 @@ daemon_cleanup(struct daemon* daemon)
        /* before stopping main worker, handle signals ourselves, so we
           don't die on multiple reload signals for example. */
        signal_handling_record();
+       log_thread_set(NULL);
        for(i=0; i<daemon->num; i++)
                worker_delete(daemon->workers[i]);
        free(daemon->workers);
index 9afc958242fdcbee870005fd29c4ff5672e6e251..fb3caacf3766f0c0ace0c41570b80d7011af0b0a 100644 (file)
@@ -1,3 +1,8 @@
+2 March 2007: Wouter
+       - do not compile fork funcs unless needed. Otherwise will give
+         type errors as their typedefs have not been enabled.
+       - log shows thread numbers much more nicely (and portably).
+
 1 March 2007: Wouter
        - Signals, libevent and threads work well, with libevent patch and
          changes to code (close after event_del).
index 559366e230bb54a2e9cb43ca59c016b2313e6383..50e3825cc003a4161794c3a2a32f4c2d1213b2aa 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -1,2 +1,3 @@
 TODO items.
 o use real entropy to make random (ID, port) numbers more random.
+o in production mode, do not free memory on exit. In debug mode, test leaks.
index 52f7f090a2fcb45bf4e19572342a1aeaef13d9ea..2ceae05d76ccd27a649f92ef3f8d1e9bb05d5726 100644 (file)
@@ -88,6 +88,7 @@ void ub_thread_sig_unblock(int sig)
 #endif /* HAVE_PTHREAD */
 }
 
+#if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS)
 /**
  * No threading available: fork a new process.
  * This means no shared data structure, and no locking.
@@ -127,3 +128,13 @@ void ub_thr_fork_wait(ub_thread_t thread)
                log_warn("process %d abnormal exit with status %d",
                        (int)thread, status);
 }
+#endif /* !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS) */
+
+#ifdef HAVE_SOLARIS_THREADS
+void* ub_thread_key_get(ub_thread_key_t key)
+{
+       void* ret=NULL;
+       LOCKRET(thr_getspecific(key, &ret));
+       return ret;
+}
+#endif
index fe04aae3bcf510a216e20004fbe601d98d3fe2ff..42b972e5c840ce8e7a595f67e3ab6e86ec06c554 100644 (file)
@@ -125,6 +125,10 @@ typedef pthread_t ub_thread_t;
 #define ub_thread_self() pthread_self()
 /** wait for another thread to terminate */
 #define ub_thread_join(thread) LOCKRET(pthread_join(thread, NULL))
+typedef pthread_key_t ub_thread_key_t;
+#define ub_thread_key_create(key, f) LOCKRET(pthread_key_create(key, f))
+#define ub_thread_key_set(key, v) LOCKRET(pthread_setspecific(key, v))
+#define ub_thread_key_get(key) pthread_getspecific(key)
 
 #else /* we do not HAVE_PTHREAD */
 #ifdef HAVE_SOLARIS_THREADS
@@ -156,6 +160,10 @@ typedef thread_t ub_thread_t;
 #define ub_thread_create(thr, func, arg) LOCKRET(thr_create(NULL, NULL, func, arg, NULL, thr))
 #define ub_thread_self() thr_self()
 #define ub_thread_join(thread) LOCKRET(thr_join(thread, NULL, NULL))
+typedef thread_key_t ub_thread_key_t;
+#define ub_thread_key_create(key, f) LOCKRET(thr_keycreate(key, f))
+#define ub_thread_key_set(key, v) LOCKRET(thr_setspecific(key, v))
+void* ub_thread_key_get(ub_thread_key_t key);
 
 #else /* we do not HAVE_SOLARIS_THREADS and no PTHREADS */
 
@@ -192,6 +200,10 @@ typedef pid_t ub_thread_t;
 #define ub_thread_join(thread) ub_thr_fork_wait(thread)
 void ub_thr_fork_wait(ub_thread_t thread);
 void ub_thr_fork_create(ub_thread_t* thr, void* (*func)(void*), void* arg);
+typedef void* ub_thread_key_t;
+#define ub_thread_key_create(key, f) (*(key)) = NULL
+#define ub_thread_key_set(key, v) (key) = (v)
+#define ub_thread_key_get(key) (key)
 
 #endif /* HAVE_SOLARIS_THREADS */
 #endif /* HAVE_PTHREAD */
index 044e50250e20b5e74520cf97ffc8ec33adc74c4f..81784df89f17ee64fa653334ba11e0ab495c3e62 100644 (file)
 enum verbosity_value verbosity = 0;
 /** the file logged to. */
 static FILE* logfile = 0;
+/** if key has been created */
+static int key_created = 0;
+/** pthread key for thread ids in logfile */
+static ub_thread_key_t logkey;
 
 void
 log_init(const char* filename)
 {
        FILE *f;
+       if(!key_created) {
+               key_created = 1;
+               ub_thread_key_create(&logkey, NULL);
+       }
+
        if(!filename || !filename[0]) {
                if(logfile && logfile != stderr)
                        fclose(logfile);
@@ -71,16 +80,21 @@ log_init(const char* filename)
        logfile = f;
 }
 
+void log_thread_set(int* num)
+{
+       ub_thread_key_set(logkey, num);
+}
+
 void
 log_vmsg(const char* type, const char *format, va_list args)
 {
        char message[MAXSYSLOGMSGLEN];
        const char* ident="unbound";
+       unsigned int* tid = (unsigned int*)ub_thread_key_get(logkey);
        vsnprintf(message, sizeof(message), format, args);
        fprintf(logfile, "[%d] %s[%d:%x] %s: %s\n",
                (int)time(NULL), ident, (int)getpid(), 
-               (unsigned int)ub_thread_self(),
-               type, message);
+               tid?*tid:0, type, message);
        fflush(logfile);
 }
 
index b0a5d1c45b0dce183a8624030262168d113cd315..5b58f4c749bfd2ab8e33c795d0d7b244420a90bf 100644 (file)
@@ -80,6 +80,14 @@ void verbose(enum verbosity_value level,
  */
 void log_init(const char* filename);
 
+/**
+ * Init a thread (will print this number for the thread log entries).
+ * Must be called from the thread itself. If not called 0 is printed.
+ * @param num: number to print for this thread. Owned by caller, must
+ *     continue to exist.
+ */
+void log_thread_set(int* num);
+
 /**
  * Log informational message.
  * Pass printf formatted arguments. No trailing newline is needed.