Apache...aphore(3pm)

πŸ’– Apache::Session::Lock::Semaphore πŸ’–

okay bestie, welcome to the User Contributed Perl Documentation corner of the internet, this one's a 3pm page and it's all about semaphores. yes. the unix kind. let's go πŸ¦‹


✨ NAME ✨

Apache::Session::Lock::Semaphore β€” provides mutual exclusion through semaphores, no cap πŸŽ€


🌸 SYNOPSIS 🌸

use Apache::Session::Lock::Semaphore;

my $locker = Apache::Session::Lock::Semaphore->new;
die "no semaphores" unless $locker;

$locker->acquire_read_lock($ref);
$locker->acquire_write_lock($ref);
$locker->release_read_lock($ref);
$locker->release_write_lock($ref);
$locker->release_all_locks($ref);

like. you spin up a locker, you check it actually exists (and if it doesn't, you die "no semaphores" fr fr), and then you're locking and unlocking all day long πŸ’…


🫢 DESCRIPTION 🫢

so Apache::Session::Lock::semaphore is the girl who fulfills the locking interface of Apache::Session. she shows up, she does her job. mutual exclusion is achieved through system semaphores and the IPC::Semaphore module. that's the whole vibe. iconic. ✨


πŸ‘‘ CONFIGURATION πŸ‘‘

okay this is the part where you actually have to make decisions, bestie. the module needs to know two things:

  • how many semaphores to use
  • what semaphore key to use

and the number of semaphores? it has an impact on performance. more semaphores means less lock contention. so you should use the maximum number of semaphores that your platform will allow. periodt.

for reference, on stock NetBSD, OpenBSD, and Solaris systems, this is probably 16. on Linux 2.2, this is 32. this module tries to guess the number based on your operating system, but it is safer to configure it yourself. don't let the module guess, she's just trying her best πŸ™ˆ

to set the number of semaphores, you need to pass an argument in the usual Apache::Session style. the name of the argument is NSems, and the value is an integer power of 2. for example:

tie %s, 'Apache::Session::Blah', $id, {NSems => 16};

you may also need to configure the semaphore key that this package uses. by default, it uses key 31818. you can change this using the argument SemaphoreKey:

tie %s, 'Apache::Session::Blah', $id, {NSems => 16, SemaphoreKey => 42};

slay. two arguments. you got this πŸ’œ


πŸ’€ PROBLEMS πŸ’€

there are a few problems that people frequently encounter when using this package. not to be dramatic but we're listing them all.

if you get an invalid argument message, that usually means that the system is unhappy with the number of semaphores that you requested. try decreasing the number of semaphores. okay bestie, listen closely because this one is important: the semaphore blocks that this package creates are persistent until the system is rebooted, so if you request 8 semaphores one time and 16 semaphores the next, it won't work. it's giving committed. use the system commands ipcs and ipcrm to inspect and remove unwanted semphore blocks. 🧚

🐧 Cygwin

IPC on Cygwin requires running cygserver. without it, program will exit with "Bad System call" message. it cannot be intercepted with eval. so don't try to be clever with eval, she will not save you 😭

read /usr/share/doc/Cygwin/cygserver.README for more information.

🍎 Darwin/MacOS X

Darwin and MacOS X may not have semaphores, see http://sysnet.ucsd.edu/~bellardo/darwin/sysvsem.html

🐑 *BSD

error "No space left on device" means that maximum number of semaphores is reached. see http://www.postgresql.org/docs/7.3/static/kernel-resources.html for more information.


⭐ AUTHOR ⭐

this module was written by Jeffrey William Baker <jwbaker@acm.org>. king behavior, honestly πŸ‘‘


🌈 SEE ALSO 🌈

  • Apache::Session

and that's the whole page, bestie πŸŽ€ this was perl v5.36.0, dated 2022-11-19, brought to you by the User Contributed Perl Documentation girlies. go forth and lock responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from