Merge pull request #5421 from andralex/no-more-durn-stdiobase

Get rid of static this for initializing std{in,out,err}
merged-on-behalf-of: unknown
This commit is contained in:
The Dlang Bot 2017-06-01 20:57:41 +02:00 committed by GitHub
commit 76d3a4124c
6 changed files with 69 additions and 87 deletions

View file

@ -470,7 +470,6 @@ $(COMMENT
$(LINK2 std_regex_internal_parser.html, std.regex.internal.parser)$(BR) $(LINK2 std_regex_internal_parser.html, std.regex.internal.parser)$(BR)
$(LINK2 std_regex_internal_tests.html, std.regex.internal.tests)$(BR) $(LINK2 std_regex_internal_tests.html, std.regex.internal.tests)$(BR)
$(LINK2 std_regex_internal_thompson.html, std.regex.internal.thompson)$(BR) $(LINK2 std_regex_internal_thompson.html, std.regex.internal.thompson)$(BR)
$(LINK2 std_stdiobase.html, std.stdiobase)$(BR)
) )
$(TD $(TD
Internal modules. Internal modules.

View file

@ -227,7 +227,6 @@ EXTRA_MODULES_INTERNAL := $(addprefix std/, \
processinit scopebuffer test/dummyrange \ processinit scopebuffer test/dummyrange \
$(addprefix unicode_, comp decomp grapheme norm tables) \ $(addprefix unicode_, comp decomp grapheme norm tables) \
) \ ) \
stdiobase \
) )
EXTRA_MODULES += $(EXTRA_DOCUMENTABLES) $(EXTRA_MODULES_INTERNAL) EXTRA_MODULES += $(EXTRA_DOCUMENTABLES) $(EXTRA_MODULES_INTERNAL)

View file

@ -19,7 +19,6 @@ import std.algorithm.mutation; // copy
import std.meta; // allSatisfy import std.meta; // allSatisfy
import std.range.primitives; // ElementEncodingType, empty, front, import std.range.primitives; // ElementEncodingType, empty, front,
// isBidirectionalRange, isInputRange, put // isBidirectionalRange, isInputRange, put
import std.stdiobase;
import std.traits; // isSomeChar, isSomeString, Unqual, isPointer import std.traits; // isSomeChar, isSomeString, Unqual, isPointer
import std.typecons; // Flag import std.typecons; // Flag
@ -4522,33 +4521,45 @@ Initialize with a message and an error code.
} }
} }
extern(C) void std_stdio_static_this() // Undocumented but public because the std* handles are aliasing it.
ref File makeGlobal(alias handle)()
{ {
static import core.stdc.stdio; static __gshared File.Impl impl;
//Bind stdin, stdout, stderr static __gshared File result;
__gshared File.Impl stdinImpl;
stdinImpl.handle = core.stdc.stdio.stdin; // Use an inline spinlock to make sure the initializer is only run once.
.stdin._p = &stdinImpl; // We assume there will be at most uint.max / 2 threads trying to initialize
// stdout // `handle` at once and steal the high bit to indicate that the globals have
__gshared File.Impl stdoutImpl; // been initialized.
stdoutImpl.handle = core.stdc.stdio.stdout; static shared uint spinlock;
.stdout._p = &stdoutImpl; import core.atomic;
// stderr if (atomicLoad!(MemoryOrder.acq)(spinlock) <= uint.max / 2)
__gshared File.Impl stderrImpl; {
stderrImpl.handle = core.stdc.stdio.stderr; for (;;)
.stderr._p = &stderrImpl; {
if (atomicLoad!(MemoryOrder.acq)(spinlock) > uint.max / 2)
break;
if (atomicOp!"+="(spinlock, 1) == 1)
{
impl.handle = handle;
result._p = &impl;
atomicOp!"+="(spinlock, uint.max / 2);
break;
}
atomicOp!"-="(spinlock, 1);
}
}
return result;
} }
//---------
__gshared
{
/** The standard input stream. /** The standard input stream.
Bugs: Bugs:
Due to $(LINK2 https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768), Due to $(LINK2 https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
it is thread un-safe to reassign `stdin` to a different `File` instance it is thread un-safe to reassign `stdin` to a different `File` instance
than the default. than the default.
*/ */
File stdin; alias stdin = makeGlobal!(core.stdc.stdio.stdin);
/// ///
@safe unittest @safe unittest
{ {
@ -4575,7 +4586,8 @@ __gshared
it is thread un-safe to reassign `stdout` to a different `File` instance it is thread un-safe to reassign `stdout` to a different `File` instance
than the default. than the default.
*/ */
File stdout; alias stdout = makeGlobal!(core.stdc.stdio.stdout);
/** /**
The standard error stream. The standard error stream.
Bugs: Bugs:
@ -4583,8 +4595,7 @@ __gshared
it is thread un-safe to reassign `stderr` to a different `File` instance it is thread un-safe to reassign `stderr` to a different `File` instance
than the default. than the default.
*/ */
File stderr; alias stderr = makeGlobal!(core.stdc.stdio.stderr);
}
@system unittest @system unittest
{ {

View file

@ -1,24 +0,0 @@
// Written in the D programming language.
/**
* The only purpose of this module is to do the static construction for
* std.stdio, to eliminate cyclic construction errors.
*
* Copyright: Copyright Andrei Alexandrescu 2008 - 2009.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: $(HTTP erdani.org, Andrei Alexandrescu)
* Source: $(PHOBOSSRC std/_stdiobase.d)
*/
/* Copyright Andrei Alexandrescu 2008 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module std.stdiobase;
extern(C) void std_stdio_static_this();
shared static this()
{
std_stdio_static_this();
}

View file

@ -108,7 +108,6 @@ SRC= \
# The separation is a workaround for bug 4904 (optlink bug 3372). # The separation is a workaround for bug 4904 (optlink bug 3372).
SRC_STD_1= \ SRC_STD_1= \
std\stdio.d \ std\stdio.d \
std\stdiobase.d \
std\string.d \ std\string.d \
std\format.d \ std\format.d \
std\file.d std\file.d
@ -603,7 +602,6 @@ cov : $(SRC_TO_COMPILE) $(LIB)
# cov # cov
del *.lst del *.lst
$(DMD) -conf= -cov=83 -unittest -main -run std\stdio.d $(DMD) -conf= -cov=83 -unittest -main -run std\stdio.d
$(DMD) -conf= -cov=100 -unittest -main -run std\stdiobase.d
$(DMD) -conf= -cov=95 -unittest -main -run std\string.d $(DMD) -conf= -cov=95 -unittest -main -run std\string.d
$(DMD) -conf= -cov=71 -unittest -main -run std\format.d $(DMD) -conf= -cov=71 -unittest -main -run std\format.d
$(DMD) -conf= -cov=83 -unittest -main -run std\file.d $(DMD) -conf= -cov=83 -unittest -main -run std\file.d

View file

@ -112,7 +112,6 @@ SRC= \
# The separation is a workaround for bug 4904 (optlink bug 3372). # The separation is a workaround for bug 4904 (optlink bug 3372).
SRC_STD_1= \ SRC_STD_1= \
std\stdio.d \ std\stdio.d \
std\stdiobase.d \
std\string.d \ std\string.d \
std\format.d \ std\format.d \
std\file.d std\file.d