// auto generated by go tool dist
// goos=linux goarch=386


#include "runtime.h"
#include "arch_GOARCH.h"
#include "type.h"
#include "race.h"
#include "malloc.h"
#include "chan.h"
#include "../../cmd/ld/textflag.h"

#line 14 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
uint32 runtime·Hchansize = sizeof ( Hchan ) ; 
#line 16 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void dequeueg ( WaitQ* ) ; 
static SudoG* dequeue ( WaitQ* ) ; 
static void enqueue ( WaitQ* , SudoG* ) ; 
static void destroychan ( Hchan* ) ; 
static void racesync ( Hchan* , SudoG* ) ; 
#line 22 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static Hchan* 
makechan ( ChanType *t , int64 hint ) 
{ 
Hchan *c; 
Type *elem; 
#line 28 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
elem = t->elem; 
#line 31 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( elem->size >= ( 1<<16 ) ) 
runtime·throw ( "makechan: invalid channel element type" ) ; 
if ( ( sizeof ( *c ) %MAXALIGN ) != 0 || elem->align > MAXALIGN ) 
runtime·throw ( "makechan: bad alignment" ) ; 
#line 36 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( hint < 0 || ( intgo ) hint != hint || ( elem->size > 0 && hint > ( MaxMem - sizeof ( *c ) ) / elem->size ) ) 
runtime·panicstring ( "makechan: size out of range" ) ; 
#line 40 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
c = ( Hchan* ) runtime·mallocgc ( sizeof ( *c ) + hint*elem->size , ( uintptr ) t | TypeInfo_Chan , 0 ) ; 
c->elemsize = elem->size; 
c->elemtype = elem; 
c->dataqsiz = hint; 
#line 45 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "makechan: chan=%p; elemsize=%D; elemalg=%p; dataqsiz=%D\n" , 
c , ( int64 ) elem->size , elem->alg , ( int64 ) c->dataqsiz ) ; 
#line 49 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
return c; 
} 
void
reflect·makechan(ChanType* t, uint64 size, Hchan* c)
{
	c = 0;
	FLUSH(&c);
#line 52 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	c = makechan(t, size);
	FLUSH(&c);
}
void
runtime·makechan(ChanType* t, int64 size, Hchan* c)
{
	c = 0;
	FLUSH(&c);
#line 56 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	c = makechan(t, size);
	FLUSH(&c);
}

