-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add support for pthread_mutex_init, pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock and pthread_mutex_trylock. b/302335657 Change-Id: I8774ddb8ad466b7342e045402b170a719f2e1a81
- Loading branch information
Showing
20 changed files
with
738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
starboard/nplb/posix_compliance/posix_mutex_acquire_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2015 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
#if SB_API_VERSION >= 16 | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/configuration.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
struct TestContext { | ||
TestContext() : count(0) {} | ||
pthread_mutex_t mutex; | ||
int count; | ||
}; | ||
|
||
const int kLoops = 10000; | ||
|
||
void* EntryPoint(void* parameter) { | ||
TestContext* context = static_cast<TestContext*>(parameter); | ||
|
||
for (int i = 0; i < kLoops; ++i) { | ||
pthread_mutex_lock(&context->mutex); | ||
context->count++; | ||
pthread_mutex_unlock(&context->mutex); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
// This test just tries to acquire a mutex repeatedly while other threads are | ||
// doing the same. | ||
TEST(PosixMutexAcquireTest, SunnyDayContended) { | ||
TestContext context; | ||
EXPECT_EQ(pthread_mutex_init(&context.mutex, NULL), 0); | ||
const int kThreads = 4; | ||
SbThread threads[kThreads]; | ||
for (int i = 0; i < kThreads; ++i) { | ||
threads[i] = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, | ||
true, NULL, EntryPoint, &context); | ||
} | ||
|
||
for (int i = 0; i < kLoops; ++i) { | ||
for (int j = 0; j < kThreads; ++j) { | ||
EXPECT_EQ(pthread_mutex_lock(&context.mutex), 0); | ||
int k = context.count; | ||
k = k - 1; | ||
context.count = k; | ||
EXPECT_EQ(pthread_mutex_unlock(&context.mutex), 0); | ||
} | ||
} | ||
|
||
// Join other threads and clean up. | ||
for (int i = 0; i < kThreads; ++i) { | ||
EXPECT_TRUE(SbThreadJoin(threads[i], NULL)); | ||
} | ||
EXPECT_EQ(pthread_mutex_destroy(&context.mutex), 0); | ||
EXPECT_EQ(0, context.count); | ||
} | ||
|
||
TEST(PosixMutexAcquireTest, SunnyDayUncontended) { | ||
pthread_mutex_t mutex; | ||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
|
||
EXPECT_EQ(pthread_mutex_lock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
TEST(PosixMutexAcquireTest, SunnyDayStaticallyInitialized) { | ||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
EXPECT_EQ(pthread_mutex_lock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard | ||
|
||
#endif // SB_API_VERSION >= 16 |
81 changes: 81 additions & 0 deletions
81
starboard/nplb/posix_compliance/posix_mutex_acquire_try_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2015 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
#if SB_API_VERSION >= 16 | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/configuration.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
#include "starboard/nplb/thread_helpers.h" | ||
#include "starboard/thread.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
struct TestContext { | ||
explicit TestContext(pthread_mutex_t* mutex) | ||
: was_locked_(false), mutex_(mutex) {} | ||
bool was_locked_; | ||
pthread_mutex_t* mutex_; | ||
}; | ||
|
||
void* EntryPoint(void* parameter) { | ||
TestContext* context = static_cast<TestContext*>(parameter); | ||
context->was_locked_ = (pthread_mutex_trylock(context->mutex_) == 0); | ||
return NULL; | ||
} | ||
|
||
TEST(PosixMutexAcquireTryTest, SunnyDayUncontended) { | ||
pthread_mutex_t mutex; | ||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
|
||
EXPECT_EQ(pthread_mutex_trylock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
TEST(PosixMutexAcquireTest, SunnyDayAutoInit) { | ||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
EXPECT_EQ(pthread_mutex_trylock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
TEST(PosixMutexAcquireTryTest, RainyDayReentrant) { | ||
pthread_mutex_t mutex; | ||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
|
||
EXPECT_EQ(pthread_mutex_trylock(&mutex), 0); | ||
|
||
TestContext context(&mutex); | ||
// TODO: Migrate to pthread_create when available. | ||
SbThread thread = | ||
SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true, | ||
nplb::kThreadName, &EntryPoint, &context); | ||
|
||
EXPECT_TRUE(SbThreadIsValid(thread)); | ||
EXPECT_TRUE(SbThreadJoin(thread, NULL)); | ||
EXPECT_FALSE(context.was_locked_); | ||
EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard | ||
|
||
#endif // SB_API_VERSION >= 16 |
65 changes: 65 additions & 0 deletions
65
starboard/nplb/posix_compliance/posix_mutex_create_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2015 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
#if SB_API_VERSION >= 16 | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/configuration.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
const int kALot = 128 * 1024; | ||
const int kABunch = 2 * 1024; | ||
|
||
TEST(PosixMutexCreateTest, SunnyDay) { | ||
pthread_mutex_t mutex; | ||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
TEST(PosixMutexCreateTest, SunnyDayAutoInit) { | ||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
TEST(PosixMutexCreateTest, SunnyDayALot) { | ||
for (int i = 0; i < kALot; ++i) { | ||
pthread_mutex_t mutex; | ||
EXPECT_EQ(pthread_mutex_init(&mutex, NULL), 0); | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
} | ||
|
||
TEST(PosixMutexCreateTest, SunnyDayABunchAtOnce) { | ||
pthread_mutex_t mutexes[kABunch]; | ||
for (int i = 0; i < kABunch; ++i) { | ||
EXPECT_EQ(pthread_mutex_init(&mutexes[i], NULL), 0); | ||
} | ||
|
||
for (int i = 0; i < kABunch; ++i) { | ||
EXPECT_EQ(pthread_mutex_destroy(&mutexes[i]), 0); | ||
} | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard | ||
|
||
#endif // SB_API_VERSION >= 16 |
35 changes: 35 additions & 0 deletions
35
starboard/nplb/posix_compliance/posix_mutex_destroy_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2015 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
#if SB_API_VERSION >= 16 | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/configuration.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace starboard { | ||
namespace nplb { | ||
namespace { | ||
|
||
TEST(PosixMutexDestroyTest, SunnyDayAutoInit) { | ||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); | ||
} | ||
|
||
} // namespace | ||
} // namespace nplb | ||
} // namespace starboard | ||
|
||
#endif // SB_API_VERSION >= 16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.