OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 19 matching lines...) Expand all Loading... |
30 #include "v8.h" | 30 #include "v8.h" |
31 | 31 |
32 #include "platform.h" | 32 #include "platform.h" |
33 #include "cctest.h" | 33 #include "cctest.h" |
34 | 34 |
35 | 35 |
36 using namespace ::v8::internal; | 36 using namespace ::v8::internal; |
37 | 37 |
38 | 38 |
39 class WaitAndSignalThread V8_FINAL : public Thread { | 39 class WaitAndSignalThread V8_FINAL : public Thread { |
40 public: | 40 public: |
41 explicit WaitAndSignalThread(Semaphore* semaphore) | 41 explicit WaitAndSignalThread(Semaphore* semaphore) |
42 : Thread("WaitAndSignalThread"), semaphore_(semaphore) {} | 42 : Thread("WaitAndSignalThread"), semaphore_(semaphore) {} |
43 virtual ~WaitAndSignalThread() {} | 43 virtual ~WaitAndSignalThread() {} |
44 | 44 |
45 virtual void Run() V8_OVERRIDE { | 45 virtual void Run() V8_OVERRIDE { |
46 for (int n = 0; n < 1000; ++n) { | 46 for (int n = 0; n < 1000; ++n) { |
47 semaphore_->Wait(); | 47 semaphore_->Wait(); |
48 bool result = semaphore_->WaitFor(TimeDelta::FromMicroseconds(1)); | 48 bool result = semaphore_->WaitFor(TimeDelta::FromMicroseconds(1)); |
49 ASSERT(!result); | 49 ASSERT(!result); |
50 USE(result); | 50 USE(result); |
51 semaphore_->Signal(); | 51 semaphore_->Signal(); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 private: | 55 private: |
56 Semaphore* semaphore_; | 56 Semaphore* semaphore_; |
57 }; | 57 }; |
58 | 58 |
59 | 59 |
60 TEST(WaitAndSignal) { | 60 TEST(WaitAndSignal) { |
61 Semaphore semaphore(0); | 61 Semaphore semaphore(0); |
62 WaitAndSignalThread t1(&semaphore); | 62 WaitAndSignalThread t1(&semaphore); |
63 WaitAndSignalThread t2(&semaphore); | 63 WaitAndSignalThread t2(&semaphore); |
64 | 64 |
65 t1.Start(); | 65 t1.Start(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 static const int kAlphabetSize = sizeof(alphabet) - 1; | 108 static const int kAlphabetSize = sizeof(alphabet) - 1; |
109 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 | 109 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 |
110 static char buffer[kBufferSize]; | 110 static char buffer[kBufferSize]; |
111 static const int kDataSize = kBufferSize * kAlphabetSize * 10; | 111 static const int kDataSize = kBufferSize * kAlphabetSize * 10; |
112 | 112 |
113 static Semaphore free_space(kBufferSize); | 113 static Semaphore free_space(kBufferSize); |
114 static Semaphore used_space(0); | 114 static Semaphore used_space(0); |
115 | 115 |
116 | 116 |
117 class ProducerThread V8_FINAL : public Thread { | 117 class ProducerThread V8_FINAL : public Thread { |
118 public: | 118 public: |
119 ProducerThread() : Thread("ProducerThread") {} | 119 ProducerThread() : Thread("ProducerThread") {} |
120 virtual ~ProducerThread() {} | 120 virtual ~ProducerThread() {} |
121 | 121 |
122 virtual void Run() V8_OVERRIDE { | 122 virtual void Run() V8_OVERRIDE { |
123 for (int n = 0; n < kDataSize; ++n) { | 123 for (int n = 0; n < kDataSize; ++n) { |
124 free_space.Wait(); | 124 free_space.Wait(); |
125 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize]; | 125 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize]; |
126 used_space.Signal(); | 126 used_space.Signal(); |
127 } | 127 } |
128 } | 128 } |
129 }; | 129 }; |
130 | 130 |
131 | 131 |
132 class ConsumerThread V8_FINAL : public Thread { | 132 class ConsumerThread V8_FINAL : public Thread { |
133 public: | 133 public: |
134 ConsumerThread() : Thread("ConsumerThread") {} | 134 ConsumerThread() : Thread("ConsumerThread") {} |
135 virtual ~ConsumerThread() {} | 135 virtual ~ConsumerThread() {} |
136 | 136 |
137 virtual void Run() V8_OVERRIDE { | 137 virtual void Run() V8_OVERRIDE { |
138 for (int n = 0; n < kDataSize; ++n) { | 138 for (int n = 0; n < kDataSize; ++n) { |
139 used_space.Wait(); | 139 used_space.Wait(); |
140 ASSERT_EQ(static_cast<int>(alphabet[n % kAlphabetSize]), | 140 ASSERT_EQ(static_cast<int>(alphabet[n % kAlphabetSize]), |
141 static_cast<int>(buffer[n % kBufferSize])); | 141 static_cast<int>(buffer[n % kBufferSize])); |
142 free_space.Signal(); | 142 free_space.Signal(); |
143 } | 143 } |
144 } | 144 } |
145 }; | 145 }; |
146 | 146 |
147 | 147 |
148 TEST(ProducerConsumer) { | 148 TEST(ProducerConsumer) { |
149 ProducerThread producer_thread; | 149 ProducerThread producer_thread; |
150 ConsumerThread consumer_thread; | 150 ConsumerThread consumer_thread; |
151 producer_thread.Start(); | 151 producer_thread.Start(); |
152 consumer_thread.Start(); | 152 consumer_thread.Start(); |
153 producer_thread.Join(); | 153 producer_thread.Join(); |
154 consumer_thread.Join(); | 154 consumer_thread.Join(); |
155 } | 155 } |
OLD | NEW |