summaryrefslogtreecommitdiffstats
path: root/libc/bionic
diff options
context:
space:
mode:
authorAndy McFadden <fadden@android.com>2010-05-28 13:31:45 -0700
committerAndy McFadden <fadden@android.com>2010-05-28 16:12:01 -0700
commitfcd00ebbdf3e7f4e1e7782a65ae10fb0fc03a1aa (patch)
tree859e86f36d6bf63ee284c65fff114bbbfdeff38f /libc/bionic
parent4fdbadde921ec17b4ff9e97fbd41096903b21772 (diff)
downloadbionic-fcd00ebbdf3e7f4e1e7782a65ae10fb0fc03a1aa.zip
bionic-fcd00ebbdf3e7f4e1e7782a65ae10fb0fc03a1aa.tar.gz
bionic-fcd00ebbdf3e7f4e1e7782a65ae10fb0fc03a1aa.tar.bz2
Atomic/SMP update, part 3.
Update ARM atomic ops to use LDREX/STREX. Stripped out #if 0 chunk. Insert explicit memory barriers in pthread and semaphore code. For bug 2721865. Change-Id: I0f153b797753a655702d8be41679273d1d5d6ae7
Diffstat (limited to 'libc/bionic')
-rw-r--r--libc/bionic/pthread.c15
-rw-r--r--libc/bionic/semaphore.c14
2 files changed, 24 insertions, 5 deletions
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index ae44b06..709e612 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -44,6 +44,7 @@
#include <assert.h>
#include <malloc.h>
#include <linux/futex.h>
+#include <cutils/atomic-inline.h>
extern int __pthread_clone(int (*fn)(void*), void *child_stack, int flags, void *arg);
extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
@@ -936,6 +937,7 @@ _normal_lock(pthread_mutex_t* mutex)
while (__atomic_swap(shared|2, &mutex->value ) != (shared|0))
__futex_syscall4(&mutex->value, wait_op, shared|2, 0);
}
+ ANDROID_MEMBAR_FULL();
}
/*
@@ -945,6 +947,8 @@ _normal_lock(pthread_mutex_t* mutex)
static __inline__ void
_normal_unlock(pthread_mutex_t* mutex)
{
+ ANDROID_MEMBAR_FULL();
+
/* We need to preserve the shared flag during operations */
int shared = mutex->value & MUTEX_SHARED_MASK;
@@ -1144,8 +1148,10 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex)
/* Handle common case first */
if ( __likely(mtype == MUTEX_TYPE_NORMAL) )
{
- if (__atomic_cmpxchg(shared|0, shared|1, &mutex->value) == 0)
+ if (__atomic_cmpxchg(shared|0, shared|1, &mutex->value) == 0) {
+ ANDROID_MEMBAR_FULL();
return 0;
+ }
return EBUSY;
}
@@ -1241,9 +1247,11 @@ int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
{
int wait_op = shared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE;
- /* fast path for unconteded lock */
- if (__atomic_cmpxchg(shared|0, shared|1, &mutex->value) == 0)
+ /* fast path for uncontended lock */
+ if (__atomic_cmpxchg(shared|0, shared|1, &mutex->value) == 0) {
+ ANDROID_MEMBAR_FULL();
return 0;
+ }
/* loop while needed */
while (__atomic_swap(shared|2, &mutex->value) != (shared|0)) {
@@ -1252,6 +1260,7 @@ int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
__futex_syscall4(&mutex->value, wait_op, shared|2, &ts);
}
+ ANDROID_MEMBAR_FULL();
return 0;
}
diff --git a/libc/bionic/semaphore.c b/libc/bionic/semaphore.c
index 84b9314..b624943 100644
--- a/libc/bionic/semaphore.c
+++ b/libc/bionic/semaphore.c
@@ -30,6 +30,7 @@
#include <sys/time.h>
#include <sys/atomics.h>
#include <time.h>
+#include <cutils/atomic-inline.h>
int sem_init(sem_t *sem, int pshared, unsigned int value)
{
@@ -103,6 +104,7 @@ __atomic_dec_if_positive( volatile unsigned int* pvalue )
return old;
}
+/* lock a semaphore */
int sem_wait(sem_t *sem)
{
if (sem == NULL) {
@@ -116,6 +118,7 @@ int sem_wait(sem_t *sem)
__futex_wait(&sem->count, 0, 0);
}
+ ANDROID_MEMBAR_FULL();
return 0;
}
@@ -130,8 +133,10 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
/* POSIX says we need to try to decrement the semaphore
* before checking the timeout value */
- if (__atomic_dec_if_positive(&sem->count))
+ if (__atomic_dec_if_positive(&sem->count)) {
+ ANDROID_MEMBAR_FULL();
return 0;
+ }
/* check it as per Posix */
if (abs_timeout == NULL ||
@@ -169,17 +174,21 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
return -1;
}
- if (__atomic_dec_if_positive(&sem->count))
+ if (__atomic_dec_if_positive(&sem->count)) {
+ ANDROID_MEMBAR_FULL();
break;
+ }
}
return 0;
}
+/* unlock a semaphore */
int sem_post(sem_t *sem)
{
if (sem == NULL)
return EINVAL;
+ ANDROID_MEMBAR_FULL();
if (__atomic_inc((volatile int*)&sem->count) >= 0)
__futex_wake(&sem->count, 1);
@@ -194,6 +203,7 @@ int sem_trywait(sem_t *sem)
}
if (__atomic_dec_if_positive(&sem->count) > 0) {
+ ANDROID_MEMBAR_FULL();
return 0;
} else {
errno = EAGAIN;