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