thread.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. \file thread.cpp
  3. \author Seung Geol Choi
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Receiver Thread Implementation
  17. */
  18. #include "thread.h"
  19. #include <cassert>
  20. #include <condition_variable>
  21. #include <mutex>
  22. #include <thread>
  23. CThread::CThread() : m_bRunning(false) {
  24. }
  25. CThread::~CThread() {
  26. assert(!m_bRunning);
  27. }
  28. bool CThread::Start() {
  29. thread_ = std::thread([this] { ThreadMain(); });
  30. m_bRunning = true;
  31. return true;
  32. }
  33. bool CThread::Wait() {
  34. if (!m_bRunning)
  35. return true;
  36. m_bRunning = false;
  37. thread_.join();
  38. return true;
  39. }
  40. bool CThread::IsRunning() const {
  41. return m_bRunning;
  42. }
  43. void CLock::Lock() {
  44. mutex_.lock();
  45. }
  46. void CLock::Unlock() {
  47. mutex_.unlock();
  48. }
  49. void CLock::lock() {
  50. Lock();
  51. }
  52. void CLock::unlock() {
  53. Unlock();
  54. }
  55. CEvent::CEvent(bool bManualReset, bool bInitialSet)
  56. : m_bManual(bManualReset), m_bSet(bInitialSet)
  57. {
  58. }
  59. bool CEvent::Set() {
  60. std::unique_lock<std::mutex> lock(mutex_);
  61. if (m_bSet)
  62. return true;
  63. m_bSet = true;
  64. lock.unlock();
  65. cv_.notify_one();
  66. return true;
  67. }
  68. bool CEvent::Wait() {
  69. std::unique_lock<std::mutex> lock(mutex_);
  70. cv_.wait(lock, [this]{ return m_bSet; });
  71. if (!m_bManual)
  72. m_bSet = false;
  73. return true;
  74. }
  75. bool CEvent::IsSet() const {
  76. std::lock_guard<std::mutex> lock(mutex_);
  77. return m_bSet;
  78. }
  79. bool CEvent::Reset() {
  80. std::lock_guard<std::mutex> lock(mutex_);
  81. m_bSet = false;
  82. return true;
  83. }