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

Side by Side Diff: src/platform.h

Issue 23625003: Cleanup Mutex and related classes. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE 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 | « 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 28 matching lines...) Expand all
39 // platform. This design was rejected because it was more complicated and 39 // platform. This design was rejected because it was more complicated and
40 // slower. It would require factory methods for selecting the right 40 // slower. It would require factory methods for selecting the right
41 // implementation and the overhead of virtual methods for performance 41 // implementation and the overhead of virtual methods for performance
42 // sensitive like mutex locking/unlocking. 42 // sensitive like mutex locking/unlocking.
43 43
44 #ifndef V8_PLATFORM_H_ 44 #ifndef V8_PLATFORM_H_
45 #define V8_PLATFORM_H_ 45 #define V8_PLATFORM_H_
46 46
47 #include <cstdarg> 47 #include <cstdarg>
48 48
49 #include "lazy-instance.h" 49 #include "platform/mutex.h"
50 #include "utils.h" 50 #include "utils.h"
51 #include "v8globals.h" 51 #include "v8globals.h"
52 52
53 #ifdef __sun 53 #ifdef __sun
54 # ifndef signbit 54 # ifndef signbit
55 namespace std { 55 namespace std {
56 int signbit(double x); 56 int signbit(double x);
57 } 57 }
58 # endif 58 # endif
59 #endif 59 #endif
(...skipping 27 matching lines...) Expand all
87 87
88 // Random is missing on both Visual Studio and MinGW. 88 // Random is missing on both Visual Studio and MinGW.
89 #if V8_CC_MSVC || V8_CC_MINGW 89 #if V8_CC_MSVC || V8_CC_MINGW
90 int random(); 90 int random();
91 #endif // V8_CC_MSVC || V8_CC_MINGW 91 #endif // V8_CC_MSVC || V8_CC_MINGW
92 92
93 namespace v8 { 93 namespace v8 {
94 namespace internal { 94 namespace internal {
95 95
96 class Semaphore; 96 class Semaphore;
97 class Mutex;
98 97
99 double ceiling(double x); 98 double ceiling(double x);
100 double modulo(double x, double y); 99 double modulo(double x, double y);
101 100
102 // Custom implementation of math functions. 101 // Custom implementation of math functions.
103 double fast_sin(double input); 102 double fast_sin(double input);
104 double fast_cos(double input); 103 double fast_cos(double input);
105 double fast_tan(double input); 104 double fast_tan(double input);
106 double fast_log(double input); 105 double fast_log(double input);
107 double fast_exp(double input); 106 double fast_exp(double input);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 static const int kStackWalkError = -1; 281 static const int kStackWalkError = -1;
283 static const int kStackWalkMaxNameLen = 256; 282 static const int kStackWalkMaxNameLen = 256;
284 static const int kStackWalkMaxTextLen = 256; 283 static const int kStackWalkMaxTextLen = 256;
285 struct StackFrame { 284 struct StackFrame {
286 void* address; 285 void* address;
287 char text[kStackWalkMaxTextLen]; 286 char text[kStackWalkMaxTextLen];
288 }; 287 };
289 288
290 static int StackWalk(Vector<StackFrame> frames); 289 static int StackWalk(Vector<StackFrame> frames);
291 290
292 // Factory method for creating platform dependent Mutex.
293 // Please use delete to reclaim the storage for the returned Mutex.
294 static Mutex* CreateMutex();
295
296 // Factory method for creating platform dependent Semaphore. 291 // Factory method for creating platform dependent Semaphore.
297 // Please use delete to reclaim the storage for the returned Semaphore. 292 // Please use delete to reclaim the storage for the returned Semaphore.
298 static Semaphore* CreateSemaphore(int count); 293 static Semaphore* CreateSemaphore(int count);
299 294
300 // Factory method for creating platform dependent Socket. 295 // Factory method for creating platform dependent Socket.
301 // Please use delete to reclaim the storage for the returned Socket. 296 // Please use delete to reclaim the storage for the returned Socket.
302 static Socket* CreateSocket(); 297 static Socket* CreateSocket();
303 298
304 class MemoryMappedFile { 299 class MemoryMappedFile {
305 public: 300 public:
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 672
678 char name_[kMaxThreadNameLength]; 673 char name_[kMaxThreadNameLength];
679 int stack_size_; 674 int stack_size_;
680 Semaphore* start_semaphore_; 675 Semaphore* start_semaphore_;
681 676
682 DISALLOW_COPY_AND_ASSIGN(Thread); 677 DISALLOW_COPY_AND_ASSIGN(Thread);
683 }; 678 };
684 679
685 680
686 // ---------------------------------------------------------------------------- 681 // ----------------------------------------------------------------------------
687 // Mutex
688 //
689 // Mutexes are used for serializing access to non-reentrant sections of code.
690 // The implementations of mutex should allow for nested/recursive locking.
691
692 class Mutex {
693 public:
694 virtual ~Mutex() {}
695
696 // Locks the given mutex. If the mutex is currently unlocked, it becomes
697 // locked and owned by the calling thread, and immediately. If the mutex
698 // is already locked by another thread, suspends the calling thread until
699 // the mutex is unlocked.
700 virtual int Lock() = 0;
701
702 // Unlocks the given mutex. The mutex is assumed to be locked and owned by
703 // the calling thread on entrance.
704 virtual int Unlock() = 0;
705
706 // Tries to lock the given mutex. Returns whether the mutex was
707 // successfully locked.
708 virtual bool TryLock() = 0;
709 };
710
711 struct CreateMutexTrait {
712 static Mutex* Create() {
713 return OS::CreateMutex();
714 }
715 };
716
717 // POD Mutex initialized lazily (i.e. the first time Pointer() is called).
718 // Usage:
719 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER;
720 //
721 // void my_function() {
722 // ScopedLock my_lock(my_mutex.Pointer());
723 // // Do something.
724 // }
725 //
726 typedef LazyDynamicInstance<
727 Mutex, CreateMutexTrait, ThreadSafeInitOnceTrait>::type LazyMutex;
728
729 #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
730
731 // ----------------------------------------------------------------------------
732 // ScopedLock
733 //
734 // Stack-allocated ScopedLocks provide block-scoped locking and
735 // unlocking of a mutex.
736 class ScopedLock {
737 public:
738 explicit ScopedLock(Mutex* mutex): mutex_(mutex) {
739 ASSERT(mutex_ != NULL);
740 mutex_->Lock();
741 }
742 ~ScopedLock() {
743 mutex_->Unlock();
744 }
745
746 private:
747 Mutex* mutex_;
748 DISALLOW_COPY_AND_ASSIGN(ScopedLock);
749 };
750
751
752 // ----------------------------------------------------------------------------
753 // Socket 682 // Socket
754 // 683 //
755 684
756 class Socket { 685 class Socket {
757 public: 686 public:
758 virtual ~Socket() {} 687 virtual ~Socket() {}
759 688
760 // Server initialization. 689 // Server initialization.
761 virtual bool Bind(const int port) = 0; 690 virtual bool Bind(const int port) = 0;
762 virtual bool Listen(int backlog) const = 0; 691 virtual bool Listen(int backlog) const = 0;
(...skipping 22 matching lines...) Expand all
785 static uint16_t HToN(uint16_t value); 714 static uint16_t HToN(uint16_t value);
786 static uint16_t NToH(uint16_t value); 715 static uint16_t NToH(uint16_t value);
787 static uint32_t HToN(uint32_t value); 716 static uint32_t HToN(uint32_t value);
788 static uint32_t NToH(uint32_t value); 717 static uint32_t NToH(uint32_t value);
789 }; 718 };
790 719
791 720
792 } } // namespace v8::internal 721 } } // namespace v8::internal
793 722
794 #endif // V8_PLATFORM_H_ 723 #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