Locks.cpp 2.7 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. */

X
Xin Pan 已提交
15
#include "paddle/legacy/utils/Locks.h"
Y
Yu Yang 已提交
16 17
#include <dispatch/dispatch.h>
#include <libkern/OSAtomic.h>
Y
Yu Yang 已提交
18
#include <atomic>
X
Xin Pan 已提交
19
#include "paddle/legacy/utils/Logging.h"
G
gangliao 已提交
20

Y
Yu Yang 已提交
21
namespace paddle {
22

Y
Yu Yang 已提交
23
class SemaphorePrivate {
W
Wu Yi 已提交
24
 public:
25
  ~SemaphorePrivate() { dispatch_release(sem); }
Y
Yu Yang 已提交
26 27 28 29

  dispatch_semaphore_t sem;
};

30
Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) {
Y
Yu Yang 已提交
31 32 33
  m->sem = dispatch_semaphore_create(initValue);
}

34
Semaphore::~Semaphore() { delete m; }
Y
Yu Yang 已提交
35 36 37 38 39 40 41 42 43 44

bool Semaphore::timeWait(timespec *ts) {
  dispatch_time_t tm = dispatch_walltime(ts, 0);
  return (0 == dispatch_semaphore_wait(m->sem, tm));
}

void Semaphore::wait() {
  dispatch_semaphore_wait(m->sem, DISPATCH_TIME_FOREVER);
}

45
void Semaphore::post() { dispatch_semaphore_signal(m->sem); }
Y
Yu Yang 已提交
46 47

class SpinLockPrivate {
W
Wu Yi 已提交
48
 public:
49
  std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
G
gangliao 已提交
50
  char padding_[64 - sizeof(lock_)];  // Padding to cache line size
Y
Yu Yang 已提交
51 52
};

53
SpinLock::SpinLock() : m(new SpinLockPrivate()) {}
Y
Yu Yang 已提交
54 55 56
SpinLock::~SpinLock() { delete m; }

void SpinLock::lock() {
57 58
  while (m->lock_.test_and_set(std::memory_order_acquire)) {
  }
Y
Yu Yang 已提交
59 60
}

61
void SpinLock::unlock() { m->lock_.clear(std::memory_order_release); }
Y
Yu Yang 已提交
62 63

class ThreadBarrierPrivate {
W
Wu Yi 已提交
64
 public:
65 66 67 68
  pthread_mutex_t mutex_;
  pthread_cond_t cond_;
  int count_;
  int tripCount_;
Y
Yu Yang 已提交
69

70
  inline explicit ThreadBarrierPrivate(int cnt) : count_(0), tripCount_(cnt) {
Y
Yu Yang 已提交
71
    CHECK_NE(cnt, 0);
72 73
    CHECK_GE(pthread_mutex_init(&mutex_, 0), 0);
    CHECK_GE(pthread_cond_init(&cond_, 0), 0);
Y
Yu Yang 已提交
74 75 76
  }

  inline ~ThreadBarrierPrivate() {
77 78
    pthread_cond_destroy(&cond_);
    pthread_mutex_destroy(&mutex_);
Y
Yu Yang 已提交
79 80 81 82 83 84 85
  }

  /**
   * @brief wait
   * @return true if the last wait
   */
  inline bool wait() {
86 87 88 89 90 91
    pthread_mutex_lock(&mutex_);
    ++count_;
    if (count_ >= tripCount_) {
      count_ = 0;
      pthread_cond_broadcast(&cond_);
      pthread_mutex_unlock(&mutex_);
Y
Yu Yang 已提交
92 93
      return true;
    } else {
94 95
      pthread_cond_wait(&cond_, &mutex_);
      pthread_mutex_unlock(&mutex_);
Y
Yu Yang 已提交
96 97 98 99 100
      return false;
    }
  }
};

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

}  // namespace paddle