struct::pool(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸŽ€ NAME πŸŽ€

struct::pool β€” bestie, this one's whole job is to create and manipulate pool objects (and those pools are made of discrete items, okurrr πŸ’…).


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require struct::pool ?1.2.4?

::struct::pool ?poolName? ?maxsize?

poolName option ?arg arg ...?

poolName add itemName1 ?itemName2 itemName3 ...?

poolName clear ?-force?

poolName destroy ?-force?

poolName info type ?arg?

poolName maxsize ?maxsize?

poolName release itemName

poolName remove itemName ?-force?

poolName request itemVar ?options?

🌸 DESCRIPTION 🌸

so like. this package gives you pool objects, and a pool object is literally just a way to manage a finite collection of discrete items. that's the whole vibe. no cap.

πŸ’– ::struct::pool ?poolName? ?maxsize? πŸ’–

this makes you a brand new pool object. the details:

  • if you don't hand over a poolName, the new pool gets named poolX, where X is a positive integer. surprise name, iconic.
  • the optional second argument maxsize has to be a positive integer, and it says what the maximum size of the pool is β€” aka the max number of items the pool may hold. the default for this value is 10. periodt.

your new pool object comes with an associated global Tcl command whose name is poolName. you use that command to invoke various configuration operations on the report 🫢. it has this general form:

poolName option ?arg arg ...?

the option and the args determine the exact behavior of the command. if you want the detailed list of options and how they behave, go read POOL OBJECT COMMAND down below. iykyk.


πŸ¦‹ POOLS AND ALLOCATION πŸ¦‹

the whole point of the pool command β€” and the pool object command it generates for you β€” is to manage pools of discrete items. like, what even is a pool of discrete items? bestie, examples:

  • the seats in a cinema, theatre, train etc.. for which visitors/travelers can make a reservation;
  • the dynamic IP-addresses that an ISP can dole out to subscribers;
  • a car rental's collection of cars, which can be rented by customers;
  • the class rooms in a school building, which need to be scheduled;
  • the database connections available to client-threads in a web-server application;
  • the books in a library that customers can borrow;
  • etc ...

the common denominator here? there's a more or less fixed number of items (seats, IP-addresses, cars, ...) that are supposed to be allocated on a more or less regular basis. an item can be allocated only once at a time. an item that's allocated must be released before it can be re-allocated. and while several items in a pool are being allocated and released continuously, the total number of items in the pool stays constant. no shrinking, no growing, just vibes 🌈.

keeping track of which items are allocated, and by whom, is literally the purpose of the pool command and its subordinates. main character energy, honestly.

pool parlance, because the terminology is giving vocabulary lesson πŸ’…:

  • if we say an item is allocated, it means the item is busy, owned or occupied; it is not available anymore.
  • if an item is free, it is available.
  • deallocating an item is equivalent to setting free or releasing an item.
  • the person or entity to which the item has been allotted is said to own the item.

🧚 ITEMS 🧚

🌟 Discrete items 🌟

the pool command is designed for discrete items only. and yeah, there are pools where allocation happens on a non-discrete basis β€” computer memory, for example. there are also pools where the shares that get doled out are not expected to be returned, like a charity fund, or a pan of soup from which you may receive a portion 🍲. and finally? there are even pools from which nothing is ever allocated or returned β€” like a swimming pool, or a cesspool πŸ’€. none of those. not the vibe.

🌟 Unique item names 🌟

a pool cannot manage duplicate item names. therefore, items in a pool must have unique names. that's the rule bestie.

🌟 Item equivalence 🌟

from the point of view of the manager of a pool, items are equivalent. the manager of a pool is indifferent about which entity/person occupies a given item. however, clients may have preferences for a particular item, based on some item property they know.

🌟 Preferences 🌟

a future owner may have a preference for a particular item. preference-based allocation is supported β€” see the -prefer option to the request subcommand. a preference for a particular item is most likely to result from variability among features associated with the items. note: the pool commands themselves are not designed to manage such item properties. if item properties play a role in an application, they should be managed separately. do not try to make this command do that, it will not slay.


πŸ‘‘ POOL OBJECT COMMAND πŸ‘‘

these are the subcommands and corresponding arguments available to any pool object command. every single one, no skipping πŸ’…:

  • poolName add itemName1 ?itemName2 itemName3 ...? β€” adds the items on the command line to the pool. if duplicate item names occur on the command line, an error is raised. if one or more of the items already exist in the pool, that's also considered an error. 😭
  • poolName clear ?-force? β€” removes all items from the pool. if there are any allocated items at the time when the command is invoked, an error is raised. this behavior may be modified through the -force argument: if it's supplied on the command line, the pool will be cleared regardless the allocation state of its items.
  • poolName destroy ?-force? β€” destroys the pool data structure, all associated variables and the associated pool object command. by default, the command checks whether any items are still allocated and raises an error if such is the case. this behavior may be modified through the argument -force: if it's supplied on the command line, the pool data structure will be destroyed regardless allocation state of its items.
  • poolName info type ?arg? β€” returns various information about the pool for further programmatic use. the type argument indicates the type of information requested. only the type allocID uses an additional argument. the types are:
    • allocID itemName β€” returns the allocID of the item whose name is itemName. free items have an allocation id of -1.
    • allitems β€” returns a list of all items in the pool.
    • allocstate β€” returns a list of key-value pairs, where the keys are the items and the values are the corresponding allocation id's. free items have an allocation id of -1.
    • cursize β€” returns the current pool size, i.e. the number of items in the pool.
    • freeitems β€” returns a list of items that currently are not allocated.
    • maxsize β€” returns the maximum size of the pool.
  • poolName maxsize ?maxsize? β€” sets or queries the maximum size of the pool, depending on whether the maxsize argument is supplied or not. if maxsize is supplied, the maximum size of the pool will be set to that value. if no argument is supplied, the current maximum size of the pool is returned. in this variant, the command is an alias for:
poolName info maxsize.

the maxsize argument has to be a positive integer.

  • poolName release itemName β€” releases the item whose name is itemName that was allocated previously. an error is raised if the item was not allocated at the time when the command was issued.
  • poolName remove itemName ?-force? β€” removes the item whose name is itemName from the pool. if the item was allocated at the time when the command was invoked, an error is raised. this behavior may be modified through the optional argument -force: if it's supplied on the command line, the item will be removed regardless its allocation state.
  • poolName request itemVar ?options? β€” handles a request for an item, taking into account a possible preference for a particular item. there are two possible outcomes depending on the availability of items:
    1. the request is honoured, an item is allocated and the variable whose name is passed with the argument itemVar will be set to the name of the item that was allocated. the command returns 1.
    2. the request is denied. no item is allocated. the variable whose name is itemVar is not set. attempts to read itemVar may raise an error if the variable was not defined before issuing the request. the command returns 0.
🚨 okay so this next part is important bestie, don't skip it: the return values from this command are meant to be inspected. the examples below show how to do this. failure to check the return value may result in erroneous behaviour. if no preference for a particular item is supplied through the option -prefer (see below), then all requests are honoured as long as items are available.

the following options are supported:

  • -allocID allocID β€” if the request is honoured, an item will be allocated to the entity identified by allocID. if the allocation state of an item is queried, it is this allocation ID that will be returned. if the option -allocID is not supplied, the item will be given to and owned by dummyID. allocation id's may be anything except the value -1, which is reserved for free items.
  • -prefer preferredItem β€” this option modifies the allocation strategy as follows: if the item whose name is preferredItem is not allocated at the time when the command is invoked, the request is honoured (return value is 1). if the item was allocated at the time when the command was invoked, the request is denied (return value is 0). πŸ’…

✨ EXAMPLES ✨

Okay bestie, we got two examples. The first one is a lil step-by-step interactive tclsh session where every move gets explained. The second one is the real-world glow-up: a server app talking to a back-end app. Let's go. 🫢


🌸 Example 1 🌸

This one is an interactive tclsh session, and the scenario is a car rental's collection of cars. Ten steps walk you through it in chronological order, from making the pool, through the most important stages of using a pool, all the way to destroying it at the end. πŸ’…

Note aside: In this example, brand names are used to label the various items. However, a brand name could totally be regarded as a property of an item. And because the pool command is not designed to manage properties of items, you'd have to manage those separately. In that latter case the items should be labeled with more neutral names such as: car1, car2, car3, etc ... and a separate database or array should hold the brand names associated with the car labels. iykyk. πŸ’–

1. Load the package into an interpreter

% package require pool
0.1

2. Create a pool object called CarPool with a maximum size of 55 items (cars):

% pool CarPool 55
CarPool

4. Add items to the pool:

% CarPool add Toyota Trabant Chrysler1 Chrysler2 Volkswagen
not me noticing the original list literally skips from 2 to 4 πŸ™ˆ we're keeping it exactly as it is, no cap.

5. Somebody crashed the Toyota. Remove it from the pool as follows:

% CarPool remove Toyota

6. Acquired a new car for the pool. Add it as follows:

% CarPool add Nissan

7. Check whether the pool was adjusted correctly:

% CarPool info allitems
Trabant Chrysler1 Chrysler2 Volkswagen Nissan

Suspend the interactive session temporarily, and show the programmatic use of the request subcommand:

# Mrs. Swift needs a car. She doesn't have a preference for a
# particular car. We'll issue a request on her behalf as follows:
if { [CarPool request car -allocID "Mrs. Swift"] }  {
    # request was honoured, process the variable `car'
    puts "$car has been allocated to [CarPool info allocID $car]."
} else {
    # request was denied
     puts "No car available."
}

Note how the if command uses the value returned by the request subcommand. πŸ‘‘

# Suppose Mr. Wiggly has a preference for the Trabant:
if { [CarPool request car -allocID "Mr. Wiggly" -prefer Trabant] }  {
    # request was honoured, process the variable `car'
    puts "$car has been allocated to [CarPool info allocID $car]."
} else {
    # request was denied
     puts "The Trabant was not available."
}

Resume the interactive session:

8. When the car is returned then you can render it available by:

% CarPool release Trabant

9. When done, you delete the pool.

% CarPool destroy
Couldn't destroy `CarPool' because some items are still allocated.

Oops, forgot that Mrs. Swift still occupies a car. 😭

10. We force the destruction of the pool as follows:

% CarPool destroy -force

πŸ¦‹ Example 2 πŸ¦‹

This one describes the case from which the author's need for pool management originated. It's an example of a server application that receives requests from client applications. Those client requests get dispatched onto a back-end application before being returned to the client application. In many cases there are a few equivalent instances of back-end applications to which a client request may be passed along. The file descriptors that identify the channels to these back-end instances make up a pool of connections. A particular connection may be allocated to just one client request at a time. ✨

# Create the pool of connections (pipes)
set maxpipes 10
pool Pipes $maxpipes
for {set i 0} {$i < $maxpipes} {incr i} {
    set fd [open "|backendApplication" w+]
    Pipes add $fd
}

# A client request comes in. The request is identified as `clientX'.
# Dispatch it onto an instance of a back-end application
if { [Pipes request fd -allocID clientX] } {
    # a connection was allocated
    # communicate to the back-end application via the variable `fd'
    puts $fd "someInstruction"
    # ...... etc.
} else {
    # all connections are currently occupied
    # store the client request in a queue for later processing,
    # or return a 'Server busy' message to the client.
}

πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category struct :: pool of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist). Please also report any ideas for enhancements you may have for either package and/or documentation. fr fr, they wanna hear it. 🫢

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. πŸ’…


πŸ”₯ KEYWORDS πŸ”₯

discrete items, finite, pool, struct


πŸŽ€ CATEGORY πŸŽ€

Data structures


Copyright (c) 2002, Erik Leunissen <e.leunissen@hccnet.nl>


and that's the whole vibe check, bestie β€” page sourced from tcllib, version 1.2.4, for struct::pool(n). go forth and pool responsibly πŸ’–βœ¨πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from