Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsyncOp.h Source File

AsyncOp.h

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef MBED_ASYNC_OP_H
00019 #define MBED_ASYNC_OP_H
00020 
00021 #include "Mutex.h"
00022 #include "Semaphore.h"
00023 #include "Callback.h"
00024 
00025 #include "LinkEntry.h"
00026 #include "OperationListBase.h"
00027 
00028 /** \defgroup mbed-os-internal Internal API */
00029 
00030 /** \addtogroup drivers-internal-api Drivers
00031  * \ingroup mbed-os-internal
00032  */
00033 
00034 /** \defgroup drivers-internal-api-usb USB
00035  * \ingroup drivers-internal-api
00036  */
00037 
00038 /**
00039  * \defgroup drivers_AsyncOp AsyncOp class
00040  * \ingroup drivers-internal-api-usb
00041  * @{
00042  */
00043 class AsyncOp: public LinkEntry {
00044 public:
00045 
00046     /**
00047      * Construct a new AsyncOp object
00048      */
00049     AsyncOp();
00050 
00051     /**
00052      * Construct a new AsyncOp object
00053      *
00054      * @param callback Completion callback
00055      */
00056     AsyncOp(mbed::Callback<void()> &callback);
00057 
00058     /**
00059      * Cleanup resources used by this AsyncOp
00060      */
00061     virtual ~AsyncOp();
00062 
00063     /**
00064      * Wait for this asynchronous operation to complete
00065      *
00066      * If the timeout expires then this asynchronous operation is
00067      * aborted and the timeout flag is set.
00068      *
00069      * @note - the host object's lock MUST NOT be held when this call is made
00070      */
00071     void wait(rtos::Mutex *host_mutex, uint32_t milliseconds = osWaitForever);
00072 
00073     /**
00074      * Abort this asynchronous operation
00075      *
00076      * This function has no effect if the operation is complete. Otherwise
00077      * the aborted flag is set.
00078      *
00079      * @note - the host object's lock MUST be held when this call is made
00080      */
00081     void abort();
00082 
00083     /**
00084      * Check if this operation timed out
00085      *
00086      * @return true if this operation timed out, false otherwise
00087      */
00088     bool timeout();
00089 
00090     /**
00091      * Check if this operation was aborted
00092      *
00093      * @return true if this operation was aborted, false otherwise
00094      */
00095     bool aborted();
00096 
00097 protected:
00098 
00099     /**
00100      * Callback indicating that something changed
00101      *
00102      * @return true if finished false if not
00103      */
00104     virtual bool process() = 0;
00105 
00106     /**
00107      * Callback indicating that this event finished
00108      */
00109     virtual void complete();
00110 
00111 private:
00112     friend class OperationListBase;
00113 
00114     mbed::Callback<void()> _callback;
00115     OperationListBase *_list;
00116     rtos::Semaphore *_wait;
00117     bool _aborted;
00118     bool _timeout;
00119 
00120     void _abort(bool timeout);
00121 
00122     static void _host_lock(rtos::Mutex *host_mutex);
00123 
00124     static void _host_unlock(rtos::Mutex *host_mutex);
00125 };
00126 
00127 /** @}*/
00128 
00129 #endif