1 /* Copyright (c) 2015, 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 <pthread.h>
34 
35 // abstract class to be implemented by client to provide a runnable class
36 // which gets scheduled by LocThread
37 class LocRunnable {
38 public:
LocRunnable()39     inline LocRunnable() {}
~LocRunnable()40     inline virtual ~LocRunnable() {}
41 
42     // The method to be implemented by thread clients
43     // and be scheduled by LocThread
44     // This method will be repeated called until it returns false; or
45     // until thread is stopped.
46     virtual bool run() = 0;
47 
48     // The method to be run before thread loop (conditionally repeatedly)
49     // calls run()
prerun()50     inline virtual void prerun() {}
51 
52     // The method to be run after thread loop (conditionally repeatedly)
53     // calls run()
postrun()54     inline virtual void postrun() {}
55 };
56 
57 // opaque class to provide service implementation.
58 class LocThreadDelegate;
59 
60 // A utility class to create a thread and run LocRunnable
61 // caller passes in.
62 class LocThread {
63     LocThreadDelegate* mThread;
64 public:
LocThread()65     inline LocThread() : mThread(NULL) {}
66     virtual ~LocThread();
67 
68     typedef pthread_t (*tCreate)(const char* name, void* (*start)(void*), void* arg);
69     // client starts thread with a runnable, which implements
70     // the logics to fun in the created thread context.
71     // The thread could be either joinable or detached.
72     // runnable is an obj managed by client. Client creates and
73     //          frees it (but must be after stop() is called, or
74     //          this LocThread obj is deleted).
75     //          The obj will be deleted by LocThread if start()
76     //          returns true. Else it is client's responsibility
77     //          to delete the object
78     // Returns 0 if success; false if failure.
79     bool start(tCreate creator, const char* threadName, LocRunnable* runnable, bool joinable = true);
80     inline bool start(const char* threadName, LocRunnable* runnable, bool joinable = true) {
81         return start(NULL, threadName, runnable, joinable);
82     }
83 
84     // NOTE: if this is a joinable thread, this stop may block
85     // for a while until the thread is joined.
86     void stop();
87 
88     // thread status check
isRunning()89     inline bool isRunning() { return NULL != mThread; }
90 };
91 
92 #endif //__LOC_THREAD__
93