curl-cpp
static c++17 wrapper for curl with -fno-exceptions support
Public Member Functions | List of all members
curl::utils::shared_mutex Class Reference

#include <shared_mutex.hpp>

Collaboration diagram for curl::utils::shared_mutex:
Collaboration graph

Public Member Functions

 shared_mutex () noexcept
 
 shared_mutex (const shared_mutex &)=delete
 
 shared_mutex (shared_mutex &&)=delete
 
shared_mutexoperator= (const shared_mutex &)=delete
 
shared_mutexoperator= (shared_mutex &&)=delete
 
void lock () noexcept
 
void lock_shared () noexcept
 
void unlock () noexcept
 

Detailed Description

C++ RAII wrapper for pthread_rwlock_t.

To use this interface, you'd have to add -lpthread to LDFLAGS of your project.

This class is designed specifically for curl::Share, which requires a std::shared_mutex like interface, but have to be able to unlock shared lock/normal lock with a single function.

Definition at line 18 of file shared_mutex.hpp.

Constructor & Destructor Documentation

◆ shared_mutex()

curl::utils::shared_mutex::shared_mutex ( )
noexcept

On failure, call err to terminate the program.

Definition at line 14 of file shared_mutex.cc.

15 {
16  pthread_rwlockattr_t attr;
17 
18  CHECK(pthread_rwlockattr_init(&attr));
19  pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
20 
21  CHECK(pthread_rwlock_init(&rwlock, &attr));
22 
23  pthread_rwlockattr_destroy(&attr);
24 }

Member Function Documentation

◆ lock()

void curl::utils::shared_mutex::lock ( )
noexcept

Undefined behavior if deadlocks.

Definition at line 30 of file shared_mutex.cc.

31 {
32  pthread_rwlock_wrlock(&rwlock);
33 }

◆ lock_shared()

void curl::utils::shared_mutex::lock_shared ( )
noexcept

Undefined behavior if deadlocks.

Definition at line 34 of file shared_mutex.cc.

35 {
36  int ret;
37  do {
38  ret = pthread_rwlock_rdlock(&rwlock);
39  } while (ret != 0 && errno == EAGAIN);
40 }

◆ unlock()

void curl::utils::shared_mutex::unlock ( )
noexcept

One unlock function for lock()/lock_shared().

Definition at line 42 of file shared_mutex.cc.

43 {
44  pthread_rwlock_unlock(&rwlock);
45 }

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