Remove closed function. Fix tuple receive in select.

From-SVN: r171440
This commit is contained in:
Ian Lance Taylor 2011-03-25 05:14:57 +00:00
parent e533b2a4c9
commit fe052134f6
18 changed files with 258 additions and 211 deletions

View File

@ -6530,7 +6530,6 @@ class Builtin_call_expression : public Call_expression
BUILTIN_APPEND, BUILTIN_APPEND,
BUILTIN_CAP, BUILTIN_CAP,
BUILTIN_CLOSE, BUILTIN_CLOSE,
BUILTIN_CLOSED,
BUILTIN_COMPLEX, BUILTIN_COMPLEX,
BUILTIN_COPY, BUILTIN_COPY,
BUILTIN_IMAG, BUILTIN_IMAG,
@ -6588,8 +6587,6 @@ Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
this->code_ = BUILTIN_CAP; this->code_ = BUILTIN_CAP;
else if (name == "close") else if (name == "close")
this->code_ = BUILTIN_CLOSE; this->code_ = BUILTIN_CLOSE;
else if (name == "closed")
this->code_ = BUILTIN_CLOSED;
else if (name == "complex") else if (name == "complex")
this->code_ = BUILTIN_COMPLEX; this->code_ = BUILTIN_COMPLEX;
else if (name == "copy") else if (name == "copy")
@ -7185,9 +7182,6 @@ Builtin_call_expression::do_type()
case BUILTIN_PRINTLN: case BUILTIN_PRINTLN:
return Type::make_void_type(); return Type::make_void_type();
case BUILTIN_CLOSED:
return Type::lookup_bool_type();
case BUILTIN_RECOVER: case BUILTIN_RECOVER:
return Type::make_interface_type(NULL, BUILTINS_LOCATION); return Type::make_interface_type(NULL, BUILTINS_LOCATION);
@ -7451,7 +7445,6 @@ Builtin_call_expression::do_check_types(Gogo*)
break; break;
case BUILTIN_CLOSE: case BUILTIN_CLOSE:
case BUILTIN_CLOSED:
if (this->check_one_arg()) if (this->check_one_arg())
{ {
if (this->one_arg()->type()->channel_type() == NULL) if (this->one_arg()->type()->channel_type() == NULL)
@ -7936,7 +7929,6 @@ Builtin_call_expression::do_get_tree(Translate_context* context)
} }
case BUILTIN_CLOSE: case BUILTIN_CLOSE:
case BUILTIN_CLOSED:
{ {
const Expression_list* args = this->args(); const Expression_list* args = this->args();
gcc_assert(args != NULL && args->size() == 1); gcc_assert(args != NULL && args->size() == 1);
@ -7944,8 +7936,6 @@ Builtin_call_expression::do_get_tree(Translate_context* context)
tree arg_tree = arg->get_tree(context); tree arg_tree = arg->get_tree(context);
if (arg_tree == error_mark_node) if (arg_tree == error_mark_node)
return error_mark_node; return error_mark_node;
if (this->code_ == BUILTIN_CLOSE)
{
static tree close_fndecl; static tree close_fndecl;
return Gogo::call_builtin(&close_fndecl, return Gogo::call_builtin(&close_fndecl,
location, location,
@ -7955,18 +7945,6 @@ Builtin_call_expression::do_get_tree(Translate_context* context)
TREE_TYPE(arg_tree), TREE_TYPE(arg_tree),
arg_tree); arg_tree);
} }
else
{
static tree closed_fndecl;
return Gogo::call_builtin(&closed_fndecl,
location,
"__go_builtin_closed",
1,
boolean_type_node,
TREE_TYPE(arg_tree),
arg_tree);
}
}
case BUILTIN_SIZEOF: case BUILTIN_SIZEOF:
case BUILTIN_OFFSETOF: case BUILTIN_OFFSETOF:

View File

