ICU-10865 Thread safety fixes to u_get_stdout().

X-SVN-Rev: 35693
This commit is contained in:
Andy Heninger 2014-05-06 21:52:26 +00:00
parent 6f97fd0a2a
commit a54177be4b
3 changed files with 19 additions and 11 deletions

View file

@ -252,7 +252,7 @@
</ClCompile>
<ClCompile Include="ufile.c" />
<ClCompile Include="ufmt_cmn.c" />
<ClCompile Include="uprintf.c" />
<ClCompile Include="uprintf.cpp" />
<ClCompile Include="uprntf_p.c" />
<ClCompile Include="uscanf.c" />
<ClCompile Include="uscanf_p.c" />

View file

@ -33,7 +33,7 @@
<ClCompile Include="ufmt_cmn.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="uprintf.c">
<ClCompile Include="uprintf.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="uprntf_p.c">

View file

@ -1,12 +1,12 @@
/*
******************************************************************************
*
* Copyright (C) 1998-2012, International Business Machines
* Copyright (C) 1998-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
*
* File uprintf.c
* File uprintf.cpp
*
* Modification History:
*
@ -28,14 +28,18 @@
#include "unicode/udat.h"
#include "unicode/putil.h"
#include "cmemory.h"
#include "locbund.h"
#include "mutex.h"
#include "uassert.h"
#include "uprintf.h"
#include "ufile.h"
#include "ucln_io.h"
#include "locbund.h"
#include "cmemory.h"
U_NAMESPACE_USE
static UFILE *gStdOut = NULL;
static UInitOnce gStdOutInitOnce = U_INITONCE_INITIALIZER;
static UBool U_CALLCONV uprintf_cleanup(void)
{
@ -43,16 +47,20 @@ static UBool U_CALLCONV uprintf_cleanup(void)
u_fclose(gStdOut);
gStdOut = NULL;
}
gStdOutInitOnce.reset();
return TRUE;
}
static void U_CALLCONV u_stdout_init() {
U_ASSERT(gStdOut == NULL);
gStdOut = u_finit(stdout, NULL, NULL);
ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
}
U_CAPI UFILE * U_EXPORT2
u_get_stdout()
{
if (gStdOut == NULL) {
gStdOut = u_finit(stdout, NULL, NULL);
ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
}
umtx_initOnce(gStdOutInitOnce, &u_stdout_init);
return gStdOut;
}
@ -161,7 +169,7 @@ u_vfprintf( UFILE *f,
int32_t count;
UChar *pattern;
UChar buffer[UFMT_DEFAULT_BUFFER_SIZE];
int32_t size = (int32_t)strlen(patternSpecification) + 1;
size_t size = strlen(patternSpecification) + 1;
/* convert from the default codepage to Unicode */
if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) {