Commit d448d18c authored by Russ Cox's avatar Russ Cox

test and fix non-blocking chan ops on buffered chans

R=ken
DELTA=68  (19 added, 0 deleted, 49 changed)
OCL=15966
CL=15969
parent c3d841f5
...@@ -198,12 +198,18 @@ sendchan(Hchan *c, byte *ep, bool *pres) ...@@ -198,12 +198,18 @@ sendchan(Hchan *c, byte *ep, bool *pres)
sg = g->param; sg = g->param;
freesg(c, sg); freesg(c, sg);
unlock(&chanlock); unlock(&chanlock);
if(pres != nil)
*pres = true;
return; return;
asynch: asynch:
//prints("\nasend\n"); //prints("\nasend\n");
while(c->qcount >= c->dataqsiz) { while(c->qcount >= c->dataqsiz) {
// (rsc) should check for pres != nil if(pres != nil) {
unlock(&chanlock);
*pres = false;
return;
}
sg = allocsg(c); sg = allocsg(c);
g->status = Gwaiting; g->status = Gwaiting;
enqueue(&c->sendq, sg); enqueue(&c->sendq, sg);
...@@ -227,6 +233,8 @@ asynch: ...@@ -227,6 +233,8 @@ asynch:
ready(gp); ready(gp);
} else } else
unlock(&chanlock); unlock(&chanlock);
if(pres != nil)
*pres = true;
} }
static void static void
...@@ -277,10 +285,17 @@ chanrecv(Hchan* c, byte *ep, bool* pres) ...@@ -277,10 +285,17 @@ chanrecv(Hchan* c, byte *ep, bool* pres)
c->elemalg->copy(c->elemsize, ep, sg->elem); c->elemalg->copy(c->elemsize, ep, sg->elem);
freesg(c, sg); freesg(c, sg);
unlock(&chanlock); unlock(&chanlock);
if(pres != nil)
*pres = true;
return; return;
asynch: asynch:
while(c->qcount <= 0) { while(c->qcount <= 0) {
if(pres != nil) {
unlock(&chanlock);
*pres = false;
return;
}
sg = allocsg(c); sg = allocsg(c);
g->status = Gwaiting; g->status = Gwaiting;
enqueue(&c->recvq, sg); enqueue(&c->recvq, sg);
...@@ -300,6 +315,8 @@ asynch: ...@@ -300,6 +315,8 @@ asynch:
ready(gp); ready(gp);
} else } else
unlock(&chanlock); unlock(&chanlock);
if(pres != nil)
*pres = true;
} }
// chansend1(hchan *chan any, elem any); // chansend1(hchan *chan any, elem any);
......
...@@ -52,61 +52,63 @@ func main() { ...@@ -52,61 +52,63 @@ func main() {
var s string; var s string;
var ok bool; var ok bool;
c32 := new(chan int32); for buffer := 0; buffer < 2; buffer++ {
c64 := new(chan int64); c32 := new(chan int32, buffer);
cb := new(chan bool); c64 := new(chan int64, buffer);
cs := new(chan string); cb := new(chan bool, buffer);
cs := new(chan string, buffer);
i32, ok = <-c32;
if ok { panic("blocked i32sender") } i32, ok = <-c32;
if ok { panic("blocked i32sender") }
i64, ok = <-c64;
if ok { panic("blocked i64sender") } i64, ok = <-c64;
if ok { panic("blocked i64sender") }
b, ok = <-cb;
if ok { panic("blocked bsender") } b, ok = <-cb;
if ok { panic("blocked bsender") }
s, ok = <-cs;
if ok { panic("blocked ssender") } s, ok = <-cs;
if ok { panic("blocked ssender") }
go i32receiver(c32);
pause(); go i32receiver(c32);
ok = c32 <- 123; pause();
if !ok { panic("i32receiver") } ok = c32 <- 123;
go i32sender(c32); if !ok { panic("i32receiver") }
pause(); go i32sender(c32);
i32, ok = <-c32; pause();
if !ok { panic("i32sender") } i32, ok = <-c32;
if i32 != 234 { panic("i32sender value") } if !ok { panic("i32sender") }
if i32 != 234 { panic("i32sender value") }
go i64receiver(c64);
pause(); go i64receiver(c64);
ok = c64 <- 123456; pause();
if !ok { panic("i64receiver") } ok = c64 <- 123456;
go i64sender(c64); if !ok { panic("i64receiver") }
pause(); go i64sender(c64);
i64, ok = <-c64; pause();
if !ok { panic("i64sender") } i64, ok = <-c64;
if i64 != 234567 { panic("i64sender value") } if !ok { panic("i64sender") }
if i64 != 234567 { panic("i64sender value") }
go breceiver(cb);
pause(); go breceiver(cb);
ok = cb <- true; pause();
if !ok { panic("breceiver") } ok = cb <- true;
go bsender(cb); if !ok { panic("breceiver") }
pause(); go bsender(cb);
b, ok = <-cb; pause();
if !ok { panic("bsender") } b, ok = <-cb;
if !b{ panic("bsender value") } if !ok { panic("bsender") }
if !b{ panic("bsender value") }
go sreceiver(cs);
pause(); go sreceiver(cs);
ok = cs <- "hello"; pause();
if !ok { panic("sreceiver") } ok = cs <- "hello";
go ssender(cs); if !ok { panic("sreceiver") }
pause(); go ssender(cs);
s, ok = <-cs; pause();
if !ok { panic("ssender") } s, ok = <-cs;
if s != "hello again" { panic("ssender value") } if !ok { panic("ssender") }
if s != "hello again" { panic("ssender value") }
}
print("PASS\n") print("PASS\n")
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment