2 Copyright (C) 2012 Intel Corporation
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.
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.
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
19 #ifndef _CUTIL_THREAD_H_
20 #define _CUTIL_THREAD_H_
23 #include <sys/prctl.h>
26 /** \addtogroup libamb-plugins-common
33 * \brief Encapsulation of the POSIX thread.
35 * Supports starting, stopping, restarting and thread status checking.
41 * Condition variable used to signal runnable status changes
47 * Mutex used to lock runnable status changes
51 pthread_mutex_t mutex;
53 * Instance of the encapsulated posix thread
59 * Runnable status of the thread. True means that thread is running
60 * @property runnableFlag
68 * Sets new runnable status of the thread. Holds mutex and signals condition variable
71 * @param[in] flag New status of the thread to be set
72 * @return Actual runnable status after operation
74 bool setRunnableFlag(bool flag);
77 * Sleeps current thread for specified time in miliseconds. Wait can be canceled if thread is in stopped/stopping state.
80 * @param miliseconds Sleep time in miliseconds.
81 * @return True if wait wasn't canceled, false otherwise.
83 bool wait( long miliseconds );
88 * Method to be executed in this thread. Has to be overwritten.
92 virtual void run() = 0;
97 * Gets runnable status of current thread. Sleeps calling thread for a specified time.
100 * @param miliseconds Time in miliseconds. 0 means no sleep.
101 * @return True if thread should still run.
106 * void MyCustomThread::run()
108 * while(isRunnable(1000)) { // execute some op once per second. Exits thread if stop() was called.
114 bool isRunnable(long miliseconds = 0);
124 * @return True if thread was started
127 virtual bool start();
130 * Sets the priority of the thread for FIFO scheduling.
132 * @param priority Integer ranging from 1 (lowest) to 99 (highest).
133 * @return True if the operation was successful.
136 bool setPriority(int priority);
146 * Restarts the thread.
148 * @return True if thread was started again