#line 74 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static bool 
chansend ( ChanType *t , Hchan *c , byte *ep , bool block , void *pc ) 
{ 
SudoG *sg; 
SudoG mysg; 
G* gp; 
int64 t0; 
#line 82 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) 
runtime·racereadobjectpc ( ep , t->elem , runtime·getcallerpc ( &t ) , chansend ) ; 
#line 85 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c == nil ) { 
USED ( t ) ; 
if ( !block ) 
return false; 
runtime·park ( nil , nil , "chan send (nil chan)" ) ; 
return false; 
} 
#line 93 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) { 
runtime·printf ( "chansend: chan=%p; elem=" , c ) ; 
c->elemtype->alg->print ( c->elemsize , ep ) ; 
runtime·prints ( "\n" ) ; 
} 
#line 99 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
t0 = 0; 
mysg.releasetime = 0; 
if ( runtime·blockprofilerate > 0 ) { 
t0 = runtime·cputicks ( ) ; 
mysg.releasetime = -1; 
} 
#line 106 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·lock ( c ) ; 
if ( raceenabled ) 
runtime·racereadpc ( c , pc , chansend ) ; 
if ( c->closed ) 
goto closed; 
#line 112 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c->dataqsiz > 0 ) 
goto asynch; 
#line 115 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sg = dequeue ( &c->recvq ) ; 
if ( sg != nil ) { 
if ( raceenabled ) 
racesync ( c , sg ) ; 
runtime·unlock ( c ) ; 
#line 121 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
gp = sg->g; 
gp->param = sg; 
if ( sg->elem != nil ) 
c->elemtype->alg->copy ( c->elemsize , sg->elem , ep ) ; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
return true; 
} 
#line 131 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( !block ) { 
runtime·unlock ( c ) ; 
return false; 
} 
#line 136 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
mysg.elem = ep; 
mysg.g = g; 
mysg.selectdone = nil; 
g->param = nil; 
enqueue ( &c->sendq , &mysg ) ; 
runtime·parkunlock ( c , "chan send" ) ; 
#line 143 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( g->param == nil ) { 
runtime·lock ( c ) ; 
if ( !c->closed ) 
runtime·throw ( "chansend: spurious wakeup" ) ; 
goto closed; 
} 
#line 150 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( mysg.releasetime > 0 ) 
runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; 
#line 153 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
return true; 
#line 155 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
asynch: 
if ( c->closed ) 
goto closed; 
#line 159 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c->qcount >= c->dataqsiz ) { 
if ( !block ) { 
runtime·unlock ( c ) ; 
return false; 
} 
mysg.g = g; 
mysg.elem = nil; 
mysg.selectdone = nil; 
enqueue ( &c->sendq , &mysg ) ; 
runtime·parkunlock ( c , "chan send" ) ; 
#line 170 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·lock ( c ) ; 
goto asynch; 
} 
#line 174 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
runtime·raceacquire ( chanbuf ( c , c->sendx ) ) ; 
runtime·racerelease ( chanbuf ( c , c->sendx ) ) ; 
} 
#line 179 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->sendx ) , ep ) ; 
if ( ++c->sendx == c->dataqsiz ) 
c->sendx = 0; 
c->qcount++; 
#line 184 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sg = dequeue ( &c->recvq ) ; 
if ( sg != nil ) { 
gp = sg->g; 
runtime·unlock ( c ) ; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} else 
runtime·unlock ( c ) ; 
if ( mysg.releasetime > 0 ) 
runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; 
return true; 
#line 197 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
closed: 
runtime·unlock ( c ) ; 
runtime·panicstring ( "send on closed channel" ) ; 
return false; 
} 
#line 204 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static bool 
chanrecv ( ChanType *t , Hchan* c , byte *ep , bool block , bool *received ) 
{ 
SudoG *sg; 
SudoG mysg; 
G *gp; 
int64 t0; 
#line 214 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "chanrecv: chan=%p\n" , c ) ; 
#line 217 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c == nil ) { 
USED ( t ) ; 
if ( !block ) 
return false; 
runtime·park ( nil , nil , "chan receive (nil chan)" ) ; 
return false; 
} 
#line 225 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
t0 = 0; 
mysg.releasetime = 0; 
if ( runtime·blockprofilerate > 0 ) { 
t0 = runtime·cputicks ( ) ; 
mysg.releasetime = -1; 
} 
#line 232 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·lock ( c ) ; 
if ( c->dataqsiz > 0 ) 
goto asynch; 
#line 236 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c->closed ) 
goto closed; 
#line 239 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sg = dequeue ( &c->sendq ) ; 
if ( sg != nil ) { 
if ( raceenabled ) 
racesync ( c , sg ) ; 
runtime·unlock ( c ) ; 
#line 245 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( ep != nil ) 
c->elemtype->alg->copy ( c->elemsize , ep , sg->elem ) ; 
gp = sg->g; 
gp->param = sg; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
#line 253 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( received != nil ) 
*received = true; 
return true; 
} 
#line 258 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( !block ) { 
runtime·unlock ( c ) ; 
return false; 
} 
#line 263 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
mysg.elem = ep; 
mysg.g = g; 
mysg.selectdone = nil; 
g->param = nil; 
enqueue ( &c->recvq , &mysg ) ; 
runtime·parkunlock ( c , "chan receive" ) ; 
#line 270 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( g->param == nil ) { 
runtime·lock ( c ) ; 
if ( !c->closed ) 
runtime·throw ( "chanrecv: spurious wakeup" ) ; 
goto closed; 
} 
#line 277 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( received != nil ) 
*received = true; 
if ( mysg.releasetime > 0 ) 
runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; 
return true; 
#line 283 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
asynch: 
if ( c->qcount <= 0 ) { 
if ( c->closed ) 
goto closed; 
#line 288 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( !block ) { 
runtime·unlock ( c ) ; 
if ( received != nil ) 
*received = false; 
return false; 
} 
mysg.g = g; 
mysg.elem = nil; 
mysg.selectdone = nil; 
enqueue ( &c->recvq , &mysg ) ; 
runtime·parkunlock ( c , "chan receive" ) ; 
#line 300 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·lock ( c ) ; 
goto asynch; 
} 
#line 304 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
runtime·raceacquire ( chanbuf ( c , c->recvx ) ) ; 
runtime·racerelease ( chanbuf ( c , c->recvx ) ) ; 
} 
#line 309 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( ep != nil ) 
c->elemtype->alg->copy ( c->elemsize , ep , chanbuf ( c , c->recvx ) ) ; 
c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->recvx ) , nil ) ; 
if ( ++c->recvx == c->dataqsiz ) 
c->recvx = 0; 
c->qcount--; 
#line 316 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sg = dequeue ( &c->sendq ) ; 
if ( sg != nil ) { 
gp = sg->g; 
runtime·unlock ( c ) ; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} else 
runtime·unlock ( c ) ; 
#line 326 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( received != nil ) 
*received = true; 
if ( mysg.releasetime > 0 ) 
runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; 
return true; 
#line 332 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
closed: 
if ( ep != nil ) 
c->elemtype->alg->copy ( c->elemsize , ep , nil ) ; 
if ( received != nil ) 
*received = false; 
if ( raceenabled ) 
runtime·raceacquire ( c ) ; 
runtime·unlock ( c ) ; 
if ( mysg.releasetime > 0 ) 
runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; 
return true; 
} 
#pragma textflag NOSPLIT

