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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 #include "v8threads.h" | 55 #include "v8threads.h" |
56 | 56 |
57 #include "platform-posix.h" | 57 #include "platform-posix.h" |
58 #include "platform.h" | 58 #include "platform.h" |
59 #include "vm-state-inl.h" | 59 #include "vm-state-inl.h" |
60 | 60 |
61 | 61 |
62 namespace v8 { | 62 namespace v8 { |
63 namespace internal { | 63 namespace internal { |
64 | 64 |
65 // 0 is never a valid thread id on FreeBSD since tids and pids share a | |
66 // name space and pid 0 is used to kill the group (see man 2 kill). | |
67 static const pthread_t kNoThread = (pthread_t) 0; | |
68 | |
69 | 65 |
70 double ceiling(double x) { | 66 double ceiling(double x) { |
71 // Correct as on OS X | 67 // Correct as on OS X |
72 if (-1.0 < x && x < 0.0) { | 68 if (-1.0 < x && x < 0.0) { |
73 return -0.0; | 69 return -0.0; |
74 } else { | 70 } else { |
75 return ceil(x); | 71 return ceil(x); |
76 } | 72 } |
77 } | 73 } |
78 | 74 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return munmap(base, size) == 0; | 386 return munmap(base, size) == 0; |
391 } | 387 } |
392 | 388 |
393 | 389 |
394 bool VirtualMemory::HasLazyCommits() { | 390 bool VirtualMemory::HasLazyCommits() { |
395 // TODO(alph): implement for the platform. | 391 // TODO(alph): implement for the platform. |
396 return false; | 392 return false; |
397 } | 393 } |
398 | 394 |
399 | 395 |
400 class Thread::PlatformData : public Malloced { | |
401 public: | |
402 pthread_t thread_; // Thread handle for pthread. | |
403 }; | |
404 | |
405 | |
406 Thread::Thread(const Options& options) | |
407 : data_(new PlatformData), | |
408 stack_size_(options.stack_size()), | |
409 start_semaphore_(NULL) { | |
410 set_name(options.name()); | |
411 } | |
412 | |
413 | |
414 Thread::~Thread() { | |
415 delete data_; | |
416 } | |
417 | |
418 | |
419 static void* ThreadEntry(void* arg) { | |
420 Thread* thread = reinterpret_cast<Thread*>(arg); | |
421 // This is also initialized by the first argument to pthread_create() but we | |
422 // don't know which thread will run first (the original thread or the new | |
423 // one) so we initialize it here too. | |
424 thread->data()->thread_ = pthread_self(); | |
425 ASSERT(thread->data()->thread_ != kNoThread); | |
426 thread->NotifyStartedAndRun(); | |
427 return NULL; | |
428 } | |
429 | |
430 | |
431 void Thread::set_name(const char* name) { | |
432 strncpy(name_, name, sizeof(name_)); | |
433 name_[sizeof(name_) - 1] = '\0'; | |
434 } | |
435 | |
436 | |
437 void Thread::Start() { | |
438 pthread_attr_t* attr_ptr = NULL; | |
439 pthread_attr_t attr; | |
440 if (stack_size_ > 0) { | |
441 pthread_attr_init(&attr); | |
442 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); | |
443 attr_ptr = &attr; | |
444 } | |
445 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this); | |
446 ASSERT(data_->thread_ != kNoThread); | |
447 } | |
448 | |
449 | |
450 void Thread::Join() { | |
451 pthread_join(data_->thread_, NULL); | |
452 } | |
453 | |
454 | |
455 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | |
456 pthread_key_t key; | |
457 int result = pthread_key_create(&key, NULL); | |
458 USE(result); | |
459 ASSERT(result == 0); | |
460 return static_cast<LocalStorageKey>(key); | |
461 } | |
462 | |
463 | |
464 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { | |
465 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
466 int result = pthread_key_delete(pthread_key); | |
467 USE(result); | |
468 ASSERT(result == 0); | |
469 } | |
470 | |
471 | |
472 void* Thread::GetThreadLocal(LocalStorageKey key) { | |
473 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
474 return pthread_getspecific(pthread_key); | |
475 } | |
476 | |
477 | |
478 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | |
479 pthread_key_t pthread_key = static_cast<pthread_key_t>(key); | |
480 pthread_setspecific(pthread_key, value); | |
481 } | |
482 | |
483 | |
484 class FreeBSDSemaphore : public Semaphore { | 396 class FreeBSDSemaphore : public Semaphore { |
485 public: | 397 public: |
486 explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); } | 398 explicit FreeBSDSemaphore(int count) { sem_init(&sem_, 0, count); } |
487 virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); } | 399 virtual ~FreeBSDSemaphore() { sem_destroy(&sem_); } |
488 | 400 |
489 virtual void Wait(); | 401 virtual void Wait(); |
490 virtual bool Wait(int timeout); | 402 virtual bool Wait(int timeout); |
491 virtual void Signal() { sem_post(&sem_); } | 403 virtual void Signal() { sem_post(&sem_); } |
492 private: | 404 private: |
493 sem_t sem_; | 405 sem_t sem_; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 limit_mutex = CreateMutex(); | 460 limit_mutex = CreateMutex(); |
549 } | 461 } |
550 | 462 |
551 | 463 |
552 void OS::TearDown() { | 464 void OS::TearDown() { |
553 delete limit_mutex; | 465 delete limit_mutex; |
554 } | 466 } |
555 | 467 |
556 | 468 |
557 } } // namespace v8::internal | 469 } } // namespace v8::internal |
OLD | NEW |