1 /* Copyright (c) 2015, 2020 The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation, nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 #ifndef __LOC_THREAD__ 30 #define __LOC_THREAD__ 31 32 #include <stddef.h> 33 #include <memory> 34 35 using std::shared_ptr; 36 37 namespace loc_util { 38 39 // abstract class to be implemented by client to provide a runnable class 40 // which gets scheduled by LocThread 41 class LocRunnable { 42 public: 43 inline LocRunnable() = default; 44 inline virtual ~LocRunnable() = default; 45 46 // The method to be implemented by thread clients 47 // and be scheduled by LocThread 48 // This method will be repeated called until it returns false; or 49 // until thread is stopped. 50 virtual bool run() = 0; 51 52 // The method to be run before thread loop (conditionally repeatedly) 53 // calls run() prerun()54 inline virtual void prerun() {} 55 56 // The method to be run after thread loop (conditionally repeatedly) 57 // calls run() postrun()58 inline virtual void postrun() {} 59 60 // The method to wake up the potential blocking thread 61 // no op if not applicable 62 inline virtual void interrupt() = 0; 63 }; 64 65 // opaque class to provide service implementation. 66 class LocThreadDelegate; 67 68 // A utility class to create a thread and run LocRunnable 69 // caller passes in. 70 class LocThread { 71 LocThreadDelegate* mThread; 72 public: LocThread()73 inline LocThread() : mThread(NULL) {} ~LocThread()74 inline virtual ~LocThread() { stop(); } 75 76 // client starts thread with a runnable, which implements 77 // the logics to fun in the created thread context. 78 // The thread is always detached. 79 // runnable is an obj managed by client. Client creates and 80 // frees it (but must be after stop() is called, or 81 // this LocThread obj is deleted). 82 // The obj will be deleted by LocThread if start() 83 // returns true. Else it is client's responsibility 84 // to delete the object 85 // Returns 0 if success; false if failure. 86 bool start(const char* threadName, shared_ptr<LocRunnable> runnable); 87 88 void stop(); 89 90 // thread status check isRunning()91 inline bool isRunning() { return NULL != mThread; } 92 }; 93 94 } // loc_util 95 #endif //__LOC_THREAD__ 96