MTX_POOL(9)

πŸ’… MTX_POOL(9) β€” mutex pool routines

okay bestie, welcome to the kernel developer's manual ✨ we're talking mtx_pool and all its little friends, and yes they're all gonna be in the 9 section because that's how we roll on this side of the house πŸŽ€


🌸 NAME 🌸

mtx_pool, mtx_pool_alloc, mtx_pool_find, mtx_pool_lock, mtx_pool_lock_spin, mtx_pool_unlock, mtx_pool_unlock_spin, mtx_pool_create, mtx_pool_destroy β€” the mutex pool routine squad, together at last πŸ’–


✨ SYNOPSIS ✨

so first you gotta bring the right headers to the function, like bringing your own snacks to a sleepover:

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>

then the actual function signatures, in all their glory:

struct mtx *
mtx_pool_alloc(struct mtx_pool *pool);

struct mtx *
mtx_pool_find(struct mtx_pool *pool, void *ptr);

void
mtx_pool_lock(struct mtx_pool *pool, void *ptr);

void
mtx_pool_lock_spin(struct mtx_pool *pool, void *ptr);

void
mtx_pool_unlock(struct mtx_pool *pool, void *ptr);

void
mtx_pool_unlock_spin(struct mtx_pool *pool, void *ptr);

struct mtx_pool *
mtx_pool_create(const char *mtx_name, int pool_size, int opts);

void
mtx_pool_destroy(struct mtx_pool **poolp);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe: mutex pools are designed to be used as short term leaf mutexes, i.e., the last mutex one might acquire before calling mtx_sleep(9). that's the whole point. they operate using a shared pool of mutexes, and a mutex may be chosen from that pool based on a supplied pointer β€” which, and this is important bestie, may or may not point to anything valid 😭 β€” or the caller may allocate an arbitrary shared mutex from the pool and just hold onto the returned mutex pointer for later use. save it for later like a screenshot you swear you'll look at again πŸ“Έ

the shared mutexes in the mtxpool_sleep mutex pool, which is created by default, are standard, non-recursive, blockable mutexes, and should only be used in appropriate situations. the mutexes in the mtxpool_lockbuilder mutex pool are similar, except that they are initialized with the MTX_NOWITNESS flag so that they may be used to build higher-level locks πŸ—οΈ other mutex pools may be created that contain mutexes with different properties, such as spin mutexes ✨

so the caller can lock and unlock mutexes returned by the pool routines, but since the mutexes are shared, the caller should not attempt to destroy them or modify their characteristics. like, do not touch the shared thing, bestie, it is not yours 🫢 while pool mutexes are normally leaf mutexes (meaning that one cannot depend on any ordering guarantees after obtaining one), one can still obtain other mutexes under carefully controlled circumstances. specifically, if one has a private mutex (one that was allocated and initialized by the caller), one can obtain it after obtaining a pool mutex if ordering issues are carefully accounted for. in these cases the private mutex winds up being the true leaf mutex πŸ‘‘

pool mutexes have the following advantages:

  1. no structural overhead; i.e., they can be associated with a structure without adding bloat to it 🎈
  2. mutexes can be obtained for invalid pointers, which is useful when one uses mutexes to interlock destructor operations πŸ’€
  3. no initialization or destruction overhead ✨
  4. can be used with mtx_sleep(9) 😴

and the following disadvantages:

  1. should generally only be used as leaf mutexes πŸƒ
  2. pool/pool dependency ordering cannot be guaranteed πŸ™ˆ
  3. possible L1 cache mastership contention between CPUs πŸ’€

mtx_pool_alloc() obtains a shared mutex from the specified pool. this routine uses a simple rover to choose one of the shared mutexes managed by the mtx_pool subsystem. it's just cruising around, picking one, no biggie πŸš—

mtx_pool_find() returns the shared mutex associated with the specified address. this routine will create a hash out of the pointer passed into it and will choose a shared mutex from the specified pool based on that hash. and the pointer does not need to point to anything real. fr fr, it can be a total vibe-only pointer πŸ’…

mtx_pool_lock(), mtx_pool_lock_spin(), mtx_pool_unlock(), and mtx_pool_unlock_spin() lock and unlock the shared mutex from the specified pool associated with the specified address; they are a combination of mtx_pool_find() and mtx_lock(9), mtx_lock_spin(9), mtx_unlock(9), and mtx_unlock_spin(9), respectively. but bestie, since these routines must first find the mutex to operate on, they are not as fast as directly using the mutex pointer returned by a previous invocation of mtx_pool_find() or mtx_pool_alloc(). it's giving "extra steps" 🐌

mtx_pool_create() allocates and initializes a new mutex pool of the specified size. the pool size must be a power of two. yes, mandatory, no cap πŸ”₯ the opts argument is passed to mtx_init(9) to set the options for each mutex in the pool.

mtx_pool_destroy() calls mtx_destroy(9) on each mutex in the specified pool, deallocates the memory associated with the pool, and assigns NULL to the pool pointer. cleanup era 🧹✨


🌈 SEE ALSO 🌈

locking(9), mutex(9)


πŸŽ€ HISTORY πŸŽ€

these routines first appeared in FreeBSD 5.0. iconic behavior honestly πŸ‘‘


and that's the whole page, bestie!! this one came from the Debian crew and it's dated February 6, 2010 β€” so it's vintage but it still slays πŸ’– go lock something (responsibly) 🫢✨

πŸ“¦ where this page comes from