-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathcppcompat.hh
67 lines (56 loc) · 1.44 KB
/
cppcompat.hh
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
// This is a -*- C++ -*- header
#ifndef __CPP_COMPAT_H
#define __CPP_COMPAT_H
#include <pthread.h>
// We can't use <mutex> on Android because it requires linking libc++ into the rutime, see below.
//
// Android NDK currently provides a build of libc++ which we cannot link into .NET for Android runtime because it would
// expose libc++ symbols which would conflict with a version of libc++ potentially included in a mixed
// native/Xamarin.Android application.
//
// Until we can figure out a way to take full advantage of the STL, this header will
// contain implementations of certain C++ standard functions, classes
// etc we want to use despite lack of the STL.
//
// When/if we have any STL implementation available on standard Android
// we can remove this file.
namespace xamarin::android
{
template<typename TMutex>
class lock_guard
{
public:
using mutex_type = TMutex;
public:
lock_guard (const lock_guard&) = delete;
explicit lock_guard (mutex_type& _mutex)
: _mutex (_mutex)
{
_mutex.lock ();
}
~lock_guard ()
{
_mutex.unlock ();
}
lock_guard& operator= (const lock_guard&) = delete;
private:
mutex_type &_mutex;
};
class mutex
{
public:
mutex () noexcept = default;
~mutex () noexcept = default;
void lock () noexcept
{
pthread_mutex_lock (&_pmutex);
}
void unlock () noexcept
{
pthread_mutex_unlock (&_pmutex);
}
private:
pthread_mutex_t _pmutex = PTHREAD_MUTEX_INITIALIZER;
};
}
#endif