Locks.cpp 3.3 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
#include <semaphore.h>
#include <unistd.h>
18
#include "paddle/utils/Logging.h"
Y
Yu Yang 已提交
19 20 21 22 23 24 25

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

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

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

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

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

41
void Semaphore::post() { sem_post(&m->sem); }
Y
Yu Yang 已提交
42

43 44
#ifdef PADDLE_USE_PTHREAD_SPINLOCK

Y
Yu Yang 已提交
45 46
class SpinLockPrivate {
public:
47 48 49 50 51 52
  inline SpinLockPrivate() { pthread_spin_init(&lock_, 0); }
  inline ~SpinLockPrivate() { pthread_spin_destroy(&lock_); }

  inline void lock() { pthread_spin_lock(&lock_); }
  inline void unlock() { pthread_spin_unlock(&lock_); }

Y
Yu Yang 已提交
53
  pthread_spinlock_t lock_;
54
  char padding_[64 - sizeof(pthread_spinlock_t)];
Y
Yu Yang 已提交
55 56
};

57
#else
Y
Yu Yang 已提交
58

59 60 61 62 63 64 65 66
#include <atomic>
class SpinLockPrivate {
public:
  inline void lock() {
    while (lock_.test_and_set(std::memory_order_acquire)) {
    }
  }
  inline void unlock() { lock_.clear(std::memory_order_release); }
Y
Yu Yang 已提交
67

68 69 70
  std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
  char padding_[64 - sizeof(lock_)];  // Padding to cache line size
};
Y
Yu Yang 已提交
71

L
Liu Yiqun 已提交
72
#endif
73 74 75 76 77 78 79

SpinLock::SpinLock() : m(new SpinLockPrivate()) {}
SpinLock::~SpinLock() { delete m; }
void SpinLock::lock() { m->lock(); }
void SpinLock::unlock() { m->unlock(); }

#ifdef PADDLE_USE_PTHREAD_BARRIER
Y
Yu Yang 已提交
80 81 82 83

class ThreadBarrierPrivate {
public:
  pthread_barrier_t barrier_;
84 85 86 87 88 89 90 91

  inline explicit ThreadBarrierPrivate(int count) {
    pthread_barrier_init(&barrier_, nullptr, count);
  }

  inline ~ThreadBarrierPrivate() { pthread_barrier_destroy(&barrier_); }

  inline void wait() { pthread_barrier_wait(&barrier_); }
Y
Yu Yang 已提交
92 93
};

94
#else
Y
Yu Yang 已提交
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
class ThreadBarrierPrivate {
public:
  pthread_mutex_t mutex_;
  pthread_cond_t cond_;
  int count_;
  int tripCount_;

  inline explicit ThreadBarrierPrivate(int cnt) : count_(0), tripCount_(cnt) {
    CHECK_NE(cnt, 0);
    CHECK_GE(pthread_mutex_init(&mutex_, 0), 0);
    CHECK_GE(pthread_cond_init(&cond_, 0), 0);
  }

  inline ~ThreadBarrierPrivate() {
    pthread_cond_destroy(&cond_);
    pthread_mutex_destroy(&mutex_);
  }

  /**
   * @brief wait
   * @return true if the last wait
   */
  inline bool wait() {
    pthread_mutex_lock(&mutex_);
    ++count_;
    if (count_ >= tripCount_) {
      count_ = 0;
      pthread_cond_broadcast(&cond_);
      pthread_mutex_unlock(&mutex_);
      return true;
    } else {
      pthread_cond_wait(&cond_, &mutex_);
      pthread_mutex_unlock(&mutex_);
      return false;
    }
  }
};
Y
Yu Yang 已提交
133

L
Liu Yiqun 已提交
134
#endif
135 136 137 138

ThreadBarrier::ThreadBarrier(int count) : m(new ThreadBarrierPrivate(count)) {}
ThreadBarrier::~ThreadBarrier() { delete m; }
void ThreadBarrier::wait() { m->wait(); }
Y
Yu Yang 已提交
139

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