1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28#include <pthread.h> 29 30#include "private/bionic_lock.h" 31 32// User-level spinlocks can be hazardous to battery life on Android. 33// We implement a simple compromise that behaves mostly like a spinlock, 34// but prevents excessively long spinning. 35 36struct pthread_spinlock_internal_t { 37 Lock lock; 38}; 39 40static_assert(sizeof(pthread_spinlock_t) == sizeof(pthread_spinlock_internal_t), 41 "pthread_spinlock_t should actually be pthread_spinlock_internal_t."); 42 43static_assert(alignof(pthread_spinlock_t) >= 4, 44 "pthread_spinlock_t should fulfill the alignment of pthread_spinlock_internal_t."); 45 46static inline pthread_spinlock_internal_t* __get_internal_spinlock(pthread_spinlock_t* lock) { 47 return reinterpret_cast<pthread_spinlock_internal_t*>(lock); 48} 49 50int pthread_spin_init(pthread_spinlock_t* lock_interface, int pshared) { 51 pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface); 52 lock->lock.init(pshared); 53 return 0; 54} 55 56int pthread_spin_destroy(pthread_spinlock_t* lock_interface) { 57 pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface); 58 return lock->lock.trylock() ? 0 : EBUSY; 59} 60 61int pthread_spin_trylock(pthread_spinlock_t* lock_interface) { 62 pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface); 63 return lock->lock.trylock() ? 0 : EBUSY; 64} 65 66int pthread_spin_lock(pthread_spinlock_t* lock_interface) { 67 pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface); 68 for (int i = 0; i < 10000; ++i) { 69 if (lock->lock.trylock()) { 70 return 0; 71 } 72 } 73 lock->lock.lock(); 74 return 0; 75} 76 77int pthread_spin_unlock(pthread_spinlock_t* lock_interface) { 78 pthread_spinlock_internal_t* lock = __get_internal_spinlock(lock_interface); 79 lock->lock.unlock(); 80 return 0; 81} 82