void
runtime·chansend1(ChanType* t, Hchan* c, byte* elem)
{
#line 346 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	chansend(t, c, elem, true, runtime·getcallerpc(&t));
}

#pragma textflag NOSPLIT
void
runtime·chanrecv1(ChanType* t, Hchan* c, byte* elem)
{
#line 351 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	chanrecv(t, c, elem, true, nil);
}

#pragma textflag NOSPLIT
void
runtime·chanrecv2(ChanType* t, Hchan* c, byte* elem, bool received)
{
	received = 0;
	FLUSH(&received);
#line 357 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	chanrecv(t, c, elem, true, &received);
	FLUSH(&received);
}

#pragma textflag NOSPLIT
void
runtime·selectnbsend(ChanType* t, Hchan* c, byte* elem, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 379 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = chansend(t, c, elem, false, runtime·getcallerpc(&t));
	FLUSH(&selected);
}

#pragma textflag NOSPLIT
void
runtime·selectnbrecv(ChanType* t, byte* elem, Hchan* c, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 401 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = chanrecv(t, c, elem, false, nil);
	FLUSH(&selected);
}

#pragma textflag NOSPLIT
void
runtime·selectnbrecv2(ChanType* t, byte* elem, bool* received, Hchan* c, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 423 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = chanrecv(t, c, elem, false, received);
	FLUSH(&selected);
}

#pragma textflag NOSPLIT
void
reflect·chansend(ChanType* t, Hchan* c, byte* elem, bool nb, uint8, uint16, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 428 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = chansend(t, c, elem, !nb, runtime·getcallerpc(&t));
	FLUSH(&selected);
}
void
reflect·chanrecv(ChanType* t, Hchan* c, bool nb, byte* elem, bool selected, bool received)
{
	selected = 0;
	FLUSH(&selected);
	received = 0;
	FLUSH(&received);
#line 432 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	received = false;
	selected = chanrecv(t, c, elem, !nb, &received);
	FLUSH(&selected);
	FLUSH(&received);
}

#line 437 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static Select* newselect ( int32 ) ; 
#pragma textflag NOSPLIT

void
runtime·newselect(int32 size, byte* sel)
{
	sel = 0;
	FLUSH(&sel);
#line 440 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	sel = (byte*)newselect(size);
	FLUSH(&sel);
}

