nearby: snapshot as of cl/296436629

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
This commit is contained in:
Alexey Polyudov
2020-04-04 12:52:31 -07:00
parent 598516303b
commit 204f76077d
195 changed files with 27318 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
cc_library(
name = "default",
srcs = [
"default_condition_variable.cc",
"default_lock.cc",
"default_platform.cc",
],
hdrs = [
"default_condition_variable.h",
"default_lock.h",
"default_platform.h",
],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//core:__subpackages__",
],
deps = [
"//platform:types",
"//platform/api",
],
)
cc_library(
name = "lock",
srcs = ["default_lock.cc"],
hdrs = ["default_lock.h"],
visibility = [
"//platform:__subpackages__",
],
deps = ["//platform/api:lock"],
)
cc_library(
name = "condition_variable",
srcs = ["default_condition_variable.cc"],
hdrs = ["default_condition_variable.h"],
visibility = [
"//platform:__subpackages__",
],
deps = [
":default",
"//platform:types",
"//platform/api:condition_variable",
],
)
@@ -0,0 +1,28 @@
#include "platform/impl/default/default_condition_variable.h"
namespace location {
namespace nearby {
DefaultConditionVariable::DefaultConditionVariable(Ptr<DefaultLock> lock)
: lock_(lock), attr_(), cond_() {
pthread_condattr_init(&attr_);
pthread_cond_init(&cond_, &attr_);
}
DefaultConditionVariable::~DefaultConditionVariable() {
pthread_cond_destroy(&cond_);
pthread_condattr_destroy(&attr_);
}
void DefaultConditionVariable::notify() { pthread_cond_broadcast(&cond_); }
Exception::Value DefaultConditionVariable::wait() {
pthread_cond_wait(&cond_, &(lock_->mutex_));
return Exception::NONE;
}
} // namespace nearby
} // namespace location
@@ -0,0 +1,30 @@
#ifndef PLATFORM_IMPL_DEFAULT_DEFAULT_CONDITION_VARIABLE_H_
#define PLATFORM_IMPL_DEFAULT_DEFAULT_CONDITION_VARIABLE_H_
#include <pthread.h>
#include "platform/api/condition_variable.h"
#include "platform/impl/default/default_lock.h"
#include "platform/ptr.h"
namespace location {
namespace nearby {
class DefaultConditionVariable : public ConditionVariable {
public:
explicit DefaultConditionVariable(Ptr<DefaultLock> lock);
~DefaultConditionVariable() override;
void notify() override;
Exception::Value wait() override;
private:
Ptr<DefaultLock> lock_;
pthread_condattr_t attr_;
pthread_cond_t cond_;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_DEFAULT_DEFAULT_CONDITION_VARIABLE_H_
+24
View File
@@ -0,0 +1,24 @@
#include "platform/impl/default/default_lock.h"
namespace location {
namespace nearby {
DefaultLock::DefaultLock() : attr_(), mutex_() {
pthread_mutexattr_init(&attr_);
pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex_, &attr_);
}
DefaultLock::~DefaultLock() {
pthread_mutex_destroy(&mutex_);
pthread_mutexattr_destroy(&attr_);
}
void DefaultLock::lock() { pthread_mutex_lock(&mutex_); }
void DefaultLock::unlock() { pthread_mutex_unlock(&mutex_); }
} // namespace nearby
} // namespace location
+29
View File
@@ -0,0 +1,29 @@
#ifndef PLATFORM_IMPL_DEFAULT_DEFAULT_LOCK_H_
#define PLATFORM_IMPL_DEFAULT_DEFAULT_LOCK_H_
#include <pthread.h>
#include "platform/api/lock.h"
namespace location {
namespace nearby {
class DefaultLock : public Lock {
public:
DefaultLock();
~DefaultLock() override;
void lock() override;
void unlock() override;
private:
friend class DefaultConditionVariable;
pthread_mutexattr_t attr_;
pthread_mutex_t mutex_;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_DEFAULT_DEFAULT_LOCK_H_
@@ -0,0 +1,17 @@
#include "platform/impl/default/default_platform.h"
#include "platform/impl/default/default_condition_variable.h"
#include "platform/impl/default/default_lock.h"
namespace location {
namespace nearby {
Ptr<Lock> DefaultPlatform::createLock() { return MakePtr(new DefaultLock()); }
Ptr<ConditionVariable> DefaultPlatform::createConditionVariable(
Ptr<Lock> lock) {
return MakePtr(new DefaultConditionVariable(DowncastPtr<DefaultLock>(lock)));
}
} // namespace nearby
} // namespace location
@@ -0,0 +1,26 @@
#ifndef PLATFORM_IMPL_DEFAULT_DEFAULT_PLATFORM_H_
#define PLATFORM_IMPL_DEFAULT_DEFAULT_PLATFORM_H_
#include "platform/api/condition_variable.h"
#include "platform/api/lock.h"
#include "platform/ptr.h"
namespace location {
namespace nearby {
// Provides obvious portable implementations of a subset of the hooks specified
// within //platform/api/.
//
// It's highly recommended that custom Platform implementations delegate to
// these methods unless there's a very good reason not to.
class DefaultPlatform {
public:
static Ptr<Lock> createLock();
static Ptr<ConditionVariable> createConditionVariable(Ptr<Lock> lock);
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_DEFAULT_DEFAULT_PLATFORM_H_