PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/tiwlan1251/CUDK/OAL/Pform/Linux/TILibLinux.cpp

http://github.com/CyanogenMod/cm-kernel
C++ | 299 lines | 99 code | 30 blank | 170 comment | 3 complexity | 31998f6fb6ef374b4a21d22472f6a269 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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. // TILibLinux.cpp :
  21. //
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <time.h>
  25. #include "CommonOAL.h"
  26. #include "TILibLinux.h"
  27. #include <pthread.h>
  28. //////////////////////////////////////////////////////////////////////
  29. // CTI_LibLinux Class
  30. //////////////////////////////////////////////////////////////////////
  31. TIOALib_SINGLETON_CLASS_IMP( CTI_LibLinux )
  32. TIOALib_OBJECT_CREATOR_IMP( CTI_OSCriticalSectionLinux, TI_OSWrapCriticalSection )
  33. //////////////////////////////////////////////////////////////////////
  34. // Construction/Destruction
  35. //////////////////////////////////////////////////////////////////////
  36. CTI_LibLinux::CTI_LibLinux()
  37. {
  38. }
  39. CTI_LibLinux::~CTI_LibLinux()
  40. {
  41. }
  42. tiVOID
  43. CTI_LibLinux::TIOutputDebugString (tiCHAR* lpOutputString)
  44. {
  45. if ( lpOutputString )
  46. fprintf(stderr, lpOutputString);
  47. }
  48. tiUINT32
  49. CTI_LibLinux::TILoadLibrary(tiCHAR* lpLibFileName)
  50. {
  51. return 0;
  52. }
  53. tiUINT32
  54. CTI_LibLinux::TIGetCurrentThreadId()
  55. {
  56. return pthread_self();
  57. }
  58. tiBOOL
  59. CTI_LibLinux::TIFreeLibrary( tiUINT32 hLibModule )
  60. {
  61. return 0;
  62. }
  63. tiUINT32
  64. CTI_LibLinux::TIRegisterWindowMessage (tiCHAR* lpszMsgName)
  65. {
  66. return 0;
  67. }
  68. tiBOOL
  69. CTI_LibLinux::TIPostMessage(tiUINT32 hWnd, tiUINT32 uMsg, tiUINT32 wParam, tiUINT32 lParam)
  70. {
  71. return 0;
  72. }
  73. tiUINT32
  74. CTI_LibLinux::TIGetProcAddress(tiUINT32 hModule, tiCHAR* lpProcName )
  75. {
  76. return 0;
  77. }
  78. tiBOOL
  79. CTI_LibLinux::TIIsBadWritePtr(tiVOID *lp, tiUINT32 ucb )
  80. {
  81. return FALSE; //IsBadWritePtr(lp, ucb );
  82. }
  83. tiVOID
  84. CTI_LibLinux::TIPrintLastError(tiCHAR* lpsz)
  85. {
  86. #ifdef DEBUG_MESSAGES
  87. tiCHAR szTmp[512];
  88. sprintf(szTmp,"%s LastError(0x%Xh)\n", lpsz, errno);
  89. TIOutputDebugString(szTmp);
  90. #endif
  91. }
  92. tiUINT32
  93. CTI_LibLinux::TICreateThread(tiPTHREAD_START_ROUTINE pStartAddress, tiVOID* pParameter )
  94. {
  95. uxTHREAD_START_ROUTINE thread_start_address = (uxTHREAD_START_ROUTINE)pStartAddress;
  96. pthread_t supp_thread_id;
  97. pthread_attr_t supp_thread_attrs;
  98. pthread_attr_init(&supp_thread_attrs);
  99. int iRet = pthread_create(&supp_thread_id, &supp_thread_attrs, thread_start_address, pParameter);
  100. pthread_attr_destroy(&supp_thread_attrs);
  101. if ( iRet == 0)
  102. return TI_RESULT_OK;
  103. return TI_RESULT_FAILED;
  104. }
  105. CTI_OSCriticalSectionLinux::CTI_OSCriticalSectionLinux()
  106. {
  107. /* m_pCS = (tiVOID*) new pthread_rwlock_t;
  108. if ( m_pCS )
  109. {
  110. memset( m_pCS, 0, sizeof(pthread_rwlock_t));
  111. pthread_rwlock_init((pthread_rwlock_t*) m_pCS, NULL )
  112. }
  113. */
  114. }
  115. CTI_OSCriticalSectionLinux::~CTI_OSCriticalSectionLinux()
  116. {
  117. /*
  118. if (m_pCS)
  119. {
  120. pthread_rwlock_destroy((pthread_rwlock_t*) m_pCS);
  121. delete m_pCS;
  122. m_pCS = NULL;
  123. }
  124. */
  125. }
  126. tiVOID
  127. CTI_OSCriticalSectionLinux::Enter()
  128. {
  129. /*
  130. if ( m_pCS )
  131. pthread_rwlock_wrlock((pthread_rwlock_t*) m_pCS);
  132. */
  133. }
  134. tiVOID
  135. CTI_OSCriticalSectionLinux::Leave()
  136. {
  137. /*
  138. if ( m_pCS )
  139. pthread_rwlock_unlock((pthread_rwlock_t*) m_pCS);
  140. */
  141. }
  142. tiVOID
  143. CTI_LibLinux::TISleep(tiUINT32 msec)
  144. {
  145. struct timespec req;
  146. req.tv_sec = 0;
  147. req.tv_nsec = 100000; /* sleep for 100 msec */
  148. nanosleep( &req, NULL );
  149. }
  150. /*
  151. CTI_OSEventLinux::CTI_OSEventLinux()
  152. {
  153. m_bSet = FALSE;
  154. pthread_condattr_init(&m_CondAttr);
  155. pthread_cond_init(&m_Cond, &m_CondAttr);
  156. pthread_mutexattr_init(&m_MutexAttr);
  157. pthread_mutexattr_settype(&m_MutexAttr, PTHREAD_MUTEX_NORMAL);
  158. m_pEvent = (tiVOID*) new pthread_mutex_t;
  159. if ( m_pEvent )
  160. {
  161. memset( m_pEvent, 0, sizeof(pthread_mutex_t));
  162. pthread_mutex_init( (pthread_mutex_t*) m_pEvent, &m_MutexAttr);
  163. }
  164. }
  165. CTI_OSEventLinux::~CTI_OSEventLinux()
  166. {
  167. if (m_pEvent)
  168. {
  169. pthread_mutex_destroy((pthread_mutex_t*) m_pEvent);
  170. delete m_pEvent;
  171. m_pEvent = NULL;
  172. }
  173. pthread_mutexattr_destroy(&m_attr);
  174. pthread_cond_destroy(&m_Cond);
  175. pthread_condattr_destroy(&m_CondAttr);
  176. }
  177. tiVOID
  178. CTI_OSEventLinux::Set()
  179. {
  180. if ( m_pEvent )
  181. pthread_cond_signal((pthread_mutex_t*) m_pEvent);
  182. }
  183. tiUINT32
  184. CTI_OSEventLinux::Wait(tiUINT32 uTimeout)
  185. {
  186. if (m_pEvent)
  187. {
  188. pthread_mutex_lock((pthread_mutex_t*) m_pEvent);
  189. if (uTimeout == INFINITE)
  190. {
  191. while (!m_bSet) // have to wait for the event
  192. pthread_cond_wait(&m_Cond, (pthread_mutex_t*) m_pEvent );
  193. }
  194. else
  195. {
  196. timespec timeOut;
  197. clock_gettime(CLOCK_REALTIME, &timeOut);
  198. // if there are seconds involved
  199. if (uTimeout >= 1000)
  200. {
  201. timeOut.tv_sec += uTimeout / 1000;
  202. // get rest
  203. int nRemain = nMilliseconds % 1000;
  204. if (nRemain != 0)
  205. {
  206. timeOut.tv_nsec += nRemain * 1000000L;
  207. // wrapped into the next second
  208. if (timeOut.tv_nsec > 1000000000L)
  209. {
  210. timeOut.tv_nsec -= 1000000000L;
  211. timeOut.tv_sec++;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. timeOut.tv_nsec += uTimeout * 1000000L;
  218. }
  219. while (!m_bSet) // have to wait for the event
  220. {
  221. // the following line releases the mutex and waits until the
  222. // condition is signalled. when the call returns, we own the
  223. // mutex again, unless an exception is thrown, in which case
  224. // the mutex is unlocked
  225. int nResult = pthread_cond_timedwait(&m_Cond, (pthread_mutex_t*) m_pEvent , &timeOut));
  226. if ( nResult != 0 )
  227. {
  228. pthread_mutex_unlock((pthread_mutex_t*) m_pEvent);
  229. // time out, we have the lock
  230. pthread_mutex_lock((pthread_mutex_t*) m_pEvent);
  231. // return "time out" condition
  232. return;
  233. }
  234. }
  235. };
  236. inline _dcfTimeOut::_dcfTimeOut(unsigned int nMilliseconds)
  237. {
  238. int nResult = clock_gettime(CLOCK_REALTIME, &_tsWhen);
  239. #ifdef _DEBUG
  240. DCF_ASSERT(nResult == 0);
  241. #endif
  242. }
  243. }
  244. return TI_RESULT_OK;
  245. }
  246. */