1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 runtime_initlock(Lock
*l
)
13 if(sem_init(&l
->sem
, 0, 0) != 0)
14 runtime_throw("sem_init failed");
17 // noinline so that runtime_lock doesn't have to split the stack.
18 static void runtime_lock_full(Lock
*l
) __attribute__ ((noinline
));
21 runtime_lock_full(Lock
*l
)
24 if(sem_wait(&l
->sem
) == 0)
27 runtime_throw("sem_wait failed");
36 runtime_throw("lock count");
40 if(runtime_xadd(&l
->key
, 1) > 1) // someone else has it; wait
44 static void runtime_unlock_full(Lock
*l
) __attribute__ ((noinline
));
47 runtime_unlock_full(Lock
*l
)
49 if(sem_post(&l
->sem
) != 0)
50 runtime_throw("sem_post failed");
54 runtime_unlock(Lock
*l
)
59 runtime_throw("lock count");
62 if(runtime_xadd(&l
->key
, -1) > 0) // someone else is waiting
63 runtime_unlock_full(l
);
67 runtime_destroylock(Lock
*l
)
72 #ifndef HAVE_SYNC_BOOL_COMPARE_AND_SWAP_4
74 // For targets which don't have the required sync support. Really
75 // this should be provided by gcc itself. FIXME.
77 static pthread_mutex_t sync_lock
= PTHREAD_MUTEX_INITIALIZER
;
80 __sync_bool_compare_and_swap_4(uint32
*, uint32
, uint32
)
81 __attribute__((visibility("hidden")));
84 __sync_bool_compare_and_swap_4(uint32
* ptr
, uint32 old
, uint32
new)
89 i
= pthread_mutex_lock(&sync_lock
);
99 i
= pthread_mutex_unlock(&sync_lock
);