AtmoThread.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:4k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*
  2.  * AtmoThread.cpp: Base thread class for all threads inside AtmoWin
  3.  *
  4.  * See the README.txt file for copyright information and how to reach the author(s).
  5.  *
  6.  * $Id: 3670d54f24590b9c526a67d3e5aaa5f1c4b571f5 $
  7.  */
  8. #include "AtmoThread.h"
  9. #if defined(_ATMO_VLC_PLUGIN_)
  10. CThread::CThread(vlc_object_t *pOwner)
  11. {
  12.     int err;
  13.     m_pAtmoThread = (atmo_thread_t *)vlc_object_create( pOwner,
  14.                                                         sizeof(atmo_thread_t) );
  15.     if(m_pAtmoThread)
  16.     {
  17.         m_pAtmoThread->p_thread = this;
  18.         this->m_pOwner = pOwner;
  19.         vlc_object_attach( m_pAtmoThread, m_pOwner);
  20.         vlc_mutex_init( &m_TerminateLock );
  21.         err = vlc_cond_init( &m_TerminateCond );
  22.         if(err) {
  23.            msg_Err( m_pAtmoThread, "vlc_cond_init failed %d",err);
  24.         }
  25.     }
  26. }
  27. #else
  28. CThread::CThread(void)
  29. {
  30.   m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
  31.                            this, CREATE_SUSPENDED, &m_dwThreadID);
  32.   m_hTerminateEvent = CreateEvent(NULL,ATMO_FALSE,ATMO_FALSE,NULL);
  33. }
  34. #endif
  35. #if defined(_ATMO_VLC_PLUGIN_)
  36. CThread::~CThread(void)
  37. {
  38.   if(m_pAtmoThread)
  39.   {
  40.       vlc_mutex_destroy( &m_TerminateLock );
  41.       vlc_cond_destroy( &m_TerminateCond );
  42.       vlc_object_detach(m_pAtmoThread);
  43.       vlc_object_release(m_pAtmoThread);
  44.   }
  45. }
  46. #else
  47. CThread::~CThread(void)
  48. {
  49.   CloseHandle(m_hThread);
  50.   CloseHandle(m_hTerminateEvent);
  51. }
  52. #endif
  53. #if defined(_ATMO_VLC_PLUGIN_)
  54. void *CThread::ThreadProc(vlc_object_t *obj)
  55. {
  56.       atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
  57.       CThread *pThread = (CThread *)pAtmoThread->p_thread;
  58.       if(pThread) {
  59.          int canc;
  60.          canc = vlc_savecancel ();
  61.          pThread->Execute();
  62.          vlc_restorecancel (canc);
  63.       }
  64.       return NULL;
  65. }
  66. #else
  67. DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
  68. {
  69.    CThread *aThread = (CThread *)lpParameter;
  70.    if(aThread)
  71.       return aThread->Execute();
  72.    else
  73.   return (DWORD)-1;
  74. }
  75. #endif
  76. DWORD CThread::Execute(void)
  77. {
  78.   /*
  79.     to do implement! override!
  80. while(!bTerminated) {
  81.  ...
  82. }
  83.   */
  84.  return 0;
  85. }
  86. void CThread::Terminate(void)
  87. {
  88.    // Set Termination Flag and EventObject!
  89.    // and wait for Termination
  90.    m_bTerminated = ATMO_TRUE;
  91. #if defined(_ATMO_VLC_PLUGIN_)
  92.    if(m_pAtmoThread)
  93.    {
  94.       vlc_mutex_lock( &m_TerminateLock );
  95.       vlc_cond_signal( &m_TerminateCond  );
  96.       vlc_mutex_unlock( &m_TerminateLock );
  97.       vlc_object_kill( m_pAtmoThread );
  98.       vlc_thread_join( m_pAtmoThread );
  99.    }
  100. #else
  101.    SetEvent(m_hTerminateEvent);
  102.    WaitForSingleObject(m_hThread,INFINITE);
  103. #endif
  104. }
  105. void CThread::Run()
  106. {
  107.    m_bTerminated = ATMO_FALSE;
  108. #if defined(_ATMO_VLC_PLUGIN_)
  109.    m_pAtmoThread->b_die = false;
  110.    if(vlc_thread_create( m_pAtmoThread,
  111.                          "Atmo-CThread-Class",
  112.                          CThread::ThreadProc,
  113.                          VLC_THREAD_PRIORITY_LOW ))
  114.    {
  115.       msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
  116.    }
  117. #else
  118.    ResetEvent(m_hTerminateEvent);
  119.    ResumeThread(m_hThread);
  120. #endif
  121. }
  122. /*
  123.    does a sleep if the sleep was interrupted through
  124.    the thread kill event return false...
  125. */
  126. ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
  127. {
  128. #if defined(_ATMO_VLC_PLUGIN_)
  129.      vlc_mutex_lock( &m_TerminateLock );
  130.      int value = vlc_cond_timedwait(&m_TerminateCond,
  131.                                     &m_TerminateLock,
  132.                                     mdate() + (mtime_t)(millisekunden * 1000));
  133.      vlc_mutex_unlock( &m_TerminateLock );
  134.      return (value != 0);
  135. #else
  136.      DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
  137.  return (res == WAIT_TIMEOUT);
  138. #endif
  139. }