Skip to content

Commit 7d03474

Browse files
authored
Merge pull request #4023 from roystgnr/no_libmeshinit_fix
Fix error reporting without LibMeshInit in use
2 parents db32ead + d359533 commit 7d03474

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

include/base/libmesh_common.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,7 @@ struct casting_compare {
397397
do { \
398398
std::stringstream message_stream; \
399399
message_stream << msg << '\n'; \
400-
libMesh::Threads::lock_singleton_spin_mutex(); \
401400
libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
402-
libMesh::Threads::unlock_singleton_spin_mutex(); \
403401
LIBMESH_THROW(libMesh::LogicError(message_stream.str())); \
404402
} while (0)
405403

@@ -415,9 +413,9 @@ struct casting_compare {
415413
do { \
416414
libMesh::Threads::lock_singleton_spin_mutex(); \
417415
libMesh::err << msg << '\n'; \
416+
libMesh::Threads::unlock_singleton_spin_mutex(); \
418417
libmesh_try { libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME); } \
419418
libmesh_catch (...) {} \
420-
libMesh::Threads::unlock_singleton_spin_mutex(); \
421419
std::terminate(); \
422420
} while (0)
423421

@@ -427,9 +425,7 @@ struct casting_compare {
427425
do { \
428426
std::stringstream message_stream; \
429427
message_stream << msg << '\n'; \
430-
libMesh::Threads::lock_singleton_spin_mutex(); \
431428
libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
432-
libMesh::Threads::unlock_singleton_spin_mutex(); \
433429
LIBMESH_THROW(libMesh::NotImplemented(message_stream.str())); \
434430
} while (0)
435431

@@ -439,9 +435,7 @@ struct casting_compare {
439435
do { \
440436
std::stringstream message_stream; \
441437
message_stream << msg << '\n'; \
442-
libMesh::Threads::lock_singleton_spin_mutex(); \
443438
libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
444-
libMesh::Threads::unlock_singleton_spin_mutex(); \
445439
LIBMESH_THROW(libMesh::FileError(filename, message_stream.str())); \
446440
} while (0)
447441

src/base/libmesh.C

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,12 @@ std::vector<std::string> command_line_names()
948948

949949
bool on_command_line (std::string arg)
950950
{
951-
// Make sure the command line parser is ready for use
952-
libmesh_assert(command_line.get());
951+
// Make sure the command line parser is ready for use. If it's not,
952+
// then we'll have to treat the command line as empty, for maximum
953+
// compatibility with programs that don't use LibMeshInit but
954+
// indirectly (e.g. via error handling code) query the command line.
955+
if (!command_line.get())
956+
return false;
953957

954958
// Keep track of runtime queries, for later
955959
add_command_line_name(arg);

src/base/libmesh_common.C

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "libmesh/libmesh.h"
2121
#include "libmesh/libmesh_common.h"
2222
#include "libmesh/print_trace.h"
23+
#include "libmesh/threads.h"
2324

2425
// C/C++ includes
2526
#ifdef LIBMESH_HAVE_SYS_TYPES_H
@@ -66,8 +67,13 @@ void stop(const char * file, int line, const char * date, const char * time)
6667
}
6768

6869

70+
Threads::spin_mutex report_error_spin_mtx;
71+
6972
void report_error(const char * file, int line, const char * date, const char * time, std::ostream & os)
7073
{
74+
// Avoid a race condition on that static bool
75+
Threads::spin_mutex::scoped_lock lock(report_error_spin_mtx);
76+
7177
// It is possible to have an error *inside* report_error; e.g. from
7278
// print_trace. We don't want to infinitely recurse.
7379
static bool reporting_error = false;

0 commit comments

Comments
 (0)