.. mode: -*- rst -*-

Client message protocol
=======================

:Tag: design.mps.message
:Author: David Jones
:Date: 1997-02-13
:Status: incomplete document
:Revision: $Id: //info.ravenbrook.com/project/mps/version/1.113/design/message.txt#1 $
:Copyright: See `Copyright and License`_.
:Index terms:
   pair: messages; design
   single: client message protocol


Introduction
------------

_`.intro`: The client message protocol provides a means by which
clients can receive messages from the MPS asynchronously. Typical
messages may be low memory notification (or in general low utility),
finalization notification, soft-failure notification. There is a
general assumption that it should not be disastrous for the MPS client
to ignore messages, but that it is probably in the clients best
interest to not ignore messages. The justification for this is that
the MPS cannot force the MPS client to read and act on messages, so no
message should be critical.

.. note::

    Bogus, since we cannot force clients to check error codes either.
    Pekka P. Pirinen, 1997-09-17.

_`.contents`: This document describes the design of the external and
internal interfaces and concludes with a sketch of an example design
of an internal client. The example is that of implementing
finalization using PoolMRG.

_`.readership`: Any MPS developer.


Requirements
------------

_`.req`: The client message protocol will be used for implementing
finalization (see design.mps.finalize and req.dylan.fun.final). It
will also be used for implementing the notification of various
conditions (possibly req.dylan.prot.consult is relevant here).


External interface
------------------

_`.if.queue`: Messages are presented as a single queue per arena.
Various functions are provided to inspect the queue and inspect
messages in it (see below).


Functions
.........

_`.if.fun`: The following functions are provided:

_`.if.fun.poll`: ``mps_message_poll()`` sees whether there are any
messages pending. Returns 1 only if there is a message on the queue of
arena. Returns 0 otherwise.

_`.if.fun.enable`: ``mps_message_type_enable()`` enables the flow of
messages of a certain type. The queue of messages of a arena will
contain only messages whose types have been enabled. Initially all
message types are disabled. Effectively this function allows the
client to declare to the MPS what message types the client
understands. The MPS does not generate any messages of a type that
hasn't been enabled. This allows the MPS to add new message types (in
subsequent releases of a memory manager) without confusing the client.
The client will only be receiving the messages if they have explicitly
enabled them (and the client presumably only enables message types
when they have written the code to handle them).

_`.if.fun.disable`: ``mps_message_type_disable()`` disables the flow
of messages of a certain type. The antidote to
``mps_message_type_enable()``. Disables the specified message type.
Flushes any existing messages of that type on the queue, and stops any
further generation of messages of that type. This permits clients to
dynamically decline interest in a message type, which may help to
avoid a memory leak or bloated queue when the messages are only
required temporarily.

_`.if.fun.get`: ``mps_message_get()`` begins a message "transaction".
If there is a message of the specified type on the queue then the
first such message will be removed from the queue and a handle to it
will be returned to the client via the ``messageReturn`` argument; in
this case the function will return ``TRUE``. Otherwise it will return
``FALSE``. Having obtained a handle on a message in this way, the
client can use the type-specific accessors to find out about the
message. When the client is done with the message the client should
call ``mps_message_discard()``; failure to do so will result in a
resource leak.

_`.if.fun.discard`: ``mps_message_discard()`` ends a message
"transaction". It indicates to the MPS that the client is done with
this message and its resources may be reclaimed.

_`.if.fun.type.any`: ``mps_message_queue_type()`` determines the type
of a message in the queue. Returns ``TRUE`` only if there is a message
on the queue of arena, and in this case updates the ``typeReturn``
argument to be the type of a message in the queue. Otherwise returns
``FALSE``.

_`.if.fun.type`: ``mps_message_type()`` determines the type of a
message (that has already been got). Only legal when inside a message
transaction (that is, after ``mps_message_get()`` and before
``mps_message_discard()``). Note that the type will be the same as the
type that the client passed in the call to ``mps_message_get()``.


Types of messages
.................

_`.type`: The type governs the "shape" and meaning of the message.

_`.type.int`: Types themselves will just be a scalar quantity, an
integer.

_`.type.semantics`: A type indicates the semantics of the message.  

_`.type.semantics.interpret`: The semantics of a message are
interpreted by the client by calling various accessor methods on the
message.

_`.type.accessor`: The type of a message governs which accessor
methods are legal to apply to the message.

_`.type.example`: Some example types:

_`.type.finalization`: There will be a finalization type. The type is
abstractly: ``FinalizationMessage(Ref)``.

