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

Side by Side Diff: mojo/dart/embedder/mojo_natives.cc

Issue 1395043003: Use a singleton instead of a global for MojoWaitManyState (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stdio.h> 5 #include <stdio.h>
6 #include <string.h> 6 #include <string.h>
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
13 #include "dart/runtime/include/dart_api.h" 14 #include "dart/runtime/include/dart_api.h"
14 #include "mojo/dart/embedder/builtin.h" 15 #include "mojo/dart/embedder/builtin.h"
15 #include "mojo/dart/embedder/mojo_dart_state.h" 16 #include "mojo/dart/embedder/mojo_dart_state.h"
16 #include "mojo/public/c/system/core.h" 17 #include "mojo/public/c/system/core.h"
17 #include "mojo/public/cpp/system/core.h" 18 #include "mojo/public/cpp/system/core.h"
18 19
19 namespace mojo { 20 namespace mojo {
20 namespace dart { 21 namespace dart {
21 22
22 #define MOJO_NATIVE_LIST(V) \ 23 #define MOJO_NATIVE_LIST(V) \
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 Dart_ListSetAt(list, 1, Dart_NewInteger(blen)); 721 Dart_ListSetAt(list, 1, Dart_NewInteger(blen));
721 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen)); 722 Dart_ListSetAt(list, 2, Dart_NewInteger(hlen));
722 Dart_SetReturnValue(arguments, list); 723 Dart_SetReturnValue(arguments, list);
723 } 724 }
724 725
725 struct MojoWaitManyState { 726 struct MojoWaitManyState {
726 std::vector<uint32_t> handles; 727 std::vector<uint32_t> handles;
727 std::vector<uint32_t> signals; 728 std::vector<uint32_t> signals;
728 std::vector<uint32_t> out_index; 729 std::vector<uint32_t> out_index;
729 std::vector<MojoHandleSignalsState> out_signals; 730 std::vector<MojoHandleSignalsState> out_signals;
731
732 static MojoWaitManyState* GetInstance();
730 }; 733 };
731 734
732 // This global is safe because it is only accessed by the single handle watcher 735 // This global is safe because it is only accessed by the single handle watcher
733 // isolate. If multiple handle watcher isolates are ever needed, it will need 736 // isolate. If multiple handle watcher isolates are ever needed, it will need
734 // to be replicated. 737 // to be replicated.
735 static MojoWaitManyState handle_watcher_wait_state; 738 MojoWaitManyState* MojoWaitManyState::GetInstance() {
739 return Singleton<MojoWaitManyState>::get();
jamesr 2015/10/09 23:42:32 Singleton<> here is providing thread-safe initiali
jsimmons1 2015/10/09 23:50:46 Done.
740 }
736 741
737 void MojoHandleWatcher_GrowStateArrays(Dart_NativeArguments arguments) { 742 void MojoHandleWatcher_GrowStateArrays(Dart_NativeArguments arguments) {
738 int64_t new_length; 743 int64_t new_length;
739 CHECK_INTEGER_ARGUMENT(arguments, 0, &new_length, InvalidArgument); 744 CHECK_INTEGER_ARGUMENT(arguments, 0, &new_length, InvalidArgument);
740 745
741 handle_watcher_wait_state.handles.resize(new_length); 746 MojoWaitManyState* handle_watcher_wait_state =
jamesr 2015/10/09 23:42:32 Make this a reference and you won't have to change
abarth-chromium 2015/10/09 23:45:46 If you make this a reference, please add DISALLOW_
jsimmons1 2015/10/09 23:50:46 Done.
742 handle_watcher_wait_state.signals.resize(new_length); 747 MojoWaitManyState::GetInstance();
743 handle_watcher_wait_state.out_index.resize(1); 748
744 handle_watcher_wait_state.out_signals.resize(new_length); 749 handle_watcher_wait_state->handles.resize(new_length);
750 handle_watcher_wait_state->signals.resize(new_length);
751 handle_watcher_wait_state->out_index.resize(1);
752 handle_watcher_wait_state->out_signals.resize(new_length);
745 753
746 Dart_Handle dart_handles = Dart_NewExternalTypedData( 754 Dart_Handle dart_handles = Dart_NewExternalTypedData(
747 Dart_TypedData_kUint32, handle_watcher_wait_state.handles.data(), 755 Dart_TypedData_kUint32, handle_watcher_wait_state->handles.data(),
748 handle_watcher_wait_state.handles.size()); 756 handle_watcher_wait_state->handles.size());
749 if (Dart_IsError(dart_handles)) { 757 if (Dart_IsError(dart_handles)) {
750 Dart_PropagateError(dart_handles); 758 Dart_PropagateError(dart_handles);
751 } 759 }
752 if (Dart_IsNull(dart_handles)) { 760 if (Dart_IsNull(dart_handles)) {
753 SetNullReturn(arguments); 761 SetNullReturn(arguments);
754 return; 762 return;
755 } 763 }
756 764
757 Dart_Handle dart_signals = Dart_NewExternalTypedData( 765 Dart_Handle dart_signals = Dart_NewExternalTypedData(
758 Dart_TypedData_kUint32, handle_watcher_wait_state.signals.data(), 766 Dart_TypedData_kUint32, handle_watcher_wait_state->signals.data(),
759 handle_watcher_wait_state.signals.size()); 767 handle_watcher_wait_state->signals.size());
760 if (Dart_IsError(dart_signals)) { 768 if (Dart_IsError(dart_signals)) {
761 Dart_PropagateError(dart_signals); 769 Dart_PropagateError(dart_signals);
762 } 770 }
763 if (Dart_IsNull(dart_signals)) { 771 if (Dart_IsNull(dart_signals)) {
764 SetNullReturn(arguments); 772 SetNullReturn(arguments);
765 return; 773 return;
766 } 774 }
767 775
768 Dart_Handle dart_out_index = Dart_NewExternalTypedData( 776 Dart_Handle dart_out_index = Dart_NewExternalTypedData(
769 Dart_TypedData_kUint32, handle_watcher_wait_state.out_index.data(), 777 Dart_TypedData_kUint32, handle_watcher_wait_state->out_index.data(),
770 handle_watcher_wait_state.out_index.size()); 778 handle_watcher_wait_state->out_index.size());
771 if (Dart_IsError(dart_out_index)) { 779 if (Dart_IsError(dart_out_index)) {
772 Dart_PropagateError(dart_out_index); 780 Dart_PropagateError(dart_out_index);
773 } 781 }
774 if (Dart_IsNull(dart_out_index)) { 782 if (Dart_IsNull(dart_out_index)) {
775 SetNullReturn(arguments); 783 SetNullReturn(arguments);
776 return; 784 return;
777 } 785 }
778 786
779 Dart_Handle dart_out_signals = Dart_NewExternalTypedData( 787 Dart_Handle dart_out_signals = Dart_NewExternalTypedData(
780 Dart_TypedData_kUint64, handle_watcher_wait_state.out_signals.data(), 788 Dart_TypedData_kUint64, handle_watcher_wait_state->out_signals.data(),
781 handle_watcher_wait_state.out_signals.size()); 789 handle_watcher_wait_state->out_signals.size());
782 if (Dart_IsError(dart_out_signals)) { 790 if (Dart_IsError(dart_out_signals)) {
783 Dart_PropagateError(dart_out_signals); 791 Dart_PropagateError(dart_out_signals);
784 } 792 }
785 if (Dart_IsNull(dart_out_signals)) { 793 if (Dart_IsNull(dart_out_signals)) {
786 SetNullReturn(arguments); 794 SetNullReturn(arguments);
787 return; 795 return;
788 } 796 }
789 797
790 Dart_Handle list = Dart_NewList(4); 798 Dart_Handle list = Dart_NewList(4);
791 Dart_ListSetAt(list, 0, dart_handles); 799 Dart_ListSetAt(list, 0, dart_handles);
792 Dart_ListSetAt(list, 1, dart_signals); 800 Dart_ListSetAt(list, 1, dart_signals);
793 Dart_ListSetAt(list, 2, dart_out_index); 801 Dart_ListSetAt(list, 2, dart_out_index);
794 Dart_ListSetAt(list, 3, dart_out_signals); 802 Dart_ListSetAt(list, 3, dart_out_signals);
795 Dart_SetReturnValue(arguments, list); 803 Dart_SetReturnValue(arguments, list);
796 } 804 }
797 805
798 void MojoHandleWatcher_WaitMany(Dart_NativeArguments arguments) { 806 void MojoHandleWatcher_WaitMany(Dart_NativeArguments arguments) {
799 int64_t handles_len = 0; 807 int64_t handles_len = 0;
800 int64_t deadline = 0; 808 int64_t deadline = 0;
801 CHECK_INTEGER_ARGUMENT(arguments, 0, &handles_len, InvalidArgument); 809 CHECK_INTEGER_ARGUMENT(arguments, 0, &handles_len, InvalidArgument);
802 CHECK_INTEGER_ARGUMENT(arguments, 1, &deadline, InvalidArgument); 810 CHECK_INTEGER_ARGUMENT(arguments, 1, &deadline, InvalidArgument);
803 811
804 uint32_t* handles = handle_watcher_wait_state.handles.data(); 812 MojoWaitManyState* handle_watcher_wait_state =
805 uint32_t* signals = handle_watcher_wait_state.signals.data(); 813 MojoWaitManyState::GetInstance();
806 uint32_t* out_index = handle_watcher_wait_state.out_index.data(); 814
815 uint32_t* handles = handle_watcher_wait_state->handles.data();
816 uint32_t* signals = handle_watcher_wait_state->signals.data();
817 uint32_t* out_index = handle_watcher_wait_state->out_index.data();
807 MojoHandleSignalsState* out_signals = 818 MojoHandleSignalsState* out_signals =
808 handle_watcher_wait_state.out_signals.data(); 819 handle_watcher_wait_state->out_signals.data();
809 820
810 Dart_IsolateBlocked(); 821 Dart_IsolateBlocked();
811 MojoResult mojo_result = MojoWaitMany(handles, signals, handles_len, deadline, 822 MojoResult mojo_result = MojoWaitMany(handles, signals, handles_len, deadline,
812 out_index, out_signals); 823 out_index, out_signals);
813 Dart_IsolateUnblocked(); 824 Dart_IsolateUnblocked();
814 825
815 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(mojo_result)); 826 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(mojo_result));
816 } 827 }
817 828
818 struct ControlData { 829 struct ControlData {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 mojo_control_handle = control_handle; 892 mojo_control_handle = control_handle;
882 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); 893 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK));
883 } 894 }
884 895
885 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { 896 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) {
886 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); 897 Dart_SetIntegerReturnValue(arguments, mojo_control_handle);
887 } 898 }
888 899
889 } // namespace dart 900 } // namespace dart
890 } // namespace mojo 901 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698