Updated comments and fixed tabbing
[profile/ivi/automotive-message-broker.git] / plugins / common / thread.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef _CUTIL_THREAD_H_
20 #define _CUTIL_THREAD_H_
21
22 #include <pthread.h>
23 #include <sys/prctl.h>
24 #include "logger.h"
25
26 /** \addtogroup libamb-plugins-common
27  *  @{
28  */
29
30 namespace CUtil {
31
32 /**
33  * \brief Encapsulation of the POSIX thread.
34  *
35  * Supports starting, stopping, restarting and thread status checking.
36  * @class Thread
37  */
38 class Thread
39 {
40         /**
41         * Condition variable used to signal runnable status changes
42         * @property cond
43         * @private
44         */
45         pthread_cond_t cond;
46         /**
47         * Mutex used to lock runnable status changes
48         * @property mutex
49         * @private
50         */
51         pthread_mutex_t mutex;
52         /**
53         * Instance of the encapsulated posix thread
54         * @property thread
55         * @private
56         */
57         pthread_t thread;
58         /**
59         * Runnable status of the thread. True means that thread is running
60         * @property runnableFlag
61         * @private
62         */
63         bool runnableFlag;
64
65 private:
66
67         /**
68         * Sets new runnable status of the thread. Holds mutex and signals condition variable
69         * @fn setRunnableFlag
70         * @private
71         * @param[in] flag New status of the thread to be set
72         * @return Actual runnable status after operation
73         */
74         bool setRunnableFlag(bool flag);
75
76         /**
77         * Sleeps current thread for specified time in miliseconds. Wait can be canceled if thread is in stopped/stopping state.
78         * @fn wait
79         * @private
80         * @param miliseconds Sleep time in miliseconds.
81         * @return True if wait wasn't canceled, false otherwise.
82         */
83         bool wait( long miliseconds );
84
85 public:
86
87         /**
88         * Method to be executed in this thread. Has to be overwritten.
89         * @fn run
90         * @public
91         */
92         virtual void run() = 0;
93
94 protected:
95
96         /**
97         * Gets runnable status of current thread. Sleeps calling thread for a specified time.
98         * @protected
99         * @fn isRunnable
100         * @param miliseconds Time in miliseconds. 0 means no sleep.
101         * @return True if thread should still run.
102         *
103         * \b Example
104         * @code
105         *
106         *       void MyCustomThread::run()
107         *       {
108         *           while(isRunnable(1000)) { // execute some op once per second. Exits thread if stop() was called.
109         *           ...
110         *           }
111         *       }
112         * @endcode
113         */
114         bool isRunnable(long miliseconds = 0);
115
116 public:
117
118         Thread();
119         virtual ~Thread();
120
121         /**
122         * Starts the thread
123         * @fn start
124         * @return True if thread was started
125         * @public
126         */
127         virtual bool start();
128
129         /**
130         * Sets the priority of the thread for FIFO scheduling.
131         * @fn set_priority
132         * @param priority Integer ranging from 1 (lowest) to 99 (highest).
133         * @return True if the operation was successful.
134         * @public
135         */
136         bool setPriority(int priority);
137
138         /**
139         * Stops the thread
140         * @fn stop
141         * @public
142         */
143         virtual void stop();
144
145         /**
146         * Restarts the thread.
147         * @fn restart
148         * @return True if thread was started again
149         * @public
150         */
151         bool restart()
152         {
153                 stop();
154                 return start();
155         }
156 };
157
158 }
159
160 #endif
161
162 /** @} */
163