1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
4 **| Copyright 1998-2008 Texas Instruments, Inc. - http://www.ti.com/ |**
6 **| Licensed under the Apache License, Version 2.0 (the "License"); |**
7 **| you may not use this file except in compliance with the License. |**
8 **| You may obtain a copy of the License at |**
10 **| http://www.apache.org/licenses/LICENSE-2.0 |**
12 **| Unless required by applicable law or agreed to in writing, software |**
13 **| distributed under the License is distributed on an "AS IS" BASIS, |**
14 **| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |**
15 **| See the License for the specific language governing permissions and |**
16 **| limitations under the License. |**
18 **+--------------------------------------------------------------------------+**
19 *******************************************************************************/
27 #include "CommonOAL.h"
28 #include "TILibLinux.h"
31 //////////////////////////////////////////////////////////////////////
33 //////////////////////////////////////////////////////////////////////
34 TIOALib_SINGLETON_CLASS_IMP( CTI_LibLinux )
35 TIOALib_OBJECT_CREATOR_IMP( CTI_OSCriticalSectionLinux, TI_OSWrapCriticalSection )
37 //////////////////////////////////////////////////////////////////////
38 // Construction/Destruction
39 //////////////////////////////////////////////////////////////////////
41 CTI_LibLinux::CTI_LibLinux()
45 CTI_LibLinux::~CTI_LibLinux()
50 CTI_LibLinux::TIOutputDebugString (tiCHAR* lpOutputString)
53 fprintf(stderr, lpOutputString);
57 CTI_LibLinux::TILoadLibrary(tiCHAR* lpLibFileName)
62 CTI_LibLinux::TIGetCurrentThreadId()
64 return pthread_self();
68 CTI_LibLinux::TIFreeLibrary( tiUINT32 hLibModule )
74 CTI_LibLinux::TIRegisterWindowMessage (tiCHAR* lpszMsgName)
80 CTI_LibLinux::TIPostMessage(tiUINT32 hWnd, tiUINT32 uMsg, tiUINT32 wParam, tiUINT32 lParam)
86 CTI_LibLinux::TIGetProcAddress(tiUINT32 hModule, tiCHAR* lpProcName )
92 CTI_LibLinux::TIIsBadWritePtr(tiVOID *lp, tiUINT32 ucb )
94 return FALSE; //IsBadWritePtr(lp, ucb );
98 CTI_LibLinux::TIPrintLastError(tiCHAR* lpsz)
100 #ifdef DEBUG_MESSAGES
102 sprintf(szTmp,"%s LastError(0x%Xh)\n", lpsz, errno);
103 TIOutputDebugString(szTmp);
109 CTI_LibLinux::TICreateThread(tiPTHREAD_START_ROUTINE pStartAddress, tiVOID* pParameter )
111 uxTHREAD_START_ROUTINE thread_start_address = (uxTHREAD_START_ROUTINE)pStartAddress;
112 pthread_t supp_thread_id;
113 pthread_attr_t supp_thread_attrs;
115 pthread_attr_init(&supp_thread_attrs);
117 int iRet = pthread_create(&supp_thread_id, &supp_thread_attrs, thread_start_address, pParameter);
119 pthread_attr_destroy(&supp_thread_attrs);
124 return TI_RESULT_FAILED;
128 CTI_OSCriticalSectionLinux::CTI_OSCriticalSectionLinux()
130 /* m_pCS = (tiVOID*) new pthread_rwlock_t;
134 memset( m_pCS, 0, sizeof(pthread_rwlock_t));
135 pthread_rwlock_init((pthread_rwlock_t*) m_pCS, NULL )
140 CTI_OSCriticalSectionLinux::~CTI_OSCriticalSectionLinux()
145 pthread_rwlock_destroy((pthread_rwlock_t*) m_pCS);
153 CTI_OSCriticalSectionLinux::Enter()
157 pthread_rwlock_wrlock((pthread_rwlock_t*) m_pCS);
162 CTI_OSCriticalSectionLinux::Leave()
166 pthread_rwlock_unlock((pthread_rwlock_t*) m_pCS);
171 CTI_LibLinux::TISleep(tiUINT32 msec)
176 req.tv_nsec = 100000; /* sleep for 100 msec */
177 nanosleep( &req, NULL );
182 CTI_OSEventLinux::CTI_OSEventLinux()
186 pthread_condattr_init(&m_CondAttr);
187 pthread_cond_init(&m_Cond, &m_CondAttr);
189 pthread_mutexattr_init(&m_MutexAttr);
190 pthread_mutexattr_settype(&m_MutexAttr, PTHREAD_MUTEX_NORMAL);
192 m_pEvent = (tiVOID*) new pthread_mutex_t;
196 memset( m_pEvent, 0, sizeof(pthread_mutex_t));
197 pthread_mutex_init( (pthread_mutex_t*) m_pEvent, &m_MutexAttr);
201 CTI_OSEventLinux::~CTI_OSEventLinux()
205 pthread_mutex_destroy((pthread_mutex_t*) m_pEvent);
210 pthread_mutexattr_destroy(&m_attr);
211 pthread_cond_destroy(&m_Cond);
212 pthread_condattr_destroy(&m_CondAttr);
216 CTI_OSEventLinux::Set()
219 pthread_cond_signal((pthread_mutex_t*) m_pEvent);
223 CTI_OSEventLinux::Wait(tiUINT32 uTimeout)
227 pthread_mutex_lock((pthread_mutex_t*) m_pEvent);
229 if (uTimeout == INFINITE)
231 while (!m_bSet) // have to wait for the event
232 pthread_cond_wait(&m_Cond, (pthread_mutex_t*) m_pEvent );
237 clock_gettime(CLOCK_REALTIME, &timeOut);
239 // if there are seconds involved
240 if (uTimeout >= 1000)
242 timeOut.tv_sec += uTimeout / 1000;
245 int nRemain = nMilliseconds % 1000;
248 timeOut.tv_nsec += nRemain * 1000000L;
250 // wrapped into the next second
251 if (timeOut.tv_nsec > 1000000000L)
253 timeOut.tv_nsec -= 1000000000L;
260 timeOut.tv_nsec += uTimeout * 1000000L;
263 while (!m_bSet) // have to wait for the event
265 // the following line releases the mutex and waits until the
266 // condition is signalled. when the call returns, we own the
267 // mutex again, unless an exception is thrown, in which case
268 // the mutex is unlocked
270 int nResult = pthread_cond_timedwait(&m_Cond, (pthread_mutex_t*) m_pEvent , &timeOut));
274 pthread_mutex_unlock((pthread_mutex_t*) m_pEvent);
275 // time out, we have the lock
276 pthread_mutex_lock((pthread_mutex_t*) m_pEvent);
278 // return "time out" condition
285 inline _dcfTimeOut::_dcfTimeOut(unsigned int nMilliseconds)
287 int nResult = clock_gettime(CLOCK_REALTIME, &_tsWhen);
290 DCF_ASSERT(nResult == 0);