sync_server.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef SYNC_SERVER_H
  2. #define SYNC_SERVER_H
  3. #include "fixed_types.h"
  4. #include "sync_api.h"
  5. #include "transport.h"
  6. #include "network.h"
  7. #include "packetize.h"
  8. #include "stable_iterator.h"
  9. #include <queue>
  10. #include <vector>
  11. #include <limits.h>
  12. #include <string.h>
  13. #include <unordered_map>
  14. class SimMutex
  15. {
  16. public:
  17. static const thread_id_t NO_OWNER = UINT_MAX;
  18. SimMutex();
  19. ~SimMutex();
  20. // returns true if the lock is owned by someone that is not this thread
  21. bool isLocked(thread_id_t thread_id);
  22. // returns the time when this thread owns the lock
  23. SubsecondTime lock(thread_id_t thread_id, SubsecondTime time);
  24. // try to take the lock in name of another thread, either waking them or adding them to the list
  25. bool lock_async(thread_id_t thread_id, thread_id_t thread_by, SubsecondTime time);
  26. // signals a waiter to continue and returns the next owner
  27. thread_id_t unlock(thread_id_t thread_id, SubsecondTime time);
  28. private:
  29. typedef std::queue<thread_id_t> ThreadQueue;
  30. ThreadQueue m_waiting;
  31. thread_id_t m_owner;
  32. };
  33. class SimCond
  34. {
  35. public:
  36. SimCond();
  37. ~SimCond();
  38. SubsecondTime wait(thread_id_t thread_id, SubsecondTime time, SimMutex * mux);
  39. thread_id_t signal(thread_id_t thread_id, SubsecondTime time);
  40. void broadcast(thread_id_t thread_id, SubsecondTime time);
  41. private:
  42. class CondWaiter
  43. {
  44. public:
  45. CondWaiter(thread_id_t thread_id, SimMutex * mutex)
  46. : m_thread_id(thread_id), m_mutex(mutex) {}
  47. thread_id_t m_thread_id;
  48. SimMutex * m_mutex;
  49. };
  50. typedef std::queue< CondWaiter > ThreadQueue;
  51. ThreadQueue m_waiting;
  52. };
  53. class SimBarrier
  54. {
  55. public:
  56. SimBarrier(UInt32 count);
  57. ~SimBarrier();
  58. SubsecondTime wait(thread_id_t thread_id, SubsecondTime time);
  59. private:
  60. typedef std::queue<thread_id_t> ThreadQueue;
  61. ThreadQueue m_waiting;
  62. UInt32 m_count;
  63. };
  64. class SyncServer
  65. {
  66. typedef std::unordered_map<carbon_mutex_t *, SimMutex> MutexVector;
  67. typedef std::unordered_map<carbon_cond_t *, SimCond> CondVector;
  68. typedef std::vector<SimBarrier> BarrierVector;
  69. MutexVector m_mutexes;
  70. CondVector m_conds;
  71. BarrierVector m_barriers;
  72. public:
  73. SyncServer();
  74. ~SyncServer();
  75. void mutexInit(thread_id_t thread_id, carbon_mutex_t *mux);
  76. std::pair<SubsecondTime, bool> mutexLock(thread_id_t thread_id, carbon_mutex_t *mux, bool tryLock, SubsecondTime time);
  77. SubsecondTime mutexUnlock(thread_id_t thread_id, carbon_mutex_t *mux, SubsecondTime time);
  78. void condInit(thread_id_t thread_id, carbon_cond_t *cond);
  79. SubsecondTime condWait(thread_id_t thread_id, carbon_cond_t *cond, carbon_mutex_t *mux, SubsecondTime time);
  80. SubsecondTime condSignal(thread_id_t thread_id, carbon_cond_t *cond, SubsecondTime time);
  81. SubsecondTime condBroadcast(thread_id_t thread_id, carbon_cond_t *cond, SubsecondTime time);
  82. void barrierInit(thread_id_t thread_id, carbon_barrier_t *barrier, UInt32 count);
  83. SubsecondTime barrierWait(thread_id_t thread_id, carbon_barrier_t *barrier, SubsecondTime time);
  84. private:
  85. SubsecondTime m_reschedule_cost;
  86. SimMutex * getMutex(carbon_mutex_t * mux, bool canCreate = true);
  87. SimCond * getCond(carbon_cond_t * cond, bool canCreate = true);
  88. };
  89. #endif // SYNC_SERVER_H