This contains my bachelors thesis and associated tex files, code snippets and maybe more. Topic: Data Movement in Heterogeneous Memories with Intel Data Streaming Accelerator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
825 B

  1. #pragma once
  2. #include <mutex>
  3. #include <atomic>
  4. #include <semaphore.h>
  5. class barrier {
  6. private:
  7. std::mutex mutex_;
  8. uint32_t waiting_count_;
  9. const uint32_t barrier_size_;
  10. sem_t semaphore_;
  11. barrier(const barrier& other) = delete;
  12. public:
  13. barrier(const uint32_t size);
  14. ~barrier();
  15. void wait();
  16. void signal();
  17. };
  18. inline barrier::~barrier() {
  19. sem_destroy(&semaphore_);
  20. }
  21. inline barrier::barrier(const uint32_t size) : barrier_size_(size) {
  22. sem_init(&semaphore_, 0, 0);
  23. waiting_count_ = 0;
  24. }
  25. inline void barrier::wait() {
  26. mutex_.lock();
  27. if (++waiting_count_ >= barrier_size_) {
  28. for (uint32_t i = 1; i < waiting_count_; i++) sem_post(&semaphore_);
  29. mutex_.unlock();
  30. }
  31. else {
  32. mutex_.unlock();
  33. sem_wait(&semaphore_);
  34. }
  35. }