1/*******************************************************************************
2**+--------------------------------------------------------------------------+**
3**|                                                                          |**
4**| Copyright 1998-2008 Texas Instruments, Inc. - http://www.ti.com/         |**
5**|                                                                          |**
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                                  |**
9**|                                                                          |**
10**|     http://www.apache.org/licenses/LICENSE-2.0                           |**
11**|                                                                          |**
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.                                           |**
17**|                                                                          |**
18**+--------------------------------------------------------------------------+**
19*******************************************************************************/
20
21// TILibLinux.cpp :
22//
23#include <stdio.h>
24#include <errno.h>
25#include <time.h>
26
27#include "CommonOAL.h"
28#include "TILibLinux.h"
29#include <pthread.h>
30
31//////////////////////////////////////////////////////////////////////
32// CTI_LibLinux Class
33//////////////////////////////////////////////////////////////////////
34TIOALib_SINGLETON_CLASS_IMP( CTI_LibLinux )
35TIOALib_OBJECT_CREATOR_IMP( CTI_OSCriticalSectionLinux, TI_OSWrapCriticalSection )
36
37//////////////////////////////////////////////////////////////////////
38// Construction/Destruction
39//////////////////////////////////////////////////////////////////////
40
41CTI_LibLinux::CTI_LibLinux()
42{
43}
44
45CTI_LibLinux::~CTI_LibLinux()
46{
47}
48
49tiVOID
50CTI_LibLinux::TIOutputDebugString (tiCHAR* lpOutputString)
51{
52    if ( lpOutputString )
53        fprintf(stderr, lpOutputString);
54}
55
56tiUINT32
57CTI_LibLinux::TILoadLibrary(tiCHAR* lpLibFileName)
58{
59    return 0;
60}
61tiUINT32
62CTI_LibLinux::TIGetCurrentThreadId()
63{
64    return pthread_self();
65}
66
67tiBOOL
68CTI_LibLinux::TIFreeLibrary( tiUINT32 hLibModule )
69{
70    return 0;
71}
72
73tiUINT32
74CTI_LibLinux::TIRegisterWindowMessage (tiCHAR* lpszMsgName)
75{
76    return 0;
77}
78
79tiBOOL
80CTI_LibLinux::TIPostMessage(tiUINT32 hWnd, tiUINT32 uMsg, tiUINT32 wParam, tiUINT32 lParam)
81{
82    return 0;
83}
84
85tiUINT32
86CTI_LibLinux::TIGetProcAddress(tiUINT32 hModule, tiCHAR* lpProcName )
87{
88    return 0;
89}
90
91tiBOOL
92CTI_LibLinux::TIIsBadWritePtr(tiVOID *lp, tiUINT32 ucb )
93{
94    return FALSE;   //IsBadWritePtr(lp, ucb );
95}
96
97tiVOID
98CTI_LibLinux::TIPrintLastError(tiCHAR* lpsz)
99{
100#ifdef DEBUG_MESSAGES
101    tiCHAR szTmp[512];
102    sprintf(szTmp,"%s LastError(0x%Xh)\n", lpsz, errno);
103    TIOutputDebugString(szTmp);
104#endif
105}
106
107
108tiUINT32
109CTI_LibLinux::TICreateThread(tiPTHREAD_START_ROUTINE pStartAddress, tiVOID* pParameter )
110{
111    uxTHREAD_START_ROUTINE  thread_start_address = (uxTHREAD_START_ROUTINE)pStartAddress;
112    pthread_t               supp_thread_id;
113    pthread_attr_t          supp_thread_attrs;
114
115    pthread_attr_init(&supp_thread_attrs);
116
117    int iRet = pthread_create(&supp_thread_id, &supp_thread_attrs, thread_start_address, pParameter);
118
119    pthread_attr_destroy(&supp_thread_attrs);
120
121    if ( iRet == 0)
122        return TI_RESULT_OK;
123
124    return TI_RESULT_FAILED;
125}
126
127
128CTI_OSCriticalSectionLinux::CTI_OSCriticalSectionLinux()
129{
130  /*  m_pCS = (tiVOID*) new pthread_rwlock_t;
131
132    if ( m_pCS )
133    {
134        memset( m_pCS, 0, sizeof(pthread_rwlock_t));
135        pthread_rwlock_init((pthread_rwlock_t*) m_pCS, NULL )
136    }
137    */
138}
139
140CTI_OSCriticalSectionLinux::~CTI_OSCriticalSectionLinux()
141{
142    /*
143    if (m_pCS)
144    {
145        pthread_rwlock_destroy((pthread_rwlock_t*) m_pCS);
146        delete m_pCS;
147        m_pCS = NULL;
148    }
149    */
150}
151
152tiVOID
153CTI_OSCriticalSectionLinux::Enter()
154{
155    /*
156    if ( m_pCS )
157        pthread_rwlock_wrlock((pthread_rwlock_t*) m_pCS);
158    */
159}
160
161tiVOID
162CTI_OSCriticalSectionLinux::Leave()
163{
164    /*
165    if ( m_pCS )
166        pthread_rwlock_unlock((pthread_rwlock_t*) m_pCS);
167    */
168}
169
170tiVOID
171CTI_LibLinux::TISleep(tiUINT32 msec)
172{
173    struct timespec req;
174
175    req.tv_sec = 0;
176    req.tv_nsec = 100000; /* sleep for 100 msec */
177    nanosleep( &req, NULL );
178}
179
180/*
181
182CTI_OSEventLinux::CTI_OSEventLinux()
183{
184    m_bSet = FALSE;
185
186    pthread_condattr_init(&m_CondAttr);
187    pthread_cond_init(&m_Cond, &m_CondAttr);
188
189    pthread_mutexattr_init(&m_MutexAttr);
190    pthread_mutexattr_settype(&m_MutexAttr, PTHREAD_MUTEX_NORMAL);
191
192    m_pEvent = (tiVOID*) new pthread_mutex_t;
193
194    if ( m_pEvent )
195    {
196        memset( m_pEvent, 0, sizeof(pthread_mutex_t));
197        pthread_mutex_init( (pthread_mutex_t*) m_pEvent, &m_MutexAttr);
198    }
199}
200
201CTI_OSEventLinux::~CTI_OSEventLinux()
202{
203    if (m_pEvent)
204    {
205        pthread_mutex_destroy((pthread_mutex_t*) m_pEvent);
206        delete m_pEvent;
207        m_pEvent = NULL;
208    }
209
210    pthread_mutexattr_destroy(&m_attr);
211    pthread_cond_destroy(&m_Cond);
212    pthread_condattr_destroy(&m_CondAttr);
213}
214
215tiVOID
216CTI_OSEventLinux::Set()
217{
218    if ( m_pEvent )
219        pthread_cond_signal((pthread_mutex_t*) m_pEvent);
220}
221
222tiUINT32
223CTI_OSEventLinux::Wait(tiUINT32 uTimeout)
224{
225    if (m_pEvent)
226    {
227        pthread_mutex_lock((pthread_mutex_t*) m_pEvent);
228
229        if (uTimeout == INFINITE)
230        {
231            while (!m_bSet) // have to wait for the event
232                pthread_cond_wait(&m_Cond, (pthread_mutex_t*) m_pEvent );
233        }
234        else
235        {
236            timespec  timeOut;
237            clock_gettime(CLOCK_REALTIME, &timeOut);
238
239            // if there are seconds involved
240            if (uTimeout >= 1000)
241            {
242                timeOut.tv_sec += uTimeout / 1000;
243
244                // get rest
245                int nRemain = nMilliseconds % 1000;
246                if (nRemain != 0)
247                {
248                    timeOut.tv_nsec +=  nRemain * 1000000L;
249
250                    // wrapped into the next second
251                    if (timeOut.tv_nsec > 1000000000L)
252                    {
253                        timeOut.tv_nsec -= 1000000000L;
254                        timeOut.tv_sec++;
255                    }
256                }
257            }
258            else
259            {
260                timeOut.tv_nsec +=  uTimeout * 1000000L;
261            }
262
263            while (!m_bSet) // have to wait for the event
264            {
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
269
270                int nResult = pthread_cond_timedwait(&m_Cond, (pthread_mutex_t*) m_pEvent , &timeOut));
271
272                if ( nResult != 0 )
273                {
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);
277
278                    // return "time out" condition
279                    return;
280               }
281
282        }
283};
284
285inline _dcfTimeOut::_dcfTimeOut(unsigned int nMilliseconds)
286{
287    int nResult = clock_gettime(CLOCK_REALTIME, &_tsWhen);
288
289#ifdef _DEBUG
290    DCF_ASSERT(nResult == 0);
291#endif
292
293
294}
295    }
296    return TI_RESULT_OK;
297}
298*/
299
300