summaryrefslogtreecommitdiff
path: root/chromium/base/fuchsia/async_dispatcher.h
blob: 97342cce6a8021a0b2ecc0f95d24513a8f2a2c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_FUCHSIA_ASYNC_DISPATCHER_H_
#define BASE_FUCHSIA_ASYNC_DISPATCHER_H_

#include <lib/async/default.h>
#include <lib/async/dispatcher.h>

#include "base/containers/linked_list.h"
#include "base/fuchsia/scoped_zx_handle.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_checker.h"

namespace base {

// Implementation of dispatcher for Fuchsia's async library. It's necessary to
// run Fuchsia's library on chromium threads.
class AsyncDispatcher : public async_t {
 public:
  AsyncDispatcher();
  ~AsyncDispatcher();

  // Returns after running one or more tasks or waits until |deadline|.
  // Returns |ZX_OK| if some tasks were executed, |ZX_ERR_TIMED_OUT| - the
  // deadline expired, |ZX_ERR_CANCELED| - Stop() was called.
  zx_status_t DispatchOrWaitUntil(zx_time_t deadline);

  // If Run() is being executed then it will return as soon as possible (e.g.
  // finishing running the current task), otherwise the following Run() call
  // will quit immediately instead of waiting until deadline expires.
  void Stop();

 private:
  class WaitState;
  class TaskState;

  static zx_time_t NowOp(async_t* async);
  static zx_status_t BeginWaitOp(async_t* async, async_wait_t* wait);
  static zx_status_t CancelWaitOp(async_t* async, async_wait_t* wait);
  static zx_status_t PostTaskOp(async_t* async, async_task_t* task);
  static zx_status_t CancelTaskOp(async_t* async, async_task_t* task);
  static zx_status_t QueuePacketOp(async_t* async,
                                   async_receiver_t* receiver,
                                   const zx_packet_user_t* data);
  static zx_status_t SetGuestBellTrapOp(async_t* async,
                                        async_guest_bell_trap_t* trap,
                                        zx_handle_t guest,
                                        zx_vaddr_t addr,
                                        size_t length);

  // async_ops_t implementation. Called by corresponding *Op() methods above.
  zx_status_t BeginWait(async_wait_t* wait);
  zx_status_t CancelWait(async_wait_t* wait);
  zx_status_t PostTask(async_task_t* task);
  zx_status_t CancelTask(async_task_t* task);

  // Runs tasks in |task_list_| that have deadline in the past.
  void DispatchTasks();

  // Must be called while |lock_| is held.
  void RestartTimerLocked();

  THREAD_CHECKER(thread_checker_);

  ScopedZxHandle port_;
  ScopedZxHandle timer_;
  ScopedZxHandle stop_event_;

  LinkedList<WaitState> wait_list_;

  // |lock_| must be held when accessing |task_list_|.
  base::Lock lock_;

  LinkedList<TaskState> task_list_;

  DISALLOW_COPY_AND_ASSIGN(AsyncDispatcher);
};

}  // namespace base

#endif  // BASE_FUCHSIA_ASYNC_DISPATCHER_H_