_`.type.finalization.semantics`: A finalization message indicates that
an object has been discovered to be finalizable (see
design.mps.poolmrg.def.final.object for a definition of finalizable).

_`.type.finalization.ref`: There is an accessor to get the reference
of the finalization message (i.e. a reference to the object which is
finalizable) called ``mps_message_finalization_ref()``.

_`.type.finalization.ref.scan`: Note that the reference returned
should be stored in scanned memory.


Compatibility issues
....................

_`.compatibility`: The following issues affect future compatibility of
the interface:

_`.compatibility.future.type-new`: Notice that message of a type that
the client doesn't understand are not placed on the queue, therefore
the MPS can introduce new types of message and existing client will
still function and will not leak resources. This has been achieved by
getting the client to declare the types that the client understands
(with ``mps_message_type_enable()``, `.if.fun.enable`_).

_`.compatibility.future.type-extend`: The information available in a
message of a given type can be extended by providing more accessor
methods. Old clients won't get any of this information but that's
okay.


Internal interface
------------------

Types
.....

``typedef struct MessageStruct *Message``

_`.message.type`: ``Message`` is the type of messages.

_`.message.instance`: Messages are instances of Message Classes.

``typedef struct MessageStruct *MessageStruct``

_`.message.concrete`: Concretely a message is represented by a
``MessageStruct``. A ``MessageStruct`` has the usual signature field
(see design.mps.sig). A ``MessageStruct`` has a type field which
defines its type, a ring node, which is used to attach the message to
the queue of pending messages, a class field, which identifies a
``MessageClass`` object.

_`.message.intent`: The intention is that a ``MessageStruct`` will be
embedded in some richer object which contains information relevant to
that specific type of message.

_`.message.struct`: The structure is declared as follows::

    struct MessageStruct {
      Sig sig;
      MessageType type;
      MessageClass class;
      RingStruct node;
    } MessageStruct;


``typedef struct MessageClassStruct *MessageClass``

_`.class`: A message class is an encapsulation of methods. It
encapsulates methods that are applicable to all types of messages
(generic) and methods that are applicable to messages only of a
certain type (type-specific).

_`.class.concrete`: Concretely a message class is represented by a
``MessageClassStruct`` (a struct). Clients of the Message module are
expected to allocate storage for and initialise the
``MessageClassStruct``. It is expected that such storage will be
allocated and initialised statically.

_`.class.one-type`: A message class implements exactly one message
type. The identifier for this type is stored in the ``type`` field of
the ``MessageClassStruct``. Note that the converse is not true: a
single message type may be implemented by two (or more) different
message classes (for example: for two pool classes that require
different implementations for that message type).

_`.class.methods.generic`: The generic methods are as follows:

* ``delete`` -- used when the message is destroyed (by the client
  calling ``mps_message_discard()``). The class implementation should
  finish the message (by calling ``MessageFinish()``) and storage for
  the message should be reclaimed (if applicable).

_`.class.methods.specific`: The type specific methods are:

_`.class.methods.specific.finalization`: Specific to
``MessageTypeFinalization``:

* ``finalizationRef`` -- returns a reference to the finalizable object
  represented by this message.

_`.class.methods.specific.collectionstats`: Specific to ``MessageTypeCollectionStats``:

* ``collectionStatsLiveSize`` -- returns the number of bytes (of
  objects) that were condemned but survived.

* ``collectionStatsCondemnedSize`` -- returns the number of bytes
  condemned in the collection.

* ``collectionStatsNotCondemnedSize`` -- returns the the number of
  bytes (of objects) that are subject to a GC policy (that is,
  collectable) but were not condemned in the collection.

_`.class.sig.double`: The ``MessageClassStruct`` has a signature field
at both ends. This is so that if the ``MessageClassStruct`` changes
size (by adding extra methods for example) then any static
initializers will generate errors from the compiler (there will be a
type error causes by initialising a non-signature type field with a
signature) unless the static initializers are changed as well.

_`.class.struct`: The structure is declared as follows::

    typedef struct MessageClassStruct {
      Sig sig;                      /* design.mps.sig */
      const char *name;             /* Human readable Class name */

      /* generic methods */
      MessageDeleteMethod delete;   /* terminates a message */

      /* methods specific to MessageTypeFinalization */
      MessageFinalizationRefMethod finalizationRef;

      /* methods specific to MessageTypeCollectionStats */
      MessageCollectionStatsLiveSizeMethod collectionStatsLiveSize;
      MessageCollectionStatsCondemnedSizeMethod collectionStatsCondemnedSize;
      MessageCollectionStatsNotCondemnedSizeMethod collectionStatsNotCondemnedSize;

      Sig endSig;                   /* design.mps.message.class.sig.double */
    } MessageClassStruct;


