mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Initial public release of nearby library
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I85d490e448375aa4eb7b09680757c02fff1420b4
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
cc_library(
|
||||
name = "default",
|
||||
srcs = [
|
||||
"default_platform.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"default_condition_variable.h",
|
||||
"default_lock.h",
|
||||
"default_platform.h",
|
||||
],
|
||||
visibility = [
|
||||
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
|
||||
"//core:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":condition_variable",
|
||||
":lock",
|
||||
"//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 = [
|
||||
":lock",
|
||||
"//platform:types",
|
||||
"//platform/api:condition_variable",
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,65 @@
|
||||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
add_library(platform_impl_default STATIC)
|
||||
|
||||
target_sources(platform_impl_default
|
||||
PRIVATE
|
||||
default_platform.cc
|
||||
PUBLIC
|
||||
default_platform.h
|
||||
)
|
||||
|
||||
target_include_directories(platform_impl_default
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(platform_impl_default
|
||||
PUBLIC
|
||||
platform_api
|
||||
platform_impl_default_cond_var
|
||||
platform_impl_default_lock
|
||||
platform_types
|
||||
)
|
||||
|
||||
add_library(platform_impl_default_lock STATIC)
|
||||
|
||||
target_sources(platform_impl_default_lock
|
||||
PRIVATE
|
||||
default_lock.cc
|
||||
PUBLIC
|
||||
default_lock.h
|
||||
)
|
||||
|
||||
target_link_libraries(platform_impl_default_lock
|
||||
PUBLIC
|
||||
platform_api
|
||||
)
|
||||
|
||||
add_library(platform_impl_default_cond_var STATIC)
|
||||
|
||||
target_sources(platform_impl_default_cond_var
|
||||
PRIVATE
|
||||
default_condition_variable.cc
|
||||
PUBLIC
|
||||
default_condition_variable.h
|
||||
)
|
||||
|
||||
target_link_libraries(platform_impl_default_cond_var
|
||||
PUBLIC
|
||||
platform_api
|
||||
platform_impl_default_lock
|
||||
platform_types
|
||||
)
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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,44 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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_
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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,31 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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,40 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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_
|
||||
Reference in New Issue
Block a user