Mistake on this page?
Report an issue in GitHub or email us
AsyncOp.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef MBED_ASYNC_OP_H
19 #define MBED_ASYNC_OP_H
20 
21 #include "Mutex.h"
22 #include "Semaphore.h"
23 #include "Callback.h"
24 
25 #include "LinkEntry.h"
26 #include "OperationListBase.h"
27 
28 /** \defgroup mbed-os-internal Internal API */
29 
30 /** \addtogroup drivers-internal-api Drivers
31  * \ingroup mbed-os-internal
32  */
33 
34 /** \defgroup drivers-internal-api-usb USB
35  * \ingroup drivers-internal-api
36  */
37 
38 /**
39  * \defgroup drivers_AsyncOp AsyncOp class
40  * \ingroup drivers-internal-api-usb
41  * @{
42  */
43 class AsyncOp: public LinkEntry {
44 public:
45 
46  /**
47  * Construct a new AsyncOp object
48  */
49  AsyncOp();
50 
51  /**
52  * Construct a new AsyncOp object
53  *
54  * @param callback Completion callback
55  */
57 
58  /**
59  * Cleanup resources used by this AsyncOp
60  */
61  virtual ~AsyncOp();
62 
63  /**
64  * Wait for this asynchronous operation to complete
65  *
66  * If the timeout expires then this asynchronous operation is
67  * aborted and the timeout flag is set.
68  *
69  * @note - the host object's lock MUST NOT be held when this call is made
70  */
71  void wait(rtos::Mutex *host_mutex, rtos::Kernel::Clock::duration_u32 rel_time = rtos::Kernel::wait_for_u32_forever);
72 
73  /**
74  * Abort this asynchronous operation
75  *
76  * This function has no effect if the operation is complete. Otherwise
77  * the aborted flag is set.
78  *
79  * @note - the host object's lock MUST be held when this call is made
80  */
81  void abort();
82 
83  /**
84  * Check if this operation timed out
85  *
86  * @return true if this operation timed out, false otherwise
87  */
88  bool timeout();
89 
90  /**
91  * Check if this operation was aborted
92  *
93  * @return true if this operation was aborted, false otherwise
94  */
95  bool aborted();
96 
97 protected:
98 
99  /**
100  * Callback indicating that something changed
101  *
102  * @return true if finished false if not
103  */
104  virtual bool process() = 0;
105 
106  /**
107  * Callback indicating that this event finished
108  */
109  virtual void complete();
110 
111 private:
112  friend class OperationListBase;
113 
114  mbed::Callback<void()> _callback;
115  OperationListBase *_list;
116  rtos::Semaphore *_wait;
117  bool _aborted;
118  bool _timeout;
119 
120  void _abort(bool timeout);
121 
122  static void _host_lock(rtos::Mutex *host_mutex);
123 
124  static void _host_unlock(rtos::Mutex *host_mutex);
125 };
126 
127 /** @}*/
128 
129 #endif
virtual bool process()=0
Callback indicating that something changed.
virtual ~AsyncOp()
Cleanup resources used by this AsyncOp.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50
bool timeout()
Check if this operation timed out.
constexpr Clock::duration_u32 wait_for_u32_forever
Magic "wait forever" constant for Kernel::Clock::duration_u32-based APIs.
Definition: Kernel.h:120
bool aborted()
Check if this operation was aborted.
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678
virtual void complete()
Callback indicating that this event finished.
AsyncOp()
Construct a new AsyncOp object.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
void abort()
Abort this asynchronous operation.
Definition: LinkEntry.h:28
void wait(rtos::Mutex *host_mutex, rtos::Kernel::Clock::duration_u32 rel_time=rtos::Kernel::wait_for_u32_forever)
Wait for this asynchronous operation to complete.
Callback class based on template specialization.
Definition: Callback.h:53
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.