/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve. 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. */ #ifdef __APPLE__ #include #endif #ifdef __APPLE__ #ifndef PTHREAD_BARRIER_H_ #define PTHREAD_BARRIER_H_ #include #include typedef int pthread_barrierattr_t; typedef struct { pthread_mutex_t mutex; pthread_cond_t cond; int count; int tripCount; } pthread_barrier_t; int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count) { if (count == 0) { errno = EINVAL; return -1; } if (pthread_mutex_init(&barrier->mutex, 0) < 0) { return -1; } if (pthread_cond_init(&barrier->cond, 0) < 0) { pthread_mutex_destroy(&barrier->mutex); return -1; } barrier->tripCount = count; barrier->count = 0; return 0; } int pthread_barrier_destroy(pthread_barrier_t *barrier) { pthread_cond_destroy(&barrier->cond); pthread_mutex_destroy(&barrier->mutex); return 0; } int pthread_barrier_wait(pthread_barrier_t *barrier) { pthread_mutex_lock(&barrier->mutex); ++(barrier->count); if (barrier->count >= barrier->tripCount) { barrier->count = 0; pthread_cond_broadcast(&barrier->cond); pthread_mutex_unlock(&barrier->mutex); return 1; } else { pthread_cond_wait(&barrier->cond, &(barrier->mutex)); pthread_mutex_unlock(&barrier->mutex); return 0; } } #endif // PTHREAD_BARRIER_H_ typedef int pthread_spinlock_t; int pthread_spin_init(pthread_spinlock_t *lock, int pshared) { __asm__ __volatile__("" ::: "memory"); *lock = 0; return 0; } int pthread_spin_destroy(pthread_spinlock_t *lock) { return 0; } int pthread_spin_lock(pthread_spinlock_t *lock) { while (1) { int i; for (i=0; i < 10000; i++) { if (__sync_bool_compare_and_swap(lock, 0, 1)) { return 0; } } sched_yield(); } } int pthread_spin_unlock(pthread_spinlock_t *lock) { __asm__ __volatile__("" ::: "memory"); *lock = 0; return 0; } #endif // __APPLE__