Skip to content

Commit

Permalink
Ensure that Logger and FileResolver are initialized for external threads
Browse files Browse the repository at this point in the history
  • Loading branch information
dvicini committed Sep 12, 2024
1 parent 14fcd90 commit 6b46f37
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/core/tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,58 @@
import mitsuba as mi
from threading import Thread


def test01_use_scoped_thread_environment(variant_scalar_rgb):
def use_scoped_set_thred_env(env):
def use_scoped_set_thread_env(env):
with mi.ScopedSetThreadEnvironment(environment):
mi.Log(mi.LogLevel.Info, "Log from a thread environment.")
mi.Log(mi.LogLevel.Info, 'Log from a thread environment.')

environment = mi.ThreadEnvironment()
thread = Thread(target = use_scoped_set_thred_env, args = (environment, ))
thread = Thread(target=use_scoped_set_thread_env, args=(environment, ))
thread.start()
thread.join()


def test02_log_from_new_thread(variant_scalar_rgb, tmp_path):

# We use a StreamAppender to capture the output.
log_path = tmp_path / 'log.txt'
appender = mi.StreamAppender(str(log_path))

log_str = 'Log from a thread environment.'

def print_to_log():
logger = mi.Thread.thread().logger()
assert logger is not None
logger.add_appender(appender)
mi.set_log_level(mi.LogLevel.Info)
mi.Log(mi.LogLevel.Info, log_str)

thread = Thread(target=print_to_log)
thread.start()
thread.join()

log = appender.read_log()
assert 'print_to_log' in log
assert log_str in log


def test03_access_file_resolver_from_new_thread(variant_scalar_rgb):
def access_fresolver():
fs = mi.Thread.thread().file_resolver()
assert fs is not None
fs.resolve('./some_path')
n_paths = len(fs)
fs.prepend('./some_folder')
fs.prepend('./some_folder2')
assert len(fs) == n_paths + 2

fs = mi.Thread.thread().file_resolver()
n_paths = len(fs)

thread = Thread(target=access_fresolver)
thread.start()
thread.join()

# The original file resolver remains unchanged.
assert len(fs) == n_paths
9 changes: 9 additions & 0 deletions src/core/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
NAMESPACE_BEGIN(mitsuba)

static size_t global_thread_count = 0;

static ref<Thread> main_thread = nullptr;
static thread_local ref<Thread> self = nullptr;
static std::atomic<uint32_t> thread_ctr { 0 };
#if defined(__linux__) || defined(__APPLE__)
Expand Down Expand Up @@ -476,6 +478,11 @@ bool Thread::register_external_thread(const std::string &prefix) {
self->d->running = true;
self->d->external_thread = true;

// An external thread will re-use the main thread's Logger (thread safe)
// and create a new FileResolver (since the FileResolver is not thread safe).
self->d->logger = main_thread->d->logger;
self->d->fresolver = new FileResolver();

const std::string &thread_name = self->name();
#if defined(__linux__)
pthread_setname_np(pthread_self(), thread_name.c_str());
Expand Down Expand Up @@ -520,6 +527,7 @@ void Thread::static_initialization() {
self = new MainThread();
self->d->running = true;
self->d->fresolver = new FileResolver();
main_thread = self;
}

void Thread::static_shutdown() {
Expand All @@ -529,6 +537,7 @@ void Thread::static_shutdown() {

thread()->d->running = false;
self = nullptr;
main_thread = nullptr;
#if defined(__linux__) || defined(__APPLE__)
pthread_key_delete(this_thread_id);
#endif
Expand Down

0 comments on commit 6b46f37

Please sign in to comment.