barrier.h 3.4 KB
Newer Older
L
lxsbupt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

17 18
#if defined _WIN32 || defined __APPLE__
#else
19
#define _LINUX
20 21
#endif

22
#ifdef _LINUX
L
lxsbupt 已提交
23 24 25
#include <pthread.h>
#include <semaphore.h>
#endif
26 27
#include <condition_variable>
#include <mutex>
L
lxsbupt 已提交
28 29 30 31 32 33 34
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
class Barrier {
 public:
  explicit Barrier(int count = 1) {
35
#ifdef _LINUX
L
lxsbupt 已提交
36
    CHECK_GE(count, 1);
37 38
    int ret = pthread_barrier_init(&_barrier, NULL, count);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
39 40 41
#endif
  }
  ~Barrier() {
42 43 44
#ifdef _LINUX
    int ret = pthread_barrier_destroy(&_barrier);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
45 46 47
#endif
  }
  void reset(int count) {
48
#ifdef _LINUX
L
lxsbupt 已提交
49
    CHECK_GE(count, 1);
50 51 52 53
    int ret = pthread_barrier_destroy(&_barrier);
    CHECK_EQ(0, ret);
    ret = pthread_barrier_init(&_barrier, NULL, count);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
54 55 56 57
#endif
  }

  void wait() {
58
#ifdef _LINUX
L
lxsbupt 已提交
59
    int err = pthread_barrier_wait(&_barrier);
60 61
    err = pthread_barrier_wait(&_barrier);
    CHECK_EQ(true, (err == 0 || err == PTHREAD_BARRIER_SERIAL_THREAD));
L
lxsbupt 已提交
62 63 64 65
#endif
  }

 private:
66
#ifdef _LINUX
L
lxsbupt 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  pthread_barrier_t _barrier;
#endif
};
// Call func(args...). If interrupted by signal, recall the function.
template <class FUNC, class... ARGS>
auto ignore_signal_call(FUNC &&func, ARGS &&...args) ->
    typename std::result_of<FUNC(ARGS...)>::type {
  for (;;) {
    auto err = func(args...);

    if (err < 0 && errno == EINTR) {
      LOG(INFO) << "Signal is caught. Ignored.";
      continue;
    }
    return err;
  }
}
class Semaphore {
 public:
  Semaphore() {
87 88 89
#ifdef _LINUX
    int ret = sem_init(&_sem, 0, 0);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
90 91 92
#endif
  }
  ~Semaphore() {
93 94 95
#ifdef _LINUX
    int ret = sem_destroy(&_sem);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
96 97 98
#endif
  }
  void post() {
99 100 101
#ifdef _LINUX
    int ret = sem_post(&_sem);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
102 103 104
#endif
  }
  void wait() {
105 106 107
#ifdef _LINUX
    int ret = ignore_signal_call(sem_wait, &_sem);
    CHECK_EQ(0, ret);
L
lxsbupt 已提交
108 109 110 111
#endif
  }
  bool try_wait() {
    int err = 0;
112 113 114
#ifdef _LINUX
    err = ignore_signal_call(sem_trywait, &_sem);
    CHECK_EQ(true, (err == 0 || errno == EAGAIN));
L
lxsbupt 已提交
115 116 117 118 119
#endif
    return err == 0;
  }

 private:
120
#ifdef _LINUX
L
lxsbupt 已提交
121 122 123
  sem_t _sem;
#endif
};
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
class WaitGroup {
 public:
  WaitGroup() {}
  void clear() {
    std::lock_guard<std::mutex> lock(mutex_);
    counter_ = 0;
    cond_.notify_all();
  }
  void add(int delta) {
    if (delta == 0) {
      return;
    }

    std::lock_guard<std::mutex> lock(mutex_);
    counter_ += delta;
    if (counter_ == 0) {
      cond_.notify_all();
    }
  }
  void done() { add(-1); }
  void wait() {
    std::unique_lock<std::mutex> lock(mutex_);

    while (counter_ != 0) {
      cond_.wait(lock);
    }
  }
  int count(void) {
    std::unique_lock<std::mutex> lock(mutex_);
    return counter_;
  }

 private:
  std::mutex mutex_;
  std::condition_variable cond_;
  int counter_ = 0;
};
L
lxsbupt 已提交
161 162
}  // namespace framework
}  // namespace paddle