1
0

api_threadpool.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #include <windows.h>
  3. #include <bfc/platform/types.h>
  4. #include <bfc/dispatch.h>
  5. class ThreadID;
  6. class api_threadpool : public Dispatchable
  7. {
  8. protected:
  9. api_threadpool() {}
  10. ~api_threadpool() {}
  11. public:
  12. typedef int (*ThreadPoolFunc)(HANDLE handle, void *user_data, intptr_t id);
  13. enum
  14. {
  15. // pass this flag to AddHandle or RunFunction indicate that your thread function
  16. // might run for a long time
  17. FLAG_LONG_EXECUTION = 0x1,
  18. FLAG_REQUIRE_COM_STA = 0x2,
  19. FLAG_REQUIRE_COM_MT = 0x4,
  20. MASK_COM_FLAGS = 0x6,
  21. };
  22. public:
  23. ThreadID *ReserveThread(int flags);
  24. /* Release a thread you've previously reserved */
  25. void ReleaseThread(ThreadID *thread_id);
  26. /* adds a waitable handle to the thread pool. when the event is signalled, your function ptr will get called
  27. user_data and id values get passed to your function.
  28. your function should return 1 to indicate that it can be removed
  29. flags, see api_threadpool */
  30. int AddHandle(ThreadID *threadid, HANDLE handle, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags);
  31. void RemoveHandle(ThreadID *threadid, HANDLE handle);
  32. int RunFunction(ThreadID *threadid, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags);
  33. size_t GetNumberOfThreads(); // total number of threads in the threadpool
  34. size_t GetNumberOfActiveThreads(); // number of threads that are currently being used (inside user function but not necessarily busy)
  35. enum
  36. {
  37. RESERVETHREAD = 0,
  38. RELEASETHREAD = 1,
  39. ADDHANDLE = 2,
  40. REMOVEHANDLE = 3,
  41. RUNFUNCTION = 4,
  42. GETNUMBEROFTHREADS = 5,
  43. GETNUMBEROFACTIVETHREADS = 6,
  44. };
  45. };
  46. inline ThreadID *api_threadpool::ReserveThread(int flags)
  47. {
  48. return _call(RESERVETHREAD, (ThreadID *)0, flags);
  49. }
  50. inline void api_threadpool::ReleaseThread(ThreadID *thread_id)
  51. {
  52. _voidcall(RELEASETHREAD, thread_id);
  53. }
  54. inline int api_threadpool::AddHandle(ThreadID *threadid, HANDLE handle, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags)
  55. {
  56. return _call(ADDHANDLE, (int)1, threadid, handle, func, user_data, id, flags);
  57. }
  58. inline void api_threadpool::RemoveHandle(ThreadID *threadid, HANDLE handle)
  59. {
  60. _voidcall(REMOVEHANDLE, threadid, handle);
  61. }
  62. inline int api_threadpool::RunFunction(ThreadID *threadid, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags)
  63. {
  64. return _call(RUNFUNCTION, (int)1, threadid, func, user_data, id, flags);
  65. }
  66. inline size_t api_threadpool::GetNumberOfThreads()
  67. {
  68. return _call(GETNUMBEROFTHREADS, (size_t)0);
  69. }
  70. inline size_t api_threadpool::GetNumberOfActiveThreads()
  71. {
  72. return _call(GETNUMBEROFACTIVETHREADS, (size_t)0);
  73. }
  74. // {4DE015D3-11D8-4ac6-A3E6-216DF5252107}
  75. static const GUID ThreadPoolGUID =
  76. { 0x4de015d3, 0x11d8, 0x4ac6, { 0xa3, 0xe6, 0x21, 0x6d, 0xf5, 0x25, 0x21, 0x7 } };