#line 444 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static Select* 
newselect ( int32 size ) 
{ 
int32 n; 
Select *sel; 
#line 450 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
n = 0; 
if ( size > 1 ) 
n = size-1; 
#line 458 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sel = runtime·mal ( sizeof ( *sel ) + 
n*sizeof ( sel->scase[0] ) + 
size*sizeof ( sel->lockorder[0] ) + 
size*sizeof ( sel->pollorder[0] ) ) ; 
#line 463 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sel->tcase = size; 
sel->ncase = 0; 
sel->lockorder = ( void* ) ( sel->scase + size ) ; 
sel->pollorder = ( void* ) ( sel->lockorder + size ) ; 
#line 468 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "newselect s=%p size=%d\n" , sel , size ) ; 
return sel; 
} 
#line 474 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void selectsend ( Select *sel , Hchan *c , void *pc , void *elem , int32 so ) ; 
#pragma textflag NOSPLIT

void
runtime·selectsend(Select* sel, Hchan* c, byte* elem, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 477 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = false;

	// nil cases do not compete
	if(c != nil)
		selectsend(sel, c, runtime·getcallerpc(&sel), elem, (byte*)&selected - (byte*)&sel);
	FLUSH(&selected);
}

#line 485 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
selectsend ( Select *sel , Hchan *c , void *pc , void *elem , int32 so ) 
{ 
int32 i; 
Scase *cas; 
#line 491 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
i = sel->ncase; 
if ( i >= sel->tcase ) 
runtime·throw ( "selectsend: too many cases" ) ; 
sel->ncase = i+1; 
cas = &sel->scase[i]; 
#line 497 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
cas->pc = pc; 
cas->chan = c; 
cas->so = so; 
cas->kind = CaseSend; 
cas->sg.elem = elem; 
#line 503 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "selectsend s=%p pc=%p chan=%p so=%d\n" , 
sel , cas->pc , cas->chan , cas->so ) ; 
} 
#line 509 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void selectrecv ( Select *sel , Hchan *c , void *pc , void *elem , bool* , int32 so ) ; 
#pragma textflag NOSPLIT

void
runtime·selectrecv(Select* sel, Hchan* c, byte* elem, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 512 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = false;

	// nil cases do not compete
	if(c != nil)
		selectrecv(sel, c, runtime·getcallerpc(&sel), elem, nil, (byte*)&selected - (byte*)&sel);
	FLUSH(&selected);
}

#pragma textflag NOSPLIT
void
runtime·selectrecv2(Select* sel, Hchan* c, byte* elem, bool* received, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 521 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = false;

	// nil cases do not compete
	if(c != nil)
		selectrecv(sel, c, runtime·getcallerpc(&sel), elem, received, (byte*)&selected - (byte*)&sel);
	FLUSH(&selected);
}

#line 529 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
selectrecv ( Select *sel , Hchan *c , void *pc , void *elem , bool *received , int32 so ) 
{ 
int32 i; 
Scase *cas; 
#line 535 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
i = sel->ncase; 
if ( i >= sel->tcase ) 
runtime·throw ( "selectrecv: too many cases" ) ; 
sel->ncase = i+1; 
cas = &sel->scase[i]; 
cas->pc = pc; 
cas->chan = c; 
#line 543 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
cas->so = so; 
cas->kind = CaseRecv; 
cas->sg.elem = elem; 
cas->receivedp = received; 
#line 548 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "selectrecv s=%p pc=%p chan=%p so=%d\n" , 
sel , cas->pc , cas->chan , cas->so ) ; 
} 
#line 554 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void selectdefault ( Select* , void* , int32 ) ; 
#pragma textflag NOSPLIT

void
runtime·selectdefault(Select* sel, bool selected)
{
	selected = 0;
	FLUSH(&selected);
#line 557 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	selected = false;
	selectdefault(sel, runtime·getcallerpc(&sel), (byte*)&selected - (byte*)&sel);
	FLUSH(&selected);
}

