diff options
author | Jonathan Marler <johnnymarler@gmail.com> | 2018-10-04 19:08:22 -0600 |
---|---|---|
committer | Jan Vorlicek <janvorli@microsoft.com> | 2018-10-05 03:08:22 +0200 |
commit | 7f59d17e2741e69bf878b8b168828de3a0a3d12e (patch) | |
tree | a201c6f5783035d3a0cb8b06a2f8f6ed0f48293f /src/pal | |
parent | 957eceed83cf635fc3146eadc448bf50efde5431 (diff) | |
download | coreclr-7f59d17e2741e69bf878b8b168828de3a0a3d12e.tar.gz coreclr-7f59d17e2741e69bf878b8b168828de3a0a3d12e.tar.bz2 coreclr-7f59d17e2741e69bf878b8b168828de3a0a3d12e.zip |
Synchronize access to static variable call_count (#20259)
call_count is a static variable that is being read/modified and written to without any synchronization. Fortunately, a critical section is already available that can be leveraged to synchronize access to it.
Diffstat (limited to 'src/pal')
-rw-r--r-- | src/pal/src/misc/dbgmsg.cpp | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/pal/src/misc/dbgmsg.cpp b/src/pal/src/misc/dbgmsg.cpp index 5eb5ebf9ba..a154cb4b1e 100644 --- a/src/pal/src/misc/dbgmsg.cpp +++ b/src/pal/src/misc/dbgmsg.cpp @@ -593,7 +593,7 @@ int DBG_printf_c99(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader, LPSTR buffer_ptr; INT output_size; va_list args; - static INT call_count=0; + static INT call_count=0; // only use inside the crit section void *thread_id; int old_errno = 0; CPalThread *pthrCurrent = InternalGetCurrentThread(); @@ -640,15 +640,20 @@ int DBG_printf_c99(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader, avoid holding a libc lock while another thread is calling SuspendThread on this one. */ + BOOL do_flush = FALSE; InternalEnterCriticalSection(pthrCurrent, &fprintf_crit_section); fprintf( output_file, "%s", buffer ); + call_count++; // can use call_count because we are in the crit section + if (call_count>5) + { + call_count = 0; + do_flush = TRUE; + } InternalLeaveCriticalSection(pthrCurrent, &fprintf_crit_section); /* flush the output to file every once in a while */ - call_count++; - if(call_count>5) + if (do_flush) { - call_count=0; if ( fflush(output_file) != 0 ) { fprintf(stderr, "ERROR : fflush() failed errno:%d (%s)\n", |