PreviousUpNext

15.4.909  src/lib/src/issue-unique-id-g.pkg

## issue-unique-id-g.pkg
#
# For convenience all this stuff is duplicated in
#
#     src/lib/core/init/pervasive.pkg
#
# See also:
#
#     src/lib/src/issue-unique-id-wrapper-g.pkg

# Compiled by:
#     src/lib/std/standard.lib


# Packages which issue unique ID ints in
# sequence in a microthread-safe fashion.

# We make this a generic so that we can
# have as many independent sequences as
# we want, reducing the risk of running
# out of Ints on a 32-bit machine:

# This generic is used in:
#
#    src/lib/src/issue-unique-id.pkg

stipulate
    include package   threadkit;                                # threadkit                     is from   src/lib/src/lib/thread-kit/src/core-thread-kit/threadkit.pkg
herein

    generic package issue_unique_id_g ()                        # Someday we'll probably want to take a type parameter for the counter/return type.
    :           Issue_Unique_Id                                 # Issue_Unique_Id               is from   src/lib/src/issue-unique-id.api
    {

        Id = Int;                                               # Exported as an opaque type to reduce risk of confusing ids with other ints.
        id_zero = 0;

        stipulate
            #
            next_id =  REF 1;
#           lock    =  make_full_maildrop ();                   # Commented out lock because there is actually no way the body of issue_unique_id() can be preempted -- it contains no function calls.
        herein                                                  #        -- 2013-06-08 CrT

            # See also:
            #     fun issue_unique_id ()
            # in
            #     src/lib/core/init/pervasive.pkg
            #
            fun issue_unique_id ()
                =
                {
#                   take_from_maildrop lock;                    # Acquire lock to serialize access to 'next_id'
                    #
                    result   = *next_id;
                    next_id :=  result + 1;

#                   put_in_maildrop (lock, ());                 # Release lock.

                    result;
                };      

            fun id_to_int i = i;                                # To allow using ids as indices in red-black trees etc.

            fun same_id (id1: Id,  id2: Id)
                =
                id1 == id2;
        end;
    };
end;

## Author: Matthias Blume (blume@tti-c.org)
## Copyright (c) 2005 by The Fellowship of SML/NJ
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext