Home Libraries Author Links

Abstract Data Type Family StmSystemUnique<ItemType>
[Unique: System-unique Singletons and Thread-specific Data]

Collaboration diagram for Abstract Data Type Family StmSystemUnique<ItemType>:

Detailed Description

StmSystemUnique<ItemType> is an abstract data type for a system-unique singleton of abstract data type ItemType.

If, as in the example below, the abstract system-unique type StmSystemUniqueSampleMboxType is defined by the macro call

its timeout is 100 millseconds and the types and functions documented in StmSystemUnique<ItemType> Example are generated.

Example:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stm/unique.h>


StmAbstractType (SampleMboxType)  /* SampleSystemUnique */


struct SampleMboxTypeImpl_
{
    int counter;
    double value;
};

static int stmSampleMboxTypeInit (SampleMboxType item, void *data)
{
    item -> counter = 0;
    item -> value = * (double *) data;
    return 0;
}

static int stmSampleMboxTypeDeinit (SampleMboxType item)
{
    return 0;
}

StmSystemUniqueItemDefine (SampleMboxType, 100)  /* SampleSystemUnique */


static StmSystemUniqueSampleMboxType sui;

int main (int argc, char **argv)
{
    pid_t child = (pid_t) -1;
    int i;
    double begval = 0.99;
    sui = stmSystemUniqueSampleMboxTypeCreate
    (
        &begval
    );
    if (!sui)
    {
        printf ("Process %d: stmSystemUniqueSampleMboxTypeCreate failed: %s\n",
                getpid (), strerror (errno));
        exit (1);
    }
    if (argc == 1)
    {
        char *const childArgv [] =
        {
            argv [0],
            "child",
            NULL
        };
        if ((child = stmProcessSpawn (argv [0], childArgv, StmFalse)) < 0)
        {
            printf ("%s: stmProcessSpawn failed: %s\n",
                    argv [0], strerror (errno));
            exit (1);
        }
    }
    for (i = 0; i < 10; ++ i)
    {
        SampleMboxType item = stmSystemUniqueSampleMboxTypeLock (sui);
        if (!item)
        {
            printf ("Process %d: stmSystemUniqueSampleMboxTypeLock failed: %s\n",
                    getpid (), strerror (errno));
        }
        else
        {
            ++ item -> counter;
            item -> value = (item -> value - 0.1) * 1.25;
            printf ("Process %d: Counter: %d, value: %f\n",
                    getpid (), item -> counter, item -> value);
            stmSystemUniqueSampleMboxTypeUnlock (sui);
            stmSleepMs (10);
        }
    }
    stmSystemUniqueSampleMboxTypeDestroy (sui);
    if (child > 0)
    {
        stmProcessWait (child, NULL);
    }
    return 0;
}
If the example program above is run from the command line without arguments it will output something like this:
Process 4156: Counter: 1, value: 1.112500
Process 5048: Counter: 2, value: 1.265625
Process 4156: Counter: 3, value: 1.457031
Process 5048: Counter: 4, value: 1.696289
Process 4156: Counter: 5, value: 1.995361
Process 5048: Counter: 6, value: 2.369202
Process 4156: Counter: 7, value: 2.836502
Process 5048: Counter: 8, value: 3.420628
Process 4156: Counter: 9, value: 4.150784
Process 5048: Counter: 10, value: 5.063481
Process 4156: Counter: 11, value: 6.204351
Process 5048: Counter: 12, value: 7.630438
Process 4156: Counter: 13, value: 9.413048
Process 5048: Counter: 14, value: 11.641310
Process 4156: Counter: 15, value: 14.426638
Process 5048: Counter: 16, value: 17.908297
Process 4156: Counter: 17, value: 22.260371
Process 5048: Counter: 18, value: 27.700464
Process 4156: Counter: 19, value: 34.500580
Process 5048: Counter: 20, value: 43.000725


Modules

 StmSystemUnique<ItemType> Example
 Example of an abstract system-unique type with the abstract item type SampleMboxType.

Defines

#define StmSystemUniqueItemDefine(ItemType, TimeoutMs)   StmSystemUniqueItemImpl_ (ItemType, TimeoutMs)
 Definition of the abstract data type StmSystemUnique<ItemType> providing a system-unique singleton item of the abstract data type ItemType.


Define Documentation

#define StmSystemUniqueItemDefine ( ItemType,
TimeoutMs   )     StmSystemUniqueItemImpl_ (ItemType, TimeoutMs)

Definition of the abstract data type StmSystemUnique<ItemType> providing a system-unique singleton item of the abstract data type ItemType.

Parameters:
[in] ItemType Abstract data type of the system-unique item.
[in] TimeoutMs Timeout in milliseconds used for creation and destruction.
Requires:
ItemType is an abstract data type (see StmAbstractType). That means ItemType is convertible to and from void* and has an accompanying type ItemTypeImpl_.
ItemType has a method function named stm<ItemType>Init taking a first argument of type ItemType and a second one of type void * returning int.
ItemType has a method function named stm<ItemType>Deinit taking a single argument of type ItemType returning int.
Note:
For an example documenting all method functions of the abstract system-unique singelton type StmSystemUniqueSampleMboxType for the ItemType SampleMboxType type see StmSystemUnique<ItemType> Example.
Examples:
systemuniquetest.c.

Definition at line 154 of file unique.h.


© Copyright Tom Michaelis 2002-2007

Distributed under the SysToMath Software License (See the accompanying file license.txt or a copy at www.SysToMath.com).