home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / GimmeLib / subtinit.c < prev    next >
C/C++ Source or Header  |  1988-12-27  |  3KB  |  130 lines

  1. /*
  2.  *  FILE: subtinit.c
  3.  *  Support routines for spawning subtasks (not processes).
  4.  *  This means the subtask shares the same memory space as its parent task.
  5.  *
  6.  *  Note: this "library" is re-entrant, so any task can spawn and keep track
  7.  *  of its own subtasks independantly of other users of this "library".
  8.  *
  9.  *  Public Domain, but keep my name in it as the original author.
  10.  *  31-Oct-88    Jan Sven Trabandt   split from subtask.c
  11.  */
  12.  
  13.  
  14. #define I_AM_SUBTINIT
  15. #include "gimmelib/gimmefuncs.h"
  16. #define GIM_BUILTIN
  17. #include "gimmelib/macros.h"
  18.  
  19.  
  20. short initSubTasker( countptr, portptr )
  21.     SHORT        *countptr;
  22.     struct MsgPort  **portptr;
  23. {
  24.     if( countptr ) {
  25.     *countptr = 0;
  26.     }
  27.     if( portptr ) {
  28.     *portptr = CreatePort( NULL, 0L );
  29.     if( !*portptr ) {
  30.         return( -1 );
  31.     }
  32.     }
  33.     return( 0 );
  34. } /* initSubTasker */
  35.  
  36.  
  37. short doneSubTasker( countptr, portptr )
  38.     SHORT        *countptr;
  39.     struct MsgPort  **portptr;
  40. {
  41.     struct MsgPort  *myport;
  42.  
  43.     if( countptr && *countptr ) {
  44.     return( -1 );
  45.     }
  46.     if( portptr && (myport = *portptr) ) {
  47.     *portptr = NULL;
  48.     DeletePort( myport );
  49.     }
  50.     return( 0 );
  51. } /* doneSubTasker */
  52.  
  53.  
  54. /****************************************************************/
  55. /* routines to handle printing messages from subtasks to Output()
  56.  
  57. extern struct Message *gimmeMessage();
  58.  
  59. struct print_msg {
  60.     struct Message msg;
  61.     ULONG flags;
  62.     UBYTE buf[164];
  63. };
  64. typedef struct print_msg PRINTMSG;
  65.  
  66. #define PRINTMSG_FLAG    218572L
  67.  
  68.  
  69. short doPrintf( port, replyport, s, p1, p2, p3, p4 )
  70.     struct MsgPort  *port, *replyport;
  71.     UBYTE        *s;
  72.     LONG        p1, p2;
  73.     double        p3, p4;
  74. {
  75.     PRINTMSG    *pmsg;
  76.  
  77.     if( !port || !s ) {
  78.     return( -1 );
  79.     }
  80.     pmsg = (PRINTMSG *) gimmeMessage( (ULONG)sizeof(PRINTMSG), replyport );
  81.     if( !pmsg ) {
  82.     return( -1 );
  83.     }
  84.     pmsg->flags = PRINTMSG_FLAG;
  85.     sprintf( pmsg->buf, s, p1, p2, p3, p4 );
  86.     PutMsg( port, pmsg );
  87.     if( replyport ) {
  88.     WaitPort( replyport );
  89.     pmsg = (PRINTMSG *) GetMsg( port );
  90. /***
  91.     if( pmsg->msg.mn_Node.ln_Type != NT_REPLYMSG
  92.          || pmsg->flags != PRINTMSG_FLAG ) {
  93.     }
  94. ***/
  95.     getRidOfMessage( pmsg );
  96.     } /* endif */
  97.     return( 0 );
  98. } /* doPrintf */
  99.  
  100.  
  101. SHORT handleSpecialSubTaskMsg( countptr, portptr )
  102.     SHORT        *countptr;
  103.     struct MsgPort  **portptr;
  104. {
  105.     PRINTMSG    *msg;
  106.     SHORT    died = 0;
  107.  
  108.     if( !portptr || !*portptr ) {
  109.     return( died );
  110.     }
  111.     while( msg = (PRINTMSG *) GetMsg(*portptr) ) {
  112.     if( msg->flags == PRINTMSG_FLAG ) {
  113.         if( Output() ) {
  114.         Forbid();
  115.         Write( Output(), msg->buf, (ULONG)strlen(msg->buf) );
  116.         Permit();
  117.         }
  118.         if( !msg->msg.mn_ReplyPort ) {
  119.         getRidOfMessage( msg );
  120.         } else {
  121.         ReplyMsg( msg );
  122.         }
  123.     } else {    /* otherwise its a DONEMSG (see subtask.c) */
  124.         ReplyMsg( msg );
  125.         ++died;
  126.     } /* endif */
  127.     } /* while */
  128.     return( died );
  129. } /* handleSpecialSubTaskMsg */
  130.