Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/assert.h" | 5 #include "vm/assert.h" |
| 6 #include "vm/message_queue.h" | 6 #include "vm/message_queue.h" |
| 7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
| 8 | 8 |
| 9 namespace dart { | 9 namespace dart { |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 // A thread which receives an expected sequence of messages. | 56 // A thread which receives an expected sequence of messages. |
| 57 static Monitor* sync = NULL; | 57 static Monitor* sync = NULL; |
| 58 static MessageQueue* shared_queue = NULL; | 58 static MessageQueue* shared_queue = NULL; |
| 59 void MessageReceiver_start(uword unused) { | 59 void MessageReceiver_start(uword unused) { |
| 60 // We only need an isolate here because the MonitorLocker in the | 60 // We only need an isolate here because the MonitorLocker in the |
| 61 // MessageQueue expects it. | 61 // MessageQueue expects it. |
| 62 Dart::CreateIsolate(NULL, NULL); | 62 Dart::CreateIsolate(NULL, NULL); |
| 63 | 63 |
| 64 // Create a message queue and share it. | 64 // Create a message queue and share it. |
| 65 MessageQueue* queue = new MessageQueue(); | 65 MessageQueue* queue = new MessageQueue(); |
| 66 MessageQueueTestPeer peer(queue); | |
| 66 shared_queue = queue; | 67 shared_queue = queue; |
| 67 | 68 |
| 68 // Tell the other thread to fill the queue a bit. | 69 // Tell the other thread that the shared queue is ready. |
| 69 { | 70 { |
| 70 MonitorLocker ml(sync); | 71 MonitorLocker ml(sync); |
| 71 ml.Notify(); | 72 ml.Notify(); |
| 72 } | 73 } |
|
siva
2011/10/18 22:19:59
The update to shared_queue could be under a lock:
| |
| 73 | 74 |
| 74 // Wait for the other thread to fill the queue a bit. | 75 // Wait for the other thread to fill the queue a bit. |
| 75 { | 76 while (!peer.HasMessage()) { |
| 76 MonitorLocker ml(sync); | 77 MonitorLocker ml(sync); |
| 77 ml.Wait(0); | 78 ml.Wait(5); |
| 78 } | 79 } |
|
siva
2011/10/18 22:19:59
The check for peer.HasMessage() could be under a l
| |
| 79 | 80 |
| 80 for (int i = 0; i < 3; i++) { | 81 for (int i = 0; i < 3; i++) { |
| 81 PortMessage* msg = queue->Dequeue(0); | 82 PortMessage* msg = queue->Dequeue(0); |
| 82 EXPECT(msg != NULL); | 83 EXPECT(msg != NULL); |
| 83 EXPECT_EQ(i+10, msg->dest_port()); | 84 EXPECT_EQ(i+10, msg->dest_port()); |
| 84 EXPECT_EQ(i+100, msg->reply_port()); | 85 EXPECT_EQ(i+100, msg->reply_port()); |
| 85 EXPECT_EQ(i+1000, *(reinterpret_cast<int*>(msg->data()))); | 86 EXPECT_EQ(i+1000, *(reinterpret_cast<int*>(msg->data()))); |
| 86 delete msg; | 87 delete msg; |
| 87 } | 88 } |
| 88 for (int i = 0; i < 3; i++) { | 89 for (int i = 0; i < 3; i++) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 99 } | 100 } |
| 100 | 101 |
| 101 | 102 |
| 102 TEST_CASE(MessageQueue_WaitNotify) { | 103 TEST_CASE(MessageQueue_WaitNotify) { |
| 103 sync = new Monitor(); | 104 sync = new Monitor(); |
| 104 | 105 |
| 105 Thread* thread = new Thread(MessageReceiver_start, 0); | 106 Thread* thread = new Thread(MessageReceiver_start, 0); |
| 106 EXPECT(thread != NULL); | 107 EXPECT(thread != NULL); |
| 107 | 108 |
| 108 // Wait for the shared queue to be created. | 109 // Wait for the shared queue to be created. |
| 109 { | 110 while (shared_queue == NULL) { |
| 110 MonitorLocker ml(sync); | 111 MonitorLocker ml(sync); |
| 111 ml.Wait(0); | 112 ml.Wait(5); |
| 112 } | 113 } |
|
siva
2011/10/18 22:19:59
The check for shared queue could be under a lock:
| |
| 113 ASSERT(shared_queue != NULL); | 114 ASSERT(shared_queue != NULL); |
| 114 | 115 |
| 115 // Pile up three messages before the other thread runs. | 116 // Pile up three messages before the other thread runs. |
| 116 for (int i = 0; i < 3; i++) { | 117 for (int i = 0; i < 3; i++) { |
| 117 int* data = reinterpret_cast<int*>(malloc(sizeof(*data))); | 118 int* data = reinterpret_cast<int*>(malloc(sizeof(*data))); |
| 118 *data = i+1000; | 119 *data = i+1000; |
| 119 PortMessage* msg = new PortMessage(i+10, i+100, data); | 120 PortMessage* msg = new PortMessage(i+10, i+100, data); |
| 120 shared_queue->Enqueue(msg); | 121 shared_queue->Enqueue(msg); |
| 121 } | 122 } |
| 122 | 123 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 219 |
| 219 EXPECT(!queue_peer.HasMessage()); | 220 EXPECT(!queue_peer.HasMessage()); |
| 220 queue.Flush(port1); | 221 queue.Flush(port1); |
| 221 | 222 |
| 222 // Queue is still empty. | 223 // Queue is still empty. |
| 223 EXPECT(!queue_peer.HasMessage()); | 224 EXPECT(!queue_peer.HasMessage()); |
| 224 } | 225 } |
| 225 | 226 |
| 226 | 227 |
| 227 } // namespace dart | 228 } // namespace dart |
| OLD | NEW |