libdlog: initialise through attribute constructor 68/198868/3
authorMichal Bloch <m.bloch@samsung.com>
Fri, 25 Jan 2019 10:37:57 +0000 (11:37 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Mon, 4 Feb 2019 11:49:36 +0000 (11:49 +0000)
commita1ffe213e5c242a7e10e65e49cb50b6fae945ed8
tree9d71d7f605d0f29335a39148a24076e391a44dcb
parent7f1cff37d008c731363e85c565c2a2b7cab7ab28
libdlog: initialise through attribute constructor

The previous implementation suffered from the double-checked locking
anti-pattern. It is possible for writes to be reordered either by the
compiler OR the hardware (for example armv7l does this), potentially
setting the initialisation flag before having actually initialized
the rest of the library, meaning a parallel thread could see the flag
and try to use uninitialised variables.

Using the constructor attribute solves this neatly since it is done
before any parallel threads can do anything; even runtime invocation
through `dlopen()` is safe since in that case other threads won't be
able to access the pointer to `dlog_print` before `dlopen()` returns,
by which time the constructor must have finished.

Change-Id: Ic55a6d6858dc2112efd67338894a0c44b876fee8
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/libdlog/log.c
src/tests/libdlog_base.c