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

Side by Side Diff: mojo/edk/system/core.cc

Issue 1785843002: [mojo] Implement pipe fusion API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/system/core.h" 5 #include "mojo/edk/system/core.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 *message_pipe_handle0 = AddDispatcher( 444 *message_pipe_handle0 = AddDispatcher(
445 new MessagePipeDispatcher(GetNodeController(), port0, pipe_id, 0)); 445 new MessagePipeDispatcher(GetNodeController(), port0, pipe_id, 0));
446 if (*message_pipe_handle0 == MOJO_HANDLE_INVALID) 446 if (*message_pipe_handle0 == MOJO_HANDLE_INVALID)
447 return MOJO_RESULT_RESOURCE_EXHAUSTED; 447 return MOJO_RESULT_RESOURCE_EXHAUSTED;
448 448
449 *message_pipe_handle1 = AddDispatcher( 449 *message_pipe_handle1 = AddDispatcher(
450 new MessagePipeDispatcher(GetNodeController(), port1, pipe_id, 1)); 450 new MessagePipeDispatcher(GetNodeController(), port1, pipe_id, 1));
451 if (*message_pipe_handle1 == MOJO_HANDLE_INVALID) { 451 if (*message_pipe_handle1 == MOJO_HANDLE_INVALID) {
452 scoped_refptr<Dispatcher> unused; 452 scoped_refptr<Dispatcher> unused;
453 unused->Close(); 453 unused->Close();
454
455 base::AutoLock lock(handles_lock_);
454 handles_.GetAndRemoveDispatcher(*message_pipe_handle0, &unused); 456 handles_.GetAndRemoveDispatcher(*message_pipe_handle0, &unused);
455 return MOJO_RESULT_RESOURCE_EXHAUSTED; 457 return MOJO_RESULT_RESOURCE_EXHAUSTED;
456 } 458 }
457 459
458 return MOJO_RESULT_OK; 460 return MOJO_RESULT_OK;
459 } 461 }
460 462
461 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, 463 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle,
462 const void* bytes, 464 const void* bytes,
463 uint32_t num_bytes, 465 uint32_t num_bytes,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 MojoReadMessageFlags flags) { 518 MojoReadMessageFlags flags) {
517 RequestContext request_context; 519 RequestContext request_context;
518 CHECK((!num_handles || !*num_handles || handles) && 520 CHECK((!num_handles || !*num_handles || handles) &&
519 (!num_bytes || !*num_bytes || bytes)); 521 (!num_bytes || !*num_bytes || bytes));
520 auto dispatcher = GetDispatcher(message_pipe_handle); 522 auto dispatcher = GetDispatcher(message_pipe_handle);
521 if (!dispatcher) 523 if (!dispatcher)
522 return MOJO_RESULT_INVALID_ARGUMENT; 524 return MOJO_RESULT_INVALID_ARGUMENT;
523 return dispatcher->ReadMessage(bytes, num_bytes, handles, num_handles, flags); 525 return dispatcher->ReadMessage(bytes, num_bytes, handles, num_handles, flags);
524 } 526 }
525 527
528 MojoResult Core::FuseMessagePipes(MojoHandle handle0, MojoHandle handle1) {
529 RequestContext request_context;
530 scoped_refptr<Dispatcher> dispatcher0 = GetDispatcher(handle0);
531 scoped_refptr<Dispatcher> dispatcher1 = GetDispatcher(handle1);
532
533 if (!dispatcher0 || !dispatcher1)
534 return MOJO_RESULT_INVALID_ARGUMENT;
535
536 if (dispatcher0->GetType() != Dispatcher::Type::MESSAGE_PIPE ||
537 dispatcher1->GetType() != Dispatcher::Type::MESSAGE_PIPE) {
538 return MOJO_RESULT_INVALID_ARGUMENT;
539 }
540
541 // Both handles are valid message pipe handles. Beyond this point, both
542 // handles are always invalidated.
543 {
544 base::AutoLock lock(handles_lock_);
545
546 // Someone may have closed the handles while we were checking them above, so
547 // we need to handle that as we remove them from the handle table.
548 MojoResult result0 = handles_.GetAndRemoveDispatcher(handle0, &dispatcher0);
549 MojoResult result1 = handles_.GetAndRemoveDispatcher(handle1, &dispatcher1);
550 if (result0 != MOJO_RESULT_OK)
551 dispatcher0 = nullptr;
552 if (result1 != MOJO_RESULT_OK)
553 dispatcher1 = nullptr;
554
555 if (!dispatcher0 || !dispatcher1) {
556 if (dispatcher0)
557 dispatcher0->Close();
558 if (dispatcher1)
559 dispatcher1->Close();
560 return MOJO_RESULT_FAILED_PRECONDITION;
561 }
562 }
563
564 DCHECK(dispatcher0 && dispatcher1);
565
566 MessagePipeDispatcher* mpd0 =
567 static_cast<MessagePipeDispatcher*>(dispatcher0.get());
568 MessagePipeDispatcher* mpd1 =
569 static_cast<MessagePipeDispatcher*>(dispatcher1.get());
570
571 if (!mpd0->BeginFuse()) {
572 mpd0->Close();
573 mpd1->Close();
574 return MOJO_RESULT_FAILED_PRECONDITION;
575 }
576
577 if (!mpd1->BeginFuse()) {
578 mpd0->CancelFuse();
579 mpd0->Close();
580 mpd1->Close();
581 return MOJO_RESULT_FAILED_PRECONDITION;
582 }
583
584 if (!mpd0->CompleteFuse(mpd1))
585 return MOJO_RESULT_FAILED_PRECONDITION;
586
587 return MOJO_RESULT_OK;
588 }
589
526 MojoResult Core::CreateDataPipe( 590 MojoResult Core::CreateDataPipe(
527 const MojoCreateDataPipeOptions* options, 591 const MojoCreateDataPipeOptions* options,
528 MojoHandle* data_pipe_producer_handle, 592 MojoHandle* data_pipe_producer_handle,
529 MojoHandle* data_pipe_consumer_handle) { 593 MojoHandle* data_pipe_consumer_handle) {
530 RequestContext request_context; 594 RequestContext request_context;
531 if (options && options->struct_size != sizeof(MojoCreateDataPipeOptions)) 595 if (options && options->struct_size != sizeof(MojoCreateDataPipeOptions))
532 return MOJO_RESULT_INVALID_ARGUMENT; 596 return MOJO_RESULT_INVALID_ARGUMENT;
533 597
534 MojoCreateDataPipeOptions create_options; 598 MojoCreateDataPipeOptions create_options;
535 create_options.struct_size = sizeof(MojoCreateDataPipeOptions); 599 create_options.struct_size = sizeof(MojoCreateDataPipeOptions);
(...skipping 25 matching lines...) Expand all
561 scoped_refptr<Dispatcher> consumer = new DataPipeConsumerDispatcher( 625 scoped_refptr<Dispatcher> consumer = new DataPipeConsumerDispatcher(
562 GetNodeController(), port1, ring_buffer, create_options, 626 GetNodeController(), port1, ring_buffer, create_options,
563 true /* initialized */, pipe_id); 627 true /* initialized */, pipe_id);
564 628
565 *data_pipe_producer_handle = AddDispatcher(producer); 629 *data_pipe_producer_handle = AddDispatcher(producer);
566 *data_pipe_consumer_handle = AddDispatcher(consumer); 630 *data_pipe_consumer_handle = AddDispatcher(consumer);
567 if (*data_pipe_producer_handle == MOJO_HANDLE_INVALID || 631 if (*data_pipe_producer_handle == MOJO_HANDLE_INVALID ||
568 *data_pipe_consumer_handle == MOJO_HANDLE_INVALID) { 632 *data_pipe_consumer_handle == MOJO_HANDLE_INVALID) {
569 if (*data_pipe_producer_handle != MOJO_HANDLE_INVALID) { 633 if (*data_pipe_producer_handle != MOJO_HANDLE_INVALID) {
570 scoped_refptr<Dispatcher> unused; 634 scoped_refptr<Dispatcher> unused;
635 base::AutoLock lock(handles_lock_);
571 handles_.GetAndRemoveDispatcher(*data_pipe_producer_handle, &unused); 636 handles_.GetAndRemoveDispatcher(*data_pipe_producer_handle, &unused);
572 } 637 }
573 producer->Close(); 638 producer->Close();
574 consumer->Close(); 639 consumer->Close();
575 return MOJO_RESULT_RESOURCE_EXHAUSTED; 640 return MOJO_RESULT_RESOURCE_EXHAUSTED;
576 } 641 }
577 642
578 return MOJO_RESULT_OK; 643 return MOJO_RESULT_OK;
579 } 644 }
580 645
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 scoped_ptr<NodeController> node_controller) { 884 scoped_ptr<NodeController> node_controller) {
820 // It's OK to leak this reference. At this point we know the IO loop is still 885 // It's OK to leak this reference. At this point we know the IO loop is still
821 // running, and we know the NodeController will observe its eventual 886 // running, and we know the NodeController will observe its eventual
822 // destruction. This tells the NodeController to delete itself when that 887 // destruction. This tells the NodeController to delete itself when that
823 // happens. 888 // happens.
824 node_controller.release()->DestroyOnIOThreadShutdown(); 889 node_controller.release()->DestroyOnIOThreadShutdown();
825 } 890 }
826 891
827 } // namespace edk 892 } // namespace edk
828 } // namespace mojo 893 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698