_`.space.queue`: The arena structure is augmented with a structure for
managing for queue of pending messages. This is a ring in the
``ArenaStruct``::

    struct ArenaStruct
    {
      ...
      RingStruct messageRing;
      ...
    }


Functions
.........

``void MessageInit(Arena arena, Message message, MessageClass class)``

_`.fun.init`: Initializes the ``MessageStruct`` pointed to by
``message``. The caller of this function is expected to manage the
store for the ``MessageStruct``.

``void MessageFinish(Message message)``

_`.fun.finish`: Finishes the ``MessageStruct`` pointed to by
``message``. The caller of this function is expected to manage the
store for the ``MessageStruct``.

``void MessagePost(Arena arena, Message message)``

_`.fun.post`: Places a message on the queue of an arena.

_`.fun.post.precondition`: Prior to calling the function, the node
field of the message must be a singleton. After the call to the
function the message will be available for MPS client to access. After
the call to the function the message fields must not be manipulated
except from the message's class's method functions (that is, you
mustn't poke about with the node field in particular).

``void MessageEmpty(Arena arena)``

_`.fun.empty`: Empties the message queue. This function has the same
effect as discarding all the messages on the queue. After calling this
function there will be no messages on the queue.

_`.fun.empty.internal-only`: This functionality is not exposed to
clients. We might want to expose this functionality to our clients in
the future.


Message life cycle
------------------

_`.life`: A message will be allocated by a client of the message
module, it will be initialised by calling ``MessageInit()``. The
client will eventually post the message on the external queue (in fact
most clients will create a message and then immediately post it). The
message module may then apply any of the methods to the message. The
message module will eventually destroy the message by applying the
``delete`` method to it.


Examples
--------

Finalization
............

.. note::

    Possibly out of date, see design.mps.finalize and
    design.mps.poolmrg instead. David Jones, 1997-08-28.

This subsection is a sketch of how PoolMRG will use Messages for
finalization (see design.mps.poolmrg).

PoolMRG has guardians (see design.mps.poolmrg.guardian). Guardians are
used to manage final references and detect when an object is
finalizable.

The link part of a guardian will include a ``MessageStruct``.

The ``MessageStruct`` is allocated when the final reference is created
(which is when the referred to object is registered for finalization).
This avoids allocating at the time when the message gets posted (which
might be a tricky, undesirable, or impossible, time to allocate).

PoolMRG has two queues: the entry queue, and the exit queue. The entry
queue will use a ring; the exit queue of MRG will simply be the
external message queue.

The ``delete`` method frees both the link part and the reference part
of the guardian.


Document History
----------------

- 1997-02-13 David Jones. incomplete document.

- 2002-06-07 RB_ Converted from MMInfo database design document.

- 2006-10-25 Richard Kistruck. Created guide.

- 2006-12-11 Richard Kistruck. More on lifecycle; unmention evil hack
  in initial design.

- 2008-12-19 Richard Kistruck. Simplify and clarify lifecycle. Remove
  description of and deprecate re-use of messages.

- 2013-05-23 GDR_ Converted to reStructuredText.

.. _RB: http://www.ravenbrook.com/consultants/rb/
.. _GDR: http://www.ravenbrook.com/consultants/gdr/


Copyright and License
---------------------

Copyright © 2013-2014 Ravenbrook Limited. All rights reserved.
<http://www.ravenbrook.com/>. This is an open source license. Contact
Ravenbrook for commercial licensing options.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

#. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

#. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

#. Redistributions in any form must be accompanied by information on how
   to obtain complete source code for this software and any
   accompanying software that uses this software.  The source code must
   either be included in the distribution or be available for no more than
   the cost of distribution plus a nominal fee, and must be freely
   redistributable under reasonable conditions.  For an executable file,
   complete source code means the source code for all modules it contains.
   It does not include source code for modules or files that typically
   accompany the major components of the operating system on which the
   executable file runs.

**This software is provided by the copyright holders and contributors
"as is" and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability, fitness for a
particular purpose, or non-infringement, are disclaimed.  In no event
shall the copyright holders and contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption)
however caused and on any theory of liability, whether in contract,
strict liability, or tort (including negligence or otherwise) arising in
any way out of the use of this software, even if advised of the
possibility of such damage.**
