MySQL 9.0.0
Source Code Documentation
resource_blocker::Resource Class Reference

Represents a "Resource" which can be either "used" by a number of threads, or "blocked" by a number of threads. More...

#include <resource_blocker.h>

Public Types

using String_set = std::set< std::string >
 

Public Member Functions

 Resource ()
 
 ~Resource ()
 
 Resource (Resource &other)=delete
 
 Resource (Resource &&other)=delete
 
Resourceoperator= (Resource const &other)=delete
 
Resourceoperator= (Resource &&other)=delete
 

Private Member Functions

int try_block (const std::string &reason)
 Try to block the resource from being used, for the given reason. More...
 
void end_block (const std::string &reason)
 Unblock the resource if try_block was previously called successfully. More...
 
String_set try_use ()
 Try to start using the resource. More...
 
void end_use ()
 Stop using the resource if try_use was previously called successfully. More...
 
mysql_mutex_tget_lock ()
 

Private Attributes

String_set m_block_reasons
 
int m_user_count
 
std::atomic< int > m_initialized
 
mysql_mutex_t m_lock
 

Friends

class User
 
class Blocker
 

Detailed Description

Represents a "Resource" which can be either "used" by a number of threads, or "blocked" by a number of threads.

When one or more threads use the resource, no thread can block it. When a one or more threads is blocking the resource, no thread can use it. Each blocker provides a message that explains why the resource is blocked.

The resource is blocked or used by creating instances of the Blocker and User classes.

Resource museum;
// If the museum is open, visit it. This may be called by multiple threads.
void visit_museum() {
User user(museum);
if (!user) return;
// walk around the museum
}
// If there are no guests in the museum, close it temporarily for
// maintenance.
void close_museum_for_maintenance() {
Blocker blocker(museum, "Museum is currently closed for maintenance.");
if (!blocker) return;
// repair exhibitions
}
// If there are no guests in the museum, close it temporarily for
// cleaning.
void close_museum_for_cleaning() {
Blocker blocker(museum, "Museum is currently closed for cleaning.");
if (!blocker) return;
// clean the floors
}
friend class User
Definition: resource_blocker.h:130
friend class Blocker
Definition: resource_blocker.h:131
Resource()
Definition: resource_blocker.h:74
char * user
Definition: mysqladmin.cc:66

Member Typedef Documentation

◆ String_set

using resource_blocker::Resource::String_set = std::set<std::string>

Constructor & Destructor Documentation

◆ Resource() [1/3]

resource_blocker::Resource::Resource ( )
inline

◆ ~Resource()

resource_blocker::Resource::~Resource ( )
inline

◆ Resource() [2/3]

resource_blocker::Resource::Resource ( Resource other)
delete

◆ Resource() [3/3]

resource_blocker::Resource::Resource ( Resource &&  other)
delete

Member Function Documentation

◆ end_block()

void resource_blocker::Resource::end_block ( const std::string &  reason)
inlineprivate

Unblock the resource if try_block was previously called successfully.

◆ end_use()

void resource_blocker::Resource::end_use ( )
inlineprivate

Stop using the resource if try_use was previously called successfully.

◆ get_lock()

mysql_mutex_t & resource_blocker::Resource::get_lock ( )
inlineprivate

◆ operator=() [1/2]

Resource & resource_blocker::Resource::operator= ( Resource &&  other)
delete

◆ operator=() [2/2]

Resource & resource_blocker::Resource::operator= ( Resource const &  other)
delete

◆ try_block()

int resource_blocker::Resource::try_block ( const std::string &  reason)
inlineprivate

Try to block the resource from being used, for the given reason.

◆ try_use()

String_set resource_blocker::Resource::try_use ( )
inlineprivate

Try to start using the resource.

Friends And Related Function Documentation

◆ Blocker

friend class Blocker
friend

◆ User

friend class User
friend

Member Data Documentation

◆ m_block_reasons

String_set resource_blocker::Resource::m_block_reasons
private

◆ m_initialized

std::atomic<int> resource_blocker::Resource::m_initialized
private

◆ m_lock

mysql_mutex_t resource_blocker::Resource::m_lock
mutableprivate

◆ m_user_count

int resource_blocker::Resource::m_user_count
private

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