remove conflict
[c11concurrency-benchmarks.git] / mabain / src / resource_pool.h
1 /**
2  * Copyright (C) 2018 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #ifndef __RESOURCE_POOL__
20 #define __RESOURCE_POOL__
21
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <pthread.h>
26
27 #include "mmap_file.h"
28
29 namespace mabain {
30
31 // A singleton class for managing resource/file descriptors using
32 // shared_ptr. All db handles for the same db will share the same
33 // file descriptors so that we won't be running out of file descriptors
34 // when there are a large number of DB handles opened.
35 class ResourcePool
36 {
37 public:
38     ~ResourcePool();
39
40     std::shared_ptr<MmapFileIO> OpenFile(const std::string &fpath, int mode,
41                                          size_t file_size, bool &map_file,
42                                          bool create_file);
43     void RemoveResourceByDB(const std::string &db_path);
44     void RemoveResourceByPath(const std::string &path);
45     void RemoveAll();
46     bool CheckExistence(const std::string &header_path);
47     int  AddResourceByPath(const std::string &path, std::shared_ptr<MmapFileIO> resource);
48
49     static ResourcePool& getInstance() {
50         static ResourcePool instance; // only one instance per process
51         return instance;
52     }
53
54 private:
55     ResourcePool();
56
57     std::map<std::string, std::shared_ptr<MmapFileIO>> file_pool;
58     pthread_mutex_t pool_mutex;
59 };
60
61 }
62
63 #endif