OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "src/v8.h" | 28 #include "src/v8.h" |
29 #include "test/cctest/cctest.h" | 29 #include "test/cctest/cctest.h" |
30 | 30 |
31 #include "src/base/platform/platform.h" | 31 #include "src/base/platform/platform.h" |
32 #include "src/isolate.h" | 32 #include "src/isolate.h" |
33 | 33 |
34 | 34 |
35 enum Turn { FILL_CACHE, CLEAN_CACHE, SECOND_TIME_FILL_CACHE, CACHE_DONE }; | |
36 | |
37 static Turn turn = FILL_CACHE; | |
38 | |
39 | |
40 class ThreadA : public v8::base::Thread { | |
41 public: | |
42 ThreadA() : Thread(Options("ThreadA")) {} | |
43 void Run() { | |
44 v8::Isolate* isolate = CcTest::isolate(); | |
45 v8::Locker locker(isolate); | |
46 v8::Isolate::Scope isolate_scope(isolate); | |
47 v8::HandleScope scope(isolate); | |
48 v8::Handle<v8::Context> context = v8::Context::New(isolate); | |
49 v8::Context::Scope context_scope(context); | |
50 | |
51 CHECK_EQ(FILL_CACHE, turn); | |
52 | |
53 // Fill String.search cache. | |
54 v8::Handle<v8::Script> script = v8::Script::Compile( | |
55 v8::String::NewFromUtf8( | |
56 isolate, | |
57 "for (var i = 0; i < 3; i++) {" | |
58 " var result = \"a\".search(\"a\");" | |
59 " if (result != 0) throw \"result: \" + result + \" @\" + i;" | |
60 "};" | |
61 "true")); | |
62 CHECK(script->Run()->IsTrue()); | |
63 | |
64 turn = CLEAN_CACHE; | |
65 do { | |
66 { | |
67 v8::Unlocker unlocker(CcTest::isolate()); | |
68 } | |
69 } while (turn != SECOND_TIME_FILL_CACHE); | |
70 | |
71 // Rerun the script. | |
72 CHECK(script->Run()->IsTrue()); | |
73 | |
74 turn = CACHE_DONE; | |
75 } | |
76 }; | |
77 | |
78 | |
79 class ThreadB : public v8::base::Thread { | |
80 public: | |
81 ThreadB() : Thread(Options("ThreadB")) {} | |
82 void Run() { | |
83 do { | |
84 { | |
85 v8::Isolate* isolate = CcTest::isolate(); | |
86 v8::Locker locker(isolate); | |
87 v8::Isolate::Scope isolate_scope(isolate); | |
88 if (turn == CLEAN_CACHE) { | |
89 v8::HandleScope scope(isolate); | |
90 v8::Handle<v8::Context> context = v8::Context::New(isolate); | |
91 v8::Context::Scope context_scope(context); | |
92 | |
93 // Clear the caches by forcing major GC. | |
94 CcTest::heap()->CollectAllGarbage(); | |
95 turn = SECOND_TIME_FILL_CACHE; | |
96 break; | |
97 } | |
98 } | |
99 } while (true); | |
100 } | |
101 }; | |
102 | |
103 | |
104 TEST(JSFunctionResultCachesInTwoThreads) { | |
105 ThreadA threadA; | |
106 ThreadB threadB; | |
107 | |
108 threadA.Start(); | |
109 threadB.Start(); | |
110 | |
111 threadA.Join(); | |
112 threadB.Join(); | |
113 | |
114 CHECK_EQ(CACHE_DONE, turn); | |
115 } | |
116 | |
117 class ThreadIdValidationThread : public v8::base::Thread { | 35 class ThreadIdValidationThread : public v8::base::Thread { |
118 public: | 36 public: |
119 ThreadIdValidationThread(v8::base::Thread* thread_to_start, | 37 ThreadIdValidationThread(v8::base::Thread* thread_to_start, |
120 i::List<i::ThreadId>* refs, unsigned int thread_no, | 38 i::List<i::ThreadId>* refs, unsigned int thread_no, |
121 v8::base::Semaphore* semaphore) | 39 v8::base::Semaphore* semaphore) |
122 : Thread(Options("ThreadRefValidationThread")), | 40 : Thread(Options("ThreadRefValidationThread")), |
123 refs_(refs), | 41 refs_(refs), |
124 thread_no_(thread_no), | 42 thread_no_(thread_no), |
125 thread_to_start_(thread_to_start), | 43 thread_to_start_(thread_to_start), |
126 semaphore_(semaphore) {} | 44 semaphore_(semaphore) {} |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 refs.Add(i::ThreadId::Invalid()); | 78 refs.Add(i::ThreadId::Invalid()); |
161 } | 79 } |
162 prev->Start(); | 80 prev->Start(); |
163 for (int i = 0; i < kNThreads; i++) { | 81 for (int i = 0; i < kNThreads; i++) { |
164 semaphore.Wait(); | 82 semaphore.Wait(); |
165 } | 83 } |
166 for (int i = 0; i < kNThreads; i++) { | 84 for (int i = 0; i < kNThreads; i++) { |
167 delete threads[i]; | 85 delete threads[i]; |
168 } | 86 } |
169 } | 87 } |
OLD | NEW |