UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
resource_manager.cpp
Go to the documentation of this file.
1
10/*
11 * Copyright (c) 2013 CESNET, z. s. p. o.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, is permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * 3. Neither the name of CESNET nor the names of its contributors may be
26 * used to endorse or promote products derived from this software without
27 * specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
31 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
32 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
33 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#ifdef HAVE_CONFIG_H
44#include "config.h"
45#include "config_unix.h"
46#include "config_win32.h"
47#endif // HAVE_CONFIG_H
48
49#include "resource_manager.h"
50
51#include "utils/lock_guard.h"
52
53#include <algorithm>
54#include <map>
55#include <stdexcept>
56#include <string>
57#include <typeinfo>
58#include <utility>
59
60#define TYPE_LOCK 0
61#define TYPE_SHM 1
62#define TYPE_SINGLETON 2
63typedef int type_t;
64
65using std::logic_error;
66using std::map;
67using std::pair;
68using std::string;
70
71class options_t {
72 public:
73 virtual ~options_t() {}
74};
75
76class no_opts : public options_t {
77};
78
79class shm_opts : public options_t {
80 public:
81 shm_opts(int size) : m_size(size) {}
82 private:
83 int m_size;
84 friend class shm;
85};
86
98
99class resource {
100 public:
101 virtual ~resource() {}
102 static resource *create(type_t type, options_t const & options);
103 static string get_suffix(type_t type);
104};
105
106class lock : public resource {
107 public:
109 pthread_mutex_init(&m_lock, NULL);
110 }
111
113 pthread_mutex_destroy(&m_lock);
114 }
115
116 pthread_mutex_t *get() {
117 return &m_lock;
118 }
119 private:
120 pthread_mutex_t m_lock;
121};
122
123class shm : public resource {
124 public:
125 shm(shm_opts const &opts) {
126 m_data = calloc(1, opts.m_size);
127 }
128
130 free(m_data);
131 }
132
133 void *get() {
134 return m_data;
135 }
136
137 private:
138 void *m_data;
139};
140
141class singleton : public resource {
142 public:
143 singleton(singleton_opts const &opts) : m_opts(opts) {
144 m_data = opts.m_init(opts.m_init_data);
145 }
146
150
151 void *get() {
152 return m_data;
153 }
154
155 private:
157 void *m_data;
158};
159
160static void func_delete(pair<string, pair<resource *, int> > arg);
161static void func_delete(pair<string, pair<resource *, int> > arg) {
162 delete arg.second.first;
163}
164
166 public:
167 typedef map<string, pair<resource *, int> > obj_map_t;
168
170 pthread_mutex_init(&m_access_lock, NULL);
171 pthread_mutex_init(&m_excl_lock, NULL);
172 }
173
175 for_each(m_objs.begin(),
176 m_objs.end(), func_delete);
177 pthread_mutex_destroy(&m_access_lock);
178 pthread_mutex_destroy(&m_excl_lock);
179 }
180
181 void lock() {
182 pthread_mutex_lock(&m_excl_lock);
183 }
184
185 void unlock() {
186 pthread_mutex_unlock(&m_excl_lock);
187 }
188
190 resource *ret;
192 string item_name = name + "#" + resource::get_suffix(type);
193
194 obj_map_t::iterator it = m_objs.find(item_name);
195 if(it == m_objs.end()) {
196 // create
198 m_objs[item_name] = pair<resource *, int>(ret,
199 1);
200 } else {
201 it->second.second += 1;
202 ret = it->second.first;
203 }
204 return ret;
205 }
206
207 void release(string name, type_t type) {
209 string item_name = name + "#" + resource::get_suffix(type);
210
211 obj_map_t::iterator it = m_objs.find(item_name);
212 if(it == m_objs.end()) {
213 // create
214 throw logic_error("No such object.");
215 } else {
216 it->second.second -= 1;
217 if(it->second.second == 0) { // ref count == 0
218 delete it->second.first;
219 m_objs.erase(it);
220 }
221 }
222 }
223
224 private:
225 pthread_mutex_t m_access_lock;
226 pthread_mutex_t m_excl_lock;
228
229};
230
232
234{
235 if(type == TYPE_LOCK) {
236 return new lock;
237 } else if(type == TYPE_SHM) {
238 return new shm(dynamic_cast<const shm_opts &>(options));
239 } else if(type == TYPE_SINGLETON) {
240 return new singleton(dynamic_cast<const singleton_opts &>(options));
241 } else {
242 throw logic_error("Wrong typeid");
243 }
244}
245
247{
248 if(type == TYPE_LOCK) {
249 return string("mutex");
250 } else if(type == TYPE_SHM) {
251 return string("SHM");
252 } else if(type == TYPE_SINGLETON) {
253 return string("singleton");
254 } else {
255 throw logic_error("Wrong typeid");
256 }
257}
258
259pthread_mutex_t *rm_acquire_shared_lock(const char *name)
260{
261 lock *l = dynamic_cast<lock *>(resource_manager.acquire(
262 string(name), TYPE_LOCK, no_opts()));
263 if(l) {
264 return l->get();
265 } else {
266 return NULL;
267 }
268}
269
271{
273}
274
276{
278}
279
281{
283}
284
285void *rm_get_shm(const char *name, int size)
286{
287 shm *s = dynamic_cast<shm *>(resource_manager.acquire(
288 string(name), TYPE_SHM, shm_opts(size)));
289 if(s) {
290 return s->get();
291 } else {
292 return NULL;
293 }
294}
295
296void *rm_singleton(const char *name, singleton_initializer_t initializer, void *data,
298{
299 singleton *s = dynamic_cast<singleton *>(resource_manager.acquire(
300 string(name), TYPE_SINGLETON, singleton_opts(initializer, data, done)));
301 if(s) {
302 return s->get();
303 } else {
304 return NULL;
305 }
306}
307
static af_result_code init(struct module *parent, const char *cfg, void **state)
static void done(void *state)
pthread_mutex_t * get()
pthread_mutex_t m_lock
virtual ~options_t()
resource * acquire(string name, type_t type, options_t const &options)
pthread_mutex_t m_excl_lock
void release(string name, type_t type)
map< string, pair< resource *, int > > obj_map_t
pthread_mutex_t m_access_lock
static resource * create(type_t type, options_t const &options)
virtual ~resource()
static string get_suffix(type_t type)
shm_opts(int size)
void * get()
shm(shm_opts const &opts)
void * m_data
singleton_deleter_t m_done
singleton_initializer_t m_init
singleton_opts(singleton_initializer_t init, void *data, singleton_deleter_t done)
singleton_opts m_opts
singleton(singleton_opts const &opts)
class generic_lock_guard< pthread_mutex_t, pthread_mutex_lock, pthread_mutex_unlock > pthread_mutex_guard
Definition lock_guard.h:43
pthread_mutex_t * rm_acquire_shared_lock(const char *name)
void rm_lock()
static resource_manager_t resource_manager
int type_t
void rm_release_shared_lock(const char *name)
void * rm_get_shm(const char *name, int size)
static void func_delete(pair< string, pair< resource *, int > > arg)
void * rm_singleton(const char *name, singleton_initializer_t initializer, void *data, singleton_deleter_t done)
void rm_unlock()
#define TYPE_LOCK
#define TYPE_SHM
#define TYPE_SINGLETON
void *(* singleton_initializer_t)(void *)
void(* singleton_deleter_t)(void *)
Option::OptType type
Definition settings.cpp:194
const char * name
Definition settings.cpp:193
Definition rtp.c:253