home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / SEMA4.C < prev    next >
Text File  |  1995-04-20  |  20KB  |  415 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*                                                                           */
  4. /*  sema4.c                                                                  */
  5. /*                                                                           */
  6. /* Description:                                                              */
  7. /*                                                                           */
  8. /*   These functions are for serial connections only.  Functions for         */
  9. /*   creating, setting, querying, and waiting for the connect sema4          */
  10. /*   resource. Posting is done in the queue threads.                         */
  11. /*                                                                           */
  12. /* History:                                                                  */
  13. /*                                                                           */
  14. /*   10/14/94 Created.                                                       */
  15. /*                                                                           */
  16. /*****************************************************************************/
  17.  
  18. #include "all.h"
  19.  
  20. static HEV      ConnectSema4;
  21.  
  22. /*****************************************************************************/
  23. /* CreateConnectSema4()                                                      */
  24. /*                                                                           */
  25. /* Description:                                                              */
  26. /*                                                                           */
  27. /*   Create a connect sema4 for the pid parameter.  Register the sema4       */
  28. /*   with the queue.                                                         */
  29. /*                                                                           */
  30. /* Parameters:                                                               */
  31. /*                                                                           */
  32. /*   pid                 pid of the debugger or probe.                       */
  33. /*                       process id of the probe that this sema4 is for.     */
  34. /*   DbgOrEsp            whether the create occurs in the dbg or esp.        */
  35. /*                                                                           */
  36. /* Return:                                                                   */
  37. /*                                                                           */
  38. /* Assumptions:                                                              */
  39. /*                                                                           */
  40. /*****************************************************************************/
  41. #define INIT_STATE_RESET  FALSE
  42. #define INIT_STATE_POSTED TRUE
  43.  
  44. void CreateConnectSema4( USHORT pid, int DbgOrEsp )
  45. {
  46.  BOOL     InitState;
  47.  ALLPIDS *p;
  48.  
  49.  /****************************************************************************/
  50.  /* - Note that the QueCaller is no longer used.                             */
  51.  /****************************************************************************/
  52.  
  53.  InitState = INIT_STATE_RESET;
  54.  if( (DbgOrEsp == _DBG) && IsParent() )
  55.   InitState = INIT_STATE_POSTED;
  56.  
  57.  DosCreateEventSem(NULL, &ConnectSema4, DC_SEM_SHARED, InitState );
  58.  
  59. #ifdef __ESP__
  60. /*---------------------------------------------------------------------------*/
  61.   p = GetEspPid( pid );
  62. /*---------------------------------------------------------------------------*/
  63. #endif
  64.  
  65. #ifdef __DBG__
  66. /*---------------------------------------------------------------------------*/
  67.   p = GetDbgPid( pid );
  68. /*---------------------------------------------------------------------------*/
  69. #endif
  70.  
  71.  p->ConnectSema4 = ConnectSema4;
  72.  
  73.  if( IsVerbose() ) {printf("\nConnect Sema4 Created");fflush(0);}
  74. }
  75.  
  76. /*****************************************************************************/
  77. /* SerialConnect()                                                           */
  78. /*                                                                           */
  79. /* Description:                                                              */
  80. /*                                                                           */
  81. /*   For serial connections, these functions the connect/disconnect of       */
  82. /*   the debuggers and the probes.                                           */
  83. /*                                                                           */
  84. /* Parameters:                                                               */
  85. /*                                                                           */
  86. /*   ConnectOrDisConnect Sema4 action to take.                               */
  87. /*   pid                 process id that the action applies to.              */
  88. /*   DbgOrEsp            whether the action occurs in the dbg or esp.        */
  89. /*   QueCaller           either the esp or dbg que message sender.           */
  90. /*                                                                           */
  91. /* Return:                                                                   */
  92. /*                                                                           */
  93. /* Assumptions:                                                              */
  94. /*                                                                           */
  95. /*****************************************************************************/
  96. void SerialConnect( int   ConnectOrDisConnect,
  97.                     ULONG pid,
  98.                     int   DbgOrEsp,
  99.                     void  (* QueCaller)( ULONG , void * , ULONG ) )
  100. {
  101.  struct
  102.  {
  103.   USHORT pid1;
  104.   USHORT pid2;
  105.  }Qelement;
  106.  
  107.  ULONG QueMsg;
  108.  
  109.  /****************************************************************************/
  110.  /* - If a sema4 was not created for this debugger/probe then just return.   */
  111.  /****************************************************************************/
  112.  if( ConnectSema4 == 0 )
  113.   return;
  114.  
  115.  /****************************************************************************/
  116.  /* - When sending the message to dbg, the pid goes in element 1.            */
  117.  /* - When sending the message to esp, the pid goes in element 2.            */
  118.  /****************************************************************************/
  119.  if( DbgOrEsp == _DBG )
  120.   Qelement.pid1 = (USHORT)pid;
  121.  else
  122.   Qelement.pid2 = (USHORT)pid;
  123.  
  124.  switch( ConnectOrDisConnect )
  125.  {
  126.   case CONNECT_WAIT:
  127.  
  128.    /**************************************************************************/
  129.    /* - send a connect message to the que and wait for a post.               */
  130.    /*                                                                        */
  131.    /* Note: At this point in time, the CONNECT_WAIT is only generated        */
  132.    /*       in an esp.                                                       */
  133.    /**************************************************************************/
  134.    QueMsg = ESP_QMSG_CONNECT_REQUEST;
  135.  
  136.    (* QueCaller)( QueMsg, &Qelement, sizeof(Qelement) );
  137.  
  138.    WaitConnectSema4(ConnectSema4);
  139.    break;
  140.  
  141.   case DISCONNECT:
  142.  
  143.    /**************************************************************************/
  144.    /* - set the sema4 for this dbg/esp and inform the que of the disconnect. */
  145.    /**************************************************************************/
  146.    SetConnectSema4(&ConnectSema4, FALSE );
  147.  
  148.    QueMsg = DBG_QMSG_DISCONNECT;
  149.    if( DbgOrEsp == _ESP )
  150.     QueMsg = ESP_QMSG_DISCONNECT;
  151.  
  152.    (* QueCaller)( QueMsg, &Qelement, sizeof(Qelement) );
  153.  
  154.    break;
  155.  
  156.   case DISCONNECT_WAIT:
  157.  
  158.    /**************************************************************************/
  159.    /* - set the sema4 for this dbg/esp and inform the que of the disconnect. */
  160.    /* - wait for a post.                                                     */
  161.    /**************************************************************************/
  162.    SetConnectSema4(&ConnectSema4, FALSE );
  163.  
  164.    QueMsg = DBG_QMSG_DISCONNECT;
  165.    if( DbgOrEsp == _ESP )
  166.     QueMsg = ESP_QMSG_DISCONNECT;
  167.  
  168.    (* QueCaller)( QueMsg, &Qelement, sizeof(Qelement) );
  169.  
  170.    WaitConnectSema4(ConnectSema4);
  171.    break;
  172.  
  173.   case JUST_WAIT:
  174.  
  175.    /**************************************************************************/
  176.    /* - just wait for a post.                                                */
  177.    /**************************************************************************/
  178.    WaitConnectSema4(ConnectSema4);
  179.    break;
  180.  
  181.   case SET_WAIT:
  182.  
  183.    /**************************************************************************/
  184.    /* - set the sema4 for this dbg/esp. Do not inform the que.               */
  185.    /* - wait for a post.                                                     */
  186.    /**************************************************************************/
  187.    SetConnectSema4(&ConnectSema4, FALSE );
  188.    WaitConnectSema4(ConnectSema4);
  189.    break;
  190.  }
  191. }
  192.  
  193. /*****************************************************************************/
  194. /* SetConnectSema4()                                                         */
  195. /*                                                                           */
  196. /* Description:                                                              */
  197. /*                                                                           */
  198. /*   For serial connections, set( actually reset) the connect sema4.         */
  199. /*                                                                           */
  200. /* Parameters:                                                               */
  201. /*                                                                           */
  202. /*   pConnectSema4     ->connect sema4.                                      */
  203. /*   TorF                TRUE==>open the sema4...it's owned by another       */
  204. /*                                               process.                    */
  205. /*                       FALSE==>owned by calling process...no need to open. */
  206. /*                                                                           */
  207. /* Return:                                                                   */
  208. /*                                                                           */
  209. /* Assumptions:                                                              */
  210. /*                                                                           */
  211. /*****************************************************************************/
  212. void SetConnectSema4( HEV *pConnectSema4, BOOL TorF )
  213. {
  214.  ULONG      Count = 0;
  215.  
  216.  /****************************************************************************/
  217.  /* - open the event sema4 if it's not owned by the calling process.         */
  218.  /****************************************************************************/
  219.  if( TorF == TRUE )
  220.   DosOpenEventSem( NULL, pConnectSema4 );
  221.  
  222.  /****************************************************************************/
  223.  /* - reset the sema4.                                                       */
  224.  /****************************************************************************/
  225.  DosResetEventSem( *pConnectSema4, &Count );
  226.  
  227.  /****************************************************************************/
  228.  /* - If the sema4 is not owned by the calling process AND                   */
  229.  /*   the sema4 count is not 0, then close the sema4.                        */
  230.  /* - Closing the sema4 wiht a count=0 obliterates the sema4.                */
  231.  /****************************************************************************/
  232.  if( TorF == TRUE )
  233.  {
  234.   DosQueryEventSem( *pConnectSema4, &Count);
  235.   if( Count != 0 )
  236.    DosCloseEventSem( *pConnectSema4 );
  237.  }
  238. }
  239.  
  240. /*****************************************************************************/
  241. /* - wait for a sema4 to be posted.                                          */
  242. /*****************************************************************************/
  243. void WaitConnectSema4( HEV hSema4 )
  244. {
  245.  APIRET rc;
  246.  
  247. getbackinthere:
  248.  rc = DosWaitEventSem( hSema4, SEM_INDEFINITE_WAIT);
  249.  if( rc == ERROR_INTERRUPT ) goto getbackinthere;
  250. }
  251.  
  252. /*****************************************************************************/
  253. /* - close a sema4.                                                          */
  254. /*****************************************************************************/
  255. void CloseConnectSema4( void )
  256. {
  257.  /****************************************************************************/
  258.  /* - If a sema4 was not created for this debugger/probe then just return.   */
  259.  /****************************************************************************/
  260.  if( ConnectSema4 == 0 )
  261.   return;
  262.  
  263.  DosCloseEventSem(ConnectSema4);
  264.  
  265.  ConnectSema4 = 0;
  266.  if( IsVerbose() ) {printf("\nConnect Sema4 Closed");fflush(0);}
  267. }
  268.  
  269. /*****************************************************************************/
  270. /* - test a sema4 to see if it's reset or posted.                            */
  271. /*****************************************************************************/
  272. int QueryConnectSema4( void )
  273. {
  274.  ULONG count = 0;
  275.  
  276.  /****************************************************************************/
  277.  /* - If a sema4 was not created for this debugger/probe then just return.   */
  278.  /****************************************************************************/
  279.  if( ConnectSema4 == 0 )
  280.   return( SEMA4_NOT );
  281.  
  282.  DosQueryEventSem( ConnectSema4, &count );
  283.  
  284.  if( count == 0 )
  285.   return( SEMA4_RESET );
  286.  return( SEMA4_POSTED );
  287. }
  288.  
  289. /*****************************************************************************/
  290. /* PostConnectSema4()                                                        */
  291. /*                                                                           */
  292. /* Description:                                                              */
  293. /*                                                                           */
  294. /*   For serial connections only. Open, if need be, and post a connect       */
  295. /*   sema4 to release a debugger. Close the sema4, if need be.               */
  296. /*                                                                           */
  297. /* Parameters:                                                               */
  298. /*                                                                           */
  299. /*  pConnectSema4  -> to the connect sema4 to be posted.                     */
  300. /*  TorF              whether or not to open the sema4 before posting.       */
  301. /*                                                                           */
  302. /* Return:                                                                   */
  303. /*                                                                           */
  304. /* Assumptions:                                                              */
  305. /*                                                                           */
  306. /*****************************************************************************/
  307. void PostConnectSema4( HEV *pConnectSema4, BOOL TorF )
  308. {
  309.  ULONG  count;
  310.  APIRET rc;
  311.  
  312.  if( TorF == TRUE )
  313.   DosOpenEventSem( NULL, pConnectSema4 );
  314.  
  315.  rc = DosPostEventSem( *pConnectSema4 );
  316.  if(rc)
  317.   {printf("\npost sema4 failure rc=%d",rc);fflush(0);exit(0);}
  318.  
  319.  if( TorF == TRUE )
  320.  {
  321.   DosQueryEventSem( *pConnectSema4, &count);
  322.   if( count != 0 )
  323.    DosCloseEventSem( *pConnectSema4 );
  324.  }
  325. }
  326.  
  327. /*****************************************************************************/
  328. /* CreateSerialMutexSema4()                                                  */
  329. /* OpenSerialMutexSema4()                                                    */
  330. /* RequestSerialMutexSema4()                                                 */
  331. /* ReleaseSerialMutexSema4()                                                 */
  332. /* CloseSerialMutexSema4()                                                   */
  333. /*                                                                           */
  334. /* Description:                                                              */
  335. /*                                                                           */
  336. /*   These functions provide for the management of access to the serial      */
  337. /*   channel when debugging multiple processes.                              */
  338. /*                                                                           */
  339. /* Parameters:                                                               */
  340. /*                                                                           */
  341. /* Return:                                                                   */
  342. /*                                                                           */
  343. /* Assumptions:                                                              */
  344. /*                                                                           */
  345. /*   This function is only called by the dbg end of the connection.          */
  346. /*                                                                           */
  347. /*****************************************************************************/
  348.  
  349. static char SerialMutexSema4[]="\\SEM32\\SERSEMA4";
  350. static HMTX hmtxSerialMutexSema4 = 0;
  351.  
  352. void CreateSerialMutex( void )
  353. {
  354.  /****************************************************************************/
  355.  /* - Create a named mutex sema4 initially unowned.                          */
  356.  /* - The DC_SEM_SHARED flag is ignored, but it's included to simply         */
  357.  /*   document the fact that this sema4 is shared among the debuggers.       */
  358.  /****************************************************************************/
  359.  DosCreateMutexSem(  SerialMutexSema4,
  360.                     &hmtxSerialMutexSema4,
  361.                      DC_SEM_SHARED,
  362.                      FALSE);
  363.  if( IsVerbose() ) {printf("\nMutex Sema4 Created");fflush(0);}
  364. }
  365.  
  366. void OpenSerialMutex( void )
  367. {
  368.  APIRET rc;
  369.  /****************************************************************************/
  370.  /* - Open the serial mutex sema4.                                           */
  371.  /* - This is done in a child debugger to gain access to the sema4 created   */
  372.  /*   by the parent debugger.                                                */
  373.  /****************************************************************************/
  374.  rc = DosOpenMutexSem( SerialMutexSema4, &hmtxSerialMutexSema4 );
  375.  if(rc)
  376.   {printf("\nmutex error rc=%d",rc);fflush(0);exit(0);}
  377.  
  378. }
  379.  
  380. void RequestSerialMutex( void )
  381. {
  382.  APIRET rc;
  383.  
  384.  if( !hmtxSerialMutexSema4 ) return;
  385.  
  386.  /****************************************************************************/
  387.  /* - Request ownership of the serial mutex sema4 for this process.          */
  388.  /****************************************************************************/
  389.  rc = DosRequestMutexSem( hmtxSerialMutexSema4, SEM_INDEFINITE_WAIT );
  390.  if(rc)
  391.   {printf("\nmutex error rc=%d",rc);fflush(0);exit(0);}
  392.  
  393. }
  394.  
  395. void ReleaseSerialMutex( void )
  396. {
  397.  if( !hmtxSerialMutexSema4 ) return;
  398.  /****************************************************************************/
  399.  /* - Release ownership of the serial mutex sema4 for this process.          */
  400.  /****************************************************************************/
  401.  DosReleaseMutexSem( hmtxSerialMutexSema4 );
  402. }
  403.  
  404. void CloseSerialMutex( void )
  405. {
  406.  if( !hmtxSerialMutexSema4 ) return;
  407.  /****************************************************************************/
  408.  /* - Close the serial mutex sema4 for this process.                         */
  409.  /* - The final close by the parent debugger will remove the sema4 from      */
  410.  /*   the system.                                                            */
  411.  /****************************************************************************/
  412.  DosCloseMutexSem( hmtxSerialMutexSema4 );
  413.  if( IsVerbose() ) {printf("\nMutex Sema4 Closed");fflush(0);}
  414. }
  415.