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

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

Issue 6816038: Do not rely on uniquiness of pthread_t Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Win32 build fix Created 9 years, 8 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/platform-linux.cc ('k') | src/platform-nullos.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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 } 385 }
386 386
387 387
388 bool VirtualMemory::Uncommit(void* address, size_t size) { 388 bool VirtualMemory::Uncommit(void* address, size_t size) {
389 return mmap(address, size, PROT_NONE, 389 return mmap(address, size, PROT_NONE,
390 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, 390 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
391 kMmapFd, kMmapFdOffset) != MAP_FAILED; 391 kMmapFd, kMmapFdOffset) != MAP_FAILED;
392 } 392 }
393 393
394 394
395 class ThreadHandle::PlatformData : public Malloced { 395 class Thread::PlatformData : public Malloced {
396 public: 396 public:
397 explicit PlatformData(ThreadHandle::Kind kind) { 397 PlatformData() : thread_(kNoThread) {}
398 Initialize(kind);
399 }
400
401 void Initialize(ThreadHandle::Kind kind) {
402 switch (kind) {
403 case ThreadHandle::SELF: thread_ = pthread_self(); break;
404 case ThreadHandle::INVALID: thread_ = kNoThread; break;
405 }
406 }
407 pthread_t thread_; // Thread handle for pthread. 398 pthread_t thread_; // Thread handle for pthread.
408 }; 399 };
409 400
410
411
412 ThreadHandle::ThreadHandle(Kind kind) {
413 data_ = new PlatformData(kind);
414 }
415
416
417 void ThreadHandle::Initialize(ThreadHandle::Kind kind) {
418 data_->Initialize(kind);
419 }
420
421
422 ThreadHandle::~ThreadHandle() {
423 delete data_;
424 }
425
426
427 bool ThreadHandle::IsSelf() const {
428 return pthread_equal(data_->thread_, pthread_self());
429 }
430
431
432 bool ThreadHandle::IsValid() const {
433 return data_->thread_ != kNoThread;
434 }
435
436
437 Thread::Thread(Isolate* isolate, const Options& options) 401 Thread::Thread(Isolate* isolate, const Options& options)
438 : ThreadHandle(ThreadHandle::INVALID), 402 : data_(new PlatformData),
439 isolate_(isolate), 403 isolate_(isolate),
440 stack_size_(options.stack_size) { 404 stack_size_(options.stack_size) {
441 set_name(options.name); 405 set_name(options.name);
442 } 406 }
443 407
444 408
445 Thread::Thread(Isolate* isolate, const char* name) 409 Thread::Thread(Isolate* isolate, const char* name)
446 : ThreadHandle(ThreadHandle::INVALID), 410 : data_(new PlatformData),
447 isolate_(isolate), 411 isolate_(isolate),
448 stack_size_(0) { 412 stack_size_(0) {
449 set_name(name); 413 set_name(name);
450 } 414 }
451 415
452 416
453 Thread::~Thread() { 417 Thread::~Thread() {
418 delete data_;
454 } 419 }
455 420
456 421
457 static void SetThreadName(const char* name) { 422 static void SetThreadName(const char* name) {
458 // pthread_setname_np is only available in 10.6 or later, so test 423 // pthread_setname_np is only available in 10.6 or later, so test
459 // for it at runtime. 424 // for it at runtime.
460 int (*dynamic_pthread_setname_np)(const char*); 425 int (*dynamic_pthread_setname_np)(const char*);
461 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = 426 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
462 dlsym(RTLD_DEFAULT, "pthread_setname_np"); 427 dlsym(RTLD_DEFAULT, "pthread_setname_np");
463 if (!dynamic_pthread_setname_np) 428 if (!dynamic_pthread_setname_np)
464 return; 429 return;
465 430
466 // Mac OS X does not expose the length limit of the name, so hardcode it. 431 // Mac OS X does not expose the length limit of the name, so hardcode it.
467 static const int kMaxNameLength = 63; 432 static const int kMaxNameLength = 63;
468 USE(kMaxNameLength); 433 USE(kMaxNameLength);
469 ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength); 434 ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
470 dynamic_pthread_setname_np(name); 435 dynamic_pthread_setname_np(name);
471 } 436 }
472 437
473 438
474 static void* ThreadEntry(void* arg) { 439 static void* ThreadEntry(void* arg) {
475 Thread* thread = reinterpret_cast<Thread*>(arg); 440 Thread* thread = reinterpret_cast<Thread*>(arg);
476 // This is also initialized by the first argument to pthread_create() but we 441 // This is also initialized by the first argument to pthread_create() but we
477 // don't know which thread will run first (the original thread or the new 442 // don't know which thread will run first (the original thread or the new
478 // one) so we initialize it here too. 443 // one) so we initialize it here too.
479 thread->thread_handle_data()->thread_ = pthread_self(); 444 thread->data()->thread_ = pthread_self();
480 SetThreadName(thread->name()); 445 SetThreadName(thread->name());
481 ASSERT(thread->IsValid()); 446 ASSERT(thread->data()->thread_ != kNoThread);
482 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate()); 447 Thread::SetThreadLocal(Isolate::isolate_key(), thread->isolate());
483 thread->Run(); 448 thread->Run();
484 return NULL; 449 return NULL;
485 } 450 }
486 451
487 452
488 void Thread::set_name(const char* name) { 453 void Thread::set_name(const char* name) {
489 strncpy(name_, name, sizeof(name_)); 454 strncpy(name_, name, sizeof(name_));
490 name_[sizeof(name_) - 1] = '\0'; 455 name_[sizeof(name_) - 1] = '\0';
491 } 456 }
492 457
493 458
494 void Thread::Start() { 459 void Thread::Start() {
495 pthread_attr_t* attr_ptr = NULL; 460 pthread_attr_t* attr_ptr = NULL;
496 pthread_attr_t attr; 461 pthread_attr_t attr;
497 if (stack_size_ > 0) { 462 if (stack_size_ > 0) {
498 pthread_attr_init(&attr); 463 pthread_attr_init(&attr);
499 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_)); 464 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
500 attr_ptr = &attr; 465 attr_ptr = &attr;
501 } 466 }
502 pthread_create(&thread_handle_data()->thread_, attr_ptr, ThreadEntry, this); 467 pthread_create(&data_->thread_, attr_ptr, ThreadEntry, this);
503 ASSERT(IsValid()); 468 ASSERT(data_->thread_ != kNoThread);
504 } 469 }
505 470
506 471
507 void Thread::Join() { 472 void Thread::Join() {
508 pthread_join(thread_handle_data()->thread_, NULL); 473 pthread_join(data_->thread_, NULL);
509 } 474 }
510 475
511 476
512 #ifdef V8_FAST_TLS_SUPPORTED 477 #ifdef V8_FAST_TLS_SUPPORTED
513 478
514 static Atomic32 tls_base_offset_initialized = 0; 479 static Atomic32 tls_base_offset_initialized = 0;
515 intptr_t kMacTlsBaseOffset = 0; 480 intptr_t kMacTlsBaseOffset = 0;
516 481
517 // It's safe to do the initialization more that once, but it has to be 482 // It's safe to do the initialization more that once, but it has to be
518 // done at least once. 483 // done at least once.
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 821
857 void Sampler::Stop() { 822 void Sampler::Stop() {
858 ASSERT(IsActive()); 823 ASSERT(IsActive());
859 SamplerThread::RemoveActiveSampler(this); 824 SamplerThread::RemoveActiveSampler(this);
860 SetActive(false); 825 SetActive(false);
861 } 826 }
862 827
863 #endif // ENABLE_LOGGING_AND_PROFILING 828 #endif // ENABLE_LOGGING_AND_PROFILING
864 829
865 } } // namespace v8::internal 830 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-linux.cc ('k') | src/platform-nullos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698