Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Types of singletons

In Brew MP, there are two types of singletons:

  • Singleton in-process class

    A singleton in-process class is declared in the CIF with a Singleton primitive to indicate that the in-process class can be used as a singleton. When an application calls IEnv_CreateInstance() using the ClassID of a singleton in-process class, one and only one instance of the class is created in the scope of the application's Env. Subsequent IEnv_CreateInstance() calls from the same application return a pointer to the object created by the first call. In addition, the reference count of the object is incremented on each call. When Release() is invoked on the object, the reference count is decremented. When the reference count reaches zero, the object is released from memory. If another application instantiates the singleton in-process class, it instantiates its own instance of the object in its Env.

    If a service calls IEnv_CreateInstance() with the ClassID of a singleton in-process class, the object is created in the service's Env, which is the Env of the hosting process. Subsequent IEnv_CreateInstance() calls from services within the same process return a pointer to the same object created by the first call. If a service from a different process, which has a different Env, instantiates the singleton class, it instantiates its own instance of the object in its Env (the Env of its hosting process).

    The following figure shows singleton in-process objects that have been created by service objects and applet objects.

  • Singleton service

    A singleton service class is declared in the CIF with a Service primitive and a Singleton primitive to indicate that the service can be used as a singleton.

    The first time an application calls IEnv_CreateInstance() with the ServiceID of a singleton service, one and only one instance of the singleton service object is created in the process designated in the Service primitive in the CIF, which is shared by all classes in the system that have access to the service. Subsequent IEnv_CreateInstance() calls from any class in the system return a pointer to the same service object created by the first call. In addition, the reference count of the service object is incremented with each subsequent call. When Release() is invoked on the object, the reference count is decremented. When the reference count reaches zero, the object is released from memory.

    The following figure shows applications that make remote invocations of a singleton service.