Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LazyLogModule, a thread-safe replacement for declaring a PRLogModuleInfo

39 views
Skip to first unread message

Eric Rahm

unread,
Oct 28, 2015, 7:35:29 PM10/28/15
to
Hi All-

I recently landed a lazily-loaded log module class, LazyLogModule [1], suitable for declaring static references to log modules in a thread-safe way.

Currently this class is opt-in and PRLogModuleInfo can still be used w/ MOZ_LOG. But be forewarned, as we move forward to a glorious world where log levels can be dynamically updated w/o restarting the browser, PRLogModuleInfo will be left behind and only LogModule instances will get the benefit.

Before:
=======

PRLogModuleInfo*
GetMyLog()
{
static PRLogModuleInfo* sLogModule = PR_NewLogModule("MyLog");
return sLogModule;
}

void Foo() { MOZ_LOG(GetMyLog(), ...); }

Now:
====

LazyLogModule sMyLogModule("MyLog");

void Foo() { MOZ_LOG(sMyLogModule, ...); }

The Future:
===========

We have already converted xpcom over to using it [2] and are quite happy with how things turned out.

This is where you come in dear reader!

Please switch over your PRLogModuleInfo instances to LazyLogModule. I have a tracking bug [3] for the overall code base and have split out bugs for smaller chunks. If you intend to help out just go ahead and assign yourself one of those bugs!

Addendum on Thread Safety
=========================

There are some common mistakes that TSan runs are tripping over, such as:

// Not-quite-right use of static initialization
GetLog() {
static* myLog = nullptr; // This is thread-safe on modern compilers
if (!myLog)
myLog = ... // This is not.
}

// Global, initialize wherever it's used
static PRLogModuleInfo* myLog;

Foo::Foo() {
if (!myLog)
myLog = ...
}

And to round it out, PR_NewLogModule is not thread-safe [4].

-e

[1] https://dxr.mozilla.org/mozilla-central/rev/fc706d376f0658e560a59c3dd520437b18e8c4a4/xpcom/base/Logging.h#100
[2] https://hg.mozilla.org/mozilla-central/rev/f9cf413cb3da
[3] https://bugzilla.mozilla.org/show_bug.cgi?id=1219461
[4] https://bugzilla.mozilla.org/show_bug.cgi?id=1073578
0 new messages