LocThread.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <stddef.h>
  32. #include <memory>
  33. using std::shared_ptr;
  34. namespace loc_util {
  35. // abstract class to be implemented by client to provide a runnable class
  36. // which gets scheduled by LocThread
  37. class LocRunnable {
  38. public:
  39. inline LocRunnable() = default;
  40. inline virtual ~LocRunnable() = default;
  41. // The method to be implemented by thread clients
  42. // and be scheduled by LocThread
  43. // This method will be repeated called until it returns false; or
  44. // until thread is stopped.
  45. virtual bool run() = 0;
  46. // The method to be run before thread loop (conditionally repeatedly)
  47. // calls run()
  48. inline virtual void prerun() {}
  49. // The method to be run after thread loop (conditionally repeatedly)
  50. // calls run()
  51. inline virtual void postrun() {}
  52. // The method to wake up the potential blocking thread
  53. // no op if not applicable
  54. inline virtual void interrupt() = 0;
  55. };
  56. // opaque class to provide service implementation.
  57. class LocThreadDelegate;
  58. // A utility class to create a thread and run LocRunnable
  59. // caller passes in.
  60. class LocThread {
  61. LocThreadDelegate* mThread;
  62. public:
  63. inline LocThread() : mThread(NULL) {}
  64. inline virtual ~LocThread() { stop(); }
  65. // client starts thread with a runnable, which implements
  66. // the logics to fun in the created thread context.
  67. // The thread is always detached.
  68. // runnable is an obj managed by client. Client creates and
  69. // frees it (but must be after stop() is called, or
  70. // this LocThread obj is deleted).
  71. // The obj will be deleted by LocThread if start()
  72. // returns true. Else it is client's responsibility
  73. // to delete the object
  74. // Returns 0 if success; false if failure.
  75. bool start(const char* threadName, shared_ptr<LocRunnable> runnable);
  76. void stop();
  77. // thread status check
  78. inline bool isRunning() { return NULL != mThread; }
  79. };
  80. } // loc_util
  81. #endif //__LOC_THREAD__