houxiao
2017-01-11 97dbd476ee99f36286cd866b2208ceead3019b31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "FaceDBPool.h"
#include "logger.h"
#include "faceAPI.h"
 
#include <pthread.h>
#include <map>
 
#define PLP_MUTEX_LOCK(mut,_ret) if (mut != nullptr) {\
    int ret = pthread_mutex_lock((pthread_mutex_t*)mut); \
    if(ret != 0) \
    { \
        LOG_ERROR << "pthread_mutex_lock " << #mut <<  ": " << ret; \
        return _ret; \
    } \
}
 
#define PLP_MUTEX_UNLOCK(mut,_ret) if (mut != nullptr) {\
    int ret = pthread_mutex_unlock((pthread_mutex_t*)mut); \
    if(ret != 0) \
    { \
        LOG_ERROR << "pthread_mutex_unlock " << #mut <<  ": " << ret; \
        return _ret; \
    } \
}
 
struct MutexLocker
{
    pthread_mutex_t* mut;
    MutexLocker(pthread_mutex_t* _mut) : mut(_mut)
    {
        PLP_MUTEX_LOCK(mut,);
    }
    ~MutexLocker()
    {
        PLP_MUTEX_UNLOCK(mut,);
    }
};
 
struct ThreadSafeFaceDB
{
    int dbid;
    pthread_mutex_t db_mutex;
    faceAPI* api;
    
    ThreadSafeFaceDB() : dbid(-1), db_mutex(), api(nullptr)
    {
        pthread_mutex_init(db_mutex, NULL);
    }
    
    ~ThreadSafeFaceDB()
    {
        pthread_mutex_destroy(db_mutex);
    }
};
 
typedef std::map<int, ThreadSafeFaceDB*> facedb_map_t;
 
FaceDBPool::FaceDBPool() : 
    pool_mutex(nullptr), face_db_map(nullptr)
{
    pool_mutex = new pthread_mutex_t;
    pthread_mutex_init((pthread_mutex_t*)pool_mutex, NULL);
    
    face_db_map = new facedb_map_t();
}
 
FaceDBPool::~FaceDBPool()
{
    facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
    for (facedb_map_t::iterator iter = _face_db_map->begin(); iter != _face_db_map->end(); ++iter)
    {
        iter->second.api->db_save();
        delete iter->second.api;
        delete iter->second;
    }
    
    _face_db_map.clear();
    
    pthread_mutex_destroy((pthread_mutex_t*)pool_mutex);
    delete (pthread_mutex_t*)pool_mutex;
    pool_mutex = nullptr;
}
 
void FaceDBPool::manage(int dbid, faceAPI* db)
{
    if (dbid < 0 || db == nullptr)
        return;
 
    MutexLocker _ml(pool_mutex);
    
    facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
    
    if (_face_db_map.find(dbid) != _face_db_map.end())
        return;
    
    ThreadSafeFaceDB* tsfdb = new ThreadSafeFaceDB;
    tsfdb.dbid = dbid;
    tsfdb.api = db;
    
    _face_db_map.insert(std::make_pair(dbid, tsfdb));
}
 
void FaceDBPool::unmanage(int dbid)
{
    MutexLocker _ml(pool_mutex);
 
    facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
    facedb_map_t::iterator iter = _face_db_map.find(dbid);
    if (iter == _face_db_map.end())
        return;
    
    iter->second.api->db_save();
    delete iter->second.api;
    delete iter->second;
    
    _face_db_map.erase(iter);
}
 
faceAPI* FaceDBPool::get_free(int dbid)
{
    ThreadSafeFaceDB* tsfdb = nullptr;
    {
        //avoid dead lock
        MutexLocker _ml(pool_mutex);
        
        facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
        if (_face_db_map.empty())
            return nullptr;
        
        facedb_map_t::iterator iter = _face_db_map.find(dbid);
        if (iter == _face_db_map.end())
            return nullptr;
        
        tsfdb = iter->second;
    }
 
    if (tsfdb != nullptr)
    {
        PLP_MUTEX_LOCK(tsfdb->db_mutex, nullptr);
        return tsfdb->api;
    }
}
 
void FaceDBPool::release(int dbid)
{
    ThreadSafeFaceDB* tsfdb = nullptr;
    {
        //avoid dead lock
        MutexLocker _ml(pool_mutex);
        
        facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
        if (_face_db_map.empty())
            return;
        
        facedb_map_t::iterator iter = _face_db_map.find(dbid);
        if (iter == _face_db_map.end())
            return nullptr;
        
        tsfdb = iter->second;
    }
    
    if (tsfdb != nullptr)
    {
        PLP_MUTEX_UNLOCK(tsfdb->db_mutex,);
    }
}