Roll forward to cl/314747126

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: Ie19e006429b138b3768e97dae971a43fdc5ef8bf
This commit is contained in:
Alexey Polyudov
2020-06-04 13:50:45 -07:00
parent de31c27947
commit 4baa1ce96a
365 changed files with 28586 additions and 1503 deletions
-65
View File
@@ -1,65 +0,0 @@
# 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
)
@@ -1,40 +0,0 @@
// 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_
+41
View File
@@ -0,0 +1,41 @@
# 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 = "g3",
srcs = [
"atomic_reference_impl.h",
"platform.cc",
"settable_future_impl.h",
"system_clock_impl.h",
],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//core:__subpackages__",
"//platform:__subpackages__",
],
deps = [
"//platform:types",
"//platform/api",
"//platform/impl/shared:atomic_boolean",
"//platform/impl/shared:file",
"//platform/impl/shared:posix_condition_variable",
"//platform/impl/shared:posix_lock",
"//platform/port:string",
"//absl/base:core_headers",
"//absl/synchronization",
"//absl/time",
"//absl/types:any",
],
)
+42
View File
@@ -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.
add_library(platform_impl_g3 STATIC)
target_sources(platform_impl_g3
PRIVATE
"atomic_reference_impl.h"
"platform.cc"
"settable_future_impl.h"
"system_clock_impl.h"
)
target_include_directories(platform_impl_g3
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(platform_impl_g3
PUBLIC
"platform_types"
"platform_api"
"platform_impl_shared_atomic_boolean"
"platform_impl_shared_file"
"platform_impl_shared_posix_condition_variable"
"platform_impl_shared_posix_lock"
"platform_port_string"
"absl::base"
"absl::synchronization"
"absl::time"
)
@@ -0,0 +1,52 @@
// 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_G3_ATOMIC_REFERENCE_IMPL_H_
#define PLATFORM_IMPL_G3_ATOMIC_REFERENCE_IMPL_H_
#include "platform/api/atomic_reference.h"
#include "platform/ptr.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
// Provide implementation for absl::any.
class AtomicReferenceImpl : public AtomicReference<absl::any> {
public:
explicit AtomicReferenceImpl(absl::any initial_value)
: value_(std::move(initial_value)) {}
~AtomicReferenceImpl() override = default;
absl::any get() override {
absl::MutexLock lock(&mutex_);
return value_;
}
void set(absl::any value) override {
absl::MutexLock lock(&mutex_);
value_ = std::move(value);
}
private:
absl::Mutex mutex_;
absl::any value_;
};
} // namespace platform
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_G3_ATOMIC_REFERENCE_IMPL_H_
+167
View File
@@ -0,0 +1,167 @@
// 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/api/platform.h"
#include <atomic>
#include <memory>
#include "platform/api/atomic_boolean.h"
#include "platform/api/atomic_reference.h"
#include "platform/api/ble.h"
#include "platform/api/ble_v2.h"
#include "platform/api/bluetooth_adapter.h"
#include "platform/api/bluetooth_classic.h"
#include "platform/api/condition_variable.h"
#include "platform/api/count_down_latch.h"
#include "platform/api/hash_utils.h"
#include "platform/api/lock.h"
#include "platform/api/scheduled_executor.h"
#include "platform/api/server_sync.h"
#include "platform/api/settable_future.h"
#include "platform/api/submittable_executor.h"
#include "platform/api/system_clock.h"
#include "platform/api/thread_utils.h"
#include "platform/api/webrtc.h"
#include "platform/api/wifi.h"
#include "platform/impl/g3/atomic_reference_impl.h"
#include "platform/impl/g3/settable_future_impl.h"
#include "platform/impl/g3/system_clock_impl.h"
#include "platform/impl/shared/atomic_boolean_impl.h"
#include "platform/impl/shared/file_impl.h"
#include "platform/impl/shared/posix_condition_variable.h"
#include "platform/impl/shared/posix_lock.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
namespace location {
namespace nearby {
namespace platform {
namespace {
std::string getPayloadPath(std::int64_t payload_id) {
return "/tmp/" + std::to_string(payload_id);
}
} // namespace
Ptr<SubmittableExecutor> ImplementationPlatform::createSingleThreadExecutor() {
return Ptr<SubmittableExecutor>(/*new SingleThreadExecutorImpl()*/);
}
Ptr<SubmittableExecutor> ImplementationPlatform::createMultiThreadExecutor(
int max_concurrency) {
return Ptr<SubmittableExecutor>(/*new MultiThreadExecutorImpl()*/);
}
Ptr<ScheduledExecutor> ImplementationPlatform::createScheduledExecutor() {
return Ptr<ScheduledExecutor>(/*new ScheduledExecutorImpl()*/);
}
Ptr<AtomicReference<absl::any>>
ImplementationPlatform::createAtomicReferenceAny(absl::any initial_value) {
return Ptr<AtomicReference<absl::any>>(
new AtomicReferenceImpl(initial_value));
}
Ptr<SettableFuture<absl::any>>
ImplementationPlatform::createSettableFutureAny() {
return Ptr<SettableFuture<std::any>>(new SettableFutureImpl{});
}
Ptr<BluetoothAdapter> ImplementationPlatform::createBluetoothAdapter() {
return Ptr<BluetoothAdapter>{};
}
Ptr<WifiMedium> ImplementationPlatform::createWifiMedium() {
return Ptr<WifiMedium>();
}
Ptr<CountDownLatch> ImplementationPlatform::createCountDownLatch(
std::int32_t count) {
return Ptr<CountDownLatch>(/*new CountDownLatchImpl(count)*/);
}
Ptr<ThreadUtils> ImplementationPlatform::createThreadUtils() {
return Ptr<ThreadUtils>(/*new ThreadUtilsImpl()*/);
}
Ptr<SystemClock> ImplementationPlatform::createSystemClock() {
return Ptr<SystemClock>(new SystemClockImpl());
}
Ptr<AtomicBoolean> ImplementationPlatform::createAtomicBoolean(
bool initial_value) {
return Ptr<AtomicBoolean>(new AtomicBooleanImpl(initial_value));
}
Ptr<InputFile> ImplementationPlatform::createInputFile(
std::int64_t payload_id, std::int64_t total_size) {
return MakePtr(new InputFileImpl(getPayloadPath(payload_id), total_size));
}
Ptr<OutputFile> ImplementationPlatform::createOutputFile(
std::int64_t payload_id) {
return MakePtr(new OutputFileImpl(getPayloadPath(payload_id)));
}
Ptr<BluetoothClassicMedium>
ImplementationPlatform::createBluetoothClassicMedium() {
return Ptr<BluetoothClassicMedium>();
}
Ptr<BLEMedium> ImplementationPlatform::createBLEMedium() {
return Ptr<BLEMedium>();
}
Ptr<BLEMediumV2> ImplementationPlatform::createBLEMediumV2() {
return Ptr<BLEMediumV2>();
}
Ptr<ServerSyncMedium> ImplementationPlatform::createServerSyncMedium() {
return Ptr<ServerSyncMedium>(/*new ServerSyncMediumImpl()*/);
}
Ptr<WifiLanMedium> ImplementationPlatform::createWifiLanMedium() {
return Ptr<WifiLanMedium>();
}
//Ptr<WebRtcSignalingMessenger>
//ImplementationPlatform::createWebRtcSignalingMessenger(
// const std::string& self_id) {
// return Ptr<WebRtcSignalingMessenger>(/*new FCMSignalingMessenger()*/);
//}
Ptr<Lock> ImplementationPlatform::createLock() {
return Ptr<Lock>(new PosixLock());
}
Ptr<ConditionVariable> ImplementationPlatform::createConditionVariable(
Ptr<Lock> lock) {
return Ptr<ConditionVariable>(new PosixConditionVariable(lock));
}
Ptr<HashUtils> ImplementationPlatform::createHashUtils() {
return Ptr<HashUtils>(/*new HashUtilsImpl()*/);
}
std::string ImplementationPlatform::getDeviceId() {
// TODO(alexchau): Get deviceId from base
return "google3";
}
} // namespace platform
} // namespace nearby
} // namespace location
+108
View File
@@ -0,0 +1,108 @@
// 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_G3_SETTABLE_FUTURE_IMPL_H_
#define PLATFORM_IMPL_G3_SETTABLE_FUTURE_IMPL_H_
#include <utility>
#include "platform/api/platform.h"
#include "platform/api/settable_future.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
class SettableFutureImpl : public SettableFuture<absl::any> {
public:
explicit SettableFutureImpl() = default;
~SettableFutureImpl() override = default;
bool set(absl::any value) override {
absl::MutexLock lock(&mutex_);
if (!done_) {
value_ = std::move(value);
done_ = true;
exception_ = {Exception::kSuccess};
completed_.SignalAll();
}
return true;
}
bool setException(Exception exception) override {
absl::MutexLock lock(&mutex_);
return SetExceptionLocked(exception);
}
void addListener(Ptr<Runnable> runnable, Executor* executor) override {}
ExceptionOr<std::any> get() override {
absl::MutexLock lock(&mutex_);
while (!done_) {
completed_.Wait(&mutex_);
}
return exception_.value != Exception::kSuccess
? ExceptionOr<std::any>{exception_.value}
: ExceptionOr<std::any>{value_};
}
ExceptionOr<std::any> get(std::int64_t timeout_ms) override {
absl::MutexLock lock(&mutex_);
absl::Duration timeout = absl::Milliseconds(timeout_ms);
while (!done_) {
absl::Time start_time = absl::Now();
if (completed_.WaitWithTimeout(&mutex_, timeout)) {
SetExceptionLocked({Exception::kTimeout});
break;
}
absl::Duration spent = absl::Now() - start_time;
if (spent < timeout) {
timeout -= spent;
} else if (!done_) {
SetExceptionLocked({Exception::kTimeout});
break;
}
}
return exception_.value != Exception::kSuccess
? ExceptionOr<std::any>{exception_.value}
: ExceptionOr<std::any>{value_};
}
private:
bool SetExceptionLocked(Exception exception) {
if (!done_) {
exception_ = exception.value != Exception::kSuccess
? exception
: Exception{Exception::kFailed};
done_ = true;
completed_.SignalAll();
}
return true;
}
absl::Mutex mutex_;
absl::CondVar completed_;
bool done_{false};
absl::any value_;
Exception exception_{Exception::kFailed};
};
} // namespace platform
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_G3_SETTABLE_FUTURE_IMPL_H_
@@ -12,20 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "platform/impl/default/default_platform.h"
#ifndef PLATFORM_IMPL_G3_SYSTEM_CLOCK_IMPL_H_
#define PLATFORM_IMPL_G3_SYSTEM_CLOCK_IMPL_H_
#include "platform/impl/default/default_condition_variable.h"
#include "platform/impl/default/default_lock.h"
#include <cstdint>
#include "platform/api/system_clock.h"
#include "absl/time/clock.h"
#include "absl/time/time.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)));
}
class SystemClockImpl : public SystemClock {
public:
std::int64_t elapsedRealtime() override {
return absl::ToUnixMillis(absl::Now());
}
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_G3_SYSTEM_CLOCK_IMPL_H_
+7 -4
View File
@@ -13,12 +13,12 @@
# limitations under the License.
cc_library(
name = "sample",
name = "sample_platform",
srcs = [
"sample_wifi_medium.cc",
"sample_wifi_medium.h",
"atomic_reference_impl.h",
"sample_platform.cc",
"settable_future_impl.h",
],
hdrs = ["sample_platform.h"],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//core:__subpackages__",
@@ -28,7 +28,10 @@ cc_library(
"//platform:types",
"//platform:utils",
"//platform/api",
"//platform/impl/shared:file",
"//platform/impl/shared/sample:sample_wifi_medium",
"//platform/port:string",
"//absl/time",
"//absl/types:any",
],
)
+1
View File
@@ -26,6 +26,7 @@ target_link_libraries(platform_impl_sample
PUBLIC
absl::time
platform_api
platform_impl_shared_file
platform_port_string
platform_types
platform_utils
@@ -0,0 +1,39 @@
// 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_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
#define PLATFORM_IMPL_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
#include "platform/api/atomic_reference_def.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
// Provide implementation for absl::any.
class AtomicReferenceImpl : public AtomicReference<absl::any> {
public:
explicit AtomicReferenceImpl(absl::any initial_value) {}
~AtomicReferenceImpl() override = default;
absl::any get() override { return {}; }
void set(absl::any value) override {}
};
} // namespace platform
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
+152
View File
@@ -0,0 +1,152 @@
// 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 <cstdint>
#include "platform/api/atomic_boolean.h"
#include "platform/api/atomic_reference_def.h"
#include "platform/api/ble.h"
#include "platform/api/ble_v2.h"
#include "platform/api/bluetooth_adapter.h"
#include "platform/api/bluetooth_classic.h"
#include "platform/api/condition_variable.h"
#include "platform/api/count_down_latch.h"
#include "platform/api/hash_utils.h"
#include "platform/api/lock.h"
#include "platform/api/platform.h"
#include "platform/api/server_sync.h"
#include "platform/api/settable_future_def.h"
#include "platform/api/submittable_executor_def.h"
#include "platform/api/system_clock.h"
#include "platform/api/thread_utils.h"
#include "platform/api/wifi.h"
#include "platform/cancelable.h"
#include "platform/impl/sample/atomic_reference_impl.h"
#include "platform/impl/sample/settable_future_impl.h"
#include "platform/impl/shared/file_impl.h"
#include "platform/impl/shared/sample/sample_wifi_medium.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "platform/runnable.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
namespace {
std::string getPayloadPath(std::int64_t payload_id) {
return "/tmp/sample-" + std::to_string(payload_id);
}
} // namespace
Ptr<ScheduledExecutor> ImplementationPlatform::createScheduledExecutor() {
return Ptr<ScheduledExecutor>{};
}
Ptr<SubmittableExecutor> ImplementationPlatform::createSingleThreadExecutor() {
return Ptr<SubmittableExecutor>{};
}
Ptr<SubmittableExecutor> ImplementationPlatform::createMultiThreadExecutor(
int max_concurrency) {
return Ptr<SubmittableExecutor>{};
}
Ptr<AtomicReference<absl::any>>
ImplementationPlatform::createAtomicReferenceAny(absl::any initial_value) {
return Ptr<AtomicReference<absl::any>>(
new AtomicReferenceImpl(initial_value));
}
Ptr<SettableFuture<absl::any>>
ImplementationPlatform::createSettableFutureAny() {
return Ptr<SettableFuture<absl::any>>(new SettableFutureImpl{});
}
Ptr<BluetoothAdapter> ImplementationPlatform::createBluetoothAdapter() {
return Ptr<BluetoothAdapter>{};
}
Ptr<WifiMedium> ImplementationPlatform::createWifiMedium() {
return Ptr<WifiMedium>();
}
Ptr<CountDownLatch> ImplementationPlatform::createCountDownLatch(
std::int32_t count) {
return Ptr<CountDownLatch>{};
}
Ptr<ThreadUtils> ImplementationPlatform::createThreadUtils() {
return Ptr<ThreadUtils>{};
}
Ptr<SystemClock> ImplementationPlatform::createSystemClock() {
return Ptr<SystemClock>{};
}
Ptr<AtomicBoolean> ImplementationPlatform::createAtomicBoolean(
bool initial_value) {
return Ptr<AtomicBoolean>{};
}
Ptr<InputFile> ImplementationPlatform::createInputFile(
std::int64_t payload_id, std::int64_t total_size) {
return MakePtr(new InputFileImpl(getPayloadPath(payload_id), total_size));
}
Ptr<OutputFile> ImplementationPlatform::createOutputFile(
std::int64_t payload_id) {
return MakePtr(new OutputFileImpl(getPayloadPath(payload_id)));
}
Ptr<BluetoothClassicMedium>
ImplementationPlatform::createBluetoothClassicMedium() {
return Ptr<BluetoothClassicMedium>();
}
Ptr<BLEMedium> ImplementationPlatform::createBLEMedium() {
return Ptr<BLEMedium>();
}
Ptr<BLEMediumV2> ImplementationPlatform::createBLEMediumV2() {
return Ptr<BLEMediumV2>();
}
Ptr<ServerSyncMedium> ImplementationPlatform::createServerSyncMedium() {
return Ptr<ServerSyncMedium>{};
}
Ptr<WebRtcSignalingMessenger>
ImplementationPlatform::createWebRtcSignalingMessenger(
const std::string& self_id) {
return Ptr<WebRtcSignalingMessenger>{};
}
Ptr<Lock> ImplementationPlatform::createLock() { return Ptr<Lock>{}; }
Ptr<ConditionVariable> ImplementationPlatform::createConditionVariable(
Ptr<Lock> lock) {
return Ptr<ConditionVariable>{};
}
Ptr<HashUtils> ImplementationPlatform::createHashUtils() {
return Ptr<HashUtils>{};
}
std::string ImplementationPlatform::getDeviceId() { return "sample"; }
} // namespace platform
} // namespace nearby
} // namespace location
-155
View File
@@ -1,155 +0,0 @@
// 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_SAMPLE_SAMPLE_PLATFORM_H_
#define PLATFORM_IMPL_SAMPLE_SAMPLE_PLATFORM_H_
#include <cstdint>
#include "platform/api/atomic_boolean.h"
#include "platform/api/atomic_reference.h"
#include "platform/api/ble.h"
#include "platform/api/ble_v2.h"
#include "platform/api/bluetooth_adapter.h"
#include "platform/api/bluetooth_classic.h"
#include "platform/api/condition_variable.h"
#include "platform/api/count_down_latch.h"
#include "platform/api/hash_utils.h"
#include "platform/api/lock.h"
#include "platform/api/multi_thread_executor.h"
#include "platform/api/settable_future.h"
#include "platform/api/single_thread_executor.h"
#include "platform/api/system_clock.h"
#include "platform/api/thread_utils.h"
#include "platform/api/wifi.h"
#include "platform/cancelable.h"
#include "platform/impl/sample/sample_wifi_medium.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "platform/runnable.h"
namespace location {
namespace nearby {
namespace sample {
// The SamplePlatform class below shows an example of the factory functions
// and typedefs.
class SamplePlatform {
public:
class SampleSubmittableExecutor
: public SubmittableExecutor<SampleSubmittableExecutor> {
public:
template <typename T>
Ptr<Future<T> > submit(Ptr<Callable<T> > callable) {
return Ptr<Future<T> >();
}
};
class SampleSingleThreadExecutor
: public SingleThreadExecutor<SampleSubmittableExecutor> {
public:
void execute(Ptr<Runnable> runnable) override {}
void shutdown() override {}
};
class SampleMultiThreadExecutor
: public MultiThreadExecutor<SampleSubmittableExecutor> {
public:
void execute(Ptr<Runnable> runnable) override {}
void shutdown() override {}
};
class SampleScheduledExecutor {
public:
Ptr<Cancelable> schedule(Ptr<Runnable> runnable,
std::int64_t delay_millis) {
return Ptr<Cancelable>();
}
void shutdown() {}
};
typedef SampleSingleThreadExecutor SingleThreadExecutorType;
static Ptr<SingleThreadExecutorType> createSingleThreadExecutor() {
return MakePtr(new SingleThreadExecutorType());
}
typedef SampleMultiThreadExecutor MultiThreadExecutorType;
static Ptr<MultiThreadExecutorType> createMultiThreadExecutor(
std::int32_t max_concurrency) {
return MakePtr(new MultiThreadExecutorType());
}
typedef SampleScheduledExecutor ScheduledExecutorType;
static Ptr<ScheduledExecutorType> createScheduledExecutor() {
return MakePtr(new ScheduledExecutorType());
}
static Ptr<BluetoothAdapter> createBluetoothAdapter() {
return Ptr<BluetoothAdapter>();
}
static Ptr<WifiMedium> createWifiMedium() {
return MakePtr(new SampleWifiMedium());
}
static Ptr<CountDownLatch> createCountDownLatch(std::int32_t count) {
return Ptr<CountDownLatch>();
}
template <typename T>
static Ptr<SettableFuture<T> > createSettableFuture() {
return Ptr<SettableFuture<T> >();
}
static Ptr<ThreadUtils> createThreadUtils() { return Ptr<ThreadUtils>(); }
static Ptr<SystemClock> createSystemClock() { return Ptr<SystemClock>(); }
static Ptr<AtomicBoolean> createAtomicBoolean(bool initial_value) {
return Ptr<AtomicBoolean>();
}
template <typename T>
static Ptr<AtomicReference<T> > createAtomicReference(T initial_value) {
return Ptr<AtomicReference<T> >();
}
static Ptr<BluetoothClassicMedium> createBluetoothClassicMedium() {
return Ptr<BluetoothClassicMedium>();
}
static Ptr<BLEMedium> createBLEMedium() { return Ptr<BLEMedium>(); }
static Ptr<BLEMediumV2> createBLEMediumV2() { return Ptr<BLEMediumV2>(); }
static Ptr<Lock> createLock() { return Ptr<Lock>(); }
static Ptr<ConditionVariable> createConditionVariable(Ptr<Lock> lock) {
return Ptr<ConditionVariable>();
}
static Ptr<HashUtils> createHashUtils() { return Ptr<HashUtils>(); }
static std::string getDeviceId() { return ""; }
static std::string getPayloadPath(int64_t payload_id) {
return "/tmp/" + std::to_string(payload_id);
}
};
} // namespace sample
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SAMPLE_SAMPLE_PLATFORM_H_
@@ -0,0 +1,49 @@
// 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_SAMPLE_SETTABLE_FUTURE_IMPL_H_
#define PLATFORM_IMPL_SAMPLE_SETTABLE_FUTURE_IMPL_H_
#include "platform/api/settable_future_def.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
class SettableFutureImpl : public SettableFuture<absl::any> {
public:
explicit SettableFutureImpl() = default;
~SettableFutureImpl() override = default;
bool set(absl::any value) override { return true; }
bool setException(Exception exception) override { return true; }
void addListener(Ptr<Runnable> runnable, Executor* executor) override {}
ExceptionOr<std::any> get() override {
return ExceptionOr<std::any>{Exception{Exception::kFailed}};
}
ExceptionOr<std::any> get(std::int64_t timeout_ms) override {
return ExceptionOr<std::any>{Exception{Exception::kFailed}};
}
};
} // namespace platform
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SAMPLE_SETTABLE_FUTURE_IMPL_H_
+87
View File
@@ -0,0 +1,87 @@
# 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 = "posix_lock",
srcs = [
"posix_lock.cc",
],
hdrs = [
"posix_lock.h",
],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//platform/impl:__subpackages__",
],
deps = [
"//platform/api",
],
)
cc_library(
name = "posix_condition_variable",
srcs = [
"posix_condition_variable.cc",
],
hdrs = [
"posix_condition_variable.h",
],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//platform/impl:__subpackages__",
],
deps = [
":posix_lock",
"//platform:types",
"//platform/api:condition_variable",
],
)
cc_library(
name = "atomic_boolean",
hdrs = ["atomic_boolean_impl.h"],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//platform/impl:__subpackages__",
],
deps = ["//platform/api"],
)
cc_library(
name = "file",
srcs = ["file_impl.cc"],
hdrs = ["file_impl.h"],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//core:__subpackages__",
"//platform/impl:__subpackages__",
],
deps = [
"//platform:types",
"//platform/api",
],
)
cc_test(
name = "file_test",
timeout = "short",
srcs = [
"file_impl_test.cc",
],
deps = [
":file",
"//file/util:temp_path",
"//testing/base/public:gunit_main",
],
)
+101
View File
@@ -0,0 +1,101 @@
# 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_shared_posix_lock STATIC)
target_sources(platform_impl_shared_posix_lock
PRIVATE
"posix_lock.cc"
PUBLIC
"posix_lock.h"
)
target_include_directories(platform_impl_shared_posix_lock
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(platform_impl_shared_posix_lock
PUBLIC
platform_api
platform_types
)
add_library(platform_impl_shared_posix_condition_variable STATIC)
target_sources(platform_impl_shared_posix_condition_variable
PRIVATE
"posix_condition_variable.cc"
PUBLIC
"posix_condition_variable.h"
)
target_include_directories(platform_impl_shared_posix_condition_variable
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(platform_impl_shared_posix_condition_variable
PUBLIC
platform_api
platform_impl_shared_posix_lock
platform_types
absl::raw_logging_internal
)
add_library(platform_impl_shared_file STATIC)
target_sources(platform_impl_shared_file
PRIVATE
"file_impl.cc"
PUBLIC
"file_impl.h"
)
target_include_directories(platform_impl_shared_file
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(platform_impl_shared_file
PUBLIC
platform_api
platform_types
)
add_library(platform_impl_shared_atomic_boolean STATIC)
target_sources(platform_impl_shared_atomic_boolean
PUBLIC
"atomic_boolean_impl.h"
)
target_include_directories(platform_impl_shared_atomic_boolean
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(platform_impl_shared_atomic_boolean
PUBLIC
platform_api
platform_types
)
set_target_properties(platform_impl_shared_atomic_boolean
PROPERTIES
LINKER_LANGUAGE CXX
)
add_subdirectory(sample)
@@ -0,0 +1,47 @@
// 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_SHARED_ATOMIC_BOOLEAN_IMPL_H_
#define PLATFORM_IMPL_SHARED_ATOMIC_BOOLEAN_IMPL_H_
#include <atomic>
#include "platform/api/atomic_boolean.h"
namespace location {
namespace nearby {
class AtomicBooleanImpl : public AtomicBoolean {
public:
explicit AtomicBooleanImpl(bool initial_value) : value_(initial_value) {}
~AtomicBooleanImpl() override = default;
// AtomicBoolean:
bool get() override {
return value_.load();
}
// AtomicBoolean:
void set(bool value) override {
value_.store(value);
}
private:
std::atomic_bool value_;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SHARED_ATOMIC_BOOLEAN_IMPL_H_
+89
View File
@@ -0,0 +1,89 @@
// 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/shared/file_impl.h"
#include <cstddef>
#include <memory>
namespace location {
namespace nearby {
// InputFile
InputFileImpl::InputFileImpl(const std::string& path, std::int64_t size)
: file_(path), path_(path), total_size_(size) {}
ExceptionOr<ConstPtr<ByteArray>> InputFileImpl::read(int64_t size) {
if (!file_.is_open()) {
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
}
if (file_.peek() == EOF) {
return ExceptionOr<ConstPtr<ByteArray>>(ConstPtr<ByteArray>());
}
if (!file_.good()) {
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
}
std::unique_ptr<char[]> read_bytes {new char [size]};
file_.read(read_bytes.get(), static_cast<ptrdiff_t>(size));
auto num_bytes_read = file_.gcount();
if (num_bytes_read == 0) {
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
}
return ExceptionOr<ConstPtr<ByteArray>>(
MakeConstPtr(new ByteArray(read_bytes.get(), num_bytes_read)));
}
std::string InputFileImpl::getFilePath() const { return path_; }
std::int64_t InputFileImpl::getTotalSize() const { return total_size_; }
void InputFileImpl::close() {
if (file_.is_open()) {
file_.close();
}
}
// OutputFile
OutputFileImpl::OutputFileImpl(const std::string& path) : file_(path) {}
Exception::Value OutputFileImpl::write(ConstPtr<ByteArray> data) {
ScopedPtr<ConstPtr<ByteArray>> scoped_data(data);
if (!file_.is_open()) {
return Exception::IO;
}
if (!file_.good()) {
return Exception::IO;
}
file_.write(data->getData(), data->size());
file_.flush();
return file_.good() ? Exception::NONE : Exception::IO;
}
void OutputFileImpl::close() {
if (file_.is_open()) {
file_.close();
}
}
} // namespace nearby
} // namespace location
+60
View File
@@ -0,0 +1,60 @@
// 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_SHARED_FILE_IMPL_H_
#define PLATFORM_IMPL_SHARED_FILE_IMPL_H_
#include <cstdint>
#include <fstream>
#include "platform/api/input_file.h"
#include "platform/api/output_file.h"
#include "platform/exception.h"
#include "platform/ptr.h"
namespace location {
namespace nearby {
class InputFileImpl final : public InputFile {
public:
InputFileImpl(const std::string& path, std::int64_t size);
~InputFileImpl() override {}
ExceptionOr<ConstPtr<ByteArray>> read(std::int64_t size) override;
std::string getFilePath() const override;
std::int64_t getTotalSize() const override;
void close() override;
private:
std::ifstream file_;
const std::string path_;
const std::int64_t total_size_;
};
class OutputFileImpl final : public OutputFile {
public:
explicit OutputFileImpl(const std::string& path);
~OutputFileImpl() override {}
Exception::Value write(ConstPtr<ByteArray> data) override;
void close() override;
private:
std::ofstream file_;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SHARED_FILE_IMPL_H_
+147
View File
@@ -0,0 +1,147 @@
// 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/shared/file_impl.h"
#include <cstdio>
#include <cstring>
#include <fstream>
#include <memory>
#include <ostream>
#include <unistd.h>
#include "gtest/gtest.h"
namespace location {
namespace nearby {
class FileImplTest : public ::testing::Test {
protected:
void SetUp() override {
path_ = std::tmpnam(nullptr);;
std::ofstream output_file(path_);
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
}
void WriteToFile(const std::string& text) {
file_ << text;
file_.flush();
size_ += text.size();
}
size_t GetSize() const { return size_; }
void AssertEquals(const ExceptionOr<ConstPtr<ByteArray>>& bytes,
const std::string& expected) {
ASSERT_TRUE(bytes.ok());
ScopedPtr<ConstPtr<ByteArray>> byte_array(bytes.result());
ASSERT_STREQ(byte_array->getData(), expected.c_str());
ASSERT_EQ(byte_array->size(), expected.length());
}
void AssertNull(const ExceptionOr<ConstPtr<ByteArray>>& bytes) {
ASSERT_TRUE(bytes.ok());
ASSERT_TRUE(bytes.result().isNull());
}
static constexpr int64_t kMaxSize = 3;
std::string path_;
std::fstream file_;
size_t size_ = 0;
};
TEST_F(FileImplTest, InputFile_NonExistentPath) {
InputFileImpl input_file("/not/a/valid/path.txt", GetSize());
ExceptionOr<ConstPtr<ByteArray>> read_result = input_file.read(kMaxSize);
ASSERT_FALSE(read_result.ok());
ASSERT_EQ(read_result.exception(), Exception::IO);
}
TEST_F(FileImplTest, InputFile_GetFilePath) {
InputFileImpl input_file(path_, GetSize());
ASSERT_EQ(input_file.getFilePath(), path_);
}
TEST_F(FileImplTest, InputFile_EmptyFileEOF) {
InputFileImpl input_file(path_, GetSize());
AssertNull(input_file.read(kMaxSize));
}
TEST_F(FileImplTest, InputFile_ReadWorks) {
WriteToFile("abc");
InputFileImpl input_file(path_, GetSize());
auto read_data = input_file.read(kMaxSize);
read_data.result().destroy();
SUCCEED();
}
TEST_F(FileImplTest, InputFile_ReadUntilEOF) {
WriteToFile("abc");
InputFileImpl input_file(path_, GetSize());
AssertEquals(input_file.read(kMaxSize), "abc");
AssertNull(input_file.read(kMaxSize));
}
TEST_F(FileImplTest, InputFile_ReadWithSize) {
WriteToFile("abc");
InputFileImpl input_file(path_, GetSize());
AssertEquals(input_file.read(2), "ab");
AssertEquals(input_file.read(1), "c");
AssertNull(input_file.read(kMaxSize));
}
TEST_F(FileImplTest, InputFile_GetTotalSize) {
WriteToFile("abc");
InputFileImpl input_file(path_, GetSize());
EXPECT_EQ(input_file.getTotalSize(), 3);
AssertEquals(input_file.read(1), "a");
EXPECT_EQ(input_file.getTotalSize(), 3);
}
TEST_F(FileImplTest, InputFile_Close) {
WriteToFile("abc");
InputFileImpl input_file(path_, GetSize());
input_file.close();
ExceptionOr<ConstPtr<ByteArray>> read_result = input_file.read(kMaxSize);
ASSERT_FALSE(read_result.ok());
ASSERT_EQ(read_result.exception(), Exception::IO);
}
TEST_F(FileImplTest, OutputFile_NonExistentPath) {
OutputFileImpl output_file("/not/a/valid/path.txt");
ConstPtr<ByteArray> bytes = MakeConstPtr(new ByteArray("a", 1));
Exception::Value write_result = output_file.write(bytes);
ASSERT_EQ(write_result, Exception::IO);
}
TEST_F(FileImplTest, OutputFile_Write) {
OutputFileImpl output_file(path_);
ConstPtr<ByteArray> bytes1 = MakeConstPtr(new ByteArray("a", 1));
ConstPtr<ByteArray> bytes2 = MakeConstPtr(new ByteArray("bc", 2));
ASSERT_EQ(output_file.write(bytes1), Exception::NONE);
ASSERT_EQ(output_file.write(bytes2), Exception::NONE);
InputFileImpl input_file(path_, GetSize());
AssertEquals(input_file.read(kMaxSize), "abc");
}
TEST_F(FileImplTest, OutputFile_Close) {
OutputFileImpl output_file(path_);
output_file.close();
ConstPtr<ByteArray> bytes = MakeConstPtr(new ByteArray("a", 1));
ASSERT_EQ(output_file.write(bytes), Exception::IO);
}
} // namespace nearby
} // namespace location
@@ -12,30 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "platform/impl/default/default_condition_variable.h"
#include "platform/impl/shared/posix_condition_variable.h"
namespace location {
namespace nearby {
DefaultConditionVariable::DefaultConditionVariable(Ptr<DefaultLock> lock)
PosixConditionVariable::PosixConditionVariable(Ptr<PosixLock> lock)
: lock_(lock), attr_(), cond_() {
pthread_condattr_init(&attr_);
pthread_cond_init(&cond_, &attr_);
}
DefaultConditionVariable::~DefaultConditionVariable() {
PosixConditionVariable::~PosixConditionVariable() {
pthread_cond_destroy(&cond_);
pthread_condattr_destroy(&attr_);
}
void DefaultConditionVariable::notify() { pthread_cond_broadcast(&cond_); }
void PosixConditionVariable::notify() { pthread_cond_broadcast(&cond_); }
Exception::Value DefaultConditionVariable::wait() {
Exception::Value PosixConditionVariable::wait() {
pthread_cond_wait(&cond_, &(lock_->mutex_));
return Exception::NONE;
return Exception::kSuccess;
}
} // namespace nearby
@@ -12,28 +12,28 @@
// 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_
#ifndef PLATFORM_IMPL_SHARED_POSIX_CONDITION_VARIABLE_H_
#define PLATFORM_IMPL_SHARED_POSIX_CONDITION_VARIABLE_H_
#include <pthread.h>
#include "platform/api/condition_variable.h"
#include "platform/impl/default/default_lock.h"
#include "platform/impl/shared/posix_lock.h"
#include "platform/ptr.h"
namespace location {
namespace nearby {
class DefaultConditionVariable : public ConditionVariable {
class PosixConditionVariable : public ConditionVariable {
public:
explicit DefaultConditionVariable(Ptr<DefaultLock> lock);
~DefaultConditionVariable() override;
explicit PosixConditionVariable(Ptr<PosixLock> lock);
~PosixConditionVariable() override;
void notify() override;
Exception::Value wait() override;
private:
Ptr<DefaultLock> lock_;
Ptr<PosixLock> lock_;
pthread_condattr_t attr_;
pthread_cond_t cond_;
};
@@ -41,4 +41,4 @@ class DefaultConditionVariable : public ConditionVariable {
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_DEFAULT_DEFAULT_CONDITION_VARIABLE_H_
#endif // PLATFORM_IMPL_SHARED_POSIX_CONDITION_VARIABLE_H_
@@ -12,27 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "platform/impl/default/default_lock.h"
#include "platform/impl/shared/posix_lock.h"
namespace location {
namespace nearby {
DefaultLock::DefaultLock() : attr_(), mutex_() {
PosixLock::PosixLock() : attr_(), mutex_() {
pthread_mutexattr_init(&attr_);
pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex_, &attr_);
}
DefaultLock::~DefaultLock() {
PosixLock::~PosixLock() {
pthread_mutex_destroy(&mutex_);
pthread_mutexattr_destroy(&attr_);
}
void DefaultLock::lock() { pthread_mutex_lock(&mutex_); }
void PosixLock::lock() { pthread_mutex_lock(&mutex_); }
void DefaultLock::unlock() { pthread_mutex_unlock(&mutex_); }
void PosixLock::unlock() { pthread_mutex_unlock(&mutex_); }
} // namespace nearby
} // namespace location
@@ -12,8 +12,8 @@
// 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_
#ifndef PLATFORM_IMPL_SHARED_POSIX_LOCK_H_
#define PLATFORM_IMPL_SHARED_POSIX_LOCK_H_
#include <pthread.h>
@@ -22,16 +22,16 @@
namespace location {
namespace nearby {
class DefaultLock : public Lock {
class PosixLock : public Lock {
public:
DefaultLock();
~DefaultLock() override;
PosixLock();
~PosixLock() override;
void lock() override;
void unlock() override;
private:
friend class DefaultConditionVariable;
friend class PosixConditionVariable;
pthread_mutexattr_t attr_;
pthread_mutex_t mutex_;
@@ -40,4 +40,4 @@ class DefaultLock : public Lock {
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_DEFAULT_DEFAULT_LOCK_H_
#endif // PLATFORM_IMPL_SHARED_POSIX_LOCK_H_
@@ -13,47 +13,24 @@
# limitations under the License.
cc_library(
name = "default",
name = "sample_wifi_medium",
srcs = [
"default_platform.cc",
"sample_wifi_medium.cc",
],
hdrs = [
"default_condition_variable.h",
"default_lock.h",
"default_platform.h",
"sample_wifi_medium.h",
],
visibility = [
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
"//core:__subpackages__",
"//platform/impl:__subpackages__",
"//location/nearby/setup/core:__subpackages__",
],
deps = [
":condition_variable",
":lock",
"//platform:types",
"//platform:utils",
"//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",
"//platform/port:string",
"//absl/time",
],
)
@@ -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.
add_library(platform_impl_shared_sample STATIC)
target_sources(platform_impl_shared_sample
PRIVATE
sample_wifi_medium.cc
PUBLIC
sample_wifi_medium.h
)
target_link_libraries(platform_impl_shared_sample
PUBLIC
absl::time
platform_api
platform_port_string
platform_types
platform_utils
)
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "platform/impl/sample/sample_wifi_medium.h"
#include "platform/impl/shared/sample/sample_wifi_medium.h"
#include <cstdint>
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
#define PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
#ifndef PLATFORM_IMPL_SHARED_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
#define PLATFORM_IMPL_SHARED_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
#include "platform/api/wifi.h"
@@ -70,4 +70,4 @@ class SampleWifiMedium : public WifiMedium {
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
#endif // PLATFORM_IMPL_SHARED_SAMPLE_SAMPLE_WIFI_MEDIUM_H_