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

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

Issue 19490010: Avoid duplication of OS::Thread methods for every POSIX platform. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Tweaks 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
« no previous file with comments | « no previous file | src/platform-freebsd.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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "platform-posix.h" 44 #include "platform-posix.h"
45 #include "platform.h" 45 #include "platform.h"
46 #include "simulator.h" 46 #include "simulator.h"
47 #include "v8threads.h" 47 #include "v8threads.h"
48 #include "vm-state-inl.h" 48 #include "vm-state-inl.h"
49 #include "win32-headers.h" 49 #include "win32-headers.h"
50 50
51 namespace v8 { 51 namespace v8 {
52 namespace internal { 52 namespace internal {
53 53
54 // 0 is never a valid thread id
55 static const pthread_t kNoThread = (pthread_t) 0;
56
57 54
58 double ceiling(double x) { 55 double ceiling(double x) {
59 return ceil(x); 56 return ceil(x);
60 } 57 }
61 58
62 59
63 static Mutex* limit_mutex = NULL; 60 static Mutex* limit_mutex = NULL;
64 61
65 62
66 uint64_t OS::CpuFeaturesImpliedByPlatform() { 63 uint64_t OS::CpuFeaturesImpliedByPlatform() {
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 return VirtualFree(base, 0, MEM_RELEASE) != 0; 408 return VirtualFree(base, 0, MEM_RELEASE) != 0;
412 } 409 }
413 410
414 411
415 bool VirtualMemory::HasLazyCommits() { 412 bool VirtualMemory::HasLazyCommits() {
416 // TODO(alph): implement for the platform. 413 // TODO(alph): implement for the platform.
417 return false; 414 return false;
418 } 415 }
419 416
420 417
421 class Thread::PlatformData : public Malloced {
422 public:
423 PlatformData() : thread_(kNoThread) {}
424 pthread_t thread_; // Thread handle for pthread.
425 };
426
427
428 Thread::Thread(const Options& options)
429 : data_(new PlatformData()),
430 stack_size_(options.stack_size()),
431 start_semaphore_(NULL) {
432 set_name(options.name());
433 }
434
435
436 Thread::~Thread() {
437 delete data_;
438 }
439
440
441 static void* ThreadEntry(void* arg) {
442 Thread* thread = reinterpret_cast<Thread*>(arg);
443 // This is also initialized by the first argument to pthread_create() but we
444 // don't know which thread will run first (the original thread or the new
445 // one) so we initialize it here too.
446 thread->data()->thread_ = pthread_self();
447 ASSERT(thread->data()->thread_ != kNoThread);
448 thread->NotifyStartedAndRun();
449 return NULL;
450 }
451
452
453 void Thread::set_name(const char* name) {
454 strncpy(name_, name, sizeof(name_));
455 name_[sizeof(name_) - 1] = '\0';
456 }
457
458
459 void Thread::Start() {
460 pthread_attr_t* attr_ptr = NULL;
461 pthread_attr_t attr;
462 if (stack_size_ > 0) {
463 pthread_attr_init(&attr);
464 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
465 attr_ptr = &attr;
466 }
467 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
468 ASSERT(data_->thread_ != kNoThread);
469 }
470
471
472 void Thread::Join() {
473 pthread_join(data_->thread_, NULL);
474 }
475
476
477 static inline Thread::LocalStorageKey PthreadKeyToLocalKey(
478 pthread_key_t pthread_key) {
479 // We need to cast pthread_key_t to Thread::LocalStorageKey in two steps
480 // because pthread_key_t is a pointer type on Cygwin. This will probably not
481 // work on 64-bit platforms, but Cygwin doesn't support 64-bit anyway.
482 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
483 intptr_t ptr_key = reinterpret_cast<intptr_t>(pthread_key);
484 return static_cast<Thread::LocalStorageKey>(ptr_key);
485 }
486
487
488 static inline pthread_key_t LocalKeyToPthreadKey(
489 Thread::LocalStorageKey local_key) {
490 STATIC_ASSERT(sizeof(Thread::LocalStorageKey) == sizeof(pthread_key_t));
491 intptr_t ptr_key = static_cast<intptr_t>(local_key);
492 return reinterpret_cast<pthread_key_t>(ptr_key);
493 }
494
495
496 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
497 pthread_key_t key;
498 int result = pthread_key_create(&key, NULL);
499 USE(result);
500 ASSERT(result == 0);
501 return PthreadKeyToLocalKey(key);
502 }
503
504
505 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
506 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
507 int result = pthread_key_delete(pthread_key);
508 USE(result);
509 ASSERT(result == 0);
510 }
511
512
513 void* Thread::GetThreadLocal(LocalStorageKey key) {
514 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
515 return pthread_getspecific(pthread_key);
516 }
517
518
519 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
520 pthread_key_t pthread_key = LocalKeyToPthreadKey(key);
521 pthread_setspecific(pthread_key, value);
522 }
523
524
525 class CygwinSemaphore : public Semaphore { 418 class CygwinSemaphore : public Semaphore {
526 public: 419 public:
527 explicit CygwinSemaphore(int count) { sem_init(&sem_, 0, count); } 420 explicit CygwinSemaphore(int count) { sem_init(&sem_, 0, count); }
528 virtual ~CygwinSemaphore() { sem_destroy(&sem_); } 421 virtual ~CygwinSemaphore() { sem_destroy(&sem_); }
529 422
530 virtual void Wait(); 423 virtual void Wait();
531 virtual bool Wait(int timeout); 424 virtual bool Wait(int timeout);
532 virtual void Signal() { sem_post(&sem_); } 425 virtual void Signal() { sem_post(&sem_); }
533 private: 426 private:
534 sem_t sem_; 427 sem_t sem_;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 limit_mutex = CreateMutex(); 491 limit_mutex = CreateMutex();
599 } 492 }
600 493
601 494
602 void OS::TearDown() { 495 void OS::TearDown() {
603 delete limit_mutex; 496 delete limit_mutex;
604 } 497 }
605 498
606 499
607 } } // namespace v8::internal 500 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/platform-freebsd.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698