OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "bin/loader.h" | 6 #include "bin/loader.h" |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/extensions.h" | 10 #include "bin/extensions.h" |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 Loader::LoaderInfo* Loader::loader_infos_ = NULL; | 461 Loader::LoaderInfo* Loader::loader_infos_ = NULL; |
462 intptr_t Loader::loader_infos_length_ = 0; | 462 intptr_t Loader::loader_infos_length_ = 0; |
463 intptr_t Loader::loader_infos_capacity_ = 0; | 463 intptr_t Loader::loader_infos_capacity_ = 0; |
464 | 464 |
465 | 465 |
466 // Add a mapping from |port| to |isolate_data| (really the loader). When a | 466 // Add a mapping from |port| to |isolate_data| (really the loader). When a |
467 // native message arrives, we use this map to report the I/O result to the | 467 // native message arrives, we use this map to report the I/O result to the |
468 // correct loader. | 468 // correct loader. |
469 // This happens whenever an isolate begins loading. | 469 // This happens whenever an isolate begins loading. |
470 void Loader::AddLoader(Dart_Port port, IsolateData* isolate_data) { | 470 void Loader::AddLoader(Dart_Port port, IsolateData* isolate_data) { |
471 ASSERT(LoaderFor(port) == NULL); | 471 MutexLocker ml(&loader_infos_lock_); |
472 ASSERT(LoaderForLocked(port) == NULL); | |
472 if (loader_infos_length_ == loader_infos_capacity_) { | 473 if (loader_infos_length_ == loader_infos_capacity_) { |
473 // Grow to an initial capacity or double in size. | 474 // Grow to an initial capacity or double in size. |
474 loader_infos_capacity_ = | 475 loader_infos_capacity_ = |
475 (loader_infos_capacity_ == 0) ? 4 : loader_infos_capacity_ * 2; | 476 (loader_infos_capacity_ == 0) ? 4 : loader_infos_capacity_ * 2; |
476 loader_infos_ = | 477 loader_infos_ = |
477 reinterpret_cast<Loader::LoaderInfo*>( | 478 reinterpret_cast<Loader::LoaderInfo*>( |
478 realloc(loader_infos_, | 479 realloc(loader_infos_, |
479 sizeof(Loader::LoaderInfo) * loader_infos_capacity_)); | 480 sizeof(Loader::LoaderInfo) * loader_infos_capacity_)); |
480 ASSERT(loader_infos_ != NULL); | 481 ASSERT(loader_infos_ != NULL); |
481 // Initialize new entries. | 482 // Initialize new entries. |
482 for (intptr_t i = loader_infos_length_; i < loader_infos_capacity_; i++) { | 483 for (intptr_t i = loader_infos_length_; i < loader_infos_capacity_; i++) { |
483 loader_infos_[i].port = ILLEGAL_PORT; | 484 loader_infos_[i].port = ILLEGAL_PORT; |
484 loader_infos_[i].isolate_data = NULL; | 485 loader_infos_[i].isolate_data = NULL; |
485 } | 486 } |
486 } | 487 } |
487 ASSERT(loader_infos_length_ < loader_infos_capacity_); | 488 ASSERT(loader_infos_length_ < loader_infos_capacity_); |
488 loader_infos_[loader_infos_length_].port = port; | 489 loader_infos_[loader_infos_length_].port = port; |
489 loader_infos_[loader_infos_length_].isolate_data = isolate_data; | 490 loader_infos_[loader_infos_length_].isolate_data = isolate_data; |
490 loader_infos_length_++; | 491 loader_infos_length_++; |
491 ASSERT(LoaderFor(port) != NULL); | 492 ASSERT(LoaderForLocked(port) != NULL); |
492 } | 493 } |
493 | 494 |
494 | 495 |
495 // Remove |port| from the map. | 496 // Remove |port| from the map. |
496 // This happens once an isolate has finished loading. | 497 // This happens once an isolate has finished loading. |
497 void Loader::RemoveLoader(Dart_Port port) { | 498 void Loader::RemoveLoader(Dart_Port port) { |
499 MutexLocker ml(&loader_infos_lock_); | |
498 const intptr_t index = LoaderIndexFor(port); | 500 const intptr_t index = LoaderIndexFor(port); |
499 ASSERT(index >= 0); | 501 ASSERT(index >= 0); |
500 const intptr_t last = loader_infos_length_ - 1; | 502 const intptr_t last = loader_infos_length_ - 1; |
501 ASSERT(last >= 0); | 503 ASSERT(last >= 0); |
502 if (index != last) { | 504 if (index != last) { |
503 // Swap with the tail. | 505 // Swap with the tail. |
504 loader_infos_[index] = loader_infos_[last]; | 506 loader_infos_[index] = loader_infos_[last]; |
505 } | 507 } |
506 loader_infos_length_--; | 508 loader_infos_length_--; |
507 } | 509 } |
508 | 510 |
509 | 511 |
510 intptr_t Loader::LoaderIndexFor(Dart_Port port) { | 512 intptr_t Loader::LoaderIndexFor(Dart_Port port) { |
511 for (intptr_t i = 0; i < loader_infos_length_; i++) { | 513 for (intptr_t i = 0; i < loader_infos_length_; i++) { |
512 if (loader_infos_[i].port == port) { | 514 if (loader_infos_[i].port == port) { |
513 return i; | 515 return i; |
514 } | 516 } |
515 } | 517 } |
516 return -1; | 518 return -1; |
517 } | 519 } |
518 | 520 |
519 | 521 |
520 Loader* Loader::LoaderFor(Dart_Port port) { | 522 Loader* Loader::LoaderForLocked(Dart_Port port) { |
zra
2016/06/07 04:51:46
ASSERT(loader_infos_lock_.IsOwnedByCurrentThread()
Cutch
2016/06/07 14:22:15
The threads interface under bin/ does not have IsO
| |
521 intptr_t index = LoaderIndexFor(port); | 523 intptr_t index = LoaderIndexFor(port); |
522 if (index < 0) { | 524 if (index < 0) { |
523 return NULL; | 525 return NULL; |
524 } | 526 } |
525 return loader_infos_[index].isolate_data->loader(); | 527 return loader_infos_[index].isolate_data->loader(); |
526 } | 528 } |
527 | 529 |
528 | 530 |
531 Loader* Loader::LoaderFor(Dart_Port port) { | |
532 MutexLocker ml(&loader_infos_lock_); | |
533 return LoaderForLocked(port); | |
534 } | |
535 | |
536 | |
529 void Loader::NativeMessageHandler(Dart_Port dest_port_id, | 537 void Loader::NativeMessageHandler(Dart_Port dest_port_id, |
530 Dart_CObject* message) { | 538 Dart_CObject* message) { |
531 Loader* loader = LoaderFor(dest_port_id); | 539 MutexLocker ml(&loader_infos_lock_); |
540 Loader* loader = LoaderForLocked(dest_port_id); | |
532 if (loader == NULL) { | 541 if (loader == NULL) { |
533 return; | 542 return; |
534 } | 543 } |
535 loader->QueueMessage(message); | 544 loader->QueueMessage(message); |
536 } | 545 } |
537 | 546 |
538 } // namespace bin | 547 } // namespace bin |
539 } // namespace dart | 548 } // namespace dart |
OLD | NEW |