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);
96 pthread_cond_destroy( &cond );
97 pthread_mutex_destroy( &mutex );
103 if (setRunnableFlag(false) == true) {
105 if (thread == pthread_self()){
106 int s = pthread_detach(thread);
107 ((void)s);// prevent compiler warning in RELEASE build
108 LOG_MESSAGE("Thread::stop() - thread " << std::hex << int(thread) << std::dec << " detached, returned value was " << s);
111 int s = pthread_join(thread, NULL);
113 LOG_ERROR("Thread::stop() - Joined with thread " << std::hex << int(thread) << std::dec << ", returned value was " << s);
116 LOG_MESSAGE("Thread::stop() - Joined with thread " << std::hex << int(thread) << std::dec << ", returned value was " << s);
125 bool Thread::setRunnableFlag(bool flag)
127 pthread_mutex_lock(&mutex);
128 bool retval(runnableFlag);
132 pthread_cond_signal(&cond);
133 pthread_mutex_unlock(&mutex);
137 bool Thread::isRunnable(long miliseconds)
139 bool runnable(false);
141 pthread_mutex_lock(&mutex);
142 if (miliseconds != 0){
145 runnable = runnableFlag;
147 pthread_mutex_unlock(&mutex);
151 bool Thread::wait( long miliseconds )
153 struct timespec abstime;
154 clock_gettime(CLOCK_REALTIME, &abstime);
155 abstime.tv_sec += ( miliseconds / 1000 );
157 abstime.tv_nsec += ( miliseconds * 1000000L ); // in nanoseconds
158 if ( abstime.tv_nsec > 1000000000L /* > 1s */ ){
159 abstime.tv_sec += 1; // +1s
160 abstime.tv_nsec -= 1000000000L; // -1s
163 int status = pthread_cond_timedwait( &cond, &mutex, &abstime );
164 return ( status == ETIMEDOUT );