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

Side by Side Diff: src/platform.h

Issue 21087012: Simplify implementation of Mutex. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix unit test. Created 7 years, 4 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 | « src/optimizing-compiler-thread.cc ('k') | src/platform-cygwin.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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 #endif // _MSC_VER 94 #endif // _MSC_VER
95 95
96 #ifndef __CYGWIN__ 96 #ifndef __CYGWIN__
97 // Random is missing on both Visual Studio and MinGW. 97 // Random is missing on both Visual Studio and MinGW.
98 int random(); 98 int random();
99 #endif 99 #endif
100 100
101 #endif // WIN32 101 #endif // WIN32
102 102
103 #include "lazy-instance.h" 103 #include "mutex.h"
104 #include "utils.h" 104 #include "utils.h"
105 #include "v8globals.h" 105 #include "v8globals.h"
106 106
107 namespace v8 { 107 namespace v8 {
108 namespace internal { 108 namespace internal {
109 109
110 class Semaphore; 110 class Semaphore;
111 class Mutex; 111 class Mutex;
112 112
113 double ceiling(double x); 113 double ceiling(double x);
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 static const int kStackWalkError = -1; 302 static const int kStackWalkError = -1;
303 static const int kStackWalkMaxNameLen = 256; 303 static const int kStackWalkMaxNameLen = 256;
304 static const int kStackWalkMaxTextLen = 256; 304 static const int kStackWalkMaxTextLen = 256;
305 struct StackFrame { 305 struct StackFrame {
306 void* address; 306 void* address;
307 char text[kStackWalkMaxTextLen]; 307 char text[kStackWalkMaxTextLen];
308 }; 308 };
309 309
310 static int StackWalk(Vector<StackFrame> frames); 310 static int StackWalk(Vector<StackFrame> frames);
311 311
312 // Factory method for creating platform dependent Mutex.
313 // Please use delete to reclaim the storage for the returned Mutex.
314 static Mutex* CreateMutex();
315
316 // Factory method for creating platform dependent Semaphore. 312 // Factory method for creating platform dependent Semaphore.
317 // Please use delete to reclaim the storage for the returned Semaphore. 313 // Please use delete to reclaim the storage for the returned Semaphore.
318 static Semaphore* CreateSemaphore(int count); 314 static Semaphore* CreateSemaphore(int count);
319 315
320 // Factory method for creating platform dependent Socket. 316 // Factory method for creating platform dependent Socket.
321 // Please use delete to reclaim the storage for the returned Socket. 317 // Please use delete to reclaim the storage for the returned Socket.
322 static Socket* CreateSocket(); 318 static Socket* CreateSocket();
323 319
324 class MemoryMappedFile { 320 class MemoryMappedFile {
325 public: 321 public:
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 705
710 char name_[kMaxThreadNameLength]; 706 char name_[kMaxThreadNameLength];
711 int stack_size_; 707 int stack_size_;
712 Semaphore* start_semaphore_; 708 Semaphore* start_semaphore_;
713 709
714 DISALLOW_COPY_AND_ASSIGN(Thread); 710 DISALLOW_COPY_AND_ASSIGN(Thread);
715 }; 711 };
716 712
717 713
718 // ---------------------------------------------------------------------------- 714 // ----------------------------------------------------------------------------
719 // Mutex
720 //
721 // Mutexes are used for serializing access to non-reentrant sections of code.
722 // The implementations of mutex should allow for nested/recursive locking.
723
724 class Mutex {
725 public:
726 virtual ~Mutex() {}
727
728 // Locks the given mutex. If the mutex is currently unlocked, it becomes
729 // locked and owned by the calling thread, and immediately. If the mutex
730 // is already locked by another thread, suspends the calling thread until
731 // the mutex is unlocked.
732 virtual int Lock() = 0;
733
734 // Unlocks the given mutex. The mutex is assumed to be locked and owned by
735 // the calling thread on entrance.
736 virtual int Unlock() = 0;
737
738 // Tries to lock the given mutex. Returns whether the mutex was
739 // successfully locked.
740 virtual bool TryLock() = 0;
741 };
742
743 struct CreateMutexTrait {
744 static Mutex* Create() {
745 return OS::CreateMutex();
746 }
747 };
748
749 // POD Mutex initialized lazily (i.e. the first time Pointer() is called).
750 // Usage:
751 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER;
752 //
753 // void my_function() {
754 // ScopedLock my_lock(my_mutex.Pointer());
755 // // Do something.
756 // }
757 //
758 typedef LazyDynamicInstance<
759 Mutex, CreateMutexTrait, ThreadSafeInitOnceTrait>::type LazyMutex;
760
761 #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
762
763 // ----------------------------------------------------------------------------
764 // ScopedLock
765 //
766 // Stack-allocated ScopedLocks provide block-scoped locking and
767 // unlocking of a mutex.
768 class ScopedLock {
769 public:
770 explicit ScopedLock(Mutex* mutex): mutex_(mutex) {
771 ASSERT(mutex_ != NULL);
772 mutex_->Lock();
773 }
774 ~ScopedLock() {
775 mutex_->Unlock();
776 }
777
778 private:
779 Mutex* mutex_;
780 DISALLOW_COPY_AND_ASSIGN(ScopedLock);
781 };
782
783
784 // ----------------------------------------------------------------------------
785 // Socket 715 // Socket
786 // 716 //
787 717
788 class Socket { 718 class Socket {
789 public: 719 public:
790 virtual ~Socket() {} 720 virtual ~Socket() {}
791 721
792 // Server initialization. 722 // Server initialization.
793 virtual bool Bind(const int port) = 0; 723 virtual bool Bind(const int port) = 0;
794 virtual bool Listen(int backlog) const = 0; 724 virtual bool Listen(int backlog) const = 0;
(...skipping 22 matching lines...) Expand all
817 static uint16_t HToN(uint16_t value); 747 static uint16_t HToN(uint16_t value);
818 static uint16_t NToH(uint16_t value); 748 static uint16_t NToH(uint16_t value);
819 static uint32_t HToN(uint32_t value); 749 static uint32_t HToN(uint32_t value);
820 static uint32_t NToH(uint32_t value); 750 static uint32_t NToH(uint32_t value);
821 }; 751 };
822 752
823 753
824 } } // namespace v8::internal 754 } } // namespace v8::internal
825 755
826 #endif // V8_PLATFORM_H_ 756 #endif // V8_PLATFORM_H_
OLDNEW
« no previous file with comments | « src/optimizing-compiler-thread.cc ('k') | src/platform-cygwin.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698