thread_manager.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef THREAD_MANAGER_H
  2. #define THREAD_MANAGER_H
  3. #include "fixed_types.h"
  4. #include "semaphore.h"
  5. #include "core.h"
  6. #include "lock.h"
  7. #include "subsecond_time.h"
  8. #include <vector>
  9. #include <queue>
  10. class TLS;
  11. class Thread;
  12. class Scheduler;
  13. class ThreadManager
  14. {
  15. public:
  16. typedef void *(*thread_func_t)(void *);
  17. enum stall_type_t {
  18. STALL_UNSCHEDULED, // Thread is not scheduled on any core
  19. STALL_BROKEN, // Thread is on a core that suffered hardware failure
  20. STALL_JOIN, // Thread is calling pthread_join
  21. STALL_MUTEX, // Thread is calling pthread_mutex_lock
  22. STALL_COND, // Thread is calling pthread_cond_wait
  23. STALL_BARRIER, // Thread is calling pthread_barrier_wait
  24. STALL_FUTEX, // Thread is calling syscall(SYS_futex, FUTEX_WAIT)
  25. STALL_PAUSE, // pause system call
  26. STALL_SLEEP, // sleep system call
  27. STALL_SYSCALL, // blocking system call
  28. STALL_TYPES_MAX,
  29. };
  30. static const char* stall_type_names[];
  31. ThreadManager();
  32. ~ThreadManager();
  33. Lock &getLock() { return m_thread_lock; }
  34. Scheduler *getScheduler() const { return m_scheduler; }
  35. Thread* createThread(app_id_t app_id, thread_id_t creator_thread_id);
  36. Thread *getThreadFromID(thread_id_t thread_id);
  37. Thread *getCurrentThread(int threadIndex = -1);
  38. UInt64 getNumThreads() const { return m_threads.size(); }
  39. Core::State getThreadState(thread_id_t thread_id) const { return m_thread_state.at(thread_id).status; }
  40. stall_type_t getThreadStallReason(thread_id_t thread_id) const { return m_thread_state.at(thread_id).stalled_reason; }
  41. Thread *findThreadByTid(pid_t tid);
  42. // services
  43. thread_id_t spawnThread(thread_id_t thread_id, app_id_t app_id);
  44. void joinThread(thread_id_t thread_id, thread_id_t join_thread_id);
  45. thread_id_t getThreadToSpawn(SubsecondTime &time);
  46. void waitForThreadStart(thread_id_t thread_id, thread_id_t wait_thread_id);
  47. // events
  48. void onThreadStart(thread_id_t thread_id, SubsecondTime time);
  49. void onThreadExit(thread_id_t thread_id);
  50. // misc
  51. SubsecondTime stallThread(thread_id_t thread_id, stall_type_t reason, SubsecondTime time);
  52. void stallThread_async(thread_id_t thread_id, stall_type_t reason, SubsecondTime time);
  53. void resumeThread(thread_id_t thread_id, thread_id_t thread_id_by, SubsecondTime time, void *msg = NULL);
  54. void resumeThread_async(thread_id_t thread_id, thread_id_t thread_id_by, SubsecondTime time, void *msg = NULL);
  55. bool isThreadRunning(thread_id_t thread_id);
  56. bool isThreadInitializing(thread_id_t thread_id);
  57. bool anyThreadRunning();
  58. void moveThread(thread_id_t thread_id, core_id_t core_id, SubsecondTime time);
  59. bool areAllCoresRunning();
  60. private:
  61. struct ThreadSpawnRequest
  62. {
  63. thread_id_t thread_by;
  64. thread_id_t thread_id;
  65. SubsecondTime time;
  66. };
  67. struct ThreadState
  68. {
  69. Core::State status;
  70. stall_type_t stalled_reason; //< If status == Core::STALLED, why?
  71. thread_id_t waiter;
  72. ThreadState() : status(Core::IDLE), waiter(INVALID_THREAD_ID) {}
  73. };
  74. Lock m_thread_lock;
  75. std::vector<ThreadState> m_thread_state;
  76. std::queue<ThreadSpawnRequest> m_thread_spawn_list;
  77. std::vector<Thread*> m_threads;
  78. TLS *m_thread_tls;
  79. Scheduler *m_scheduler;
  80. Thread* createThread_unlocked(app_id_t app_id, thread_id_t creator_thread_id);
  81. void wakeUpWaiter(thread_id_t thread_id, SubsecondTime time);
  82. };
  83. #endif // THREAD_MANAGER_H