parent
33767364d1
commit
445d1e8201
@ -0,0 +1,54 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#include "Excepts.h"
|
||||||
|
|
||||||
|
#if defined(__APPLE__) || defined(__OSX__)
|
||||||
|
|
||||||
|
#include <fenv.h>
|
||||||
|
|
||||||
|
int fegetexcept(void) {
|
||||||
|
static fenv_t fenv;
|
||||||
|
return fegetenv(&fenv) ? -1 : (fenv.__control & FE_ALL_EXCEPT);
|
||||||
|
}
|
||||||
|
|
||||||
|
int feenableexcept(unsigned int excepts) {
|
||||||
|
static fenv_t fenv;
|
||||||
|
unsigned int new_excepts = excepts & FE_ALL_EXCEPT, old_excepts;
|
||||||
|
|
||||||
|
if ( fegetenv (&fenv) ) return -1;
|
||||||
|
old_excepts = fenv.__control & FE_ALL_EXCEPT;
|
||||||
|
|
||||||
|
// unmask
|
||||||
|
fenv.__control &= ~new_excepts;
|
||||||
|
fenv.__mxcsr &= ~(new_excepts << 7);
|
||||||
|
|
||||||
|
return ( fesetenv (&fenv) ? -1 : old_excepts );
|
||||||
|
}
|
||||||
|
|
||||||
|
int fedisableexcept(unsigned int excepts) {
|
||||||
|
static fenv_t fenv;
|
||||||
|
unsigned int new_excepts = excepts & FE_ALL_EXCEPT, old_excepts;
|
||||||
|
|
||||||
|
if ( fegetenv (&fenv) ) return -1;
|
||||||
|
old_excepts = fenv.__control & FE_ALL_EXCEPT;
|
||||||
|
|
||||||
|
// mask
|
||||||
|
fenv.__control |= new_excepts;
|
||||||
|
fenv.__mxcsr |= new_excepts << 7;
|
||||||
|
|
||||||
|
return ( fesetenv (&fenv) ? -1 : old_excepts );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,26 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#ifndef EXCEPTS_H_
|
||||||
|
#define EXCEPTS_H_
|
||||||
|
|
||||||
|
#if defined(__APPLE__) || defined(__OSX__)
|
||||||
|
|
||||||
|
int fegetexcept(void);
|
||||||
|
int feenableexcept(unsigned int excepts);
|
||||||
|
int fedisableexcept(unsigned int excepts);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // EXCEPTS_H_
|
@ -0,0 +1,106 @@
|
|||||||
|
/* 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 <dispatch/dispatch.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#ifndef PTHREAD_BARRIER_H_
|
||||||
|
#define PTHREAD_BARRIER_H_
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
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__
|
Loading…
Reference in new issue