Sun Microsystems 2 Wireless Office Headset User Manual


 
Chapter 2 Multitasking Safety 9
You might find that you cannot organize the singleton’s maintenance in this way,
because its state must be updated synchronously and atomically. Maintaining the
foreground state is an example of this type of singleton. In this case, migrate a key
piece of state into native memory and handle updates through calls to native
methods.
Multitasking Safety Example
Consider a simple though somewhat contrived example of controlling an external
device, a microwave oven. Assume that this device has a native API defined in the
header file shown in
CODE EXAMPLE 2-1.
CODE EXAMPLE 2-1 Native API for a Microwave Oven
/* mw.h */
/*
* Status codes passed to cook callback. The cooking might have been
* interrupted, for instance, if the user pressed the STOP button or
* opened the oven door.
*/
typedef enum {
MW_DONE, /* cooking finished normally */
MW_INTERRUPTED, /* cooking interrupted */
} MWSTATUS;
/* Callback for the cook operation. */
typedef void (*MWCB)(MWSTATUS status, void *context);
/* Initializes the microwave oven. Must be called exactly once. */
extern void mw_init(void);
/* Sets the cook time for the next cook operation, in seconds. */
extern void mw_settime(int nsec);
/*
* Sets the cook power for the next cook operation, an integer
* in the range [1-100].
*/
extern void mw_setpower(int power);
/*
* Initiates the cooking operation. When the cooking finishes,
* the callback is called with the status code. The context pointer
* is passed to the callback for its own use, unmodified by the
* library.