Locks.cpp 3.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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 "paddle/utils/Locks.h"
#include "paddle/utils/Logging.h"
#include <dispatch/dispatch.h>
#include <libkern/OSAtomic.h>
G
gangliao 已提交
19 20 21 22 23

#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
#include <os/lock.h>
#endif

Y
Yu Yang 已提交
24
namespace paddle {
25

Y
Yu Yang 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
class SemaphorePrivate {
public:
  ~SemaphorePrivate() {
    dispatch_release(sem);
  }

  dispatch_semaphore_t sem;
};

Semaphore::Semaphore(int initValue): m(new SemaphorePrivate()) {
  m->sem = dispatch_semaphore_create(initValue);
}

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

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);
}

void Semaphore::post() {
  dispatch_semaphore_signal(m->sem);
}

class SpinLockPrivate {
public:
G
gangliao 已提交
58 59 60
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
  os_unfair_lock lock_;
#else
L
liaogang 已提交
61
  SpinLockPrivate(): lock_(OS_SPINLOCK_INIT) {}
Y
Yu Yang 已提交
62
  OSSpinLock lock_;
G
gangliao 已提交
63 64
#endif
  char padding_[64 - sizeof(lock_)];  // Padding to cache line size
Y
Yu Yang 已提交
65 66 67 68 69 70
};

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

void SpinLock::lock() {
G
gangliao 已提交
71 72 73
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
  os_unfair_lock_lock(&m->lock_);
#else
Y
Yu Yang 已提交
74
  OSSpinLockLock(&m->lock_);
G
gangliao 已提交
75
#endif
Y
Yu Yang 已提交
76 77 78
}

void SpinLock::unlock() {
G
gangliao 已提交
79 80 81
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
  os_unfair_lock_unlock(&m->lock_);
#else
Y
Yu Yang 已提交
82
  OSSpinLockUnlock(&m->lock_);
G
gangliao 已提交
83
#endif
Y
Yu Yang 已提交
84 85 86 87 88
}


class ThreadBarrierPrivate {
public:
89 90 91 92
  pthread_mutex_t mutex_;
  pthread_cond_t cond_;
  int count_;
  int tripCount_;
Y
Yu Yang 已提交
93

94
  inline explicit ThreadBarrierPrivate(int cnt):count_(0), tripCount_(cnt) {
Y
Yu Yang 已提交
95
    CHECK_NE(cnt, 0);
96 97
    CHECK_GE(pthread_mutex_init(&mutex_, 0), 0);
    CHECK_GE(pthread_cond_init(&cond_, 0), 0);
Y
Yu Yang 已提交
98 99 100
  }

  inline ~ThreadBarrierPrivate() {
101 102
    pthread_cond_destroy(&cond_);
    pthread_mutex_destroy(&mutex_);
Y
Yu Yang 已提交
103 104 105 106 107 108 109
  }

  /**
   * @brief wait
   * @return true if the last wait
   */
  inline bool wait() {
110 111 112 113 114 115
    pthread_mutex_lock(&mutex_);
    ++count_;
    if (count_ >= tripCount_) {
      count_ = 0;
      pthread_cond_broadcast(&cond_);
      pthread_mutex_unlock(&mutex_);
Y
Yu Yang 已提交
116 117
      return true;
    } else {
118 119
      pthread_cond_wait(&cond_, &mutex_);
      pthread_mutex_unlock(&mutex_);
Y
Yu Yang 已提交
120 121 122 123 124 125 126 127 128 129
      return false;
    }
  }
};

ThreadBarrier::ThreadBarrier(int count): m(new ThreadBarrierPrivate(count)) {}
ThreadBarrier::~ThreadBarrier() { delete m; }
void ThreadBarrier::wait() { m->wait(); }

}  // namespace paddle