OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <pthread.h> |
| 6 #include <stdio.h> |
| 7 #include <string.h> |
| 8 #include <new> |
| 9 #include <vector> |
| 10 |
| 11 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 12 #include "native_client/src/shared/platform/nacl_check.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_var.h" |
| 14 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" |
| 15 #include "native_client/tests/ppapi_test_lib/test_interface.h" |
| 16 |
| 17 #include "ppapi/c/ppb_var.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const int kNumThreads = 100; |
| 22 const int kNumTriesPerThread = 100; |
| 23 |
| 24 const PPB_Var* var_interface = NULL; |
| 25 |
| 26 // Creates a bunch of new PP_Var(string), then deletes them. |
| 27 // This tests locking on the dictionary that looks up PP_Vars. |
| 28 void* StringCreateDeleteThreadFunc(void* thread_argument) { |
| 29 PP_Var* vars = new(std::nothrow) PP_Var[kNumTriesPerThread]; |
| 30 EXPECT(vars); |
| 31 static const char* kTestString = "A test string"; |
| 32 const uint32_t kTestStringLen = strlen(kTestString); |
| 33 for (int i = 0; i < kNumTriesPerThread; ++i) { |
| 34 vars[i] = |
| 35 var_interface->VarFromUtf8(pp_module(), kTestString, kTestStringLen); |
| 36 } |
| 37 for (int i = 0; i < kNumTriesPerThread; ++i) { |
| 38 var_interface->Release(vars[i]); |
| 39 } |
| 40 delete vars; |
| 41 return NULL; |
| 42 } |
| 43 |
| 44 // Tests creation and deletion. |
| 45 // Spawns kNumThreads threads, each of which creates kNumTriesPerThread PP_Vars |
| 46 // and then calls Release on each of them. |
| 47 void TestStringVarCreateDelete() { |
| 48 static pthread_t tids[kNumThreads]; |
| 49 for (int i = 0; i < kNumThreads; ++i) { |
| 50 EXPECT(pthread_create(&tids[i], |
| 51 NULL, |
| 52 StringCreateDeleteThreadFunc, |
| 53 NULL) == 0); |
| 54 } |
| 55 // Join the threads back before reporting success. |
| 56 for (int i = 0; i < kNumThreads; ++i) { |
| 57 EXPECT(pthread_join(tids[i], NULL) == 0); |
| 58 } |
| 59 TEST_PASSED; |
| 60 } |
| 61 |
| 62 // Creates a bunch of copies of a single PP_Var(string), then deletes them. |
| 63 // This tests locked reference counting. |
| 64 void* StringVarRefCountThreadFunc(void* thread_argument) { |
| 65 PP_Var* test_var = reinterpret_cast<PP_Var*>(thread_argument); |
| 66 // Run the ref count up. |
| 67 for (int i = 0; i < kNumTriesPerThread; ++i) { |
| 68 var_interface->AddRef(*test_var); |
| 69 } |
| 70 // Run the ref count back down. |
| 71 for (int i = 0; i < kNumTriesPerThread; ++i) { |
| 72 var_interface->Release(*test_var); |
| 73 } |
| 74 // Run the ref count up and down in rapid succession. |
| 75 for (int i = 0; i < kNumTriesPerThread; ++i) { |
| 76 var_interface->AddRef(*test_var); |
| 77 var_interface->Release(*test_var); |
| 78 } |
| 79 return NULL; |
| 80 } |
| 81 |
| 82 // Tests reference count atomicity. |
| 83 // Creates one variable and spawns kNumThreads, each of which does: |
| 84 // 1) kNumTriesPerThread AddRefs |
| 85 // 2) kNumTriesPerThread Releases |
| 86 // 3) kNumTriesPerThread (AddRef, Release) pairs |
| 87 void TestStringVarRefCount() { |
| 88 pthread_t tid[kNumThreads]; |
| 89 static const char* kTestString = "A test string"; |
| 90 const uint32_t kTestStringLen = strlen(kTestString); |
| 91 PP_Var test_var = |
| 92 var_interface->VarFromUtf8(pp_module(), kTestString, kTestStringLen); |
| 93 for (int i = 0; i < kNumThreads; ++i) { |
| 94 EXPECT(pthread_create(&tid[i], |
| 95 NULL, |
| 96 StringVarRefCountThreadFunc, |
| 97 &test_var) == 0); |
| 98 } |
| 99 // Join the threads back before reporting success. |
| 100 for (int i = 0; i < kNumThreads; ++i) { |
| 101 EXPECT(pthread_join(tid[i], NULL) == 0); |
| 102 } |
| 103 var_interface->Release(test_var); |
| 104 TEST_PASSED; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 void SetupTests() { |
| 110 var_interface = PPBVar(); |
| 111 EXPECT(var_interface != NULL); |
| 112 RegisterTest("TestStringVarCreateDelete", TestStringVarCreateDelete); |
| 113 RegisterTest("TestStringVarRefCount", TestStringVarRefCount); |
| 114 } |
| 115 |
| 116 void SetupPluginInterfaces() { |
| 117 // none |
| 118 } |
OLD | NEW |