sync_client.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "sync_client.h"
  2. #include "sync_server.h"
  3. #include "simulator.h"
  4. #include "thread.h"
  5. #include "thread_manager.h"
  6. #include "core.h"
  7. #include "performance_model.h"
  8. #include "instruction.h"
  9. #include <iostream>
  10. SyncClient::SyncClient(Thread *thread)
  11. : m_thread(thread)
  12. , m_server(Sim()->getSyncServer())
  13. {
  14. }
  15. SyncClient::~SyncClient()
  16. {
  17. }
  18. void SyncClient::mutexInit(carbon_mutex_t *mux)
  19. {
  20. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  21. m_server->mutexInit(thread->getId(), mux);
  22. }
  23. SubsecondTime SyncClient::mutexLock(carbon_mutex_t *mux, SubsecondTime delay)
  24. {
  25. return __mutexLock(mux, false, delay).first;
  26. }
  27. std::pair<SubsecondTime, bool> SyncClient::mutexTrylock(carbon_mutex_t *mux)
  28. {
  29. return __mutexLock(mux, true, SubsecondTime::Zero());
  30. }
  31. std::pair<SubsecondTime, bool> SyncClient::__mutexLock(carbon_mutex_t *mux, bool tryLock, SubsecondTime delay)
  32. {
  33. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  34. Core *core = thread->getCore();
  35. SubsecondTime start_time = core->getPerformanceModel()->getElapsedTime() + delay;
  36. std::pair<SubsecondTime, bool> result = m_server->mutexLock(thread->getId(), mux, tryLock, start_time);
  37. if (thread->reschedule(result.first, core))
  38. core = thread->getCore();
  39. core->getPerformanceModel()->queuePseudoInstruction(new SyncInstruction(result.first, SyncInstruction::PTHREAD_MUTEX));
  40. return std::pair<SubsecondTime, bool>(result.first > start_time ? result.first - start_time : SubsecondTime::Zero(), result.second);
  41. }
  42. SubsecondTime SyncClient::mutexUnlock(carbon_mutex_t *mux, SubsecondTime delay)
  43. {
  44. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  45. SubsecondTime start_time = thread->getCore()->getPerformanceModel()->getElapsedTime() + delay;
  46. SubsecondTime time = m_server->mutexUnlock(thread->getId(), mux, start_time);
  47. if (time > start_time)
  48. thread->getCore()->getPerformanceModel()->queuePseudoInstruction(new SyncInstruction(time, SyncInstruction::PTHREAD_MUTEX));
  49. return time > start_time ? time - start_time : SubsecondTime::Zero();
  50. }
  51. void SyncClient::condInit(carbon_cond_t *cond)
  52. {
  53. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  54. m_server->condInit(thread->getId(), cond);
  55. }
  56. SubsecondTime SyncClient::condWait(carbon_cond_t *cond, carbon_mutex_t *mux)
  57. {
  58. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  59. Core *core = thread->getCore();
  60. SubsecondTime start_time = core->getPerformanceModel()->getElapsedTime();
  61. SubsecondTime time = m_server->condWait(thread->getId(), cond, mux, start_time);
  62. if (thread->reschedule(time, core))
  63. core = thread->getCore();
  64. core->getPerformanceModel()->queuePseudoInstruction(new SyncInstruction(time, SyncInstruction::PTHREAD_COND));
  65. return time > start_time ? time - start_time : SubsecondTime::Zero();
  66. }
  67. SubsecondTime SyncClient::condSignal(carbon_cond_t *cond)
  68. {
  69. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  70. SubsecondTime start_time = thread->getCore()->getPerformanceModel()->getElapsedTime();
  71. m_server->condSignal(thread->getId(), cond, start_time);
  72. return SubsecondTime::Zero();
  73. }
  74. SubsecondTime SyncClient::condBroadcast(carbon_cond_t *cond)
  75. {
  76. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  77. SubsecondTime start_time = thread->getCore()->getPerformanceModel()->getElapsedTime();
  78. m_server->condBroadcast(thread->getId(), cond, start_time);
  79. return SubsecondTime::Zero();
  80. }
  81. void SyncClient::barrierInit(carbon_barrier_t *barrier, UInt32 count)
  82. {
  83. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  84. m_server->barrierInit(thread->getId(), barrier, count);
  85. }
  86. SubsecondTime SyncClient::barrierWait(carbon_barrier_t *barrier)
  87. {
  88. Thread *thread = Sim()->getThreadManager()->getCurrentThread();
  89. Core *core = thread->getCore();
  90. SubsecondTime start_time = core->getPerformanceModel()->getElapsedTime();
  91. SubsecondTime time = m_server->barrierWait(thread->getId(), barrier, start_time);
  92. if (thread->reschedule(time, core))
  93. core = thread->getCore();
  94. core->getPerformanceModel()->queuePseudoInstruction(new SyncInstruction(time, SyncInstruction::PTHREAD_BARRIER));
  95. return time > start_time ? time - start_time : SubsecondTime::Zero();
  96. }