2018-03-18 01:31:52 +03:00
/* -------------------------------------------------------------------------- */
2024-07-29 15:25:20 +03:00
/* Copyright 2002-2024, OpenNebula Project, OpenNebula Systems */
2018-03-18 01:31:52 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef POOL_SQL_CACHE_H_
# define POOL_SQL_CACHE_H_
# include <map>
2020-09-10 14:32:52 +03:00
# include <mutex>
2018-03-18 01:31:52 +03:00
# include "PoolObjectSQL.h"
/**
* This class stores the active reference to pool objects . It can also
* cache to not reload object state from the DB .
*
* Access to the cache needs to happen in a critical section .
*/
class PoolSQLCache
{
public :
PoolSQLCache ( ) ;
2020-09-10 14:32:52 +03:00
~ PoolSQLCache ( ) = default ;
2018-03-18 01:31:52 +03:00
/**
* Allocates a new cache line to hold an active pool object . If the line
* does not exist it is created . Any active reference is deallocated to
* load a fresh copy of the object .
*
* The cache line is locked to sync access to the given object .
*
* @ param oid of the object
*/
2020-09-10 14:32:52 +03:00
std : : mutex * lock_line ( int oid ) ;
2018-03-18 01:31:52 +03:00
private :
/**
* This class represents a cache line . It stores a reference to the pool
* object and a mutex to control concurrent access to the object
*/
struct CacheLine
{
2018-10-09 12:05:08 +03:00
CacheLine ( ) : active ( 0 )
2018-03-18 01:31:52 +03:00
{
}
2020-09-10 14:32:52 +03:00
~ CacheLine ( ) = default ;
2018-03-18 01:31:52 +03:00
void lock ( )
{
2020-09-10 14:32:52 +03:00
_mutex . lock ( ) ;
}
2018-03-18 01:31:52 +03:00
void unlock ( )
{
2020-09-10 14:32:52 +03:00
_mutex . unlock ( ) ;
2018-03-18 01:31:52 +03:00
}
2020-09-10 14:32:52 +03:00
bool trylock ( )
2018-03-18 01:31:52 +03:00
{
2020-09-10 14:32:52 +03:00
return _mutex . try_lock ( ) ;
2018-03-18 01:31:52 +03:00
}
/**
2018-10-09 12:05:08 +03:00
* Concurrent access to object
2018-03-18 01:31:52 +03:00
*/
2020-09-10 14:32:52 +03:00
std : : mutex _mutex ;
2018-03-18 01:31:52 +03:00
/**
* Number of threads waiting on the line mutex
*/
int active ;
} ;
/**
* Max number of references in the cache .
*/
static unsigned int MAX_ELEMENTS ;
/**
* Cache of pool objects indexed by their oid
*/
std : : map < int , CacheLine * > cache ;
/**
* Deletes all cache lines if they are not in use .
*/
void flush_cache_lines ( ) ;
/**
* Controls concurrent access to the cache map .
*/
2020-09-10 14:32:52 +03:00
std : : mutex _mutex ;
2018-03-18 01:31:52 +03:00
} ;
# endif /*POOL_SQL_CACHE_H_*/