#line 562 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
selectdefault ( Select *sel , void *callerpc , int32 so ) 
{ 
int32 i; 
Scase *cas; 
#line 568 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
i = sel->ncase; 
if ( i >= sel->tcase ) 
runtime·throw ( "selectdefault: too many cases" ) ; 
sel->ncase = i+1; 
cas = &sel->scase[i]; 
cas->pc = callerpc; 
cas->chan = nil; 
#line 576 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
cas->so = so; 
cas->kind = CaseDefault; 
#line 579 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "selectdefault s=%p pc=%p so=%d\n" , 
sel , cas->pc , cas->so ) ; 
} 
#line 584 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
sellock ( Select *sel ) 
{ 
uint32 i; 
Hchan *c , *c0; 
#line 590 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
c = nil; 
for ( i=0; i<sel->ncase; i++ ) { 
c0 = sel->lockorder[i]; 
if ( c0 && c0 != c ) { 
c = sel->lockorder[i]; 
runtime·lock ( c ) ; 
} 
} 
} 
#line 600 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
selunlock ( Select *sel ) 
{ 
int32 i , n , r; 
Hchan *c; 
#line 614 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
n = ( int32 ) sel->ncase; 
r = 0; 
#line 617 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( n>0 && sel->lockorder[0] == nil ) 
r = 1; 
for ( i = n-1; i >= r; i-- ) { 
c = sel->lockorder[i]; 
if ( i>0 && sel->lockorder[i-1] == c ) 
continue; 
runtime·unlock ( c ) ; 
} 
} 
#line 627 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static bool 
selparkcommit ( G *gp , void *sel ) 
{ 
USED ( gp ) ; 
selunlock ( sel ) ; 
return true; 
} 
void
runtime·block()
{
#line 635 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	runtime·park(nil, nil, "select (no cases)");	// forever
}

#line 639 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void* selectgo ( Select** ) ; 
#pragma textflag NOSPLIT

void
runtime·selectgo(Select* sel)
{
#line 646 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	runtime·setcallerpc(&sel, selectgo(&sel));
}

