uv_task_runner.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
  5. #define ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
  6. #include <map>
  7. #include "base/callback.h"
  8. #include "base/task/single_thread_task_runner.h"
  9. #include "uv.h" // NOLINT(build/include_directory)
  10. namespace base {
  11. class Location;
  12. }
  13. namespace electron {
  14. // TaskRunner implementation that posts tasks into libuv's default loop.
  15. class UvTaskRunner : public base::SingleThreadTaskRunner {
  16. public:
  17. explicit UvTaskRunner(uv_loop_t* loop);
  18. // disable copy
  19. UvTaskRunner(const UvTaskRunner&) = delete;
  20. UvTaskRunner& operator=(const UvTaskRunner&) = delete;
  21. // base::SingleThreadTaskRunner:
  22. bool PostDelayedTask(const base::Location& from_here,
  23. base::OnceClosure task,
  24. base::TimeDelta delay) override;
  25. bool RunsTasksInCurrentSequence() const override;
  26. bool PostNonNestableDelayedTask(const base::Location& from_here,
  27. base::OnceClosure task,
  28. base::TimeDelta delay) override;
  29. private:
  30. ~UvTaskRunner() override;
  31. static void OnTimeout(uv_timer_t* timer);
  32. static void OnClose(uv_handle_t* handle);
  33. uv_loop_t* loop_;
  34. std::map<uv_timer_t*, base::OnceClosure> tasks_;
  35. };
  36. } // namespace electron
  37. #endif // ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_