ApiLock.h 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
ApiLock.h
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 18
#pragma once

19 20 21 22 23 24
// Do not treat doxygen documentation as XML
#pragma warning(push)
#pragma warning(disable : 4635)
#include "belle-sip/object.h"
#pragma warning(pop)

25
#include <list>
26 27
#include <mutex>

28 29
#include "Utils.h"

Ghislain MARY's avatar
Ghislain MARY committed
30

31 32 33
//#define API_LOCK_DEBUG


34
#define API_LOCK ApiLock apiLock(__FUNCTION__)
Ghislain MARY's avatar
Ghislain MARY committed
35 36


37
namespace BelledonneCommunications
38
{
39 40 41
	namespace Linphone
	{
		namespace Native
42
		{
43 44 45 46 47 48 49
#ifdef API_LOCK_DEBUG
			struct LockInfo
			{
				unsigned long thread;
				std::string func;
				bool locked;
			};
50 51
#endif

52 53 54 55
			class GlobalApiLock
			{
			public:
				static GlobalApiLock * Instance();
56
#ifdef API_LOCK_DEBUG
57
				static std::list<LockInfo> lock_info_list;
58
#endif
59 60
				void Lock(std::string func);
				void Unlock(std::string func);
61

62 63 64
			private:
				GlobalApiLock();
				~GlobalApiLock();
65
#ifdef API_LOCK_DEBUG
66 67 68
				void RegisterLocking(std::string func);
				void RegisterLocked(std::string func);
				void UnregisterLocked(std::string func);
69
#endif
70

71 72 73 74 75 76
				static GlobalApiLock *instance;
				static std::mutex instance_mutex;
				std::recursive_mutex mutex;
				int count;
				belle_sip_object_pool_t *pool;
			};
77

78 79 80 81 82 83 84 85
			/// <summary>
			/// A class that implements a mutex mechanism to protect objects accessible from the API surface exposed by this DLL
			/// </summary>
			class ApiLock
			{
			public:
				ApiLock(std::string func);
				~ApiLock();
86

87 88 89 90 91
			private:
				std::string func;
			};
		}
	}
92
}