#line 650 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void* 
selectgo ( Select **selp ) 
{ 
Select *sel; 
uint32 o , i , j , k , done; 
int64 t0; 
Scase *cas , *dfl; 
Hchan *c; 
SudoG *sg; 
G *gp; 
byte *as; 
void *pc; 
#line 663 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sel = *selp; 
#line 665 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "select: sel=%p\n" , sel ) ; 
#line 668 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
t0 = 0; 
if ( runtime·blockprofilerate > 0 ) { 
t0 = runtime·cputicks ( ) ; 
for ( i=0; i<sel->ncase; i++ ) 
sel->scase[i].sg.releasetime = -1; 
} 
#line 684 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
for ( i=0; i<sel->ncase; i++ ) 
sel->pollorder[i] = i; 
for ( i=1; i<sel->ncase; i++ ) { 
o = sel->pollorder[i]; 
j = runtime·fastrand1 ( ) % ( i+1 ) ; 
sel->pollorder[i] = sel->pollorder[j]; 
sel->pollorder[j] = o; 
} 
#line 695 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
for ( i=0; i<sel->ncase; i++ ) { 
j = i; 
c = sel->scase[j].chan; 
while ( j > 0 && sel->lockorder[k= ( j-1 ) /2] < c ) { 
sel->lockorder[j] = sel->lockorder[k]; 
j = k; 
} 
sel->lockorder[j] = c; 
} 
for ( i=sel->ncase; i-->0; ) { 
c = sel->lockorder[i]; 
sel->lockorder[i] = sel->lockorder[0]; 
j = 0; 
for ( ;; ) { 
k = j*2+1; 
if ( k >= i ) 
break; 
if ( k+1 < i && sel->lockorder[k] < sel->lockorder[k+1] ) 
k++; 
if ( c < sel->lockorder[k] ) { 
sel->lockorder[j] = sel->lockorder[k]; 
j = k; 
continue; 
} 
break; 
} 
sel->lockorder[j] = c; 
} 
#line 730 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sellock ( sel ) ; 
#line 732 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
loop: 
#line 734 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
dfl = nil; 
for ( i=0; i<sel->ncase; i++ ) { 
o = sel->pollorder[i]; 
cas = &sel->scase[o]; 
c = cas->chan; 
#line 740 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
switch ( cas->kind ) { 
case CaseRecv: 
if ( c->dataqsiz > 0 ) { 
if ( c->qcount > 0 ) 
goto asyncrecv; 
} else { 
sg = dequeue ( &c->sendq ) ; 
if ( sg != nil ) 
goto syncrecv; 
} 
if ( c->closed ) 
goto rclose; 
break; 
#line 754 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
case CaseSend: 
if ( raceenabled ) 
runtime·racereadpc ( c , cas->pc , chansend ) ; 
if ( c->closed ) 
goto sclose; 
if ( c->dataqsiz > 0 ) { 
if ( c->qcount < c->dataqsiz ) 
goto asyncsend; 
} else { 
sg = dequeue ( &c->recvq ) ; 
if ( sg != nil ) 
goto syncsend; 
} 
break; 
#line 769 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
case CaseDefault: 
dfl = cas; 
break; 
} 
} 
#line 775 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( dfl != nil ) { 
selunlock ( sel ) ; 
cas = dfl; 
goto retc; 
} 
#line 783 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
done = 0; 
for ( i=0; i<sel->ncase; i++ ) { 
o = sel->pollorder[i]; 
cas = &sel->scase[o]; 
c = cas->chan; 
sg = &cas->sg; 
sg->g = g; 
sg->selectdone = &done; 
#line 792 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
switch ( cas->kind ) { 
case CaseRecv: 
enqueue ( &c->recvq , sg ) ; 
break; 
#line 797 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
case CaseSend: 
enqueue ( &c->sendq , sg ) ; 
break; 
} 
} 
#line 803 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
g->param = nil; 
runtime·park ( selparkcommit , sel , "select" ) ; 
#line 806 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sellock ( sel ) ; 
sg = g->param; 
#line 811 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
for ( i=0; i<sel->ncase; i++ ) { 
cas = &sel->scase[i]; 
if ( cas != ( Scase* ) sg ) { 
c = cas->chan; 
if ( cas->kind == CaseSend ) 
dequeueg ( &c->sendq ) ; 
else 
dequeueg ( &c->recvq ) ; 
} 
} 
#line 822 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( sg == nil ) 
goto loop; 
#line 825 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
cas = ( Scase* ) sg; 
c = cas->chan; 
#line 828 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c->dataqsiz > 0 ) 
runtime·throw ( "selectgo: shouldn't happen" ) ; 
#line 831 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( debug ) 
runtime·printf ( "wait-return: sel=%p c=%p cas=%p kind=%d\n" , 
sel , c , cas , cas->kind ) ; 
#line 835 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( cas->kind == CaseRecv ) { 
if ( cas->receivedp != nil ) 
*cas->receivedp = true; 
} 
#line 840 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
if ( cas->kind == CaseRecv && cas->sg.elem != nil ) 
runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; 
else if ( cas->kind == CaseSend ) 
runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; 
} 
#line 847 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
selunlock ( sel ) ; 
goto retc; 
#line 850 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
asyncrecv: 
#line 852 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
if ( cas->sg.elem != nil ) 
runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; 
runtime·raceacquire ( chanbuf ( c , c->recvx ) ) ; 
runtime·racerelease ( chanbuf ( c , c->recvx ) ) ; 
} 
if ( cas->receivedp != nil ) 
*cas->receivedp = true; 
if ( cas->sg.elem != nil ) 
c->elemtype->alg->copy ( c->elemsize , cas->sg.elem , chanbuf ( c , c->recvx ) ) ; 
c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->recvx ) , nil ) ; 
if ( ++c->recvx == c->dataqsiz ) 
c->recvx = 0; 
c->qcount--; 
sg = dequeue ( &c->sendq ) ; 
if ( sg != nil ) { 
gp = sg->g; 
selunlock ( sel ) ; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} else { 
selunlock ( sel ) ; 
} 
goto retc; 
#line 878 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
asyncsend: 
#line 880 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
runtime·raceacquire ( chanbuf ( c , c->sendx ) ) ; 
runtime·racerelease ( chanbuf ( c , c->sendx ) ) ; 
runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; 
} 
c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->sendx ) , cas->sg.elem ) ; 
if ( ++c->sendx == c->dataqsiz ) 
c->sendx = 0; 
c->qcount++; 
sg = dequeue ( &c->recvq ) ; 
if ( sg != nil ) { 
gp = sg->g; 
selunlock ( sel ) ; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} else { 
selunlock ( sel ) ; 
} 
goto retc; 
#line 901 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
syncrecv: 
#line 903 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
if ( cas->sg.elem != nil ) 
runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; 
racesync ( c , sg ) ; 
} 
selunlock ( sel ) ; 
if ( debug ) 
runtime·printf ( "syncrecv: sel=%p c=%p o=%d\n" , sel , c , o ) ; 
if ( cas->receivedp != nil ) 
*cas->receivedp = true; 
if ( cas->sg.elem != nil ) 
c->elemtype->alg->copy ( c->elemsize , cas->sg.elem , sg->elem ) ; 
gp = sg->g; 
gp->param = sg; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
goto retc; 
#line 922 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
rclose: 
#line 924 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
selunlock ( sel ) ; 
if ( cas->receivedp != nil ) 
*cas->receivedp = false; 
if ( cas->sg.elem != nil ) 
c->elemtype->alg->copy ( c->elemsize , cas->sg.elem , nil ) ; 
if ( raceenabled ) 
runtime·raceacquire ( c ) ; 
goto retc; 
#line 933 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
syncsend: 
#line 935 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; 
racesync ( c , sg ) ; 
} 
selunlock ( sel ) ; 
if ( debug ) 
runtime·printf ( "syncsend: sel=%p c=%p o=%d\n" , sel , c , o ) ; 
if ( sg->elem != nil ) 
c->elemtype->alg->copy ( c->elemsize , sg->elem , cas->sg.elem ) ; 
gp = sg->g; 
gp->param = sg; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
#line 950 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
retc: 
#line 956 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
pc = cas->pc; 
if ( cas->so > 0 ) { 
as = ( byte* ) selp + cas->so; 
*as = true; 
} 
if ( cas->sg.releasetime > 0 ) 
runtime·blockevent ( cas->sg.releasetime - t0 , 2 ) ; 
runtime·free ( sel ) ; 
return pc; 
#line 966 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
sclose: 
#line 968 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
selunlock ( sel ) ; 
runtime·panicstring ( "send on closed channel" ) ; 
return nil; 
} 
#line 974 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
typedef struct runtimeSelect runtimeSelect; 
struct runtimeSelect 
{ 
uintptr dir; 
ChanType *typ; 
Hchan *ch; 
byte *val; 
} ; 
#line 984 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
enum SelectDir { 
SelectSend = 1 , 
SelectRecv , 
SelectDefault , 
} ; 
void
reflect·rselect(Slice cases, intgo chosen, bool recvOK)
{
	chosen = 0;
	FLUSH(&chosen);
	recvOK = 0;
	FLUSH(&recvOK);
#line 990 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	int32 i;
	Select *sel;
	runtimeSelect* rcase, *rc;

	chosen = -1;
	recvOK = false;

	rcase = (runtimeSelect*)cases.array;

	sel = newselect(cases.len);
	for(i=0; i<cases.len; i++) {
		rc = &rcase[i];
		switch(rc->dir) {
		case SelectDefault:
			selectdefault(sel, (void*)i, 0);
			break;
		case SelectSend:
			if(rc->ch == nil)
				break;
			selectsend(sel, rc->ch, (void*)i, rc->val, 0);
			break;
		case SelectRecv:
			if(rc->ch == nil)
				break;
			selectrecv(sel, rc->ch, (void*)i, rc->val, &recvOK, 0);
			break;
		}
	}

	chosen = (intgo)(uintptr)selectgo(&sel);
	FLUSH(&chosen);
	FLUSH(&recvOK);
}

