sync_server.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "sync_server.h"
  2. #include "sync_client.h"
  3. #include "simulator.h"
  4. #include "thread_manager.h"
  5. #include "subsecond_time.h"
  6. #include "config.hpp"
  7. // -- SimMutex -- //
  8. SimMutex::SimMutex()
  9. : m_owner(NO_OWNER)
  10. { }
  11. SimMutex::~SimMutex()
  12. {
  13. #if 0 // Disabled: applications are not required to do proper cleanup
  14. if (! m_waiting.empty()) {
  15. printf("WARNING: Waiters remaining for SimMutex@%p: ", this);
  16. while(! m_waiting.empty()) {
  17. printf("%d ", m_waiting.front());
  18. m_waiting.pop();
  19. }
  20. printf("\n");
  21. }
  22. #endif
  23. }
  24. bool SimMutex::isLocked(thread_id_t thread_id)
  25. {
  26. if (m_owner == NO_OWNER)
  27. return false;
  28. else if (m_owner == thread_id)
  29. return false;
  30. else
  31. return true;
  32. }
  33. SubsecondTime SimMutex::lock(thread_id_t thread_id, SubsecondTime time)
  34. {
  35. if (m_owner == NO_OWNER)
  36. {
  37. m_owner = thread_id;
  38. return time;
  39. }
  40. else
  41. {
  42. m_waiting.push(thread_id);
  43. return Sim()->getThreadManager()->stallThread(thread_id, ThreadManager::STALL_MUTEX, time);
  44. }
  45. }
  46. bool SimMutex::lock_async(thread_id_t thread_id, thread_id_t thread_by, SubsecondTime time)
  47. {
  48. if (m_owner == NO_OWNER)
  49. {
  50. m_owner = thread_id;
  51. Sim()->getThreadManager()->resumeThread(m_owner, thread_by, time);
  52. return true;
  53. }
  54. else
  55. {
  56. m_waiting.push(thread_id);
  57. return false;
  58. }
  59. }
  60. thread_id_t SimMutex::unlock(thread_id_t thread_id, SubsecondTime time)
  61. {
  62. assert(m_owner == thread_id);
  63. if (m_waiting.empty())
  64. {
  65. m_owner = NO_OWNER;
  66. }
  67. else
  68. {
  69. thread_id_t waiter = m_waiting.front();
  70. m_waiting.pop();
  71. m_owner = waiter;
  72. Sim()->getThreadManager()->resumeThread(m_owner, thread_id, time);
  73. }
  74. return m_owner;
  75. }
  76. // -- SimCond -- //
  77. SimCond::SimCond() {}
  78. SimCond::~SimCond()
  79. {
  80. #if 0 // Disabled: applications are not required to do proper cleanup
  81. if (!m_waiting.empty()) {
  82. printf("Threads still waiting for SimCond@%p: ", this);
  83. while(!m_waiting.empty()) {
  84. printf("%u ", m_waiting.back().m_thread_id);
  85. m_waiting.pop();
  86. }
  87. printf("\n");
  88. }
  89. #endif
  90. }
  91. SubsecondTime SimCond::wait(thread_id_t thread_id, SubsecondTime time, SimMutex * simMux)
  92. {
  93. simMux->unlock(thread_id, time);
  94. m_waiting.push(CondWaiter(thread_id, simMux));
  95. return Sim()->getThreadManager()->stallThread(thread_id, ThreadManager::STALL_COND, time);
  96. }
  97. thread_id_t SimCond::signal(thread_id_t thread_id, SubsecondTime time)
  98. {
  99. // If there is a list of threads waiting, wake up one of them
  100. if (!m_waiting.empty())
  101. {
  102. CondWaiter woken = m_waiting.front();
  103. m_waiting.pop();
  104. if (woken.m_mutex->lock_async(woken.m_thread_id, thread_id, time))
  105. {
  106. // Woken up thread is able to grab lock immediately
  107. return woken.m_thread_id;
  108. }
  109. else
  110. {
  111. // Woken up thread is *NOT* able to grab lock immediately
  112. return INVALID_THREAD_ID;
  113. }
  114. }
  115. // There are *NO* threads waiting on the condition variable
  116. return INVALID_THREAD_ID;
  117. }
  118. void SimCond::broadcast(thread_id_t thread_id, SubsecondTime time)
  119. {
  120. while(!m_waiting.empty())
  121. {
  122. CondWaiter woken = m_waiting.front();
  123. m_waiting.pop();
  124. woken.m_mutex->lock_async(woken.m_thread_id, thread_id, time);
  125. }
  126. }
  127. // -- SimBarrier -- //
  128. SimBarrier::SimBarrier(UInt32 count)
  129. : m_count(count)
  130. {
  131. }
  132. SimBarrier::~SimBarrier()
  133. {
  134. if (!m_waiting.empty()) {
  135. printf("Threads still waiting for SimBarrier@%p: ", this);
  136. while(!m_waiting.empty()) {
  137. printf("%u ", m_waiting.back());
  138. m_waiting.pop();
  139. }
  140. printf("\n");
  141. }
  142. }
  143. SubsecondTime SimBarrier::wait(thread_id_t thread_id, SubsecondTime time)
  144. {
  145. // We are the last thread to reach the barrier
  146. if (m_waiting.size() == m_count - 1)
  147. {
  148. while(! m_waiting.empty())
  149. {
  150. // Resuming all the threads stalled at the barrier
  151. thread_id_t waiter = m_waiting.front();
  152. m_waiting.pop();
  153. Sim()->getThreadManager()->resumeThread(waiter, thread_id, time);
  154. }
  155. return time;
  156. }
  157. else
  158. {
  159. m_waiting.push(thread_id);
  160. return Sim()->getThreadManager()->stallThread(thread_id, ThreadManager::STALL_BARRIER, time);
  161. }
  162. }
  163. // -- SyncServer -- //
  164. SyncServer::SyncServer()
  165. {
  166. m_reschedule_cost = SubsecondTime::NS() * Sim()->getCfg()->getInt("perf_model/sync/reschedule_cost");
  167. }
  168. SyncServer::~SyncServer()
  169. { }
  170. SimMutex * SyncServer::getMutex(carbon_mutex_t *mux, bool canCreate)
  171. {
  172. // if mux is the address of a pthread mutex (with default initialization, not through pthread_mutex_init),
  173. // look it up in m_mutexes or create a new one if it's the first time we see it
  174. if (m_mutexes.count(mux))
  175. return &m_mutexes[mux];
  176. else if (canCreate)
  177. {
  178. m_mutexes[mux] = SimMutex();
  179. return &m_mutexes[mux];
  180. }
  181. else
  182. {
  183. LOG_ASSERT_ERROR(false, "Invalid mutex id passed");
  184. return NULL;
  185. }
  186. }
  187. void SyncServer::mutexInit(thread_id_t thread_id, carbon_mutex_t *mux)
  188. {
  189. ScopedLock sl(Sim()->getThreadManager()->getLock());
  190. getMutex(mux);
  191. }
  192. std::pair<SubsecondTime, bool> SyncServer::mutexLock(thread_id_t thread_id, carbon_mutex_t *mux, bool tryLock, SubsecondTime time)
  193. {
  194. ScopedLock sl(Sim()->getThreadManager()->getLock());
  195. SimMutex *psimmux = getMutex(mux);
  196. if (tryLock && psimmux->isLocked(thread_id))
  197. {
  198. // notify the owner of failure
  199. return std::make_pair(time, false);
  200. }
  201. else
  202. {
  203. SubsecondTime time_end = psimmux->lock(thread_id, time);
  204. return std::make_pair(time_end + m_reschedule_cost, true);
  205. }
  206. }
  207. SubsecondTime SyncServer::mutexUnlock(thread_id_t thread_id, carbon_mutex_t *mux, SubsecondTime time)
  208. {
  209. ScopedLock sl(Sim()->getThreadManager()->getLock());
  210. SimMutex *psimmux = getMutex(mux, false);
  211. thread_id_t new_owner = psimmux->unlock(thread_id, time + m_reschedule_cost);
  212. SubsecondTime new_time = time + (new_owner == SimMutex::NO_OWNER ? SubsecondTime::Zero() : m_reschedule_cost /* we had to call futex_wake */);
  213. return new_time;
  214. }
  215. // -- Condition Variable Stuffs -- //
  216. SimCond * SyncServer::getCond(carbon_cond_t *cond, bool canCreate)
  217. {
  218. // if cond is the address of a pthread cond (with default initialization, not through pthread_cond_init),
  219. // look it up in m_conds or create a new one if it's the first time we see it
  220. if (m_conds.count(cond))
  221. return &m_conds[cond];
  222. else if (canCreate)
  223. {
  224. m_conds[cond] = SimCond();
  225. return &m_conds[cond];
  226. }
  227. else
  228. {
  229. LOG_ASSERT_ERROR(false, "Invalid cond id passed");
  230. return NULL;
  231. }
  232. }
  233. void SyncServer::condInit(thread_id_t thread_id, carbon_cond_t *cond)
  234. {
  235. ScopedLock sl(Sim()->getThreadManager()->getLock());
  236. getCond(cond);
  237. }
  238. SubsecondTime SyncServer::condWait(thread_id_t thread_id, carbon_cond_t *cond, carbon_mutex_t *mux, SubsecondTime time)
  239. {
  240. ScopedLock sl(Sim()->getThreadManager()->getLock());
  241. SimMutex *psimmux = getMutex(mux);
  242. SimCond *psimcond = getCond(cond);
  243. return psimcond->wait(thread_id, time, psimmux);
  244. }
  245. SubsecondTime SyncServer::condSignal(thread_id_t thread_id, carbon_cond_t *cond, SubsecondTime time)
  246. {
  247. ScopedLock sl(Sim()->getThreadManager()->getLock());
  248. SimCond *psimcond = getCond(cond);
  249. psimcond->signal(thread_id, time);
  250. return time;
  251. }
  252. SubsecondTime SyncServer::condBroadcast(thread_id_t thread_id, carbon_cond_t *cond, SubsecondTime time)
  253. {
  254. ScopedLock sl(Sim()->getThreadManager()->getLock());
  255. SimCond *psimcond = getCond(cond);
  256. psimcond->broadcast(thread_id, time);
  257. return time;
  258. }
  259. void SyncServer::barrierInit(thread_id_t thread_id, carbon_barrier_t *barrier, UInt32 count)
  260. {
  261. ScopedLock sl(Sim()->getThreadManager()->getLock());
  262. m_barriers.push_back(SimBarrier(count));
  263. *barrier = (carbon_barrier_t)m_barriers.size()-1;
  264. }
  265. SubsecondTime SyncServer::barrierWait(thread_id_t thread_id, carbon_barrier_t *barrier, SubsecondTime time)
  266. {
  267. ScopedLock sl(Sim()->getThreadManager()->getLock());
  268. SimBarrier *psimbarrier = &m_barriers[*barrier];
  269. return psimbarrier->wait(thread_id, time);
  270. }