CodeWarrior (OS 9)/MPPeriodicalTest.c

/*
    File:       "MPPeriodicalTest.c"
    
    Description:
                This is a application to test the MT/MP timers.
 
    Version:    v0.0
 
    File Ownership:
 
        DRI:                George Warner
 
        Other Contact:      
 
        Technology:         MultiTasking/MultiProcessing
 
    Copyright:  © Copyright 2000 Apple Computer, Inc. All rights reserved.
    
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
    Writers:
 
        gaw - George Warner
 
    Change History (most recent first):
 
         <1>    02/29/00    gaw     Initial build
 
    Author Initials:
 
    gaw - George Warner
 
*/
 
#pragma mark compiler directives
#define USE_ABSOLUTE_TIMER 0    // set to 0 to use duration timer
 
#pragma mark #Includes
#include <MacTypes.h>
#include <Events.h>
#include <Fonts.h>
#include <Multiprocessing.h>
 
#include <stdio.h>          // for printf & fflush
#include <SIOUX.h>          // for SIOUXSettings stuff
#include <SIOUXGlobals.h>   // for SIOUXQuitting
 
#pragma mark typedefs, structs, enums, defines, etc.
#define kNumCounts 20   // This is the number of times we count down
 
#pragma mark exported functions
void main(void)
{
    MPTimerID       tMPTimerID;
    MPSemaphoreID   tMPSemaphoreID;
    AbsoluteTime    expirationTime;
    Duration        timeout = 1 * durationSecond;
    OSStatus        anErr;
    SInt32          tCount = kNumCounts;
 
    // Set the SIOUX window defaults
    SIOUXSettings.autocloseonquit   = false;
    SIOUXSettings.asktosaveonclose  = false;
    SIOUXSettings.showstatusline    = false;
    SIOUXSettings.columns           = 132;
    SIOUXSettings.rows              = 24;
    SIOUXSettings.fontsize          = 10;
    GetFNum("\pMonaco",&SIOUXSettings.fontid);
    SIOUXSettings.standalone        = true;
 
    printf("MPPeriodicalTest Starting!\n");
 
    // Make sure that the MP library is loaded
    if  (!MPLibraryIsLoaded())
    {
        printf("The MP library did not load.\n");
        return;
    }
 
    {   // for giggles we'll dump out the MP Library version info
        Ptr tVersionCStringPtr;
        UInt32 major,minor,release,revision;
 
        _MPLibraryVersion(&tVersionCStringPtr,&major,&minor,&release,&revision);
 
        printf("\n Version: \"%s\", major: %ld, minor: %ld, release: %ld, revision: %ld.\n",tVersionCStringPtr,major,minor,release,revision);
    }
 
    // Now create a timer
    anErr = MPCreateTimer(&tMPTimerID);
    if (noErr != anErr)
    {
        printf("MPCreateTimer error: %ld.\n", anErr);
        return;
    }
 
    // and a semaphore
    anErr = MPCreateSemaphore(kNumCounts,0,&tMPSemaphoreID);
    if (noErr != anErr)
    {
        printf("MPCreateSemaphore error: %ld.\n", anErr);
        return;
    }
 
    // Link the timer to the semaphore so that when the timer
    // fires the semaphore will be notified
    anErr = MPSetTimerNotify(tMPTimerID,(MPOpaqueID) tMPSemaphoreID,nil,nil,nil);
    if (noErr != anErr)
    {
        printf("MPSetTimerNotify error: %ld.\n", anErr);
        return;
    }
 
#if USE_ABSOLUTE_TIMER
    // calculate the expiration time based on the current
    // absolute time plus a duration and arm the timer.
    // The kMPPreserveTimerIDMask flag tells MP that we will be reusing the timer
    expirationTime = UpTime();
    expirationTime = AddDurationToAbsolute(timeout,expirationTime);
    anErr = MPArmTimer(tMPTimerID,&expirationTime,kMPPreserveTimerIDMask);
#else // use duration
    // Pass the duration by address and set the kMPTimeIsDurationMask flag.
    // Set the kMPTimeIsDeltaMask flag tell MP to add this delta to the 
    // last time the timer fired (or in this case, when the timer was created).
    // The kMPPreserveTimerIDMask flag tells MP that we will be reusing the timer
    anErr = MPArmTimer(tMPTimerID,(AbsoluteTime*) &timeout,
                kMPPreserveTimerIDMask | kMPTimeIsDeltaMask | kMPTimeIsDurationMask);
#endif
    if (noErr != anErr)
        printf("MPArmTimer error: %ld.\n", anErr);
 
    printf("Waiting %ld seconds...\n",tCount);
 
    while ((tCount > 0) && (!SIOUXQuitting))
    {
        // it's importaint that our WaitNextEvent loop not be CPU bound or
        // blocked by the MPWaitOnSemaphore so we use kDurationImmediate
        // to poll the semaphore and use a sleep time of one with WNE.
        if (kMPTimeoutErr == MPWaitOnSemaphore(tMPSemaphoreID,kDurationImmediate))
        {
            EventRecord theEvent;
            WaitNextEvent(everyEvent,&theEvent,1,nil);
            SIOUXHandleOneEvent(&theEvent); // tell SIOUX to handle the event
        }
        else
        {
#if USE_ABSOLUTE_TIMER
            // calculate the expiration time based on the last absolute
            // time plus our duration and then arm the timer again.
            expirationTime = AddDurationToAbsolute(timeout,expirationTime);
            anErr = MPArmTimer(tMPTimerID,&expirationTime,kMPPreserveTimerIDMask);
#else   // use duration
            // Pass the duration by address and set the kMPTimeIsDurationMask flag.
            // Set the kMPTimeIsDeltaMask flag tell MP to add this delta 
            // to the last time the timer fired.
            anErr = MPArmTimer(tMPTimerID,(AbsoluteTime*) &timeout,
                        kMPPreserveTimerIDMask | kMPTimeIsDeltaMask | kMPTimeIsDurationMask);
#endif
            if (noErr != anErr)
                printf("MPArmTimer error: %ld.\n", anErr);
 
            printf("%d ",--tCount);
            fflush(stdout);
        }
    }
 
    // cancel the timer
    anErr = MPCancelTimer(tMPTimerID,&expirationTime);
    if (noErr != anErr)
        printf("MPCancelTimer error: %d.\n",anErr);
 
    // dispose of our timer
    anErr = MPDeleteTimer(tMPTimerID);
    if (noErr != anErr)
        printf("MPDeleteTimer error: %d.\n",anErr);
 
    // dispose of our semaphore
    MPDeleteSemaphore(tMPSemaphoreID);
 
    printf("Done!\nPress command-Q to quit.\n");
}