#line 1023 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void closechan ( Hchan *c , void *pc ) ; 
#pragma textflag NOSPLIT

void
runtime·closechan(Hchan* c)
{
#line 1026 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	closechan(c, runtime·getcallerpc(&c));
}

#pragma textflag NOSPLIT
void
reflect·chanclose(Hchan* c)
{
#line 1031 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	closechan(c, runtime·getcallerpc(&c));
}

#line 1035 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
closechan ( Hchan *c , void *pc ) 
{ 
SudoG *sg; 
G* gp; 
#line 1041 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( c == nil ) 
runtime·panicstring ( "close of nil channel" ) ; 
#line 1044 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·lock ( c ) ; 
if ( c->closed ) { 
runtime·unlock ( c ) ; 
runtime·panicstring ( "close of closed channel" ) ; 
} 
#line 1050 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( raceenabled ) { 
runtime·racewritepc ( c , pc , runtime·closechan ) ; 
runtime·racerelease ( c ) ; 
} 
#line 1055 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
c->closed = true; 
#line 1058 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
for ( ;; ) { 
sg = dequeue ( &c->recvq ) ; 
if ( sg == nil ) 
break; 
gp = sg->g; 
gp->param = nil; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} 
#line 1070 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
for ( ;; ) { 
sg = dequeue ( &c->sendq ) ; 
if ( sg == nil ) 
break; 
gp = sg->g; 
gp->param = nil; 
if ( sg->releasetime ) 
sg->releasetime = runtime·cputicks ( ) ; 
runtime·ready ( gp ) ; 
} 
#line 1081 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
runtime·unlock ( c ) ; 
} 
void
reflect·chanlen(Hchan* c, intgo len)
{
	len = 0;
	FLUSH(&len);
#line 1084 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	if(c == nil)
		len = 0;
	else
		len = c->qcount;
	FLUSH(&len);
}
void
reflect·chancap(Hchan* c, intgo cap)
{
	cap = 0;
	FLUSH(&cap);
#line 1091 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"

	if(c == nil)
		cap = 0;
	else
		cap = c->dataqsiz;
	FLUSH(&cap);
}

