t.Errorf("TrySend 6, recv %d", i);
}
}
+
+ // Close
+ c <- 123;
+ cv.Close();
+ if cv.Closed() {
+ t.Errorf("closed too soon - 1");
+ }
+ if i := cv.Recv().(*IntValue).Get(); i != 123 {
+ t.Errorf("send 123 then close; Recv %d", i);
+ }
+ if cv.Closed() {
+ t.Errorf("closed too soon - 2");
+ }
+ if i := cv.Recv().(*IntValue).Get(); i != 0 {
+ t.Errorf("after close Recv %d", i);
+ }
+ if !cv.Closed() {
+ t.Errorf("not closed");
+ }
}
// check creation of unbuffered channel
func makechan(typ *runtime.ChanType, size uint32) (ch *byte)
func chansend(ch, val *byte, pres *bool)
func chanrecv(ch, val *byte, pres *bool)
+func chanclosed(ch *byte) bool
+func chanclose(ch *byte)
+
+// Closed returns the result of closed(c) on the underlying channel.
+func (v *ChanValue) Closed() bool {
+ ch := *(**byte)(v.addr);
+ return chanclosed(ch);
+}
+
+// Close closes the channel.
+func (v *ChanValue) Close() {
+ ch := *(**byte)(v.addr);
+ chanclose(ch);
+}
// internal send; non-blocking if b != nil
func (v *ChanValue) send(x Value, b *bool) {
unlock(&chanlock);
}
+void
+chanclose(Hchan *c)
+{
+ sys·closechan(c);
+}
+
+bool
+chanclosed(Hchan *c)
+{
+ return (c->closed & Rclosed) != 0;
+}
+
+
// closedchan(sel *byte) bool;
void
sys·closedchan(Hchan *c, bool closed)
{
- // test Rclosed
- closed = 0;
- if(c->closed & Rclosed)
- closed = 1;
+ closed = chanclosed(c);
FLUSH(&closed);
}
chanrecv((Hchan*)ch, val, pres);
}
+func chanclose(ch *byte) {
+ chanclose((Hchan*)ch);
+}
+
+func chanclosed(ch *byte) (r bool) {
+ r = chanclosed((Hchan*)ch);
+}
+
/*
* Go wrappers around the functions in iface.c
* amd64: allocated downwards from R15
* x86: allocated upwards from 0(FS)
* arm: allocated upwards from R9
- *
+ *
* every C file linked into a Go program must include runtime.h
* so that the C compiler knows to avoid other uses of these registers.
* the Go compilers know to avoid them.
Hchan* makechan(uint32, uint32, uint32);
void chansend(Hchan*, void*, bool*);
void chanrecv(Hchan*, void*, bool*);
+void chanclose(Hchan*);
+bool chanclosed(Hchan*);
void ifaceE2I(struct InterfaceType*, Eface, Iface*);