ApiLock.cpp 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
ApiLock.cpp
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16
*/

17
#include "ApiLock.h"
18
#include "mediastreamer2/mscommon.h"
19

20 21 22 23
using namespace BelledonneCommunications::Linphone::Native;

GlobalApiLock *GlobalApiLock::instance = nullptr;
std::mutex GlobalApiLock::instance_mutex;
24
#ifdef API_LOCK_DEBUG
25
std::list<LockInfo> GlobalApiLock::lock_info_list;
26
#endif
27

28 29
GlobalApiLock::GlobalApiLock() : count(0), pool(nullptr)
{}
30

31 32
GlobalApiLock::~GlobalApiLock()
{}
33

34 35 36 37 38 39
GlobalApiLock * GlobalApiLock::Instance()
{
	if (instance == nullptr) {
		instance_mutex.lock();
		if (instance == nullptr) {
			instance = new GlobalApiLock();
40
		}
41 42 43 44
		instance_mutex.unlock();
	}
	return instance;
}
45

46 47
void GlobalApiLock::Lock(std::string func)
{
48
#ifdef API_LOCK_DEBUG
49
	RegisterLocking(func);
50
#endif
51
	mutex.lock();
52
#ifdef API_LOCK_DEBUG
53
	RegisterLocked(func);
54
#endif
55 56 57 58 59
	if (count == 0) {
		pool = belle_sip_object_pool_push();
	}
	count++;
}
60

61 62 63 64 65 66 67
void GlobalApiLock::Unlock(std::string func)
{
	count--;
	if ((count == 0) && (pool != nullptr)) {
		belle_sip_object_unref(pool);
		pool = nullptr;
	}
68
#ifdef API_LOCK_DEBUG
69
	UnregisterLocked(func);
70
#endif
71 72
	mutex.unlock();
}
73

74
#ifdef API_LOCK_DEBUG
75 76 77 78 79 80 81
void GlobalApiLock::RegisterLocking(std::string func)
{
	instance_mutex.lock();
	LockInfo li = { WIN_thread_self(), func, false };
	lock_info_list.push_back(li);
	instance_mutex.unlock();
}
82

83 84 85 86 87 88 89 90
void GlobalApiLock::RegisterLocked(std::string func)
{
	instance_mutex.lock();
	unsigned long current_thread = WIN_thread_self();
	for (std::list<LockInfo>::reverse_iterator it = lock_info_list.rbegin(); it != lock_info_list.rend(); it++) {
		if ((it->func == func) && (it->thread = current_thread) && !it->locked) {
			it->locked = true;
			break;
91
		}
92 93 94
	}
	instance_mutex.unlock();
}
Ghislain MARY's avatar
Ghislain MARY committed
95

96 97 98 99 100 101 102 103
void GlobalApiLock::UnregisterLocked(std::string func)
{
	instance_mutex.lock();
	unsigned long current_thread = WIN_thread_self();
	for (std::list<LockInfo>::reverse_iterator it = lock_info_list.rbegin(); it != lock_info_list.rend(); it++) {
		if ((it->func == func) && (it->thread = current_thread) && it->locked) {
			lock_info_list.erase(--(it.base()));
			break;
104
		}
105 106 107
	}
	instance_mutex.unlock();
}
108
#endif
109

110 111 112 113
ApiLock::ApiLock(std::string func) : func(func)
{
	GlobalApiLock::Instance()->Lock(func);
}
114

115 116 117
ApiLock::~ApiLock()
{
	GlobalApiLock::Instance()->Unlock(func);
118
}