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

#include "runtime.h"
#include "arch_GOARCH.h"

#line 969 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
typedef struct Sema Sema; 
struct Sema 
{ 
uint32 volatile *addr; 
G *g; 
Sema *prev; 
Sema *next; 
} ; 
#line 978 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
typedef struct SemaRoot SemaRoot; 
struct SemaRoot 
{ 
Lock; 
Sema *head; 
Sema *tail; 
#line 985 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
uint32 volatile nwait; 
} ; 
#line 989 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
#define SEMTABLESZ 251 
#line 991 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
static union 
{ 
SemaRoot; 
uint8 pad[CacheLineSize]; 
} semtable[SEMTABLESZ]; 
#line 997 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
static SemaRoot* 
semroot ( uint32 *addr ) 
{ 
return &semtable[ ( ( uintptr ) addr >> 3 ) % SEMTABLESZ]; 
} 
#line 1003 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
static void 
semqueue ( SemaRoot *root , uint32 volatile *addr , Sema *s ) 
{ 
s->g = g; 
s->addr = addr; 
s->next = nil; 
s->prev = root->tail; 
if ( root->tail ) 
root->tail->next = s; 
else 
root->head = s; 
root->tail = s; 
} 
#line 1017 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
static void 
semdequeue ( SemaRoot *root , Sema *s ) 
{ 
if ( s->next ) 
s->next->prev = s->prev; 
else 
root->tail = s->prev; 
if ( s->prev ) 
s->prev->next = s->next; 
else 
root->head = s->next; 
s->prev = nil; 
s->next = nil; 
} 
#line 1032 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
static int32 
cansemacquire ( uint32 *addr ) 
{ 
uint32 v; 
#line 1037 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
while ( ( v = runtime·atomicload ( addr ) ) > 0 ) 
if ( runtime·cas ( addr , v , v-1 ) ) 
return 1; 
return 0; 
} 
#line 1043 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
void 
runtime·semacquire ( uint32 volatile *addr ) 
{ 
Sema s; 
SemaRoot *root; 
#line 1050 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
if ( cansemacquire ( addr ) ) 
return; 
#line 1059 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
root = semroot ( addr ) ; 
for ( ;; ) { 
runtime·lock ( root ) ; 
#line 1063 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
runtime·xadd ( &root->nwait , 1 ) ; 
#line 1065 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
if ( cansemacquire ( addr ) ) { 
runtime·xadd ( &root->nwait , -1 ) ; 
runtime·unlock ( root ) ; 
return; 
} 
#line 1072 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
semqueue ( root , addr , &s ) ; 
g->status = Gwaiting; 
g->waitreason = "semacquire" ; 
runtime·unlock ( root ) ; 
runtime·gosched ( ) ; 
if ( cansemacquire ( addr ) ) 
return; 
} 
} 
#line 1082 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
void 
runtime·semrelease ( uint32 volatile *addr ) 
{ 
Sema *s; 
SemaRoot *root; 
#line 1088 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
root = semroot ( addr ) ; 
runtime·xadd ( addr , 1 ) ; 
#line 1094 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
if ( runtime·atomicload ( &root->nwait ) == 0 ) 
return; 
#line 1098 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
runtime·lock ( root ) ; 
if ( runtime·atomicload ( &root->nwait ) == 0 ) { 
#line 1102 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"
runtime·unlock ( root ) ; 
return; 
} 
for ( s = root->head; s; s = s->next ) { 
if ( s->addr == addr ) { 
runtime·xadd ( &root->nwait , -1 ) ; 
semdequeue ( root , s ) ; 
break; 
} 
} 
runtime·unlock ( root ) ; 
if ( s ) 
runtime·ready ( s->g ) ; 
} 
void
sync·runtime_Semacquire(uint32* addr)
{
#line 1117 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"

	runtime·semacquire(addr);
}
void
sync·runtime_Semrelease(uint32* addr)
{
#line 1121 "/root/fast/go.build.hg/src/pkg/runtime/sema.goc"

	runtime·semrelease(addr);
}
