CUBRID Engine  latest
resource_shared_pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * resource_shared_pool - template implementation for preallocated shared resources pool & dispatch
21  */
22 
23 #ifndef _RESOURCE_SHARED_POOL_HPP_
24 #define _RESOURCE_SHARED_POOL_HPP_
25 
26 #include <mutex>
27 
28 template <class T>
30 {
31  public:
32  resource_shared_pool (size_t size, bool allow_claimed_on_destruction = false)
33  : m_size (size)
34  , m_free_stack_size (size)
35  , m_mutex ()
36  , m_allow_claimed_on_destruction (allow_claimed_on_destruction)
37  {
38  m_resources = new T [size];
41  }
42 
43  resource_shared_pool (T *resources, size_t size, bool allow_claimed_on_destruction = false)
44  : m_size (size)
45  , m_free_stack_size (size)
46  , m_mutex ()
47  , m_resources (resources)
49  , m_free_stack (NULL)
50  , m_allow_claimed_on_destruction (allow_claimed_on_destruction)
51  {
53  }
54 
56  {
58  delete [] m_free_stack;
59  delete [] m_own_resources;
60  }
61 
62  inline T *claim (void)
63  {
65 
66  std::unique_lock<std::mutex> ulock (m_mutex);
67  if (m_free_stack_size == 0)
68  {
69  return NULL;
70  }
72  }
73 
74  inline void retire (T &claimed)
75  {
76  assert (&claimed >= m_resources && &claimed < m_resources + m_size);
78 
79  std::unique_lock<std::mutex> ulock (m_mutex);
80 
81  m_free_stack[m_free_stack_size++] = &claimed;
82  }
83 
84  private:
85  resource_shared_pool (); // no implicit constructor
86  resource_shared_pool (const resource_shared_pool &); // no copy constructor
87 
89  {
90  m_free_stack = new T* [m_size];
91  for (size_t i = 0; i < m_free_stack_size; i++)
92  {
94  }
95  }
96 
97  size_t m_size;
100 
104 
106 };
107 
108 #endif // _RESOURCE_SHARED_POOL_HPP_
static API_MUTEX mutex
Definition: api_util.c:72
resource_shared_pool(T *resources, size_t size, bool allow_claimed_on_destruction=false)
#define assert(x)
#define NULL
Definition: freelistheap.h:34
resource_shared_pool(size_t size, bool allow_claimed_on_destruction=false)
int i
Definition: dynamic_load.c:954