home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 3.1 KB | 101 lines | [TEXT/MPS ] |
- /*
- File: TMacSemaphoreExample.cp
-
- Contains: This module shows an example use of TMacSemaphore.
-
- Copyright: © 1993-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TInitSLM.h" // the TInitSLM class and SLM include files
-
- #include <Events.h>
- #include <LowMem.h>
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// PROTOTYPES
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void move_cursor( Point newMouse);
-
- /*————————————————————————————————————————————————————————————————————————————————————
- main
-
- since the Macintosh OS supports only one thread of execution, the only way that
- data can be changed while you are trying to access it is for the data to be changed
- by an interrupt. One way to demonstrate is by changing the cursor position, since
- the cursor can change at interrupt time. Not only do we have to disable interrupts
- when we change the cursor position, but we also have to check to see if the cursor
- is already busy. If it is then we have to re-enable interrupts so then cursor
- can become "unbusy".
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- main()
- {
- TInitSLM initLibraryManager; // initialize the shared library manager
-
- if( initLibraryManager.Failed() ) // If we failed, let's go home
- return 1;
-
- TMacSemaphore *semaphore = new TMacSemaphore;
-
- TStopwatch stopwatch;
- while( stopwatch.ElapsedSeconds() < 2) { // do this for 2 seconds
-
- // we need to go into a loop until we can successfully grab the semaphore
- // while the cursor isn't busy.
- while( true ) {
- semaphore->Grab(); // disable interrupts, so we can change cursor
-
- if( ! LMGetCrsrBusy() ) // if cursor isn't busy our grab is good
- break;
-
- semaphore->Release(); // release and try again
- }
-
- Point newmouse;
-
- SetPt( &newmouse, 0,0 ); // move to cursor to 0,0
- move_cursor( newmouse );
-
- semaphore->Release(); // release
- }
-
- delete semaphore; // deallocate the space used
-
- return 0;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- move_cursor
-
- move the cursor to the new position passed. Please, don't try this at home. This
- is only to demonstrate the semaphores.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- //
- // Some enums stolen from the old SysEqu.h file.
- //
- enum {
- MTemp = 0x828, /*[GLOBAL VAR] Low-level interrupt mouse location [long]*/
- RawMouse = 0x82C, /*[GLOBAL VAR] un-jerked mouse coordinates [long]*/
- CrsrNew = 0x8CE, /*[GLOBAL VAR] Cursor changed? [byte]*/
- CrsrState = 0x8D0 /*[GLOBAL VAR] Cursor nesting level [word]*/
- };
-
- static void move_cursor( Point newMouse )
- {
- long *vh;
-
- vh = (long *)&newMouse;
-
- HideCursor(); // Hide the cursor since we are changing it
-
- *((long *)RawMouse) = *vh; // force the mouse position to top of scren
- *((long *)MTemp) = *vh; // also the temporary position
- *((short *)CrsrState) = 0; // indicate the cursor has changed
- *((char *)CrsrNew) = 1; // indicate a new cursor
-
- ShowCursor(); // redisplay the cursor now
- }
-