Software Synthesis for OS-Independent Coding
by Bob Zeidman

Example 1: 


(a)
taskLed (g_in, rotation);

(b)
// SynthOS: begin generated code
// original statement: taskLed (g_in, rotation); 
// Create a new TCB.
{
     struct SYNTHOS_TCB *stcpNewTcb;
     stcpNewTcb = getFreeTcb(g_stcpTcbQ__taskLed);
     // make sure we get a free TCB
     ASSERT (stcpNewTcb != NULL); 
     stcpNewTcb->u32TaskState = 1;
     stcpNewTcb->pTaskId = NULL;
     stcpNewTcb->u32Params[0] = g_in;
     stcpNewTcb->u32Params[1] = rotation;
     (taskLed__type *)stcpNewTcb->u32pRetVal = &taskLed__dummy;
}
// SynthOS: end generated code



Listing One

int count = 0;
void valPrint(int p)
{
    count++;
    printf("%d : count = %d\n", p, count);
}
void main()
{
    // Create two threads for the valPrint() task
    //  Create a new thread
    Thread *the_thread = new Thread("child");
    // Start the new thread running valPrint()
    the_thread->Fork(valPrint, 1);
    // Run the valPrint() task in the current thread
   valprint (0);
}


Listing Two

int count = 0;
Semaphore *the_semaphore;

void valPrint(int p)
{
    int local_count;        // Local variable used to hold global count
    // Wait until no other task is executing the following code
    the_semaphore->P(); 
    count++;                // Increment count
    local_count = count;    // Store a local copy of count

    // Allow other tasks to proceed now that we're done executing the code
    the_semaphore->V();
    printf("%d : count = %d\n", p, local_count);
}
void main()
    // Create two threads for the valPrint() task
    //  Create a new thread
    Thread *the_thread = new Thread("child");
    // Create a semaphore
    the_semaphore = new Semaphore("the_semaphore", 1);
    // Start the new thread running valPrint()
    the_thread->Fork(valPrint, 1);
    // Run the valPrint() task in the current thread
    valPrint(0);
}


Listing Three

void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
{
    // Send a message to the queue
    qbuf->mtype = type;
    strcpy(qbuf->mtext, text);

    if((msgsnd(qid, (struct msgbuf *)qbuf, strlen(qbuf->mtext)+1,0)) ==-1)
    {
        perror("msgsnd");
        exit(1);
    }
}
void read_message(int qid, struct mymsgbuf *qbuf, long type)
{
    // Read a message from the queue
    printf("Reading a message ...\n");
    qbuf->mtype = type;
    msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
}
void remove_queue(int qid)
{
    // Remove the queue
    msgctl(qid, IPC_RMID, 0);
}


Listing Four

int count = 0;

void valPrint(int p)
{
    count++;
    printf("%d : count = %d\n", p, count);
}
void main()
{
    // Create two threads for the valPrint() task
    // Start the new thread running valPrint()
    SynthesizeThread(valPrint(1));
    // Run the valPrint() task in the current thread
    valprint (0);
}


Listing Five

int count = 0;
struct StringMsg        // Create a structure for passing messages
{
    char StringBuf[20];
} Message;
struct StringMsg valPrint(int p)
{
    struct StringMsg Message;       // Buffer to hold the message
    count++;
    printf("%d : count = %d\n", p, count);
    // Create the message and pass it to the calling routine
    strcpy(Message.StringBuf, "message #");
    _itoa(count, &Message.StringBuf[9], 10);
    return (Message);
}
void main()
{
    struct StringMsg ThreadMsg1;    // Pointer to the message from thread 1
    struct StringMsg ThreadMsg2;    // Pointer to the message from thread 2
    // Create two threads for the valPrint() task
    // Start the new thread running valPrint()
    SynthesizeThread(ThreadMsg1 = valPrint(1));
    // Run the valPrint() task in the current thread
    ThreadMsg2 = valPrint (0);
    printf("First message = %s\n", ThreadMsg1.StringBuf);
    printf("Second message = %s\n", ThreadMsg2.StringBuf);
}






3


