Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: test/cctest/test-debug.cc

Issue 23748003: Cleanup Semaphore class. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Build fix for Mac OS X. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/test-circular-queue.cc ('k') | test/cctest/test-lock.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 4661 matching lines...) Expand 10 before | Expand all | Expand 10 after
4672 // be used once. 4672 // be used once.
4673 class ThreadBarrier { 4673 class ThreadBarrier {
4674 public: 4674 public:
4675 explicit ThreadBarrier(int num_threads); 4675 explicit ThreadBarrier(int num_threads);
4676 ~ThreadBarrier(); 4676 ~ThreadBarrier();
4677 void Wait(); 4677 void Wait();
4678 private: 4678 private:
4679 int num_threads_; 4679 int num_threads_;
4680 int num_blocked_; 4680 int num_blocked_;
4681 v8::internal::Mutex lock_; 4681 v8::internal::Mutex lock_;
4682 v8::internal::Semaphore* sem_; 4682 v8::internal::Semaphore sem_;
4683 bool invalid_; 4683 bool invalid_;
4684 }; 4684 };
4685 4685
4686 ThreadBarrier::ThreadBarrier(int num_threads) 4686 ThreadBarrier::ThreadBarrier(int num_threads)
4687 : num_threads_(num_threads), num_blocked_(0) { 4687 : num_threads_(num_threads), num_blocked_(0), sem_(0) {
4688 sem_ = OS::CreateSemaphore(0);
4689 invalid_ = false; // A barrier may only be used once. Then it is invalid. 4688 invalid_ = false; // A barrier may only be used once. Then it is invalid.
4690 } 4689 }
4691 4690
4692 4691
4693 // Do not call, due to race condition with Wait(). 4692 // Do not call, due to race condition with Wait().
4694 // Could be resolved with Pthread condition variables. 4693 // Could be resolved with Pthread condition variables.
4695 ThreadBarrier::~ThreadBarrier() { 4694 ThreadBarrier::~ThreadBarrier() {
4696 delete sem_;
4697 } 4695 }
4698 4696
4699 4697
4700 void ThreadBarrier::Wait() { 4698 void ThreadBarrier::Wait() {
4701 lock_.Lock(); 4699 lock_.Lock();
4702 CHECK(!invalid_); 4700 CHECK(!invalid_);
4703 if (num_blocked_ == num_threads_ - 1) { 4701 if (num_blocked_ == num_threads_ - 1) {
4704 // Signal and unblock all waiting threads. 4702 // Signal and unblock all waiting threads.
4705 for (int i = 0; i < num_threads_ - 1; ++i) { 4703 for (int i = 0; i < num_threads_ - 1; ++i) {
4706 sem_->Signal(); 4704 sem_.Signal();
4707 } 4705 }
4708 invalid_ = true; 4706 invalid_ = true;
4709 printf("BARRIER\n\n"); 4707 printf("BARRIER\n\n");
4710 fflush(stdout); 4708 fflush(stdout);
4711 lock_.Unlock(); 4709 lock_.Unlock();
4712 } else { // Wait for the semaphore. 4710 } else { // Wait for the semaphore.
4713 ++num_blocked_; 4711 ++num_blocked_;
4714 lock_.Unlock(); // Potential race condition with destructor because 4712 lock_.Unlock(); // Potential race condition with destructor because
4715 sem_->Wait(); // these two lines are not atomic. 4713 sem_.Wait(); // these two lines are not atomic.
4716 } 4714 }
4717 } 4715 }
4718 4716
4719 4717
4720 // A set containing enough barriers and semaphores for any of the tests. 4718 // A set containing enough barriers and semaphores for any of the tests.
4721 class Barriers { 4719 class Barriers {
4722 public: 4720 public:
4723 Barriers(); 4721 Barriers();
4724 void Initialize(); 4722 void Initialize();
4725 ThreadBarrier barrier_1; 4723 ThreadBarrier barrier_1;
4726 ThreadBarrier barrier_2; 4724 ThreadBarrier barrier_2;
4727 ThreadBarrier barrier_3; 4725 ThreadBarrier barrier_3;
4728 ThreadBarrier barrier_4; 4726 ThreadBarrier barrier_4;
4729 ThreadBarrier barrier_5; 4727 ThreadBarrier barrier_5;
4730 v8::internal::Semaphore* semaphore_1; 4728 v8::internal::Semaphore* semaphore_1;
4731 v8::internal::Semaphore* semaphore_2; 4729 v8::internal::Semaphore* semaphore_2;
4732 }; 4730 };
4733 4731
4734 Barriers::Barriers() : barrier_1(2), barrier_2(2), 4732 Barriers::Barriers() : barrier_1(2), barrier_2(2),
4735 barrier_3(2), barrier_4(2), barrier_5(2) {} 4733 barrier_3(2), barrier_4(2), barrier_5(2) {}
4736 4734
4737 void Barriers::Initialize() { 4735 void Barriers::Initialize() {
4738 semaphore_1 = OS::CreateSemaphore(0); 4736 semaphore_1 = new v8::internal::Semaphore(0);
4739 semaphore_2 = OS::CreateSemaphore(0); 4737 semaphore_2 = new v8::internal::Semaphore(0);
4740 } 4738 }
4741 4739
4742 4740
4743 // We match parts of the message to decide if it is a break message. 4741 // We match parts of the message to decide if it is a break message.
4744 bool IsBreakEventMessage(char *message) { 4742 bool IsBreakEventMessage(char *message) {
4745 const char* type_event = "\"type\":\"event\""; 4743 const char* type_event = "\"type\":\"event\"";
4746 const char* event_break = "\"event\":\"break\""; 4744 const char* event_break = "\"event\":\"break\"";
4747 // Does the message contain both type:event and event:break? 4745 // Does the message contain both type:event and event:break?
4748 return strstr(message, type_event) != NULL && 4746 return strstr(message, type_event) != NULL &&
4749 strstr(message, event_break) != NULL; 4747 strstr(message, event_break) != NULL;
(...skipping 1233 matching lines...) Expand 10 before | Expand all | Expand 10 after
5983 } 5981 }
5984 5982
5985 5983
5986 class DebuggerAgentProtocolServerThread : public i::Thread { 5984 class DebuggerAgentProtocolServerThread : public i::Thread {
5987 public: 5985 public:
5988 explicit DebuggerAgentProtocolServerThread(int port) 5986 explicit DebuggerAgentProtocolServerThread(int port)
5989 : Thread("DebuggerAgentProtocolServerThread"), 5987 : Thread("DebuggerAgentProtocolServerThread"),
5990 port_(port), 5988 port_(port),
5991 server_(NULL), 5989 server_(NULL),
5992 client_(NULL), 5990 client_(NULL),
5993 listening_(OS::CreateSemaphore(0)) { 5991 listening_(0) {
5994 } 5992 }
5995 ~DebuggerAgentProtocolServerThread() { 5993 ~DebuggerAgentProtocolServerThread() {
5996 // Close both sockets. 5994 // Close both sockets.
5997 delete client_; 5995 delete client_;
5998 delete server_; 5996 delete server_;
5999 delete listening_;
6000 } 5997 }
6001 5998
6002 void Run(); 5999 void Run();
6003 void WaitForListening() { listening_->Wait(); } 6000 void WaitForListening() { listening_.Wait(); }
6004 char* body() { return *body_; } 6001 char* body() { return *body_; }
6005 6002
6006 private: 6003 private:
6007 int port_; 6004 int port_;
6008 i::SmartArrayPointer<char> body_; 6005 i::SmartArrayPointer<char> body_;
6009 i::Socket* server_; // Server socket used for bind/accept. 6006 i::Socket* server_; // Server socket used for bind/accept.
6010 i::Socket* client_; // Single client connection used by the test. 6007 i::Socket* client_; // Single client connection used by the test.
6011 i::Semaphore* listening_; // Signalled when the server is in listen mode. 6008 i::Semaphore listening_; // Signalled when the server is in listen mode.
6012 }; 6009 };
6013 6010
6014 6011
6015 void DebuggerAgentProtocolServerThread::Run() { 6012 void DebuggerAgentProtocolServerThread::Run() {
6016 bool ok; 6013 bool ok;
6017 6014
6018 // Create the server socket and bind it to the requested port. 6015 // Create the server socket and bind it to the requested port.
6019 server_ = i::OS::CreateSocket(); 6016 server_ = i::OS::CreateSocket();
6020 CHECK(server_ != NULL); 6017 CHECK(server_ != NULL);
6021 ok = server_->Bind(port_); 6018 ok = server_->Bind(port_);
6022 CHECK(ok); 6019 CHECK(ok);
6023 6020
6024 // Listen for new connections. 6021 // Listen for new connections.
6025 ok = server_->Listen(1); 6022 ok = server_->Listen(1);
6026 CHECK(ok); 6023 CHECK(ok);
6027 listening_->Signal(); 6024 listening_.Signal();
6028 6025
6029 // Accept a connection. 6026 // Accept a connection.
6030 client_ = server_->Accept(); 6027 client_ = server_->Accept();
6031 CHECK(client_ != NULL); 6028 CHECK(client_ != NULL);
6032 6029
6033 // Receive a debugger agent protocol message. 6030 // Receive a debugger agent protocol message.
6034 i::DebuggerAgentUtil::ReceiveMessage(client_); 6031 i::DebuggerAgentUtil::ReceiveMessage(client_);
6035 } 6032 }
6036 6033
6037 6034
(...skipping 1512 matching lines...) Expand 10 before | Expand all | Expand 10 after
7550 TEST(LiveEditDisabled) { 7547 TEST(LiveEditDisabled) {
7551 v8::internal::FLAG_allow_natives_syntax = true; 7548 v8::internal::FLAG_allow_natives_syntax = true;
7552 LocalContext env; 7549 LocalContext env;
7553 v8::HandleScope scope(env->GetIsolate()); 7550 v8::HandleScope scope(env->GetIsolate());
7554 v8::Debug::SetLiveEditEnabled(false); 7551 v8::Debug::SetLiveEditEnabled(false);
7555 CompileRun("%LiveEditCompareStrings('', '')"); 7552 CompileRun("%LiveEditCompareStrings('', '')");
7556 } 7553 }
7557 7554
7558 7555
7559 #endif // ENABLE_DEBUGGER_SUPPORT 7556 #endif // ENABLE_DEBUGGER_SUPPORT
OLDNEW
« no previous file with comments | « test/cctest/test-circular-queue.cc ('k') | test/cctest/test-lock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698