OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "public/platform/WebThread.h" | 37 #include "public/platform/WebThread.h" |
38 #include "public/platform/WebTraceLocation.h" | 38 #include "public/platform/WebTraceLocation.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
40 #include "wtf/OwnPtr.h" | 40 #include "wtf/OwnPtr.h" |
41 #include "wtf/PassOwnPtr.h" | 41 #include "wtf/PassOwnPtr.h" |
42 | 42 |
43 namespace blink { | 43 namespace blink { |
44 | 44 |
45 static const size_t bufferSize = 16; | 45 static const size_t bufferSize = 16; |
46 | 46 |
47 static int lock = 0; | 47 static SpinLock lock; |
48 | 48 |
49 static void fillBuffer(volatile char* buffer, char fillPattern) | 49 static void fillBuffer(volatile char* buffer, char fillPattern) |
50 { | 50 { |
51 for (size_t i = 0; i < bufferSize; ++i) | 51 for (size_t i = 0; i < bufferSize; ++i) |
52 buffer[i] = fillPattern; | 52 buffer[i] = fillPattern; |
53 } | 53 } |
54 | 54 |
55 static void changeAndCheckBuffer(volatile char* buffer) | 55 static void changeAndCheckBuffer(volatile char* buffer) |
56 { | 56 { |
57 fillBuffer(buffer, '\0'); | 57 fillBuffer(buffer, '\0'); |
58 int total = 0; | 58 int total = 0; |
59 for (size_t i = 0; i < bufferSize; ++i) | 59 for (size_t i = 0; i < bufferSize; ++i) |
60 total += buffer[i]; | 60 total += buffer[i]; |
61 | 61 |
62 EXPECT_EQ(0, total); | 62 EXPECT_EQ(0, total); |
63 | 63 |
64 // This will mess with the other thread's calculation if we accidentally get | 64 // This will mess with the other thread's calculation if we accidentally get |
65 // concurrency. | 65 // concurrency. |
66 fillBuffer(buffer, '!'); | 66 fillBuffer(buffer, '!'); |
67 } | 67 } |
68 | 68 |
69 static void threadMain(volatile char* buffer) | 69 static void threadMain(volatile char* buffer) |
70 { | 70 { |
71 for (int i = 0; i < 500000; ++i) { | 71 for (int i = 0; i < 500000; ++i) { |
72 spinLockLock(&lock); | 72 SpinLock::Guard guard(lock); |
73 changeAndCheckBuffer(buffer); | 73 changeAndCheckBuffer(buffer); |
74 spinLockUnlock(&lock); | |
75 } | 74 } |
76 } | 75 } |
77 | 76 |
78 TEST(SpinLockTest, Torture) | 77 TEST(SpinLockTest, Torture) |
79 { | 78 { |
80 char sharedBuffer[bufferSize]; | 79 char sharedBuffer[bufferSize]; |
81 | 80 |
82 OwnPtr<WebThread> thread1 = adoptPtr(Platform::current()->createThread("thre
ad1")); | 81 OwnPtr<WebThread> thread1 = adoptPtr(Platform::current()->createThread("thre
ad1")); |
83 OwnPtr<WebThread> thread2 = adoptPtr(Platform::current()->createThread("thre
ad2")); | 82 OwnPtr<WebThread> thread2 = adoptPtr(Platform::current()->createThread("thre
ad2")); |
84 | 83 |
85 thread1->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&th
readMain, AllowCrossThreadAccess(static_cast<char*>(sharedBuffer))))); | 84 thread1->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&th
readMain, AllowCrossThreadAccess(static_cast<char*>(sharedBuffer))))); |
86 thread2->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&th
readMain, AllowCrossThreadAccess(static_cast<char*>(sharedBuffer))))); | 85 thread2->taskRunner()->postTask(BLINK_FROM_HERE, new Task(threadSafeBind(&th
readMain, AllowCrossThreadAccess(static_cast<char*>(sharedBuffer))))); |
87 | 86 |
88 thread1.clear(); | 87 thread1.clear(); |
89 thread2.clear(); | 88 thread2.clear(); |
90 } | 89 } |
91 | 90 |
92 } // namespace blink | 91 } // namespace blink |
OLD | NEW |