Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
ProjectBuilder (OS X)/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): |
<2> 7/2003 MK Updated for Project Builder |
<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 <CoreServices/CoreServices.h> |
//#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> s// 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) |
int 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; |
//this is an error, exit condition |
//reflects this appropriately. |
return 1; |
//% |
} |
{ // for giggles we'll dump out the MP Library version info |
Ptr tVersionCStringPtr; |
UInt32 major,minor,release,revision; |
//% |
//I'm not sure why the original gives a warning...it shouldn't...ahhh...const |
//_MPLibraryVersion(&tVersionCStringPtr,&major,&minor,&release,&revision); |
_MPLibraryVersion((const char**)&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; |
//this is an error, exit condition |
//reflects this appropriately. |
return 1; |
//% |
} |
// and a semaphore |
anErr = MPCreateSemaphore(kNumCounts,0,&tMPSemaphoreID); |
if (noErr != anErr) |
{ |
printf("MPCreateSemaphore error: %ld.\n", anErr); |
//% |
//return; |
//this is an error, exit condition |
//reflects this appropriately. |
return 1; |
//% |
} |
// 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; |
//this is an error, exit condition |
//reflects this appropriately. |
return 1; |
//% |
} |
#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)) |
while ( tCount > 0 ) |
{ |
// 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); |
printf("%d ",(signed int) --tCount); |
//% |
fflush(stdout); |
} |
} |
// cancel the timer |
anErr = MPCancelTimer(tMPTimerID,&expirationTime); |
if (noErr != anErr) |
//% |
{ |
//printf("MPCancelTimer error: %d.\n",anErr); |
printf("MPCancelTimer error: %d.\n",(int) anErr); |
} |
//% |
// dispose of our timer |
anErr = MPDeleteTimer(tMPTimerID); |
if (noErr != anErr) |
//% |
{ |
//printf("MPDeleteTimer error: %d.\n",anErr); |
printf("MPDeleteTimer error: %d.\n", (int) anErr); |
} |
//% |
// dispose of our semaphore |
MPDeleteSemaphore(tMPSemaphoreID); |
//% |
//printf("Done!\nPress command-Q to quit.\n"); |
printf("\nDone!\n"); |
return 0; |
//% |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-10-27