OLD | NEW |
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 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 return munmap(base, size) == 0; | 470 return munmap(base, size) == 0; |
471 } | 471 } |
472 | 472 |
473 | 473 |
474 bool VirtualMemory::HasLazyCommits() { | 474 bool VirtualMemory::HasLazyCommits() { |
475 // TODO(alph): implement for the platform. | 475 // TODO(alph): implement for the platform. |
476 return false; | 476 return false; |
477 } | 477 } |
478 | 478 |
479 | 479 |
480 class Thread::PlatformData : public Malloced { | |
481 public: | |
482 PlatformData() : thread_(kNoThread) {} | |
483 | |
484 pthread_t thread_; // Thread handle for pthread. | |
485 }; | |
486 | |
487 Thread::Thread(const Options& options) | |
488 : data_(new PlatformData()), | |
489 stack_size_(options.stack_size()), | |
490 start_semaphore_(NULL) { | |
491 set_name(options.name()); | |
492 } | |
493 | |
494 | |
495 Thread::~Thread() { | |
496 delete data_; | |
497 } | |
498 | |
499 | |
500 static void* ThreadEntry(void* arg) { | |
501 Thread* thread = reinterpret_cast<Thread*>(arg); | |
502 // This is also initialized by the first argument to pthread_create() but we | |
503 // don't know which thread will run first (the original thread or the new | |
504 // one) so we initialize it here too. | |
505 #ifdef PR_SET_NAME | |
506 prctl(PR_SET_NAME, | |
507 reinterpret_cast<unsigned long>(thread->name()), // NOLINT | |
508 0, 0, 0); | |
509 #endif | |
510 thread->data()->thread_ = pthread_self(); | |
511 ASSERT(thread->data()->thread_ != kNoThread); | |
512 thread->NotifyStartedAndRun(); | |
513 return NULL; | |
514 } | |
515 | |
516 | |
517 void Thread::set_name(const char* name) { | |
518 strncpy(name_, name, sizeof(name_)); | |
519 name_[sizeof(name_) - 1] = '\0'; | |
520 } | |
521 | |
522 | |
523 void Thread::Start() { | |
524 pthread_attr_t* attr_ptr = NULL; | |
525 pthread_attr_t attr; | |
526 if (stack_size_ > 0) { | |
527 pthread_attr_init(&attr); | |
528 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | |
529 attr_ptr = &attr; | |
530 } | |
531 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); | |
532 ASSERT(data_->thread_ != kNoThread); | |
533 } | |
534 | |
535 | |
536 void Thread::Join() { | |
537 pthread_join(data_->thread_, NULL); | |
538 } | |
539 | |
540 | |
541 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | |
542 pthread_key_t key; | |
543 int result = pthread_key_create(&key, NULL); | |
544 USE(result); | |
545 ASSERT(result == 0); | |
546 return static_cast<LocalStorageKey>(key); | |
547 } | |
548 | |
549 | |
550 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { | |
551 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
552 int result = pthread_key_delete(pthread_key); | |
553 USE(result); | |
554 ASSERT(result == 0); | |
555 } | |
556 | |
557 | |
558 void* Thread::GetThreadLocal(LocalStorageKey key) { | |
559 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
560 return pthread_getspecific(pthread_key); | |
561 } | |
562 | |
563 | |
564 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | |
565 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
566 pthread_setspecific(pthread_key, value); | |
567 } | |
568 | |
569 | |
570 class OpenBSDSemaphore : public Semaphore { | 480 class OpenBSDSemaphore : public Semaphore { |
571 public: | 481 public: |
572 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); } | 482 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); } |
573 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); } | 483 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); } |
574 | 484 |
575 virtual void Wait(); | 485 virtual void Wait(); |
576 virtual bool Wait(int timeout); | 486 virtual bool Wait(int timeout); |
577 virtual void Signal() { sem_post(&sem_); } | 487 virtual void Signal() { sem_post(&sem_); } |
578 private: | 488 private: |
579 sem_t sem_; | 489 sem_t sem_; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 limit_mutex = CreateMutex(); | 553 limit_mutex = CreateMutex(); |
644 } | 554 } |
645 | 555 |
646 | 556 |
647 void OS::TearDown() { | 557 void OS::TearDown() { |
648 delete limit_mutex; | 558 delete limit_mutex; |
649 } | 559 } |
650 | 560 |
651 | 561 |
652 } } // namespace v8::internal | 562 } } // namespace v8::internal |
OLD | NEW |