Mbed OS Reference
Loading...
Searching...
No Matches
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 "rtos/Mutex.h"
22#include "rtos/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
44/**
45 * Base class for asynchronous operations in the USB stack.
46 * Classes such as USBCDC use this to submit operations to be processed by an ISR.
47 */
48class AsyncOp: public LinkEntry {
49public:
50
51 /**
52 * Construct a new AsyncOp object
53 */
55
56 /**
57 * Construct a new AsyncOp object
58 *
59 * @param callback Completion callback
60 */
61 AsyncOp(mbed::Callback<void()> &callback);
62
63 /**
64 * Cleanup resources used by this AsyncOp
65 */
66 virtual ~AsyncOp();
67
68 /**
69 * Wait for this asynchronous operation to complete
70 *
71 * If the timeout expires then this asynchronous operation is
72 * aborted and the timeout flag is set.
73 *
74 * @note - the host object's lock MUST NOT be held when this call is made
75 */
76 void wait(rtos::Mutex *host_mutex, rtos::Kernel::Clock::duration_u32 rel_time = rtos::Kernel::wait_for_u32_forever);
77
78 /**
79 * Abort this asynchronous operation
80 *
81 * This function has no effect if the operation is complete. Otherwise
82 * the aborted flag is set.
83 *
84 * @note - the host object's lock MUST be held when this call is made
85 */
86 void abort();
87
88 /**
89 * Check if this operation timed out
90 *
91 * @return true if this operation timed out, false otherwise
92 */
93 bool timeout();
94
95 /**
96 * Check if this operation was aborted
97 *
98 * @return true if this operation was aborted, false otherwise
99 */
100 bool aborted();
101
102protected:
103
104 /**
105 * Callback indicating that something changed
106 *
107 * @return true if finished false if not
108 */
109 virtual bool process() = 0;
110
111 /**
112 * Callback indicating that this event finished
113 */
114 virtual void complete();
115
116private:
117 friend class OperationListBase;
118
119 mbed::Callback<void()> _callback;
120 OperationListBase *_list;
121 rtos::Semaphore *_wait;
122 bool _aborted;
123 bool _timeout;
124
125 void _abort(bool timeout);
126
127 static void _host_lock(rtos::Mutex *host_mutex);
128
129 static void _host_unlock(rtos::Mutex *host_mutex);
130};
131
132/** @}*/
133
134#endif
Base class for asynchronous operations in the USB stack.
Definition: AsyncOp.h:48
virtual bool process()=0
Callback indicating that something changed.
AsyncOp(mbed::Callback< void()> &callback)
Construct a new AsyncOp object.
AsyncOp()
Construct a new AsyncOp object.
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.
bool aborted()
Check if this operation was aborted.
bool timeout()
Check if this operation timed out.
void abort()
Abort this asynchronous operation.
virtual void complete()
Callback indicating that this event finished.
virtual ~AsyncOp()
Cleanup resources used by this AsyncOp.
Definition: LinkEntry.h:28
Callback class based on template specialization.
Definition: Callback.h:53
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50
constexpr Clock::duration_u32 wait_for_u32_forever
Magic "wait forever" constant for Kernel::Clock::duration_u32-based APIs.
Definition: Kernel.h:120