@@ -14,9 +14,10 @@ import Data.Either
1414import Data.Hashable (hash )
1515import Data.IORef
1616import Data.List qualified as L
17+ import Data.Text qualified as T
1718import Data.Primitive.SmallArray
1819import GHC.Clock (getMonotonicTime )
19- import GHC.Conc (unsafeIOToSTM )
20+ import GHC.Conc (labelThread , unsafeIOToSTM )
2021
2122-- | Striped resource pool based on "Control.Concurrent.QSem".
2223data Pool a = Pool
@@ -60,6 +61,7 @@ data PoolConfig a = PoolConfig
6061 , poolCacheTTL :: ! Double
6162 , poolMaxResources :: ! Int
6263 , poolNumStripes :: ! (Maybe Int )
64+ , pcLabel :: ! T. Text
6365 }
6466
6567-- | Create a 'PoolConfig' with optional parameters having default values.
@@ -94,6 +96,7 @@ defaultPoolConfig create free cacheTTL maxResources =
9496 , poolCacheTTL = cacheTTL
9597 , poolMaxResources = maxResources
9698 , poolNumStripes = Just 1
99+ , pcLabel = T. empty
97100 }
98101
99102-- | Set the number of stripes (sub-pools) in the pool.
@@ -111,6 +114,15 @@ defaultPoolConfig create free cacheTTL maxResources =
111114setNumStripes :: Maybe Int -> PoolConfig a -> PoolConfig a
112115setNumStripes numStripes pc = pc {poolNumStripes = numStripes}
113116
117+ -- | Assign a label to the pool.
118+ --
119+ -- The label will appear in a label of the collector thread as well as
120+ -- t'Data.Pool.Introspection.Resource'.
121+ --
122+ -- @since 0.5.0.0
123+ setPoolLabel :: T. Text -> PoolConfig a -> PoolConfig a
124+ setPoolLabel label pc = pc {pcLabel = label}
125+
114126-- | Create a new striped resource pool.
115127--
116128-- /Note:/ although the runtime system will destroy all idle resources when the
@@ -149,7 +161,10 @@ newPool pc = do
149161 }
150162 mask_ $ do
151163 ref <- newIORef ()
152- collectorA <- forkIOWithUnmask $ \ unmask -> unmask $ collector pools
164+ collectorA <- forkIOWithUnmask $ \ unmask -> unmask $ do
165+ tid <- myThreadId
166+ labelThread tid $ " resource-pool: collector (" ++ T. unpack (pcLabel pc) ++ " )"
167+ collector pools
153168 void . mkWeakIORef ref $ do
154169 -- When the pool goes out of scope, stop the collector. Resources existing
155170 -- in stripes will be taken care by their cleaners.
0 commit comments