@ -173,15 +173,6 @@ Gogo::Gogo(int int_type_size, int pointer_size)
close_type->set_is_builtin(); close_type->set_is_builtin();
this->globals_->add_function_declaration("close", NULL, close_type, loc); this->globals_->add_function_declaration("close", NULL, close_type, loc);
Typed_identifier_list* closed_result = new Typed_identifier_list();
closed_result->push_back(Typed_identifier("", Type::lookup_bool_type(),
loc));
Function_type* closed_type = Type::make_function_type(NULL, NULL,
closed_result, loc);
closed_type->set_is_varargs();
closed_type->set_is_builtin();
this->globals_->add_function_declaration("closed", NULL, closed_type, loc);
Typed_identifier_list* copy_result = new Typed_identifier_list(); Typed_identifier_list* copy_result = new Typed_identifier_list();
copy_result->push_back(Typed_identifier("", int_type, loc)); copy_result->push_back(Typed_identifier("", int_type, loc));
Function_type* copy_type = Type::make_function_type(NULL, NULL, Function_type* copy_type = Type::make_function_type(NULL, NULL,
@ -3506,12 +3497,15 @@ Variable::determine_type()
true); true);
this->init_ = NULL; this->init_ = NULL;
} }
else if (this->type_from_chan_element_)
{
Expression* init = this->init_;
init->determine_type_no_context();
this->type_ = this->type_from_chan_element(init, true);
this->init_ = NULL;
}
else else
{ {
// type_from_chan_element_ should have been cleared during
// lowering.
gcc_assert(!this->type_from_chan_element_);
Type_context context(this->type_, false); Type_context context(this->type_, false);
this->init_->determine_type(&context); this->init_->determine_type(&context);
if (this->type_ == NULL) if (this->type_ == NULL)

View File

@ -1717,6 +1717,7 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
Statement* s = Statement::make_tuple_receive_assignment(val_var, Statement* s = Statement::make_tuple_receive_assignment(val_var,
received_var, received_var,
receive->channel(), receive->channel(),
false,
location); location);
if (!this->gogo_->in_global_scope()) if (!this->gogo_->in_global_scope())
@ -3629,6 +3630,7 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
Expression* channel = receive->channel(); Expression* channel = receive->channel();
Statement* s = Statement::make_tuple_receive_assignment(val, success, Statement* s = Statement::make_tuple_receive_assignment(val, success,
channel, channel,
false,
location); location);
this->gogo_->add_statement(s); this->gogo_->add_statement(s);
} }

View File

@ -1148,10 +1148,10 @@ class Tuple_receive_assignment_statement : public Statement
{ {
public: public:
Tuple_receive_assignment_statement(Expression* val, Expression* closed, Tuple_receive_assignment_statement(Expression* val, Expression* closed,
Expression* channel, Expression* channel, bool for_select,
source_location location) source_location location)
: Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location), : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
val_(val), closed_(closed), channel_(channel) val_(val), closed_(closed), channel_(channel), for_select_(for_select)
{ } { }
protected: protected:
@ -1176,6 +1176,8 @@ class Tuple_receive_assignment_statement : public Statement
Expression* closed_; Expression* closed_;
// The channel on which we receive the value. // The channel on which we receive the value.
Expression* channel_; Expression* channel_;
// Whether this is for a select statement.
bool for_select_;
}; };
// Traversal. // Traversal.
@ -1228,6 +1230,7 @@ Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
b->add_statement(closed_temp); b->add_statement(closed_temp);
// func chanrecv2(c chan T, val *T) bool // func chanrecv2(c chan T, val *T) bool
// func chanrecv3(c chan T, val *T) bool (if for_select)
source_location bloc = BUILTINS_LOCATION; source_location bloc = BUILTINS_LOCATION;
Typed_identifier_list* param_types = new Typed_identifier_list(); Typed_identifier_list* param_types = new Typed_identifier_list();
param_types->push_back(Typed_identifier("c", channel_type, bloc)); param_types->push_back(Typed_identifier("c", channel_type, bloc));
@ -1239,12 +1242,22 @@ Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
Function_type* fntype = Type::make_function_type(NULL, param_types, Function_type* fntype = Type::make_function_type(NULL, param_types,
ret_types, bloc); ret_types, bloc);
Named_object* chanrecv2 = Named_object* chanrecv;
Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc); if (!this->for_select_)
chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2"); {
chanrecv = Named_object::make_function_declaration("chanrecv2", NULL,
fntype, bloc);
chanrecv->func_declaration_value()->set_asm_name("runtime.chanrecv2");
}
else
{
chanrecv = Named_object::make_function_declaration("chanrecv3", NULL,
fntype, bloc);
chanrecv->func_declaration_value()->set_asm_name("runtime.chanrecv3");
}
// closed_temp = chanrecv2(channel, &val_temp) // closed_temp = chanrecv[23](channel, &val_temp)
Expression* func = Expression::make_func_reference(chanrecv2, NULL, loc); Expression* func = Expression::make_func_reference(chanrecv, NULL, loc);
Expression_list* params = new Expression_list(); Expression_list* params = new Expression_list();
params->push_back(this->channel_); params->push_back(this->channel_);
Expression* ref = Expression::make_temporary_reference(val_temp, loc); Expression* ref = Expression::make_temporary_reference(val_temp, loc);
@ -1272,10 +1285,11 @@ Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
Statement* Statement*
Statement::make_tuple_receive_assignment(Expression* val, Expression* closed, Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
Expression* channel, Expression* channel,
bool for_select,
source_location location) source_location location)
{ {
return new Tuple_receive_assignment_statement(val, closed, channel, return new Tuple_receive_assignment_statement(val, closed, channel,
location); for_select, location);
} }
// An assignment to a pair of values from a type guard. This is a // An assignment to a pair of values from a type guard. This is a
@ -4151,7 +4165,7 @@ Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
this->val_ = Expression::make_sink(loc); this->val_ = Expression::make_sink(loc);
Statement* s = Statement::make_tuple_receive_assignment(this->val_, Statement* s = Statement::make_tuple_receive_assignment(this->val_,
this->closed_, this->closed_,
ref, loc); ref, true, loc);
init->add_statement(s); init->add_statement(s);
} }
else if (this->closedvar_ != NULL) else if (this->closedvar_ != NULL)
@ -4165,8 +4179,14 @@ Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
Expression* closed = Expression::make_var_reference(this->closedvar_, Expression* closed = Expression::make_var_reference(this->closedvar_,
loc); loc);
Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref, Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
loc); true, loc);
init->add_statement(s); // We have to put S in STATEMENTS_, because that is where the
// variables are declared.
gcc_assert(this->statements_ != NULL);
this->statements_->add_statement_at_front(s);
// We have to lower STATEMENTS_ again, to lower the tuple
// receive assignment we just added.
gogo->lower_block(function, this->statements_);
} }
else else
{ {
@ -5281,7 +5301,7 @@ For_range_statement::lower_range_map(Gogo* gogo,
// Lower a for range over a channel. // Lower a for range over a channel.
void void
For_range_statement::lower_range_channel(Gogo* gogo, For_range_statement::lower_range_channel(Gogo*,
Block*, Block*,
Block* body_block, Block* body_block,
Named_object* range_object, Named_object* range_object,
@ -5299,12 +5319,11 @@ For_range_statement::lower_range_channel(Gogo* gogo,
// The loop we generate: // The loop we generate:
// for { // for {
// index_temp = <-range // index_temp, ok_temp = <-range
// if closed(range) { // if !ok_temp {
// break // break
// } // }
// index = index_temp // index = index_temp
// value = value_temp
// original body // original body
// } // }
@ -5315,26 +5334,30 @@ For_range_statement::lower_range_channel(Gogo* gogo,
*ppost = NULL; *ppost = NULL;
// Set *PITER_INIT to // Set *PITER_INIT to
// index_temp = <-range // index_temp, ok_temp = <-range
// if closed(range) { // if !ok_temp {
// break // break
// } // }
Block* iter_init = new Block(body_block, loc); Block* iter_init = new Block(body_block, loc);
Expression* ref = this->make_range_ref(range_object, range_temp, loc); Temporary_statement* ok_temp =
Expression* cond = this->call_builtin(gogo, "closed", ref, loc); Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
iter_init->add_statement(ok_temp);
ref = this->make_range_ref(range_object, range_temp, loc); Expression* cref = this->make_range_ref(range_object, range_temp, loc);
Expression* recv = Expression::make_receive(ref, loc); Expression* iref = Expression::make_temporary_reference(index_temp, loc);
ref = Expression::make_temporary_reference(index_temp, loc); Expression* oref = Expression::make_temporary_reference(ok_temp, loc);
Statement* s = Statement::make_assignment(ref, recv, loc); Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
false, loc);
iter_init->add_statement(s); iter_init->add_statement(s);
Block* then_block = new Block(iter_init, loc); Block* then_block = new Block(iter_init, loc);
s = Statement::make_break_statement(this->break_label(), loc); s = Statement::make_break_statement(this->break_label(), loc);
then_block->add_statement(s); then_block->add_statement(s);
oref = Expression::make_temporary_reference(ok_temp, loc);
Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
s = Statement::make_if_statement(cond, then_block, NULL, loc); s = Statement::make_if_statement(cond, then_block, NULL, loc);
iter_init->add_statement(s); iter_init->add_statement(s);

View File

@ -160,10 +160,12 @@ class Statement
Expression* should_set, source_location); Expression* should_set, source_location);
// Make an assignment from a nonblocking receive to a pair of // Make an assignment from a nonblocking receive to a pair of
// variables. // variables. FOR_SELECT is true is this is being created for a
// case x, ok := <-c in a select statement.
static Statement* static Statement*
make_tuple_receive_assignment(Expression* val, Expression* closed, make_tuple_receive_assignment(Expression* val, Expression* closed,
Expression* channel, source_location); Expression* channel, bool for_select,
source_location);
// Make an assignment from a type guard to a pair of variables. // Make an assignment from a type guard to a pair of variables.
static Statement* static Statement*

View File

@ -21,6 +21,8 @@ var iterations *int = flag.Int("n", 100000, "number of iterations")
func sender(n int, c1, c2, c3, c4 chan<- int) { func sender(n int, c1, c2, c3, c4 chan<- int) {
defer close(c1) defer close(c1)
defer close(c2) defer close(c2)
defer close(c3)
defer close(c4)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
select { select {
@ -35,26 +37,18 @@ func sender(n int, c1, c2, c3, c4 chan<- int) {
// mux receives the values from sender and forwards them onto another channel. // mux receives the values from sender and forwards them onto another channel.
// It would be simplier to just have sender's four cases all be the same // It would be simplier to just have sender's four cases all be the same
// channel, but this doesn't actually trigger the bug. // channel, but this doesn't actually trigger the bug.
func mux(out chan<- int, in <-chan int) { func mux(out chan<- int, in <-chan int, done chan<- bool) {
for { for v := range in {
v := <-in
if closed(in) {
close(out)
break
}
out <- v out <- v
} }
done <- true
} }
// recver gets a steam of values from the four mux's and checks for duplicates. // recver gets a steam of values from the four mux's and checks for duplicates.
func recver(in <-chan int) { func recver(in <-chan int) {
seen := make(map[int]bool) seen := make(map[int]bool)
for { for v := range in {
v := <-in
if closed(in) {
break
}
if _, ok := seen[v]; ok { if _, ok := seen[v]; ok {
println("got duplicate value: ", v) println("got duplicate value: ", v)
panic("fail") panic("fail")
@ -70,15 +64,23 @@ func main() {
c2 := make(chan int) c2 := make(chan int)
c3 := make(chan int) c3 := make(chan int)
c4 := make(chan int) c4 := make(chan int)
done := make(chan bool)
cmux := make(chan int) cmux := make(chan int)
go sender(*iterations, c1, c2, c3, c4) go sender(*iterations, c1, c2, c3, c4)
go mux(cmux, c1) go mux(cmux, c1, done)
go mux(cmux, c2) go mux(cmux, c2, done)
go mux(cmux, c3) go mux(cmux, c3, done)
go mux(cmux, c4) go mux(cmux, c4, done)
go func() {
<-done
<-done
<-done
<-done
close(cmux)
}()
// We keep the recver because it might catch more bugs in the future. // We keep the recver because it might catch more bugs in the future.
// However, the result of the bug linked to at the top is that we'll // However, the result of the bug linked to at the top is that we'll
// end up panicing with: "throw: bad g->status in ready". // end up panicking with: "throw: bad g->status in ready".
recver(cmux) recver(cmux)
print("PASS\n") print("PASS\n")
} }

View File

@ -22,21 +22,18 @@ func main() {
c <- 0 // ok c <- 0 // ok
<-c // ok <-c // ok
//TODO(rsc): uncomment when this syntax is valid for receive+check closed x, ok := <-c // ok
// x, ok := <-c // ok _, _ = x, ok
// _, _ = x, ok
cr <- 0 // ERROR "send" cr <- 0 // ERROR "send"
<-cr // ok <-cr // ok
//TODO(rsc): uncomment when this syntax is valid for receive+check closed x, ok = <-cr // ok
// x, ok = <-cr // ok _, _ = x, ok
// _, _ = x, ok
cs <- 0 // ok cs <- 0 // ok
<-cs // ERROR "receive" <-cs // ERROR "receive"
////TODO(rsc): uncomment when this syntax is valid for receive+check closed x, ok = <-cs // ERROR "receive"
//// x, ok = <-cs // ERROR "receive" _, _ = x, ok
//// _, _ = x, ok
select { select {
case c <- 0: // ok case c <- 0: // ok

View File

@ -88,12 +88,16 @@ func main() {
ch <- 7 ch <- 7
}) })
// receiving (a small number of times) from a closed channel never blocks // receiving from a closed channel never blocks
testBlock(never, func() { testBlock(never, func() {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
if <-closedch != 0 { if <-closedch != 0 {
panic("expected zero value when reading from closed channel") panic("expected zero value when reading from closed channel")
} }
if x, ok := <-closedch; x != 0 || ok {
println("closedch:", x, ok)
panic("expected 0, false from closed channel")
}
} }
}) })
@ -191,9 +195,30 @@ func main() {
case <-closedch: case <-closedch:
} }
}) })
testBlock(never, func() {
select {
case x := <-closedch:
_ = x
}
})
testBlock(never, func() {
select {
case x, ok := <-closedch:
_, _ = x, ok
}
})
testPanic(always, func() { testPanic(always, func() {
select { select {
case closedch <- 7: case closedch <- 7:
} }
}) })
// select should not get confused if it sees itself
testBlock(always, func() {
c := make(chan int)
select {
case c <- 1:
case <-c:
}
})
} }

View File

@ -4,7 +4,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Test close(c), closed(c). // Test close(c), receive of closed channel.
// //
// TODO(rsc): Doesn't check behavior of close(c) when there // TODO(rsc): Doesn't check behavior of close(c) when there
// are blocked senders/receivers. // are blocked senders/receivers.
@ -14,10 +14,11 @@ package main
type Chan interface { type Chan interface {
Send(int) Send(int)
Nbsend(int) bool Nbsend(int) bool
Recv() int Recv() (int)
Nbrecv() (int, bool) Nbrecv() (int, bool)
Recv2() (int, bool)
Nbrecv2() (int, bool, bool)
Close() Close()
Closed() bool
Impl() string Impl() string
} }
@ -52,12 +53,23 @@ func (c XChan) Nbrecv() (int, bool) {
panic("nbrecv") panic("nbrecv")
} }
func (c XChan) Close() { func (c XChan) Recv2() (int, bool) {
close(c) x, ok := <-c
return x, ok
} }
func (c XChan) Closed() bool { func (c XChan) Nbrecv2() (int, bool, bool) {
return closed(c) select {
case x, ok := <-c:
return x, ok, true
default:
return 0, false, false
}
panic("nbrecv2")
}
func (c XChan) Close() {
close(c)
} }
func (c XChan) Impl() string { func (c XChan) Impl() string {
@ -101,12 +113,26 @@ func (c SChan) Nbrecv() (int, bool) {
panic("nbrecv") panic("nbrecv")
} }
func (c SChan) Close() { func (c SChan) Recv2() (int, bool) {
close(c) select {
case x, ok := <-c:
return x, ok
}
panic("recv")
} }
func (c SChan) Closed() bool { func (c SChan) Nbrecv2() (int, bool, bool) {
return closed(c) select {
default:
return 0, false, false
case x, ok := <-c:
return x, ok, true
}
panic("nbrecv")
}
func (c SChan) Close() {
close(c)
} }
func (c SChan) Impl() string { func (c SChan) Impl() string {
@ -156,12 +182,28 @@ func (c SSChan) Nbrecv() (int, bool) {
panic("nbrecv") panic("nbrecv")
} }
func (c SSChan) Close() { func (c SSChan) Recv2() (int, bool) {
close(c) select {
case <-dummy:
case x, ok := <-c:
return x, ok
}
panic("recv")
} }
func (c SSChan) Closed() bool { func (c SSChan) Nbrecv2() (int, bool, bool) {
return closed(c) select {
case <-dummy:
default:
return 0, false, false
case x, ok := <-c:
return x, ok, true
}
panic("nbrecv")
}
func (c SSChan) Close() {
close(c)
} }
func (c SSChan) Impl() string { func (c SSChan) Impl() string {
@ -179,29 +221,23 @@ func shouldPanic(f func()) {
} }
func test1(c Chan) { func test1(c Chan) {
// not closed until the close signal (a zero value) has been received.
if c.Closed() {
println("test1: Closed before Recv zero:", c.Impl())
}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
// recv a close signal (a zero value) // recv a close signal (a zero value)
if x := c.Recv(); x != 0 { if x := c.Recv(); x != 0 {
println("test1: recv on closed got non-zero:", x, c.Impl()) println("test1: recv on closed:", x, c.Impl())
}
if x, ok := c.Recv2(); x != 0 || ok {
println("test1: recv2 on closed:", x, ok, c.Impl())
} }
// should now be closed. // should work with select: received a value without blocking, so selected == true.
if !c.Closed() { x, selected := c.Nbrecv()
println("test1: not closed after recv zero", c.Impl()) if x != 0 || !selected {
println("test1: recv on closed nb:", x, selected, c.Impl())
} }
x, ok, selected := c.Nbrecv2()
// should work with ,ok: received a value without blocking, so ok == true. if x != 0 || ok || !selected {
x, ok := c.Nbrecv() println("test1: recv2 on closed nb:", x, ok, selected, c.Impl())
if !ok {
println("test1: recv on closed got not ok", c.Impl())
}
if x != 0 {
println("test1: recv ,ok on closed got non-zero:", x, c.Impl())
} }
} }
@ -221,11 +257,6 @@ func test1(c Chan) {
} }
func testasync1(c Chan) { func testasync1(c Chan) {
// not closed until the close signal (a zero value) has been received.
if c.Closed() {
println("testasync1: Closed before Recv zero:", c.Impl())
}
// should be able to get the last value via Recv // should be able to get the last value via Recv
if x := c.Recv(); x != 1 { if x := c.Recv(); x != 1 {
println("testasync1: Recv did not get 1:", x, c.Impl()) println("testasync1: Recv did not get 1:", x, c.Impl())
@ -235,19 +266,31 @@ func testasync1(c Chan) {
} }
func testasync2(c Chan) { func testasync2(c Chan) {
// not closed until the close signal (a zero value) has been received. // should be able to get the last value via Recv2
if c.Closed() { if x, ok := c.Recv2(); x != 1 || !ok {
println("testasync2: Closed before Recv zero:", c.Impl()) println("testasync1: Recv did not get 1, true:", x, ok, c.Impl())
} }
test1(c)
}
func testasync3(c Chan) {
// should be able to get the last value via Nbrecv // should be able to get the last value via Nbrecv
if x, ok := c.Nbrecv(); !ok || x != 1 { if x, selected := c.Nbrecv(); x != 1 || !selected {
println("testasync2: Nbrecv did not get 1, true:", x, ok, c.Impl()) println("testasync2: Nbrecv did not get 1, true:", x, selected, c.Impl())
} }
test1(c) test1(c)
} }
func testasync4(c Chan) {
// should be able to get the last value via Nbrecv2
if x, ok, selected := c.Nbrecv2(); x != 1 || !ok || !selected {
println("testasync2: Nbrecv did not get 1, true, true:", x, ok, selected, c.Impl())
}
test1(c)
}
func closedsync() chan int { func closedsync() chan int {
c := make(chan int) c := make(chan int)
close(c) close(c)
@ -261,15 +304,27 @@ func closedasync() chan int {
return c return c
} }
func main() { var mks = []func(chan int) Chan {
test1(XChan(closedsync())) func(c chan int) Chan { return XChan(c) },
test1(SChan(closedsync())) func(c chan int) Chan { return SChan(c) },
test1(SSChan(closedsync())) func(c chan int) Chan { return SSChan(c) },
}
testasync1(XChan(closedasync()))
testasync1(SChan(closedasync())) var testcloseds = []func(Chan) {
testasync1(SSChan(closedasync())) testasync1,
testasync2(XChan(closedasync())) testasync2,
testasync2(SChan(closedasync())) testasync3,
testasync2(SSChan(closedasync())) testasync4,
}
func main() {
for _, mk := range mks {
test1(mk(closedsync()))
}
for _, testclosed := range testcloseds {
for _, mk := range mks {
testclosed(mk(closedasync()))
}
}
} }

View File

@ -43,10 +43,6 @@ func main() {
_, b = m[2] // ERROR "cannot .* bool.*type Bool" _, b = m[2] // ERROR "cannot .* bool.*type Bool"
m[2] = 1, b // ERROR "cannot use.*type Bool.*as type bool" m[2] = 1, b // ERROR "cannot use.*type Bool.*as type bool"
////TODO(rsc): uncomment when this syntax is valid for receive+check closed
//// _, b = <-c // ERROR "cannot .* bool.*type Bool"
//// _ = b
var inter interface{} var inter interface{}
_, b = inter.(Map) // ERROR "cannot .* bool.*type Bool" _, b = inter.(Map) // ERROR "cannot .* bool.*type Bool"
_ = b _ = b
@ -57,8 +53,9 @@ func main() {
_, b = minter.(Map) // ERROR "cannot .* bool.*type Bool" _, b = minter.(Map) // ERROR "cannot .* bool.*type Bool"
_ = b _ = b
asBool(closed(c)) // ERROR "cannot use.*type bool.*as type Bool" _, bb := <-c
b = closed(c) // ERROR "cannot use.*type bool.*type Bool" asBool(bb) // ERROR "cannot use.*type bool.*as type Bool"
_, b = <-c // ERROR "cannot .* bool.*type Bool"
_ = b _ = b
asString(String(slice)) // ERROR "cannot .*type Slice.*type String" asString(String(slice)) // ERROR "cannot .*type Slice.*type String"

View File

@ -358,7 +358,6 @@ runtime_files = \
runtime/go-chan-len.c \ runtime/go-chan-len.c \
runtime/go-check-interface.c \ runtime/go-check-interface.c \
runtime/go-close.c \ runtime/go-close.c \
runtime/go-closed.c \
runtime/go-construct-map.c \ runtime/go-construct-map.c \
runtime/go-convert-interface.c \ runtime/go-convert-interface.c \
runtime/go-copy.c \ runtime/go-copy.c \

View File

@ -174,11 +174,11 @@ am__libgo_la_SOURCES_DIST = runtime/go-append.c runtime/go-assert.c \
runtime/go-caller.c runtime/go-can-convert-interface.c \ runtime/go-caller.c runtime/go-can-convert-interface.c \
runtime/go-cgo.c runtime/go-chan-cap.c runtime/go-chan-len.c \ runtime/go-cgo.c runtime/go-chan-cap.c runtime/go-chan-len.c \
runtime/go-check-interface.c runtime/go-close.c \ runtime/go-check-interface.c runtime/go-close.c \
runtime/go-closed.c runtime/go-construct-map.c \ runtime/go-construct-map.c runtime/go-convert-interface.c \
runtime/go-convert-interface.c runtime/go-copy.c \ runtime/go-copy.c runtime/go-defer.c \
runtime/go-defer.c runtime/go-deferred-recover.c \ runtime/go-deferred-recover.c runtime/go-eface-compare.c \
runtime/go-eface-compare.c runtime/go-eface-val-compare.c \ runtime/go-eface-val-compare.c runtime/go-getgoroot.c \
runtime/go-getgoroot.c runtime/go-go.c runtime/go-gomaxprocs.c \ runtime/go-go.c runtime/go-gomaxprocs.c \
runtime/go-int-array-to-string.c runtime/go-int-to-string.c \ runtime/go-int-array-to-string.c runtime/go-int-to-string.c \
runtime/go-interface-compare.c \ runtime/go-interface-compare.c \
runtime/go-interface-eface-compare.c \ runtime/go-interface-eface-compare.c \
@ -219,7 +219,7 @@ am__libgo_la_SOURCES_DIST = runtime/go-append.c runtime/go-assert.c \
am__objects_3 = go-append.lo go-assert.lo go-assert-interface.lo \ am__objects_3 = go-append.lo go-assert.lo go-assert-interface.lo \
go-byte-array-to-string.lo go-breakpoint.lo go-caller.lo \ go-byte-array-to-string.lo go-breakpoint.lo go-caller.lo \
go-can-convert-interface.lo go-cgo.lo go-chan-cap.lo \ go-can-convert-interface.lo go-cgo.lo go-chan-cap.lo \
go-chan-len.lo go-check-interface.lo go-close.lo go-closed.lo \ go-chan-len.lo go-check-interface.lo go-close.lo \
go-construct-map.lo go-convert-interface.lo go-copy.lo \ go-construct-map.lo go-convert-interface.lo go-copy.lo \
go-defer.lo go-deferred-recover.lo go-eface-compare.lo \ go-defer.lo go-deferred-recover.lo go-eface-compare.lo \
go-eface-val-compare.lo go-getgoroot.lo go-go.lo \ go-eface-val-compare.lo go-getgoroot.lo go-go.lo \
@ -771,7 +771,6 @@ runtime_files = \
runtime/go-chan-len.c \ runtime/go-chan-len.c \
runtime/go-check-interface.c \ runtime/go-check-interface.c \
runtime/go-close.c \ runtime/go-close.c \
runtime/go-closed.c \
runtime/go-construct-map.c \ runtime/go-construct-map.c \
runtime/go-convert-interface.c \ runtime/go-convert-interface.c \
runtime/go-copy.c \ runtime/go-copy.c \
@ -2177,7 +2176,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-chan-len.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-chan-len.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-check-interface.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-check-interface.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-close.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-close.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-closed.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-construct-map.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-construct-map.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-convert-interface.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-convert-interface.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-copy.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-copy.Plo@am__quote@
@ -2383,13 +2381,6 @@ go-close.lo: runtime/go-close.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o go-close.lo `test -f 'runtime/go-close.c' || echo '$(srcdir)/'`runtime/go-close.c @am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o go-close.lo `test -f 'runtime/go-close.c' || echo '$(srcdir)/'`runtime/go-close.c
go-closed.lo: runtime/go-closed.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT go-closed.lo -MD -MP -MF $(DEPDIR)/go-closed.Tpo -c -o go-closed.lo `test -f 'runtime/go-closed.c' || echo '$(srcdir)/'`runtime/go-closed.c
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/go-closed.Tpo $(DEPDIR)/go-closed.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='runtime/go-closed.c' object='go-closed.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o go-closed.lo `test -f 'runtime/go-closed.c' || echo '$(srcdir)/'`runtime/go-closed.c
go-construct-map.lo: runtime/go-construct-map.c go-construct-map.lo: runtime/go-construct-map.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT go-construct-map.lo -MD -MP -MF $(DEPDIR)/go-construct-map.Tpo -c -o go-construct-map.lo `test -f 'runtime/go-construct-map.c' || echo '$(srcdir)/'`runtime/go-construct-map.c @am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT go-construct-map.lo -MD -MP -MF $(DEPDIR)/go-construct-map.Tpo -c -o go-construct-map.lo `test -f 'runtime/go-construct-map.c' || echo '$(srcdir)/'`runtime/go-construct-map.c
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/go-construct-map.Tpo $(DEPDIR)/go-construct-map.Plo @am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/go-construct-map.Tpo $(DEPDIR)/go-construct-map.Plo

View File

@ -10,7 +10,7 @@ typedef _Bool bool;
typedef unsigned char byte; typedef unsigned char byte;
typedef struct __go_channel chan; typedef struct __go_channel chan;
/* Do a nonblocking channel receive. */ /* Do a channel receive with closed status. */
func chanrecv2(c *chan, val *byte) (received bool) { func chanrecv2(c *chan, val *byte) (received bool) {
if (c->element_size > 8) { if (c->element_size > 8) {
@ -31,3 +31,25 @@ func chanrecv2(c *chan, val *byte) (received bool) {
return received; return received;
} }
} }
/* Do a channel receive with closed status for a select statement. */
func chanrecv3(c *chan, val *byte) (received bool) {
if (c->element_size > 8) {
return __go_receive_big(c, val, 1);
} else {
union {
char b[8];
uint64_t v;
} u;
u.v = __go_receive_small_closed(c, 1, &received);
#ifndef WORDS_BIGENDIAN
__builtin_memcpy(val, u.b, c->element_size);
#else
__builtin_memcpy(val, u.b + 8 - c->element_size,
c->element_size);
#endif
return received;
}
}

View File

@ -50,9 +50,6 @@ struct __go_channel
_Bool selected_for_receive; _Bool selected_for_receive;
/* True if this channel has been closed. */ /* True if this channel has been closed. */
_Bool is_closed; _Bool is_closed;
/* True if at least one null value has been read from a closed
channel. */
_Bool saw_close;
/* The list of select statements waiting to send on a synchronous /* The list of select statements waiting to send on a synchronous
channel. */ channel. */
struct __go_channel_select *select_send_queue; struct __go_channel_select *select_send_queue;

View File

@ -1,34 +0,0 @@
/* go-closed.c -- the builtin closed function.
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file. */
#include "go-assert.h"
#include "channel.h"
/* Return whether a channel is closed. We only return true after at
least one nil value has been read from the channel. */
_Bool
__go_builtin_closed (struct __go_channel *channel)
{
int i;
_Bool ret;
i = pthread_mutex_lock (&channel->lock);
__go_assert (i == 0);
while (channel->selected_for_receive)
{
i = pthread_cond_wait (&channel->cond, &channel->lock);
__go_assert (i == 0);
}
ret = channel->saw_close;
i = pthread_mutex_unlock (&channel->lock);
__go_assert (i == 0);
return ret;
}

View File

@ -44,7 +44,6 @@ __go_new_channel (size_t element_size, size_t entries)
ret->selected_for_send = 0; ret->selected_for_send = 0;
ret->selected_for_receive = 0; ret->selected_for_receive = 0;
ret->is_closed = 0; ret->is_closed = 0;
ret->saw_close = 0;
ret->select_send_queue = NULL; ret->select_send_queue = NULL;
ret->select_receive_queue = NULL; ret->select_receive_queue = NULL;
ret->select_mutex = NULL; ret->select_mutex = NULL;

View File

@ -32,7 +32,6 @@ __go_receive_nonblocking_acquire (struct __go_channel *channel)
? channel->next_store == 0 ? channel->next_store == 0
: channel->next_fetch == channel->next_store)) : channel->next_fetch == channel->next_store))
{ {
channel->saw_close = 1;
__go_unlock_and_notify_selects (channel); __go_unlock_and_notify_selects (channel);
return RECEIVE_NONBLOCKING_ACQUIRE_CLOSED; return RECEIVE_NONBLOCKING_ACQUIRE_CLOSED;
} }

View File

@ -124,7 +124,6 @@ __go_receive_acquire (struct __go_channel *channel, _Bool for_select)
? channel->next_store == 0 ? channel->next_store == 0
: channel->next_fetch == channel->next_store)) : channel->next_fetch == channel->next_store))
{ {
channel->saw_close = 1;
channel->selected_for_receive = 0; channel->selected_for_receive = 0;
__go_unlock_and_notify_selects (channel); __go_unlock_and_notify_selects (channel);
return 0; return 0;