#line 1098 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static SudoG* 
dequeue ( WaitQ *q ) 
{ 
SudoG *sgp; 
#line 1103 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
loop: 
sgp = q->first; 
if ( sgp == nil ) 
return nil; 
q->first = sgp->link; 
#line 1110 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( sgp->selectdone != nil ) { 
#line 1112 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
if ( *sgp->selectdone != 0 || !runtime·cas ( sgp->selectdone , 0 , 1 ) ) 
goto loop; 
} 
#line 1116 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
return sgp; 
} 
#line 1119 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
dequeueg ( WaitQ *q ) 
{ 
SudoG **l , *sgp , *prevsgp; 
#line 1124 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
prevsgp = nil; 
for ( l=&q->first; ( sgp=*l ) != nil; l=&sgp->link , prevsgp=sgp ) { 
if ( sgp->g == g ) { 
*l = sgp->link; 
if ( q->last == sgp ) 
q->last = prevsgp; 
break; 
} 
} 
} 
#line 1135 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
enqueue ( WaitQ *q , SudoG *sgp ) 
{ 
sgp->link = nil; 
if ( q->first == nil ) { 
q->first = sgp; 
q->last = sgp; 
return; 
} 
q->last->link = sgp; 
q->last = sgp; 
} 
#line 1148 "/build/go/src/go-1.3/src/pkg/runtime/chan.goc"
static void 
racesync ( Hchan *c , SudoG *sg ) 
{ 
runtime·racerelease ( chanbuf ( c , 0 ) ) ; 
runtime·raceacquireg ( sg->g , chanbuf ( c , 0 ) ) ; 
runtime·racereleaseg ( sg->g , chanbuf ( c , 0 ) ) ; 
runtime·raceacquire ( chanbuf ( c , 0 ) ) ; 
} 