Locks.cpp 2.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Y
Yu Yang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yu Yang 已提交
15
#include "paddle/utils/Locks.h"
Y
Yu Yang 已提交
16 17 18 19 20 21 22 23 24
#include <semaphore.h>
#include <unistd.h>

namespace paddle {
class SemaphorePrivate {
public:
  sem_t sem;
};

25
Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) {
Y
Yu Yang 已提交
26 27 28
  sem_init(&m->sem, 0, initValue);
}

29
Semaphore::~Semaphore() { sem_destroy(&m->sem); }
Y
Yu Yang 已提交
30 31 32 33 34

bool Semaphore::timeWait(struct timespec* ts) {
  return (0 == sem_timedwait(&m->sem, ts));
}

35
void Semaphore::wait() { sem_wait(&m->sem); }
Y
Yu Yang 已提交
36

37
void Semaphore::post() { sem_post(&m->sem); }
Y
Yu Yang 已提交
38 39 40

class SpinLockPrivate {
public:
L
Liu Yiqun 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
  inline SpinLockPrivate() {
#ifndef __ANDROID__
    pthread_spin_init(&lock_, 0);
#else
    lock_ = 0;
#endif
  }
  inline ~SpinLockPrivate() {
#ifndef __ANDROID__
    pthread_spin_destroy(&lock_);
#endif
  }
#ifndef __ANDROID__
Y
Yu Yang 已提交
54
  pthread_spinlock_t lock_;
L
Liu Yiqun 已提交
55 56 57 58
#else
  unsigned long lock_;
#endif
  char padding_[64 - sizeof(lock_)];
Y
Yu Yang 已提交
59 60
};

61
SpinLock::SpinLock() : m(new SpinLockPrivate()) {}
Y
Yu Yang 已提交
62 63 64

SpinLock::~SpinLock() { delete m; }

L
Liu Yiqun 已提交
65 66 67 68 69
void SpinLock::lock() {
#ifndef __ANDROID__
  pthread_spin_lock(&m->lock_);
#endif
}
Y
Yu Yang 已提交
70

L
Liu Yiqun 已提交
71 72 73 74 75
void SpinLock::unlock() {
#ifndef __ANDROID__
  pthread_spin_unlock(&m->lock_);
#endif
}
Y
Yu Yang 已提交
76 77 78

class ThreadBarrierPrivate {
public:
L
Liu Yiqun 已提交
79
#ifndef __ANDROID__
Y
Yu Yang 已提交
80
  pthread_barrier_t barrier_;
L
Liu Yiqun 已提交
81 82 83
#else
  unsigned long barrier_;
#endif
Y
Yu Yang 已提交
84 85
};

86
ThreadBarrier::ThreadBarrier(int count) : m(new ThreadBarrierPrivate()) {
L
Liu Yiqun 已提交
87
#ifndef __ANDROID__
Y
Yu Yang 已提交
88
  pthread_barrier_init(&m->barrier_, nullptr, count);
L
Liu Yiqun 已提交
89
#endif
Y
Yu Yang 已提交
90 91 92
}

ThreadBarrier::~ThreadBarrier() {
L
Liu Yiqun 已提交
93
#ifndef __ANDROID__
Y
Yu Yang 已提交
94
  pthread_barrier_destroy(&m->barrier_);
L
Liu Yiqun 已提交
95
#endif
Y
Yu Yang 已提交
96 97 98
  delete m;
}

L
Liu Yiqun 已提交
99 100 101 102 103
void ThreadBarrier::wait() {
#ifndef __ANDROID__
  pthread_barrier_wait(&m->barrier_);
#endif
}
Y
Yu Yang 已提交
104

Y
Yu Yang 已提交
105
}  // namespace paddle