[Swan-dev] kibitzing about logger

D. Hugh Redelmeier hugh at mimosa.com
Tue Sep 1 15:33:54 UTC 2020


Passing logger around is a very clean but noisy solution.

But what is the problem?

My impression is that the main problem is that using a global logger is 
hard to do correctly with threads.  Implicitly global (shared) variables 
can be mutated in parallel, with disastrous results, some of which are 
hard to find.

Solution one: locks around globals.  This requires a lot of work, a loss 
of parallelism, and possibly subtle bugs.  The performance impact might be 
large (only measurement would tell).

Solution two: pass a logger parameter everywhere that it is needed.  This 
seems to be mostly implemented, through a lot of patches.

Solution three: have a pointer to the appropriate logging routine 
available in thread-local static memory.  This would require localized
changes to the original code base.

If the thread-local static technique is available to us, I would advocate 
for its use.  I know that this suggestion is quite late. Sorry.

When I mentioned this in IRC, Andrew pointed out that POSIX threads
didn't support static thread-local storage.  (But some mechanism had
to be created to support errno.)

Apparently recent C does support thread-local statics.  But we aren't
using recent C.  If our compilers support it, it would be good to use
the standardized form.

GCC and clang have the same non-standard C extension to support
thread-local statics.  It makes sense to use that.  (I saw a comment
that it doesn't work on OSX due to lack of OS facilities to implement
it.)

<https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html>

extern __thread logger_t *cur_logger_ptr;

Again, perhaps I don't actually understand the problem correctly.

Note: every global variable should be either thread local or
only ever used in one thread.  How could we enforce this?  Perhaps all 
objects only for use in the main thread be described in headers that are 
not #included in compilation units intended to be run in other
threads.  The same should apply to functions using those globals.

(Threads are an abomination that needs to be tamed.  I don't think
that we use threading enough that we need non-main threads to be first
class citizens.  Every right we grant them creates problems.  Like
logging.)


More information about the Swan-dev mailing list