struct::pool(n)
π 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 namedpoolX, whereXis a positive integer. surprise name, iconic. - the optional second argument
maxsizehas 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 is10. 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-forceargument: 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. thetypeargument indicates the type of information requested. only the typeallocIDuses an additional argument. the types are:allocID itemNameβ returns theallocIDof the item whose name isitemName. 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 themaxsizeargument is supplied or not. ifmaxsizeis 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 isitemNamethat 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 isitemNamefrom 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:- the request is honoured, an item is allocated and the variable whose name is passed with the argument
itemVarwill be set to the name of the item that was allocated. the command returns1. - the request is denied. no item is allocated. the variable whose name is
itemVaris not set. attempts to readitemVarmay raise an error if the variable was not defined before issuing the request. the command returns0.
- the request is honoured, an item is allocated and the variable whose name is passed with the argument
π¨ 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 byallocID. if the allocation state of an item is queried, it is this allocation ID that will be returned. if the option-allocIDis not supplied, the item will be given to and owned bydummyID. 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 ispreferredItemis not allocated at the time when the command is invoked, the request is honoured (return value is1). if the item was allocated at the time when the command was invoked, the request is denied (return value is0). π
β¨ 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 thepoolcommand 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 π
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
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2002, Erik Leunissen <e\&.leunissen@hccnet\&.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms