Locks.cpp 3.5 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

namespace paddle {
class SemaphorePrivate {
W
Wu Yi 已提交
22
 public:
Y
Yu Yang 已提交
23 24 25
  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
/// SpinLockPrivate

45 46
#ifdef PADDLE_USE_PTHREAD_SPINLOCK

Y
Yu Yang 已提交
47
class SpinLockPrivate {
W
Wu Yi 已提交
48
 public:
49 50 51 52 53 54
  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 已提交
55
  pthread_spinlock_t lock_;
56
  char padding_[64 - sizeof(pthread_spinlock_t)];
Y
Yu Yang 已提交
57 58
};

59
#else
Y
Yu Yang 已提交
60 61
// clang-format off
#include <cstddef>
62
#include <atomic>
Y
Yu Yang 已提交
63 64
// clang-format on

65
class SpinLockPrivate {
W
Wu Yi 已提交
66
 public:
67 68 69 70 71
  inline void lock() {
    while (lock_.test_and_set(std::memory_order_acquire)) {
    }
  }
  inline void unlock() { lock_.clear(std::memory_order_release); }
Y
Yu Yang 已提交
72

73 74 75
  std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
  char padding_[64 - sizeof(lock_)];  // Padding to cache line size
};
Y
Yu Yang 已提交
76

L
Liu Yiqun 已提交
77
#endif
78 79 80 81 82 83

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

84 85
/// ThreadBarrierPrivate

86
#ifdef PADDLE_USE_PTHREAD_BARRIER
Y
Yu Yang 已提交
87 88

class ThreadBarrierPrivate {
W
Wu Yi 已提交
89
 public:
Y
Yu Yang 已提交
90
  pthread_barrier_t barrier_;
91 92 93 94 95 96 97 98

  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 已提交
99 100
};

101
#else
Y
Yu Yang 已提交
102

103
class ThreadBarrierPrivate {
W
Wu Yi 已提交
104
 public:
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 133 134 135 136 137 138 139
  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 已提交
140

L
Liu Yiqun 已提交
141
#endif
142

143 144
/// ThreadBarrier

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

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