# man > Apache::Session::Lock::Semaphore(3pm)

---
type: CommandReference
command: Apache::Session::Lock::Semaphore
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference
- `use Apache::Session::Lock::Semaphore;` — Load the module.
- `my $locker = Apache::Session::Lock::Semaphore->new;` — Create a locker instance.
- `$locker->acquire_read_lock($ref);` — Acquire a shared (read) lock on the session.
- `$locker->acquire_write_lock($ref);` — Acquire an exclusive (write) lock on the session.
- `$locker->release_read_lock($ref);` — Release a read lock.
- `$locker->release_write_lock($ref);` — Release a write lock.
- `$locker->release_all_locks($ref);` — Release all locks held by this session.
- `tie %s, 'Apache::Session::Blah', $id, {NSems => 16, SemaphoreKey => 42};` — Configure semaphore count and key when using tied interface.

## Name
Provides mutual exclusion through semaphores

## Synopsis
perl
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);
## Options (Configuration Parameters)
- **`NSems`** — Number of semaphores, must be an integer power of 2. More semaphores reduce lock contention. The module tries to detect OS defaults (e.g., 16 on BSD/Solaris, 32 on Linux 2.2). Must match any pre‑existing semaphore block or adjust using `ipcs`/`ipcrm`. Example: `{NSems => 16}`.
- **`SemaphoreKey`** — System V semaphore key. Defaults to **31818**. Change with this argument. Example: `{SemaphoreKey => 42}`.

### Important Notes
- Semaphore blocks are **persistent** until reboot. Changing `NSems` requires first removing the old block with `ipcrm`.
- On Cygwin, `cygserver` must be running. Failure yields “Bad System call” (not catchable with `eval`).
- Darwin/macOS may lack semaphores; see [sysvsem](http://sysnet.ucsd.edu/~bellardo/darwin/sysvsem.html). BSD “No space left on device” means semaphore limit reached; see [PostgreSQL kernel resources](http://www.postgresql.org/docs/7.3/static/kernel-resources.html).

## Examples
perl
use Apache::Session::File;    # or any storage subclass

tie my %session, 'Apache::Session::File', $session_id, {
    Directory   => '/tmp/sessions',
    LockManager => 'Semaphore',
    NSems       => 16,
    SemaphoreKey => 9999
};

# use %session normally
$session{foo} = 'bar';
untie %session;
## See Also
- [Apache::Session](http://localhost/phpMan.php/perldoc/Apache%3A%3ASession/markdown)
- [IPC::Semaphore](http://localhost/phpMan.php/perldoc/IPC%3A%3ASemaphore/markdown)
- System commands: `ipcs`, `ipcrm`