Click
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ReadWriteLock Class Reference

A read/write lock. More...

#include <sync.hh>

Public Member Functions

 ReadWriteLock ()
 Creates a ReadWriteLock.
 
void acquire_read ()
 Acquires the ReadWriteLock for reading.
 
bool attempt_read ()
 Attempts to acquire the ReadWriteLock for reading.
 
void release_read ()
 Releases the ReadWriteLock for reading.
 
void acquire_write ()
 Acquires the ReadWriteLock for writing.
 
bool attempt_write ()
 Attempts to acquire the ReadWriteLock for writing.
 
void release_write ()
 Releases the ReadWriteLock for writing.
 

Detailed Description

A read/write lock.

The ReadWriteLock class abstracts a read/write lock in SMP Click. Multiple SMP Click threads can hold read locks simultaneously, but if any thread holds a write lock, then no other thread holds any kind of lock. The read/write lock is implemented with Spinlock objects, so acquiring a lock is a polling operation. ReadWriteLocks can be used to synchronize access to shared data among multiple Click SMP threads. ReadWriteLocks should not be held for long periods of time.

ReadWriteLock operations do nothing unless Click was compiled with –enable-multithread. Therefore, ReadWriteLock should not be used to, for example, synchronize handlers with main element threads.

The main ReadWriteLock operations are acquire_read() and acquire_write(), which acquire the lock for reading or writing, respectively, and release_read() and release_write(), which similarly release the lock. attempt_read() and attempt_write() acquire the lock only if it can be acquired instantaneously.

It is OK for a thread to acquire a lock it has already acquired, but you must release it as many times as you have acquired it.

ReadWriteLock objects are relatively large in terms of memory usage; don't create too many of them.

Constructor & Destructor Documentation

◆ ReadWriteLock()

ReadWriteLock::ReadWriteLock ( )
inline

Creates a ReadWriteLock.

Member Function Documentation

◆ acquire_read()

void ReadWriteLock::acquire_read ( )
inline

Acquires the ReadWriteLock for reading.

On return, this thread has acquired the lock for reading. The function will spin indefinitely until the lock is acquired. It is OK to acquire a lock you have already acquired, but you must release it as many times as you have acquired it.

See also
Spinlock::acquire

◆ attempt_read()

bool ReadWriteLock::attempt_read ( )
inline

Attempts to acquire the ReadWriteLock for reading.

Returns
True iff the ReadWriteLock was acquired.

This function will acquire the lock for reading and return true only if the ReadWriteLock can be acquired right away, without retries.

◆ release_read()

void ReadWriteLock::release_read ( )
inline

Releases the ReadWriteLock for reading.

The ReadWriteLock must have been previously acquired by either ReadWriteLock::acquire_read or ReadWriteLock::attempt_read. Do not call release_read() on a lock that was acquired for writing.

◆ acquire_write()

void ReadWriteLock::acquire_write ( )
inline

Acquires the ReadWriteLock for writing.

On return, this thread has acquired the lock for writing. The function will spin indefinitely until the lock is acquired. It is OK to acquire a lock you have already acquired, but you must release it as many times as you have acquired it.

See also
ReadWriteLock::acquire_read

◆ attempt_write()

bool ReadWriteLock::attempt_write ( )
inline

Attempts to acquire the ReadWriteLock for writing.

Returns
True iff the ReadWriteLock was acquired.

This function will acquire the lock for writing and return true only if the ReadWriteLock can be acquired right away, without retries. Note, however, that acquiring a ReadWriteLock requires as many operations as there are CPUs.

See also
ReadWriteLock::attempt_read

◆ release_write()

void ReadWriteLock::release_write ( )
inline

Releases the ReadWriteLock for writing.

The ReadWriteLock must have been previously acquired by either ReadWriteLock::acquire_write or ReadWriteLock::attempt_write. Do not call release_write() on a lock that was acquired for reading.

See also
ReadWriteLock::release_read

The documentation for this class was generated from the following file: