home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / windr440.zip / windrive.zip / WinDriver / include / windrvr_int_thread.h next >
C/C++ Source or Header  |  2000-03-30  |  6KB  |  217 lines

  1. #ifndef _WINDRVR_INT_THREAD_H_
  2. #define _WINDRVR_INT_THREAD_H_
  3.  
  4. #include "windrvr.h"
  5.  
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif  // __cplusplus 
  9.  
  10. #if defined(__KERNEL__)
  11.  
  12.     typedef void (*INT_HANDLER_FUNC)(PVOID pData);
  13.     typedef struct 
  14.     {
  15.         HANDLE hWD;
  16.         WD_INTERRUPT *pInt;
  17.         INT_HANDLER_FUNC func;
  18.         PVOID pData;
  19.     } INT_THREAD_DATA;
  20.  
  21.     static void __cdecl InterruptThreadHandler (PVOID pContext)
  22.     {
  23.         INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) pContext;
  24.  
  25.         WD_IntCount(pThread->hWD, pThread->pInt);
  26.         pThread->func(pThread->pData);
  27.     }
  28.  
  29.     static BOOL InterruptThreadEnable(HANDLE *phThread, HANDLE hWD, 
  30.         WD_INTERRUPT *pInt, INT_HANDLER_FUNC func, PVOID pData)
  31.     {
  32.         INT_THREAD_DATA *pThread;
  33.         *phThread = NULL;
  34.  
  35.         pThread = (INT_THREAD_DATA *) malloc(sizeof (INT_THREAD_DATA));
  36.         if (!pThread)
  37.             return FALSE;
  38.  
  39.         pInt->kpCall.hKernelPlugIn = WD_KERNEL_DRIVER_PLUGIN_HANDLE;
  40.         pInt->kpCall.dwMessage = (DWORD) InterruptThreadHandler;
  41.         pInt->kpCall.pData = pThread;
  42.         WD_IntEnable (hWD, pInt);
  43.         // check if WD_IntEnable failed
  44.         if (!pInt->fEnableOk)
  45.             return FALSE;
  46.         
  47.         pThread->pInt = pInt;
  48.         pThread->hWD = hWD;
  49.         pThread->func = func;
  50.         pThread->pData = pData;
  51.  
  52.         *phThread = (HANDLE) pThread;
  53.         return TRUE;
  54.     }
  55.  
  56.     static void InterruptThreadDisable(HANDLE hThread)
  57.     {
  58.         if (hThread)
  59.         {
  60.             INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) hThread;
  61.             WD_IntDisable(pThread->hWD, pThread->pInt);
  62.             free (pThread);
  63.         }
  64.     }
  65. #else
  66.     #if !defined(VXWORKS)
  67.         #include <malloc.h>
  68.         #if defined(UNIX)
  69.             #include <pthread.h>
  70.         #endif
  71.     #endif
  72.     
  73.     typedef VOID (*INT_HANDLER_FUNC)(PVOID pData);
  74.     typedef struct 
  75.     {
  76.         #if defined(WIN32)
  77.             HANDLE hThread;
  78.         #elif defined(UNIX)
  79.             #if defined(VXWORKS)
  80.                 int hThread;
  81.             #else
  82.                 pthread_t hThread;
  83.             #endif
  84.         #elif defined(OS2)
  85.             TID hThread;
  86.         #endif
  87.         HANDLE hWD;
  88.         INT_HANDLER_FUNC func;
  89.         PVOID pData;    
  90.         WD_INTERRUPT *pInt;     
  91.     } INT_THREAD_DATA;
  92.  
  93.     #if defined(WIN32)
  94.     static DWORD WINAPI InterruptThreadHandler (PVOID pData)
  95.     #elif defined(OS2)
  96.     static void InterruptThreadHandler (PVOID pData)
  97.     #else
  98.     static void* InterruptThreadHandler (PVOID pData)
  99.     #endif
  100.     {
  101.         INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) pData;
  102.         for (;;)
  103.         {
  104.             WD_IntWait (pThread->hWD, pThread->pInt);
  105.             if (pThread->pInt->fStopped)
  106.                 break;
  107.             pThread->func(pThread->pData);
  108.         }
  109.         #if !defined(OS2)
  110.             return 0;
  111.         #endif
  112.     }
  113.  
  114.     static BOOL InterruptThreadEnable(HANDLE *phThread, HANDLE hWD, 
  115.         WD_INTERRUPT *pInt, INT_HANDLER_FUNC func, PVOID pData)
  116.     {
  117.         #if defined(VXWORKS)
  118.             int priority;
  119.         #endif
  120.         INT_THREAD_DATA *pThread;
  121.         *phThread = NULL;
  122.  
  123.         WD_IntEnable (hWD, pInt);
  124.         // check if WD_IntEnable failed
  125.         if (!pInt->fEnableOk)
  126.             return FALSE;
  127.         
  128.         pThread = (INT_THREAD_DATA *) malloc(sizeof (INT_THREAD_DATA));
  129.         pThread->func = func;
  130.         pThread->pData = pData;
  131.         pThread->hWD = hWD;
  132.         pThread->pInt = pInt;
  133.  
  134.         #if defined(OS2)
  135.             pThread->hThread = _beginthread(InterruptThreadHandler, NULL, 0x4000, (PVOID)pThread);
  136.         #elif !defined(UNIX)
  137.             pThread->hThread = CreateThread (0, 0x1000, 
  138.                 InterruptThreadHandler, (PVOID) pThread, 0, 
  139.                 &GlobalDW);
  140.         #elif !defined(VXWORKS)
  141.             pthread_create( &pThread->hThread, NULL, InterruptThreadHandler, 
  142.                 (PVOID) pThread);
  143.         #else
  144.             if( taskPriorityGet( taskIdSelf(), &priority ) == ERROR )
  145.             {
  146.                 free( pThread );
  147.                 return FALSE;
  148.             }
  149.             pThread->hThread = taskSpawn( NULL, priority, 0, 2000, 
  150.                 (FUNCPTR)InterruptThreadHandler, (int)pThread, 0, 0, 0, 0, 0,
  151.                 0, 0, 0, 0);
  152.             if( pThread->hThread == ERROR )
  153.             {
  154.                 free( pThread );
  155.                 return FALSE;
  156.             }
  157.         #endif
  158.         *phThread = (HANDLE) pThread;
  159.         return TRUE;
  160.     }
  161.  
  162.     #if defined(VXWORKS)
  163.     
  164.     #define WAIT_FOR_EVER 0
  165.     
  166.     static void vxTask_wait(int taskId, int waitTime)
  167.     {
  168.         SEM_ID waitSem;
  169.         
  170.         if(waitTime == WAIT_FOR_EVER)
  171.         {
  172.             /* Loop while task is still alive */
  173.             while(taskIdVerify(taskId) == OK)
  174.                 taskDelay(3);
  175.         }
  176.         else
  177.         {
  178.             /* 
  179.              * create a dummy semaphore and try to take it for the specified
  180.              * time.
  181.              */ 
  182.             waitSem = semBCreate(SEM_Q_PRIORITY , SEM_EMPTY);
  183.             semTake(waitSem, waitTime);
  184.             semDelete(waitSem);
  185.         }
  186.     }
  187.     #endif
  188.     
  189.     static VOID InterruptThreadDisable(HANDLE hThread)
  190.     {
  191.         if (hThread)
  192.         {
  193.             INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) hThread;
  194.             WD_IntDisable(pThread->hWD, pThread->pInt);
  195.             #if defined(WIN23)
  196.                 WaitForSingleObject (pThread->hThread, INFINITE);
  197.                 CloseHandle(pThread->hThread);
  198.             #elif defined(OS2)
  199.                 DosWaitThread((ULONG *)&pThread->hThread, DCWW_WAIT);
  200.             #elif !defined(UNIX) && !defined(VXWORKS)
  201.                 pthread_join(pThread->hThread, NULL);
  202.             #else
  203.                 vxTask_wait((int)pThread->hThread, WAIT_FOR_EVER);
  204.             #endif
  205.             free (pThread);
  206.         }
  207.     }
  208.  
  209. #endif
  210.  
  211. #ifdef __cplusplus
  212. }
  213. #endif  // __cplusplus 
  214.  
  215. #endif
  216.  
  217.