home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / memsz331.zip / Source.zip / THREAD.CPP < prev    next >
Text File  |  1995-07-29  |  4KB  |  115 lines

  1. /***************************************************************** THREAD.CPP
  2.  *                                                                          *
  3.  *                       Thread Starter Function                            *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define INCL_BASE
  12. #define INCL_PM
  13. #include <os2.h>
  14.  
  15. #include "Debug.h"
  16. #include "Except.h"
  17. #include "Thread.h"
  18.  
  19. //#define DEBUG
  20.  
  21.  
  22. /****************************************************************************
  23.  *                                                                          *
  24.  *                         Definitions & Declarations                       *
  25.  *                                                                          *
  26.  ****************************************************************************/
  27.  
  28.   // Type Definitions
  29.  
  30. typedef struct {
  31.    char *Name ;
  32.    void (*Function)(void*) ;
  33.    void *Parameter ;
  34. } THREAD_PARMS, *PTHREAD_PARMS ;
  35.  
  36.  
  37.   // Function Prototypes
  38.  
  39. static void _Optlink Thread ( void *Parameter ) ;
  40.  
  41.  
  42. /****************************************************************************
  43.  *                                                                          *
  44.  *                         Generic Thread Starter                           *
  45.  *                                                                          *
  46.  ****************************************************************************/
  47.  
  48. extern TID StartThread ( char *Name, void(*Function)(void*), int StackSize, void *Parameter, int Priority ) {
  49.  
  50.    PTHREAD_PARMS Parms = PTHREAD_PARMS ( AllocateMemory ( sizeof(THREAD_PARMS) ) ) ;
  51.    Parms->Name = PCHAR ( AllocateMemory ( strlen(Name) + 1 ) ) ;
  52.    strcpy ( Parms->Name, Name ) ;
  53.    Parms->Function = Function ;
  54.    Parms->Parameter = Parameter ;
  55.  
  56.    TID ThreadID = _beginthread ( Thread, 0, StackSize, Parms ) ;
  57.  
  58.    if ( ThreadID < 1 )
  59.       Log ( "ERROR: Unable to start thread '%s'.", Name ) ;
  60.  
  61.    DosSetPriority ( PRTYS_THREAD, PRTYC_NOCHANGE, Priority, ThreadID ) ;
  62.  
  63.    return ( ThreadID ) ;
  64. }
  65.  
  66. /****************************************************************************
  67.  *                                                                          *
  68.  *                            Generic Thread                                *
  69.  *                                                                          *
  70.  ****************************************************************************/
  71.  
  72. static void _Optlink Thread ( void *Parameter ) {
  73.  
  74.   /**************************************************************************
  75.    * Disable exception popups and register the exception handler.           *
  76.    **************************************************************************/
  77.  
  78.    EXCEPTIONREGISTRATIONRECORD ExceptionRecord = { 0, ExceptionHandler } ;
  79.    DosSetExceptionHandler ( &ExceptionRecord ) ;
  80.  
  81.   /**************************************************************************
  82.    * Get parameters.                                                        *
  83.    **************************************************************************/
  84.  
  85.    PTHREAD_PARMS Parms = PTHREAD_PARMS ( Parameter ) ;
  86.  
  87.   /**************************************************************************
  88.    * Execute the function requested.                                        *
  89.    **************************************************************************/
  90.  
  91.    #ifdef DEBUG
  92.    Log ( "Thread '%s' starting.", Parms->Name ) ;
  93.    #endif
  94.  
  95.    Parms->Function ( Parms->Parameter ) ;
  96.  
  97.    #ifdef DEBUG
  98.    Log ( "Thread '%s' ending.", Parms->Name ) ;
  99.    #endif
  100.  
  101.   /**************************************************************************
  102.    * Release the memory allocated for the thread parameters.                *
  103.    **************************************************************************/
  104.  
  105.    FreeMemory ( Parms->Name ) ;
  106.    FreeMemory ( Parms ) ;
  107.  
  108.   /**************************************************************************
  109.    * Unhook the exception handler.                                          *
  110.    **************************************************************************/
  111.  
  112.    DosUnsetExceptionHandler ( &ExceptionRecord ) ;
  113. }
  114.  
  115.