| | |
| | | #include "FaceDBPool.h"
|
| | | #include "logger.h"
|
| | | #include "../../logger.h"
|
| | | #include "faceAPI.h"
|
| | |
|
| | | #include <pthread.h>
|
| | |
| | | struct MutexLocker
|
| | | {
|
| | | pthread_mutex_t* mut;
|
| | | MutexLocker(pthread_mutex_t* _mut) : mut(_mut)
|
| | | MutexLocker(void* _mut) : mut((pthread_mutex_t*)_mut)
|
| | | {
|
| | | PLP_MUTEX_LOCK(mut,);
|
| | | }
|
| | |
| | | {
|
| | | int dbid;
|
| | | pthread_mutex_t db_mutex;
|
| | | faceAPI* api;
|
| | | FaceDB* api;
|
| | |
|
| | | ThreadSafeFaceDB() : dbid(-1), db_mutex(), api(nullptr)
|
| | | {
|
| | | pthread_mutex_init(db_mutex, NULL);
|
| | | pthread_mutex_init(&db_mutex, NULL);
|
| | | }
|
| | |
|
| | | ~ThreadSafeFaceDB()
|
| | | {
|
| | | pthread_mutex_destroy(db_mutex);
|
| | | pthread_mutex_destroy(&db_mutex);
|
| | | }
|
| | | };
|
| | |
|
| | |
| | | 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;
|
| | | iter->second->api->finally();
|
| | | delete iter->second->api;
|
| | | delete iter->second;
|
| | | }
|
| | |
|
| | | _face_db_map.clear();
|
| | | _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)
|
| | | void FaceDBPool::manage(int dbid, FaceDB* db)
|
| | | {
|
| | | if (dbid < 0 || db == nullptr)
|
| | | return;
|
| | |
| | |
|
| | | facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
|
| | |
|
| | | if (_face_db_map.find(dbid) != _face_db_map.end())
|
| | | if (_face_db_map->find(dbid) != _face_db_map->end())
|
| | | return;
|
| | |
|
| | | ThreadSafeFaceDB* tsfdb = new ThreadSafeFaceDB;
|
| | | tsfdb.dbid = dbid;
|
| | | tsfdb.api = db;
|
| | | tsfdb->dbid = dbid;
|
| | | tsfdb->api = db;
|
| | |
|
| | | _face_db_map.insert(std::make_pair(dbid, tsfdb));
|
| | | _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())
|
| | | 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;
|
| | | iter->second->api->finally();
|
| | | delete iter->second->api;
|
| | | delete iter->second;
|
| | |
|
| | | _face_db_map.erase(iter);
|
| | | _face_db_map->erase(iter);
|
| | | }
|
| | |
|
| | | faceAPI* FaceDBPool::get_free(int dbid)
|
| | | FaceDB* FaceDBPool::get_free(int dbid)
|
| | | {
|
| | | ThreadSafeFaceDB* tsfdb = nullptr;
|
| | | {
|
| | |
| | | MutexLocker _ml(pool_mutex);
|
| | |
|
| | | facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
|
| | | if (_face_db_map.empty())
|
| | | if (_face_db_map->empty())
|
| | | return nullptr;
|
| | |
|
| | | facedb_map_t::iterator iter = _face_db_map.find(dbid);
|
| | | if (iter == _face_db_map.end())
|
| | | 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);
|
| | | PLP_MUTEX_LOCK(&(tsfdb->db_mutex), nullptr);
|
| | | return tsfdb->api;
|
| | | }
|
| | | }
|
| | |
| | | MutexLocker _ml(pool_mutex);
|
| | |
|
| | | facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
|
| | | if (_face_db_map.empty())
|
| | | if (_face_db_map->empty())
|
| | | return;
|
| | |
|
| | | facedb_map_t::iterator iter = _face_db_map.find(dbid);
|
| | | if (iter == _face_db_map.end())
|
| | | return nullptr;
|
| | | facedb_map_t::iterator iter = _face_db_map->find(dbid);
|
| | | if (iter == _face_db_map->end())
|
| | | return;
|
| | |
|
| | | tsfdb = iter->second;
|
| | | }
|
| | |
|
| | | if (tsfdb != nullptr)
|
| | | {
|
| | | PLP_MUTEX_UNLOCK(tsfdb->db_mutex,);
|
| | | PLP_MUTEX_UNLOCK(&(tsfdb->db_mutex),);
|
| | | }
|
| | | }
|