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 /******************************************************************
29 ******************************************************************/
38 ////////////////////////////////////////////////
40 ////////////////////////////////////////////////
42 static CUtil::Mutex gMutex;
43 static int gActiveThreadCount(0);
45 static void *PosixThreadProc(void *param)
51 CUtil::Thread *thread = (CUtil::Thread *)param;
56 LOG_INFO("PosixThreadProc() - active threads: " << gActiveThreadCount);
62 ////////////////////////////////////////////////
64 ////////////////////////////////////////////////
70 pthread_cond_init( &cond, NULL );
71 pthread_mutex_init( &mutex, NULL );
76 pthread_mutex_lock(&mutex);
77 if (runnableFlag) {// already running
78 pthread_mutex_unlock(&mutex);
82 if (pthread_create(&thread, NULL/*&thread_attr*/, PosixThreadProc, this) != 0) {
83 //pthread_attr_destroy(&thread_attr);
84 pthread_mutex_unlock(&mutex);
87 //pthread_attr_destroy(&thread_attr);
89 pthread_mutex_unlock(&mutex);
94 bool Thread::setPriority(int priority)
96 pthread_mutex_lock(&mutex);
97 if (!runnableFlag) {// not running yet or terminated already
98 pthread_mutex_unlock(&mutex);
102 priority = priority < 1 ? 1 : priority;
103 priority = priority < 99 ? priority : 99;
106 struct sched_param pr;
107 pr.__sched_priority = priority;
108 if (pthread_setschedparam(thread, SCHED_FIFO, &pr) < 0)
110 pthread_mutex_unlock(&mutex);
114 pthread_mutex_unlock(&mutex);
121 pthread_cond_destroy( &cond );
122 pthread_mutex_destroy( &mutex );
128 if (setRunnableFlag(false) == true) {
130 if (thread == pthread_self()){
131 int s = pthread_detach(thread);
132 ((void)s);// prevent compiler warning in RELEASE build
133 LOG_MESSAGE("Thread::stop() - thread " << std::hex << int(thread) << std::dec << " detached, returned value was " << s);
136 int s = pthread_join(thread, NULL);
138 LOG_ERROR("Thread::stop() - Joined with thread " << std::hex << int(thread) << std::dec << ", returned value was " << s);
141 LOG_MESSAGE("Thread::stop() - Joined with thread " << std::hex << int(thread) << std::dec << ", returned value was " << s);
150 bool Thread::setRunnableFlag(bool flag)
152 pthread_mutex_lock(&mutex);
153 bool retval(runnableFlag);
157 pthread_cond_signal(&cond);
158 pthread_mutex_unlock(&mutex);
162 bool Thread::isRunnable(long miliseconds)
164 bool runnable(false);
166 pthread_mutex_lock(&mutex);
167 if (miliseconds != 0){
170 runnable = runnableFlag;
172 pthread_mutex_unlock(&mutex);
176 bool Thread::wait( long miliseconds )
178 struct timespec abstime;
179 clock_gettime(CLOCK_REALTIME, &abstime);
180 abstime.tv_sec += ( miliseconds / 1000 );
182 abstime.tv_nsec += ( miliseconds * 1000000L ); // in nanoseconds
183 if ( abstime.tv_nsec > 1000000000L /* > 1s */ ){
184 abstime.tv_sec += 1; // +1s
185 abstime.tv_nsec -= 1000000000L; // -1s
188 int status = pthread_cond_timedwait( &cond, &mutex, &abstime );
189 return ( status == ETIMEDOUT );