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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 v8::Handle<v8::Script> script = v8::Script::Compile( | 43 v8::Handle<v8::Script> script = v8::Script::Compile( |
44 v8::String::New("var count = 0; var obj = new Object(); count++;\n")); | 44 v8::String::New("var count = 0; var obj = new Object(); count++;\n")); |
45 | 45 |
46 script->Run(); | 46 script->Run(); |
47 | 47 |
48 v8::Locker::StopPreemption(); | 48 v8::Locker::StopPreemption(); |
49 v8::internal::OS::Sleep(500); // Make sure the timer fires. | 49 v8::internal::OS::Sleep(500); // Make sure the timer fires. |
50 | 50 |
51 script->Run(); | 51 script->Run(); |
52 } | 52 } |
| 53 |
| 54 |
| 55 enum Turn { |
| 56 FILL_CACHE, |
| 57 CLEAN_CACHE, |
| 58 SECOND_TIME_FILL_CACHE, |
| 59 DONE |
| 60 }; |
| 61 |
| 62 static Turn turn = FILL_CACHE; |
| 63 |
| 64 |
| 65 class ThreadA: public v8::internal::Thread { |
| 66 public: |
| 67 void Run() { |
| 68 v8::Locker locker; |
| 69 v8::HandleScope scope; |
| 70 v8::Context::Scope context_scope(v8::Context::New()); |
| 71 |
| 72 CHECK_EQ(FILL_CACHE, turn); |
| 73 |
| 74 // Fill String.search cache. |
| 75 v8::Handle<v8::Script> script = v8::Script::Compile( |
| 76 v8::String::New( |
| 77 "for (var i = 0; i < 3; i++) {" |
| 78 " var result = \"a\".search(\"a\");" |
| 79 " if (result != 0) throw \"result: \" + result + \" @\" + i;" |
| 80 "};" |
| 81 "true")); |
| 82 CHECK(script->Run()->IsTrue()); |
| 83 |
| 84 turn = CLEAN_CACHE; |
| 85 do { |
| 86 { |
| 87 v8::Unlocker unlocker; |
| 88 Thread::YieldCPU(); |
| 89 } |
| 90 } while (turn != SECOND_TIME_FILL_CACHE); |
| 91 |
| 92 // Rerun the script. |
| 93 CHECK(script->Run()->IsTrue()); |
| 94 |
| 95 turn = DONE; |
| 96 } |
| 97 }; |
| 98 |
| 99 |
| 100 class ThreadB: public v8::internal::Thread { |
| 101 public: |
| 102 void Run() { |
| 103 do { |
| 104 { |
| 105 v8::Locker locker; |
| 106 if (turn == CLEAN_CACHE) { |
| 107 v8::HandleScope scope; |
| 108 v8::Context::Scope context_scope(v8::Context::New()); |
| 109 |
| 110 // Clear the caches by forcing major GC. |
| 111 v8::internal::Heap::CollectAllGarbage(false); |
| 112 turn = SECOND_TIME_FILL_CACHE; |
| 113 break; |
| 114 } |
| 115 } |
| 116 |
| 117 Thread::YieldCPU(); |
| 118 } while (true); |
| 119 } |
| 120 }; |
| 121 |
| 122 |
| 123 TEST(JSFunctionResultCachesInTwoThreads) { |
| 124 v8::V8::Initialize(); |
| 125 |
| 126 ThreadA threadA; |
| 127 ThreadB threadB; |
| 128 |
| 129 threadA.Start(); |
| 130 threadB.Start(); |
| 131 |
| 132 threadA.Join(); |
| 133 threadB.Join(); |
| 134 |
| 135 CHECK_EQ(DONE, turn); |
| 136 } |
OLD | NEW |