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

Side by Side Diff: src/platform-posix.cc

Issue 18335008: Cleanup common POSIX functionality. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Mark result as used Created 7 years, 5 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
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 13 matching lines...) Expand all
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Platform specific code for POSIX goes here. This is not a platform on its 28 // Platform specific code for POSIX goes here. This is not a platform on its
29 // own but contains the parts which are the same across POSIX platforms Linux, 29 // own but contains the parts which are the same across POSIX platforms Linux,
30 // Mac OS, FreeBSD and OpenBSD. 30 // Mac OS, FreeBSD and OpenBSD.
31 31
32 #include "platform-posix.h" 32 #include "platform-posix.h"
33 33
34 #include <pthread.h>
35 #include <sched.h> // for sched_yield
34 #include <unistd.h> 36 #include <unistd.h>
35 #include <errno.h> 37 #include <errno.h>
36 #include <time.h> 38 #include <time.h>
37 39
38 #include <sys/mman.h> 40 #include <sys/mman.h>
39 #include <sys/socket.h> 41 #include <sys/socket.h>
40 #include <sys/resource.h> 42 #include <sys/resource.h>
41 #include <sys/time.h> 43 #include <sys/time.h>
42 #include <sys/types.h> 44 #include <sys/types.h>
43 #include <sys/stat.h> 45 #include <sys/stat.h>
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 return strchr(str, c); 394 return strchr(str, c);
393 } 395 }
394 396
395 397
396 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { 398 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
397 strncpy(dest.start(), src, n); 399 strncpy(dest.start(), src, n);
398 } 400 }
399 401
400 402
401 // ---------------------------------------------------------------------------- 403 // ----------------------------------------------------------------------------
404 // POSIX thread support.
405 //
406
407 void Thread::YieldCPU() {
408 sched_yield();
409 }
410
411
412 class POSIXMutex : public Mutex {
413 public:
414 POSIXMutex() {
415 pthread_mutexattr_t attr;
416 memset(&attr, 0, sizeof(attr));
417 int result = pthread_mutexattr_init(&attr);
418 ASSERT(result == 0);
419 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
420 ASSERT(result == 0);
421 result = pthread_mutex_init(&mutex_, &attr);
422 ASSERT(result == 0);
423 result = pthread_mutexattr_destroy(&attr);
424 ASSERT(result == 0);
425 USE(result);
426 }
427
428 virtual ~POSIXMutex() { pthread_mutex_destroy(&mutex_); }
429
430 virtual int Lock() { return pthread_mutex_lock(&mutex_); }
431
432 virtual int Unlock() { return pthread_mutex_unlock(&mutex_); }
433
434 virtual bool TryLock() {
435 int result = pthread_mutex_trylock(&mutex_);
436 // Return false if the lock is busy and locking failed.
437 if (result == EBUSY) {
438 return false;
439 }
440 ASSERT(result == 0); // Verify no other errors.
441 return true;
442 }
443
444 private:
445 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
446 };
447
448
449 Mutex* OS::CreateMutex() {
450 return new POSIXMutex();
451 }
452
453
454 // ----------------------------------------------------------------------------
402 // POSIX socket support. 455 // POSIX socket support.
403 // 456 //
404 457
405 class POSIXSocket : public Socket { 458 class POSIXSocket : public Socket {
406 public: 459 public:
407 explicit POSIXSocket() { 460 explicit POSIXSocket() {
408 // Create the socket. 461 // Create the socket.
409 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 462 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
410 if (IsValid()) { 463 if (IsValid()) {
411 // Allow rapid reuse. 464 // Allow rapid reuse.
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 return ntohl(value); 643 return ntohl(value);
591 } 644 }
592 645
593 646
594 Socket* OS::CreateSocket() { 647 Socket* OS::CreateSocket() {
595 return new POSIXSocket(); 648 return new POSIXSocket();
596 } 649 }
597 650
598 651
599 } } // namespace v8::internal 652 } } // namespace v8::internal
OLDNEW
« src/platform-macos.cc ('K') | « src/platform-openbsd.cc ('k') | src/platform-solaris.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698