benchmarks compiles with clang
[c11concurrency-benchmarks.git] / mabain / src / resource_pool.cpp
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 #include <string.h>
20
21 #include "resource_pool.h"
22 #include "mabain_consts.h"
23 #include "mmap_file.h"
24 #include "error.h"
25
26 namespace mabain {
27
28 ResourcePool::ResourcePool()
29 {
30     pthread_mutex_init(&pool_mutex, NULL);
31 }
32
33 ResourcePool::~ResourcePool()
34 {
35     pthread_mutex_destroy(&pool_mutex);
36 }
37
38 void ResourcePool::RemoveAll()
39 {
40    pthread_mutex_lock(&pool_mutex);
41    file_pool.clear();
42    pthread_mutex_unlock(&pool_mutex);
43 }
44
45 // check if a in-memory db already exists
46 bool ResourcePool::CheckExistence(const std::string &header_path)
47 {
48     pthread_mutex_lock(&pool_mutex);
49     auto search = file_pool.find(header_path);
50     pthread_mutex_unlock(&pool_mutex);
51
52     return (search != file_pool.end());
53 }
54
55 void ResourcePool::RemoveResourceByPath(const std::string &path)
56 {
57     pthread_mutex_lock(&pool_mutex);
58     file_pool.erase(path);
59     pthread_mutex_unlock(&pool_mutex);
60 }
61
62 void ResourcePool::RemoveResourceByDB(const std::string &db_path)
63 {
64     pthread_mutex_lock(&pool_mutex);
65
66     for(auto it = file_pool.begin(); it != file_pool.end();)
67     {
68         if(it->first.compare(0, db_path.size(), db_path) == 0)
69             it = file_pool.erase(it);
70         else
71             it++;
72     }
73
74     pthread_mutex_unlock(&pool_mutex);
75 }
76
77 std::shared_ptr<MmapFileIO> ResourcePool::OpenFile(const std::string &fpath,
78                                                    int mode,
79                                                    size_t file_size,
80                                                    bool &map_file,
81                                                    bool create_file)
82 {
83     std::shared_ptr<MmapFileIO> mmap_file;
84
85     pthread_mutex_lock(&pool_mutex);
86
87     auto search = file_pool.find(fpath);
88     if(search == file_pool.end())
89     {
90         int flags = O_RDWR;
91         if(create_file)
92             flags |= O_CREAT;
93         if(mode & CONSTS::MEMORY_ONLY_MODE)
94             flags |= MMAP_ANONYMOUS_MODE;
95
96         mmap_file = std::shared_ptr<MmapFileIO>
97                     (
98                         new MmapFileIO(fpath,
99                                        flags,
100                                        file_size,
101                                        mode & CONSTS::SYNC_ON_WRITE)
102                     );
103         if(map_file)
104         {
105             if(mmap_file->MapFile(file_size, 0) != NULL)
106             {
107                 if(!(mode & CONSTS::MEMORY_ONLY_MODE))
108                     mmap_file->Close();
109             }
110             else
111             {
112                 map_file = false;
113             }
114         }
115
116         file_pool[fpath] = mmap_file;
117     }
118     else
119     {
120         mmap_file = search->second;
121     }
122
123     pthread_mutex_unlock(&pool_mutex);
124     return mmap_file;
125 }
126
127 int ResourcePool::AddResourceByPath(const std::string &path, std::shared_ptr<MmapFileIO> resource)
128 {
129     int rval = MBError::IN_DICT;
130
131     pthread_mutex_lock(&pool_mutex);
132     auto search = file_pool.find(path);
133     if(search == file_pool.end())
134     {
135         file_pool[path] = resource;
136         rval = MBError::SUCCESS;
137     }
138     pthread_mutex_unlock(&pool_mutex);
139
140     return rval;
141 }
142
143 }