2009-03-04 04:28:35 +01:00
|
|
|
/*
|
2014-05-15 03:18:55 +02:00
|
|
|
* Copyright (C) 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-03-04 04:28:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2012-03-28 11:25:17 +02:00
|
|
|
#include <endian.h>
|
2013-10-10 00:50:50 +02:00
|
|
|
|
|
|
|
#include "private/bionic_atomic_inline.h"
|
|
|
|
#include "private/bionic_futex.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-08-10 00:17:46 +02:00
|
|
|
// This file contains C++ ABI support functions for one time
|
|
|
|
// constructors as defined in the "Run-time ABI for the ARM Architecture"
|
|
|
|
// section 4.4.2
|
|
|
|
//
|
2012-03-28 11:25:17 +02:00
|
|
|
// ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
|
|
|
|
// one time construction:
|
|
|
|
//
|
|
|
|
// ARM C++ ABI defines the LSB of guard variable should be tested
|
|
|
|
// by compiler-generated code before calling __cxa_guard_acquire et al.
|
|
|
|
//
|
|
|
|
// The Itanium/x86 C++ ABI defines the low-order _byte_ should be
|
|
|
|
// tested instead.
|
|
|
|
//
|
|
|
|
// Meanwhile, guard variable are 32bit aligned for ARM, and 64bit
|
|
|
|
// aligned for x86.
|
|
|
|
//
|
|
|
|
// Reference documentation:
|
|
|
|
//
|
|
|
|
// section 3.2.3 of ARM IHI 0041C (for ARM)
|
|
|
|
// section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86).
|
|
|
|
//
|
|
|
|
// There is no C++ ABI available for other ARCH. But the gcc source
|
|
|
|
// shows all other ARCH follow the definition of Itanium/x86 C++ ABI.
|
|
|
|
|
|
|
|
#if defined(__arm__)
|
2014-05-15 03:18:55 +02:00
|
|
|
// The ARM C++ ABI mandates that guard variables are 32-bit aligned, 32-bit
|
|
|
|
// values. The LSB is tested by the compiler-generated code before calling
|
2012-03-28 11:25:17 +02:00
|
|
|
// __cxa_guard_acquire.
|
2014-05-15 03:18:55 +02:00
|
|
|
union _guard_t {
|
2012-03-28 11:25:17 +02:00
|
|
|
int volatile state;
|
|
|
|
int32_t aligner;
|
2014-05-15 03:18:55 +02:00
|
|
|
};
|
2012-03-28 11:25:17 +02:00
|
|
|
|
|
|
|
const static int ready = 0x1;
|
|
|
|
const static int pending = 0x2;
|
|
|
|
const static int waiting = 0x6;
|
|
|
|
|
2014-05-15 03:18:55 +02:00
|
|
|
#else
|
|
|
|
// The Itanium/x86 C++ ABI (used by all other architectures) mandates that
|
|
|
|
// guard variables are 64-bit aligned, 64-bit values. The LSB is tested by
|
|
|
|
// the compiler-generated code before calling __cxa_guard_acquire.
|
|
|
|
union _guard_t {
|
2012-03-28 11:25:17 +02:00
|
|
|
int volatile state;
|
|
|
|
int64_t aligner;
|
2014-05-15 03:18:55 +02:00
|
|
|
};
|
2012-03-28 11:25:17 +02:00
|
|
|
|
|
|
|
const static int ready = letoh32(0x1);
|
|
|
|
const static int pending = letoh32(0x100);
|
|
|
|
const static int waiting = letoh32(0x10000);
|
|
|
|
#endif
|
|
|
|
|
2014-05-15 03:18:55 +02:00
|
|
|
extern "C" int __cxa_guard_acquire(_guard_t* gv) {
|
2012-03-28 11:25:17 +02:00
|
|
|
// 0 -> pending, return 1
|
|
|
|
// pending -> waiting, wait and return 0
|
|
|
|
// waiting: untouched, wait and return 0
|
|
|
|
// ready: untouched, return 0
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
retry:
|
2012-04-16 23:40:26 +02:00
|
|
|
if (__bionic_cmpxchg(0, pending, &gv->state) == 0) {
|
2010-06-11 22:18:41 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2009-03-04 04:28:35 +01:00
|
|
|
return 1;
|
2010-06-11 22:18:41 +02:00
|
|
|
}
|
2012-04-16 23:40:26 +02:00
|
|
|
__bionic_cmpxchg(pending, waiting, &gv->state); // Indicate there is a waiter
|
2012-03-28 11:25:17 +02:00
|
|
|
__futex_wait(&gv->state, waiting, NULL);
|
2010-06-11 22:18:41 +02:00
|
|
|
|
2014-05-15 03:18:55 +02:00
|
|
|
if (gv->state != ready) {
|
|
|
|
// __cxa_guard_abort was called, let every thread try since there is no return code for this condition
|
2009-03-04 04:28:35 +01:00
|
|
|
goto retry;
|
2014-05-15 03:18:55 +02:00
|
|
|
}
|
2010-06-11 22:18:41 +02:00
|
|
|
|
|
|
|
ANDROID_MEMBAR_FULL();
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-15 03:18:55 +02:00
|
|
|
extern "C" void __cxa_guard_release(_guard_t* gv) {
|
2012-03-28 11:25:17 +02:00
|
|
|
// pending -> ready
|
|
|
|
// waiting -> ready, and wake
|
|
|
|
|
2010-06-11 22:18:41 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2012-04-16 23:40:26 +02:00
|
|
|
if (__bionic_cmpxchg(pending, ready, &gv->state) == 0) {
|
2009-03-04 04:28:35 +01:00
|
|
|
return;
|
2010-06-11 22:18:41 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-03-28 11:25:17 +02:00
|
|
|
gv->state = ready;
|
|
|
|
__futex_wake(&gv->state, 0x7fffffff);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 03:18:55 +02:00
|
|
|
extern "C" void __cxa_guard_abort(_guard_t* gv) {
|
2010-06-11 22:18:41 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2012-03-28 11:25:17 +02:00
|
|
|
gv->state= 0;
|
|
|
|
__futex_wake(&gv->state, 0x7fffffff);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|