[Contents] [index] [Help] [Retrace] [browse <] [Browse >]

To create a new message port using an operating system release prior to
V36, you must allocate and initialize a msgport structure. if you want to
make the port public, you will also need to call the addport() function.
Don't make a port public when it is not necessary for it to be so.  The
easiest way to create a port is to use the amiga.lib function
createport(name,priority).  if null is passed for the name, the port will
not be made public. Port structure initialization involves setting up a
node structure, establishing the message arrival action with its
parameters, and initializing the list header.  The following example of
port creation is equivalent to the CreatePort() function as supplied in
amiga.lib:

    struct MsgPort *CreatePort(UBYTE *name, LONG pri)
    {
        LONG sigBit;
        struct MsgPort *mp;

        if ((sigBit = AllocSignal(-1L)) == -1) return(NULL);

        mp = (struct MsgPort *) AllocMem((ULONG)sizeof(struct MsgPort),
                 (ULONG)MEMF_PUBLIC | MEMF_CLEAR);
        if (!mp) {
            FreeSignal(sigBit);
            return(NULL);
        }
        mp->mp_Node.ln_Name = name;
        mp->mp_Node.ln_Pri  = pri;
        mp->mp_Node.ln_Type = NT_MSGPORT;
        mp->mp_Flags        = PA_SIGNAL;
        mp->mp_SigBit       = sigBit;
        mp->mp_SigTask      = (struct Task *)FindTask(0L);
                                                  /* Find THIS task.   */

        if (name) AddPort(mp);
        else NewList(&(mp->mp_MsgList));          /* init message list */

        return(mp);
    }

As of V36 the Exec createmsgport() function can be used to create a
message port.  This function allocates and initializes a new message port.
Just like createport(), a signal bit will be allocated and the port will
be initialized to signal the creating task (mp_sigtask) when a message
arrives at this port.  To make the port public after CreateMsgPort(), you
must fill out the ln_name field and call addport().  if you do this, you
must remember to remport() the port from the public list in your cleanup.
If you need to create a message port and your application already requires
Release 2 or greater, you can use CreateMsgPort() instead of CreatePort().
The following is an example of the usage of CreateMsgPort().

    struct MsgPort *newmp;
            /* A private message port has been created. CreateMsgPort() */
    if (newmp = CreateMsgPort())
            /* returns NULL if the creation of the message port failed. */
    {
        newmp->mp_Node.ln_Name = "Griffin";
        newmp->mp_Node.ln_Pri  = 0;
                                /* To make it public fill in the fields */
        AddPort(newmp);         /* with appropriate values.             */
    }