diff options
author | Daniel Cheng <dcheng@chromium.org> | 2014-12-30 14:32:48 -0800 |
---|---|---|
committer | Daniel Cheng <dcheng@chromium.org> | 2014-12-30 22:33:56 +0000 |
commit | 0934dff0ed3de5b15deb676d6b6e2fa8c0cbe828 (patch) | |
tree | 27752a77555b3a32e9689d4687c1e45e5a3e058d /mojo/android | |
parent | 39a76697e0fe1769e2ebbcf2c302b02aeedd5ed5 (diff) | |
download | chromium_src-0934dff0ed3de5b15deb676d6b6e2fa8c0cbe828.zip chromium_src-0934dff0ed3de5b15deb676d6b6e2fa8c0cbe828.tar.gz chromium_src-0934dff0ed3de5b15deb676d6b6e2fa8c0cbe828.tar.bz2 |
Update mojo sdk to rev cc531b32182099a5a034a99daff35ed5d38a61c8
BUG=none
R=jamesr@chromium.org
TBR=jbudorick@chromium.org, thestig@chromium.org
Review URL: https://codereview.chromium.org/795593004
Cr-Commit-Position: refs/heads/master@{#309785}
Diffstat (limited to 'mojo/android')
5 files changed, 228 insertions, 174 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java b/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java index 9a7b473..427fbec 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java +++ b/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java @@ -5,6 +5,7 @@ package org.chromium.mojo; import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.Core.WaitResult; import org.chromium.mojo.system.DataPipe; import org.chromium.mojo.system.DataPipe.ConsumerHandle; import org.chromium.mojo.system.DataPipe.ProducerHandle; @@ -36,9 +37,11 @@ public class HandleMock implements UntypedHandle, MessagePipeHandle, * @see Handle#wait(Core.HandleSignals, long) */ @Override - public int wait(Core.HandleSignals signals, long deadline) { + public WaitResult wait(Core.HandleSignals signals, long deadline) { // Do nothing. - return MojoResult.OK; + WaitResult result = new WaitResult(); + result.setMojoResult(MojoResult.OK); + return result; } /** diff --git a/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java b/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java index 9c4718c..b35e119 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java +++ b/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java @@ -11,7 +11,10 @@ import org.chromium.mojo.system.AsyncWaiter; import org.chromium.mojo.system.AsyncWaiter.Callback; import org.chromium.mojo.system.AsyncWaiter.Cancellable; import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.Core.HandleSignals; +import org.chromium.mojo.system.Core.HandleSignalsState; import org.chromium.mojo.system.Core.WaitManyResult; +import org.chromium.mojo.system.Core.WaitResult; import org.chromium.mojo.system.DataPipe; import org.chromium.mojo.system.Handle; import org.chromium.mojo.system.InvalidHandle; @@ -35,12 +38,14 @@ import java.util.concurrent.TimeUnit; * Testing the core API. */ public class CoreImplTest extends MojoTestCase { - private static final long RUN_LOOP_TIMEOUT_MS = 5; private static final ScheduledExecutorService WORKER = Executors.newSingleThreadScheduledExecutor(); + private static final HandleSignals ALL_SIGNALS = + HandleSignals.none().setPeerClosed(true).setReadable(true).setWritable(true); + private List<Handle> mHandlesToClose = new ArrayList<Handle>(); /** @@ -101,8 +106,8 @@ public class CoreImplTest extends MojoTestCase { // Try to read into a small buffer. ByteBuffer receiveBuffer = ByteBuffer.allocateDirect(bytes.length / 2); - MessagePipeHandle.ReadMessageResult result = out.readMessage( - receiveBuffer, 0, MessagePipeHandle.ReadFlags.NONE); + MessagePipeHandle.ReadMessageResult result = + out.readMessage(receiveBuffer, 0, MessagePipeHandle.ReadFlags.NONE); assertEquals(MojoResult.RESOURCE_EXHAUSTED, result.getMojoResult()); assertEquals(bytes.length, result.getMessageSize()); assertEquals(0, result.getHandlesCount()); @@ -118,7 +123,6 @@ public class CoreImplTest extends MojoTestCase { byte[] receivedBytes = new byte[result.getMessageSize()]; receiveBuffer.get(receivedBytes); assertTrue(Arrays.equals(bytes, receivedBytes)); - } private static void checkSendingData(DataPipe.ProducerHandle in, DataPipe.ConsumerHandle out) { @@ -136,6 +140,16 @@ public class CoreImplTest extends MojoTestCase { result = out.readData(null, DataPipe.ReadFlags.none().query(true)); assertEquals(bytes.length, result); + // Peek data into a buffer. + ByteBuffer peekBuffer = ByteBuffer.allocateDirect(bytes.length); + result = out.readData(peekBuffer, DataPipe.ReadFlags.none().peek(true)); + assertEquals(bytes.length, result); + assertEquals(0, peekBuffer.position()); + assertEquals(bytes.length, peekBuffer.limit()); + byte[] peekBytes = new byte[bytes.length]; + peekBuffer.get(peekBytes); + assertTrue(Arrays.equals(bytes, peekBytes)); + // Read into a buffer. ByteBuffer receiveBuffer = ByteBuffer.allocateDirect(bytes.length); result = out.readData(receiveBuffer, DataPipe.ReadFlags.NONE); @@ -177,8 +191,10 @@ public class CoreImplTest extends MojoTestCase { Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null); addHandlePairToClose(handles); - List<Pair<Handle, Core.HandleSignals>> handlesToWaitOn = new ArrayList< - Pair<Handle, Core.HandleSignals>>(); + // Test waiting on handles of a newly created message pipe - each should be writable, but + // not readable. + List<Pair<Handle, Core.HandleSignals>> handlesToWaitOn = + new ArrayList<Pair<Handle, Core.HandleSignals>>(); handlesToWaitOn.add( new Pair<Handle, Core.HandleSignals>(handles.second, Core.HandleSignals.READABLE)); handlesToWaitOn.add( @@ -186,7 +202,12 @@ public class CoreImplTest extends MojoTestCase { WaitManyResult result = core.waitMany(handlesToWaitOn, 0); assertEquals(MojoResult.OK, result.getMojoResult()); assertEquals(1, result.getHandleIndex()); + for (HandleSignalsState state : result.getSignalStates()) { + assertEquals(HandleSignals.WRITABLE, state.getSatisfiedSignals()); + assertEquals(ALL_SIGNALS, state.getSatisfiableSignals()); + } + // Same test, but swap the handles around. handlesToWaitOn.clear(); handlesToWaitOn.add( new Pair<Handle, Core.HandleSignals>(handles.first, Core.HandleSignals.WRITABLE)); @@ -195,6 +216,10 @@ public class CoreImplTest extends MojoTestCase { result = core.waitMany(handlesToWaitOn, 0); assertEquals(MojoResult.OK, result.getMojoResult()); assertEquals(0, result.getHandleIndex()); + for (HandleSignalsState state : result.getSignalStates()) { + assertEquals(HandleSignals.WRITABLE, state.getSatisfiedSignals()); + assertEquals(ALL_SIGNALS, state.getSatisfiableSignals()); + } } /** @@ -248,37 +273,53 @@ public class CoreImplTest extends MojoTestCase { Core core = CoreImpl.getInstance(); Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null); addHandlePairToClose(handles); - // Testing wait. - assertEquals(MojoResult.OK, - handles.first.wait(Core.HandleSignals.none().setReadable(true).setWritable(true), - 0)); - assertEquals(MojoResult.OK, handles.first.wait(Core.HandleSignals.WRITABLE, 0)); - assertEquals(MojoResult.DEADLINE_EXCEEDED, - handles.first.wait(Core.HandleSignals.READABLE, 0)); + // Test waiting on handles of a newly created message pipe. + WaitResult waitResult = handles.first.wait( + Core.HandleSignals.none().setReadable(true).setWritable(true), 0); + assertEquals(MojoResult.OK, waitResult.getMojoResult()); + assertEquals( + HandleSignals.WRITABLE, waitResult.getHandleSignalsState().getSatisfiedSignals()); + assertEquals(ALL_SIGNALS, waitResult.getHandleSignalsState().getSatisfiableSignals()); + + waitResult = handles.first.wait(Core.HandleSignals.WRITABLE, 0); + assertEquals(MojoResult.OK, waitResult.getMojoResult()); + assertEquals( + HandleSignals.WRITABLE, waitResult.getHandleSignalsState().getSatisfiedSignals()); + assertEquals(ALL_SIGNALS, waitResult.getHandleSignalsState().getSatisfiableSignals()); + + waitResult = handles.first.wait(Core.HandleSignals.READABLE, 0); + assertEquals(MojoResult.DEADLINE_EXCEEDED, waitResult.getMojoResult()); + assertEquals( + HandleSignals.WRITABLE, waitResult.getHandleSignalsState().getSatisfiedSignals()); + assertEquals(ALL_SIGNALS, waitResult.getHandleSignalsState().getSatisfiableSignals()); // Testing read on an empty pipe. - MessagePipeHandle.ReadMessageResult result = handles.first.readMessage(null, 0, - MessagePipeHandle.ReadFlags.NONE); - assertEquals(MojoResult.SHOULD_WAIT, result.getMojoResult()); + MessagePipeHandle.ReadMessageResult readResult = + handles.first.readMessage(null, 0, MessagePipeHandle.ReadFlags.NONE); + assertEquals(MojoResult.SHOULD_WAIT, readResult.getMojoResult()); // Closing a pipe while waiting. WORKER.schedule(new CloseHandle(handles.first), 10, TimeUnit.MILLISECONDS); - assertEquals(MojoResult.CANCELLED, - handles.first.wait(Core.HandleSignals.READABLE, 1000000L)); + waitResult = handles.first.wait(Core.HandleSignals.READABLE, 1000000L); + assertEquals(MojoResult.CANCELLED, waitResult.getMojoResult()); + assertEquals( + HandleSignals.none(), waitResult.getHandleSignalsState().getSatisfiedSignals()); + assertEquals( + HandleSignals.none(), waitResult.getHandleSignalsState().getSatisfiableSignals()); handles = core.createMessagePipe(null); addHandlePairToClose(handles); // Closing the other pipe while waiting. WORKER.schedule(new CloseHandle(handles.first), 10, TimeUnit.MILLISECONDS); - assertEquals(MojoResult.FAILED_PRECONDITION, - handles.second.wait(Core.HandleSignals.READABLE, 1000000L)); + waitResult = handles.second.wait(Core.HandleSignals.READABLE, 1000000L); + assertEquals(MojoResult.FAILED_PRECONDITION, waitResult.getMojoResult()); // Waiting on a closed pipe. - assertEquals(MojoResult.FAILED_PRECONDITION, - handles.second.wait(Core.HandleSignals.READABLE, 0)); - assertEquals(MojoResult.FAILED_PRECONDITION, - handles.second.wait(Core.HandleSignals.WRITABLE, 0)); + waitResult = handles.second.wait(Core.HandleSignals.READABLE, 0); + assertEquals(MojoResult.FAILED_PRECONDITION, waitResult.getMojoResult()); + waitResult = handles.second.wait(Core.HandleSignals.WRITABLE, 0); + assertEquals(MojoResult.FAILED_PRECONDITION, waitResult.getMojoResult()); } /** @@ -312,8 +353,8 @@ public class CoreImplTest extends MojoTestCase { handles.first.writeMessage(buffer, null, MessagePipeHandle.WriteFlags.NONE); ByteBuffer receiveBuffer = ByteBuffer.allocateDirect(1); - MessagePipeHandle.ReadMessageResult result = handles.second - .readMessage(receiveBuffer, 0, MessagePipeHandle.ReadFlags.NONE); + MessagePipeHandle.ReadMessageResult result = + handles.second.readMessage(receiveBuffer, 0, MessagePipeHandle.ReadFlags.NONE); assertEquals(MojoResult.RESOURCE_EXHAUSTED, result.getMojoResult()); assertEquals(bytes.length, result.getMessageSize()); assertEquals(0, result.getHandlesCount()); @@ -330,15 +371,13 @@ public class CoreImplTest extends MojoTestCase { addHandlePairToClose(handles); addHandlePairToClose(handlesToShare); - handles.first.writeMessage(null, - Collections.<Handle> singletonList(handlesToShare.second), + handles.first.writeMessage(null, Collections.<Handle>singletonList(handlesToShare.second), MessagePipeHandle.WriteFlags.NONE); assertFalse(handlesToShare.second.isValid()); MessagePipeHandle.ReadMessageResult readMessageResult = handles.second.readMessage(null, 1, MessagePipeHandle.ReadFlags.NONE); assertEquals(1, readMessageResult.getHandlesCount()); - MessagePipeHandle newHandle = readMessageResult.getHandles().get(0) - .toMessagePipeHandle(); + MessagePipeHandle newHandle = readMessageResult.getHandles().get(0).toMessagePipeHandle(); addHandleToClose(newHandle); assertTrue(newHandle.isValid()); checkSendingMessage(handlesToShare.first, newHandle); @@ -347,8 +386,8 @@ public class CoreImplTest extends MojoTestCase { private static void createAndCloseDataPipe(DataPipe.CreateOptions options) { Core core = CoreImpl.getInstance(); - Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> handles = core.createDataPipe( - options); + Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> handles = + core.createDataPipe(options); handles.first.close(); handles.second.close(); } @@ -398,15 +437,14 @@ public class CoreImplTest extends MojoTestCase { // Writing a random 8 bytes message. byte[] bytes = new byte[8]; random.nextBytes(bytes); - ByteBuffer buffer = handles.first.beginWriteData(bytes.length, - DataPipe.WriteFlags.NONE); + ByteBuffer buffer = handles.first.beginWriteData(bytes.length, DataPipe.WriteFlags.NONE); assertTrue(buffer.capacity() >= bytes.length); buffer.put(bytes); handles.first.endWriteData(bytes.length); // Read into a buffer. - ByteBuffer receiveBuffer = handles.second.beginReadData(bytes.length, - DataPipe.ReadFlags.NONE); + ByteBuffer receiveBuffer = + handles.second.beginReadData(bytes.length, DataPipe.ReadFlags.NONE); assertEquals(0, receiveBuffer.position()); assertEquals(bytes.length, receiveBuffer.limit()); byte[] receivedBytes = new byte[bytes.length]; @@ -446,8 +484,8 @@ public class CoreImplTest extends MojoTestCase { assertEquals(bytes.length - nbBytesToDiscard, receiveBuffer.limit()); byte[] receivedBytes = new byte[bytes.length - nbBytesToDiscard]; receiveBuffer.get(receivedBytes); - assertTrue(Arrays.equals(Arrays.copyOfRange(bytes, nbBytesToDiscard, bytes.length), - receivedBytes)); + assertTrue(Arrays.equals( + Arrays.copyOfRange(bytes, nbBytesToDiscard, bytes.length), receivedBytes)); } /** @@ -513,8 +551,8 @@ public class CoreImplTest extends MojoTestCase { // Checking waitMany. exception = false; try { - List<Pair<Handle, Core.HandleSignals>> handles = new ArrayList< - Pair<Handle, Core.HandleSignals>>(); + List<Pair<Handle, Core.HandleSignals>> handles = + new ArrayList<Pair<Handle, Core.HandleSignals>>(); handles.add(Pair.create(handle, Core.HandleSignals.WRITABLE)); core.waitMany(handles, 0); } catch (MojoException e) { @@ -530,7 +568,7 @@ public class CoreImplTest extends MojoTestCase { Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null); addHandlePairToClose(handles); try { - handles.first.writeMessage(null, Collections.<Handle> singletonList(handle), + handles.first.writeMessage(null, Collections.<Handle>singletonList(handle), MessagePipeHandle.WriteFlags.NONE); MessagePipeHandle.ReadMessageResult readMessageResult = handles.second.readMessage(null, 1, MessagePipeHandle.ReadFlags.NONE); @@ -574,7 +612,6 @@ public class CoreImplTest extends MojoTestCase { public MojoException getException() { return mException; } - } /** @@ -596,8 +633,8 @@ public class CoreImplTest extends MojoTestCase { assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); - handles.second.writeMessage(ByteBuffer.allocateDirect(1), null, - MessagePipeHandle.WriteFlags.NONE); + handles.second.writeMessage( + ByteBuffer.allocateDirect(1), null, MessagePipeHandle.WriteFlags.NONE); runLoopUntilIdle(); assertNull(asyncWaiterResult.getException()); assertEquals(MojoResult.OK, asyncWaiterResult.getResult()); @@ -679,8 +716,8 @@ public class CoreImplTest extends MojoTestCase { assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); - core.getDefaultAsyncWaiter().asyncWait(handles.first, Core.HandleSignals.READABLE, - RUN_LOOP_TIMEOUT_MS, asyncWaiterResult); + core.getDefaultAsyncWaiter().asyncWait( + handles.first, Core.HandleSignals.READABLE, RUN_LOOP_TIMEOUT_MS, asyncWaiterResult); assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); @@ -718,8 +755,8 @@ public class CoreImplTest extends MojoTestCase { assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); - handles.second.writeMessage(ByteBuffer.allocateDirect(1), null, - MessagePipeHandle.WriteFlags.NONE); + handles.second.writeMessage( + ByteBuffer.allocateDirect(1), null, MessagePipeHandle.WriteFlags.NONE); runLoopUntilIdle(); assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); diff --git a/mojo/android/system/core_impl.cc b/mojo/android/system/core_impl.cc index b862130..35d1b36 100644 --- a/mojo/android/system/core_impl.cc +++ b/mojo/android/system/core_impl.cc @@ -57,19 +57,35 @@ static jint WaitMany(JNIEnv* env, jobject jcaller, jobject buffer, jlong deadline) { - // Buffer contains first the list of handles, then the list of signals. - const void* buffer_start = env->GetDirectBufferAddress(buffer); + // |buffer| contains, in this order + // input: The array of N handles (MojoHandle, 4 bytes each) + // input: The array of N signals (MojoHandleSignals, 4 bytes each) + // space for output: The array of N handle states (MojoHandleSignalsState, 8 + // bytes each) + // space for output: The result index (uint32_t, 4 bytes) + uint8_t* buffer_start = + static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer)); DCHECK(buffer_start); - DCHECK_EQ(reinterpret_cast<const uintptr_t>(buffer_start) % 8, 0u); - const size_t record_size = 8; + DCHECK_EQ(reinterpret_cast<uintptr_t>(buffer_start) % 8, 0u); + // Each handle of the input array contributes 4 (MojoHandle) + 4 + // (MojoHandleSignals) + 8 (MojoHandleSignalsState) = 16 bytes to the size of + // the buffer. + const size_t size_per_handle = 16; const size_t buffer_size = env->GetDirectBufferCapacity(buffer); - DCHECK_EQ(buffer_size % record_size, 0u); + DCHECK_EQ((buffer_size - 4) % size_per_handle, 0u); - const size_t nb_handles = buffer_size / record_size; - const MojoHandle* handle_start = static_cast<const MojoHandle*>(buffer_start); + const size_t nb_handles = (buffer_size - 4) / size_per_handle; + const MojoHandle* handle_start = + reinterpret_cast<const MojoHandle*>(buffer_start); const MojoHandleSignals* signals_start = - static_cast<const MojoHandleSignals*>(handle_start + nb_handles); - return MojoWaitMany(handle_start, signals_start, nb_handles, deadline); + reinterpret_cast<const MojoHandleSignals*>(buffer_start + 4 * nb_handles); + MojoHandleSignalsState* states_start = + reinterpret_cast<MojoHandleSignalsState*>(buffer_start + 8 * nb_handles); + uint32_t* result_index = + reinterpret_cast<uint32_t*>(buffer_start + 16 * nb_handles); + *result_index = static_cast<uint32_t>(-1); + return MojoNewWaitMany(handle_start, signals_start, nb_handles, deadline, + result_index, states_start); } static jobject CreateMessagePipe(JNIEnv* env, @@ -138,10 +154,19 @@ static jint Close(JNIEnv* env, jobject jcaller, jint mojo_handle) { static jint Wait(JNIEnv* env, jobject jcaller, + jobject buffer, jint mojo_handle, jint signals, jlong deadline) { - return MojoWait(mojo_handle, signals, deadline); + // Buffer contains space for the MojoHandleSignalsState + void* buffer_start = env->GetDirectBufferAddress(buffer); + DCHECK(buffer_start); + DCHECK_EQ(reinterpret_cast<const uintptr_t>(buffer_start) % 8, 0u); + DCHECK_EQ(sizeof(struct MojoHandleSignalsState), + static_cast<size_t>(env->GetDirectBufferCapacity(buffer))); + struct MojoHandleSignalsState* signals_state = + static_cast<struct MojoHandleSignalsState*>(buffer_start); + return MojoNewWait(mojo_handle, signals, deadline, signals_state); } static jint WriteMessage(JNIEnv* env, diff --git a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java index 1fd8076..0dc9839 100644 --- a/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java +++ b/mojo/android/system/src/org/chromium/mojo/system/impl/CoreImpl.java @@ -31,7 +31,6 @@ import java.util.List; */ @JNINamespace("mojo::android") public class CoreImpl implements Core, AsyncWaiter { - /** * Discard flag for the |MojoReadData| operation. */ @@ -52,9 +51,7 @@ public class CoreImpl implements Core, AsyncWaiter { */ static final int INVALID_HANDLE = 0; - private static class LazyHolder { - private static final Core INSTANCE = new CoreImpl(); - } + private static class LazyHolder { private static final Core INSTANCE = new CoreImpl(); } /** * @return the instance. @@ -63,8 +60,7 @@ public class CoreImpl implements Core, AsyncWaiter { return LazyHolder.INSTANCE; } - private CoreImpl() { - } + private CoreImpl() {} /** * @see Core#getTimeTicksNow() @@ -79,22 +75,36 @@ public class CoreImpl implements Core, AsyncWaiter { */ @Override public WaitManyResult waitMany(List<Pair<Handle, HandleSignals>> handles, long deadline) { - // Allocate a direct buffer to allow native code not to reach back to java. Buffer will - // contain all mojo handles, followed by all flags values. - ByteBuffer buffer = allocateDirectBuffer(handles.size() * 8); + // Allocate a direct buffer to allow native code not to reach back to java. The buffer + // layout will be: + // input: The array of handles (int, 4 bytes each) + // input: The array of signals (int, 4 bytes each) + // space for output: The array of handle states (2 ints, 8 bytes each) + // Space for output: The result index (int, 4 bytes) + // The handles and signals will be filled before calling the native method. When the native + // method returns, the handle states and the index will have been set. + ByteBuffer buffer = allocateDirectBuffer(handles.size() * 16 + 4); int index = 0; for (Pair<Handle, HandleSignals> handle : handles) { buffer.putInt(HANDLE_SIZE * index, getMojoHandle(handle.first)); - buffer.putInt(HANDLE_SIZE * handles.size() + FLAG_SIZE * index, - handle.second.getFlags()); + buffer.putInt( + HANDLE_SIZE * handles.size() + FLAG_SIZE * index, handle.second.getFlags()); index++; } int code = nativeWaitMany(buffer, deadline); WaitManyResult result = new WaitManyResult(); - // If result is greater than 0, result is the indexed of the available handle. To make sure - // it cannot be misinterpreted, set handleIndex to a negative number in case of error. - result.setHandleIndex(code); result.setMojoResult(filterMojoResultForWait(code)); + result.setHandleIndex(buffer.getInt(handles.size() * 16)); + if (result.getMojoResult() != MojoResult.INVALID_ARGUMENT + && result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED) { + HandleSignalsState[] states = new HandleSignalsState[handles.size()]; + for (int i = 0; i < handles.size(); ++i) { + states[i] = new HandleSignalsState( + new HandleSignals(buffer.getInt(8 * (handles.size() + i))), + new HandleSignals(buffer.getInt(8 * (handles.size() + i) + 4))); + } + result.setSignalStates(states); + } return result; } @@ -102,9 +112,17 @@ public class CoreImpl implements Core, AsyncWaiter { * @see Core#wait(Handle, HandleSignals, long) */ @Override - public int wait(Handle handle, HandleSignals signals, long deadline) { - return filterMojoResultForWait(nativeWait(getMojoHandle(handle), - signals.getFlags(), deadline)); + public WaitResult wait(Handle handle, HandleSignals signals, long deadline) { + // Allocate a direct buffer to allow native code not to reach back to java. Buffer will + // contain spaces to write the handle state. + ByteBuffer buffer = allocateDirectBuffer(8); + WaitResult result = new WaitResult(); + result.setMojoResult(filterMojoResultForWait( + nativeWait(buffer, getMojoHandle(handle), signals.getFlags(), deadline))); + HandleSignalsState signalsState = new HandleSignalsState( + new HandleSignals(buffer.getInt(0)), new HandleSignals(buffer.getInt(4))); + result.setHandleSignalsState(signalsState); + return result; } /** @@ -123,7 +141,7 @@ public class CoreImpl implements Core, AsyncWaiter { if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } - return Pair.<MessagePipeHandle, MessagePipeHandle> create( + return Pair.<MessagePipeHandle, MessagePipeHandle>create( new MessagePipeHandleImpl(this, result.getMojoHandle1()), new MessagePipeHandleImpl(this, result.getMojoHandle2())); } @@ -145,7 +163,7 @@ public class CoreImpl implements Core, AsyncWaiter { if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } - return Pair.<ProducerHandle, ConsumerHandle> create( + return Pair.<ProducerHandle, ConsumerHandle>create( new DataPipeProducerHandleImpl(this, result.getMojoHandle1()), new DataPipeConsumerHandleImpl(this, result.getMojoHandle2())); } @@ -190,8 +208,8 @@ public class CoreImpl implements Core, AsyncWaiter { * @see AsyncWaiter#asyncWait(Handle, Core.HandleSignals, long, Callback) */ @Override - public Cancellable asyncWait(Handle handle, HandleSignals signals, long deadline, - Callback callback) { + public Cancellable asyncWait( + Handle handle, HandleSignals signals, long deadline, Callback callback) { return nativeAsyncWait(getMojoHandle(handle), signals.getFlags(), deadline, callback); } @@ -220,8 +238,7 @@ public class CoreImpl implements Core, AsyncWaiter { handlesBuffer.position(0); } int mojoResult = nativeWriteMessage(pipeHandle.getMojoHandle(), bytes, - bytes == null ? 0 : bytes.limit(), handlesBuffer, - flags.getFlags()); + bytes == null ? 0 : bytes.limit(), handlesBuffer, flags.getFlags()); if (mojoResult != MojoResult.OK) { throw new MojoException(mojoResult); } @@ -238,18 +255,17 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see MessagePipeHandle#readMessage(ByteBuffer, int, MessagePipeHandle.ReadFlags) */ - MessagePipeHandle.ReadMessageResult readMessage(MessagePipeHandleImpl handle, - ByteBuffer bytes, int maxNumberOfHandles, - MessagePipeHandle.ReadFlags flags) { + MessagePipeHandle.ReadMessageResult readMessage(MessagePipeHandleImpl handle, ByteBuffer bytes, + int maxNumberOfHandles, MessagePipeHandle.ReadFlags flags) { ByteBuffer handlesBuffer = null; if (maxNumberOfHandles > 0) { handlesBuffer = allocateDirectBuffer(maxNumberOfHandles * HANDLE_SIZE); } - MessagePipeHandle.ReadMessageResult result = nativeReadMessage( - handle.getMojoHandle(), bytes, handlesBuffer, flags.getFlags()); - if (result.getMojoResult() != MojoResult.OK && - result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED && - result.getMojoResult() != MojoResult.SHOULD_WAIT) { + MessagePipeHandle.ReadMessageResult result = + nativeReadMessage(handle.getMojoHandle(), bytes, handlesBuffer, flags.getFlags()); + if (result.getMojoResult() != MojoResult.OK + && result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED + && result.getMojoResult() != MojoResult.SHOULD_WAIT) { throw new MojoException(result.getMojoResult()); } @@ -259,8 +275,7 @@ public class CoreImpl implements Core, AsyncWaiter { bytes.limit(result.getMessageSize()); } - List<UntypedHandle> handles = new ArrayList<UntypedHandle>( - result.getHandlesCount()); + List<UntypedHandle> handles = new ArrayList<UntypedHandle>(result.getHandlesCount()); for (int i = 0; i < result.getHandlesCount(); ++i) { int mojoHandle = handlesBuffer.getInt(HANDLE_SIZE * i); handles.add(new UntypedHandleImpl(this, mojoHandle)); @@ -273,8 +288,7 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ConsumerHandle#discardData(int, DataPipe.ReadFlags) */ - int discardData(DataPipeConsumerHandleImpl handle, int numBytes, - DataPipe.ReadFlags flags) { + int discardData(DataPipeConsumerHandleImpl handle, int numBytes, DataPipe.ReadFlags flags) { int result = nativeReadData(handle.getMojoHandle(), null, numBytes, flags.getFlags() | MOJO_READ_DATA_FLAG_DISCARD); if (result < 0) { @@ -286,11 +300,9 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ConsumerHandle#readData(ByteBuffer, DataPipe.ReadFlags) */ - int readData(DataPipeConsumerHandleImpl handle, ByteBuffer elements, - DataPipe.ReadFlags flags) { + int readData(DataPipeConsumerHandleImpl handle, ByteBuffer elements, DataPipe.ReadFlags flags) { int result = nativeReadData(handle.getMojoHandle(), elements, - elements == null ? 0 : elements.capacity(), - flags.getFlags()); + elements == null ? 0 : elements.capacity(), flags.getFlags()); if (result < 0) { throw new MojoException(result); } @@ -303,12 +315,10 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ConsumerHandle#beginReadData(int, DataPipe.ReadFlags) */ - ByteBuffer beginReadData(DataPipeConsumerHandleImpl handle, - int numBytes, DataPipe.ReadFlags flags) { - NativeCodeAndBufferResult result = nativeBeginReadData( - handle.getMojoHandle(), - numBytes, - flags.getFlags()); + ByteBuffer beginReadData( + DataPipeConsumerHandleImpl handle, int numBytes, DataPipe.ReadFlags flags) { + NativeCodeAndBufferResult result = + nativeBeginReadData(handle.getMojoHandle(), numBytes, flags.getFlags()); if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } @@ -318,8 +328,7 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ConsumerHandle#endReadData(int) */ - void endReadData(DataPipeConsumerHandleImpl handle, - int numBytesRead) { + void endReadData(DataPipeConsumerHandleImpl handle, int numBytesRead) { int result = nativeEndReadData(handle.getMojoHandle(), numBytesRead); if (result != MojoResult.OK) { throw new MojoException(result); @@ -329,21 +338,19 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ProducerHandle#writeData(ByteBuffer, DataPipe.WriteFlags) */ - int writeData(DataPipeProducerHandleImpl handle, ByteBuffer elements, - DataPipe.WriteFlags flags) { - return nativeWriteData(handle.getMojoHandle(), elements, elements.limit(), - flags.getFlags()); + int writeData( + DataPipeProducerHandleImpl handle, ByteBuffer elements, DataPipe.WriteFlags flags) { + return nativeWriteData( + handle.getMojoHandle(), elements, elements.limit(), flags.getFlags()); } /** * @see ProducerHandle#beginWriteData(int, DataPipe.WriteFlags) */ - ByteBuffer beginWriteData(DataPipeProducerHandleImpl handle, - int numBytes, DataPipe.WriteFlags flags) { - NativeCodeAndBufferResult result = nativeBeginWriteData( - handle.getMojoHandle(), - numBytes, - flags.getFlags()); + ByteBuffer beginWriteData( + DataPipeProducerHandleImpl handle, int numBytes, DataPipe.WriteFlags flags) { + NativeCodeAndBufferResult result = + nativeBeginWriteData(handle.getMojoHandle(), numBytes, flags.getFlags()); if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } @@ -353,8 +360,7 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see ProducerHandle#endWriteData(int) */ - void endWriteData(DataPipeProducerHandleImpl handle, - int numBytesWritten) { + void endWriteData(DataPipeProducerHandleImpl handle, int numBytesWritten) { int result = nativeEndWriteData(handle.getMojoHandle(), numBytesWritten); if (result != MojoResult.OK) { throw new MojoException(result); @@ -364,16 +370,14 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see SharedBufferHandle#duplicate(DuplicateOptions) */ - SharedBufferHandle duplicate(SharedBufferHandleImpl handle, - DuplicateOptions options) { + SharedBufferHandle duplicate(SharedBufferHandleImpl handle, DuplicateOptions options) { ByteBuffer optionsBuffer = null; if (options != null) { optionsBuffer = allocateDirectBuffer(8); optionsBuffer.putInt(0, 8); optionsBuffer.putInt(4, options.getFlags().getFlags()); } - NativeCreationResult result = nativeDuplicate(handle.getMojoHandle(), - optionsBuffer); + NativeCreationResult result = nativeDuplicate(handle.getMojoHandle(), optionsBuffer); if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } @@ -384,10 +388,9 @@ public class CoreImpl implements Core, AsyncWaiter { /** * @see SharedBufferHandle#map(long, long, MapFlags) */ - ByteBuffer map(SharedBufferHandleImpl handle, long offset, long numBytes, - MapFlags flags) { - NativeCodeAndBufferResult result = nativeMap(handle.getMojoHandle(), offset, numBytes, - flags.getFlags()); + ByteBuffer map(SharedBufferHandleImpl handle, long offset, long numBytes, MapFlags flags) { + NativeCodeAndBufferResult result = + nativeMap(handle.getMojoHandle(), offset, numBytes, flags.getFlags()); if (result.getMojoResult() != MojoResult.OK) { throw new MojoException(result.getMojoResult()); } @@ -426,19 +429,11 @@ public class CoreImpl implements Core, AsyncWaiter { } } - private static int filterMojoResult(int code) { - if (code >= 0) { - return MojoResult.OK; - } - return code; - } - private static int filterMojoResultForWait(int code) { - int finalCode = filterMojoResult(code); - if (isUnrecoverableError(finalCode)) { - throw new MojoException(finalCode); + if (isUnrecoverableError(code)) { + throw new MojoException(code); } - return finalCode; + return code; } private static ByteBuffer allocateDirectBuffer(int capacity) { @@ -478,14 +473,12 @@ public class CoreImpl implements Core, AsyncWaiter { public void setBuffer(ByteBuffer buffer) { mBuffer = buffer; } - } /** * Implementation of {@link org.chromium.mojo.system.AsyncWaiter.Cancellable}. */ private class AsyncWaiterCancellableImpl implements AsyncWaiter.Cancellable { - private final long mId; private final long mDataPtr; private boolean mActive = true; @@ -521,25 +514,23 @@ public class CoreImpl implements Core, AsyncWaiter { } @CalledByNative - private void onAsyncWaitResult(int mojoResult, - AsyncWaiter.Callback callback, - AsyncWaiterCancellableImpl cancellable) { + private void onAsyncWaitResult( + int mojoResult, AsyncWaiter.Callback callback, AsyncWaiterCancellableImpl cancellable) { if (!cancellable.isActive()) { // If cancellable is not active, the user cancelled the wait. return; } cancellable.deactivate(); - int finalCode = filterMojoResult(mojoResult); - if (isUnrecoverableError(finalCode)) { - callback.onError(new MojoException(finalCode)); + if (isUnrecoverableError(mojoResult)) { + callback.onError(new MojoException(mojoResult)); return; } - callback.onResult(finalCode); + callback.onResult(mojoResult); } @CalledByNative - private static NativeCodeAndBufferResult newNativeCodeAndBufferResult(int mojoResult, - ByteBuffer buffer) { + private static NativeCodeAndBufferResult newNativeCodeAndBufferResult( + int mojoResult, ByteBuffer buffer) { NativeCodeAndBufferResult result = new NativeCodeAndBufferResult(); result.setMojoResult(mojoResult); result.setBuffer(buffer); @@ -547,9 +538,8 @@ public class CoreImpl implements Core, AsyncWaiter { } @CalledByNative - private static MessagePipeHandle.ReadMessageResult newReadMessageResult(int mojoResult, - int messageSize, - int handlesCount) { + private static MessagePipeHandle.ReadMessageResult newReadMessageResult( + int mojoResult, int messageSize, int handlesCount) { MessagePipeHandle.ReadMessageResult result = new MessagePipeHandle.ReadMessageResult(); if (mojoResult >= 0) { result.setMojoResult(MojoResult.OK); @@ -610,8 +600,8 @@ public class CoreImpl implements Core, AsyncWaiter { } @CalledByNative - private static NativeCreationResult newNativeCreationResult(int mojoResult, - int mojoHandle1, int mojoHandle2) { + private static NativeCreationResult newNativeCreationResult( + int mojoResult, int mojoHandle1, int mojoHandle2) { NativeCreationResult result = new NativeCreationResult(); result.setMojoResult(mojoResult); result.setMojoHandle1(mojoHandle1); @@ -627,45 +617,43 @@ public class CoreImpl implements Core, AsyncWaiter { private native NativeCreationResult nativeCreateDataPipe(ByteBuffer optionsBuffer); - private native NativeCreationResult nativeCreateSharedBuffer(ByteBuffer optionsBuffer, - long numBytes); + private native NativeCreationResult nativeCreateSharedBuffer( + ByteBuffer optionsBuffer, long numBytes); private native int nativeClose(int mojoHandle); - private native int nativeWait(int mojoHandle, int signals, long deadline); + private native int nativeWait(ByteBuffer buffer, int mojoHandle, int signals, long deadline); - private native int nativeWriteMessage(int mojoHandle, ByteBuffer bytes, int numBytes, - ByteBuffer handlesBuffer, int flags); + private native int nativeWriteMessage( + int mojoHandle, ByteBuffer bytes, int numBytes, ByteBuffer handlesBuffer, int flags); - private native MessagePipeHandle.ReadMessageResult nativeReadMessage(int mojoHandle, - ByteBuffer bytes, - ByteBuffer handlesBuffer, - int flags); + private native MessagePipeHandle.ReadMessageResult nativeReadMessage( + int mojoHandle, ByteBuffer bytes, ByteBuffer handlesBuffer, int flags); - private native int nativeReadData(int mojoHandle, ByteBuffer elements, int elementsSize, - int flags); + private native int nativeReadData( + int mojoHandle, ByteBuffer elements, int elementsSize, int flags); - private native NativeCodeAndBufferResult nativeBeginReadData(int mojoHandle, int numBytes, - int flags); + private native NativeCodeAndBufferResult nativeBeginReadData( + int mojoHandle, int numBytes, int flags); private native int nativeEndReadData(int mojoHandle, int numBytesRead); private native int nativeWriteData(int mojoHandle, ByteBuffer elements, int limit, int flags); - private native NativeCodeAndBufferResult nativeBeginWriteData(int mojoHandle, int numBytes, - int flags); + private native NativeCodeAndBufferResult nativeBeginWriteData( + int mojoHandle, int numBytes, int flags); private native int nativeEndWriteData(int mojoHandle, int numBytesWritten); private native NativeCreationResult nativeDuplicate(int mojoHandle, ByteBuffer optionsBuffer); - private native NativeCodeAndBufferResult nativeMap(int mojoHandle, long offset, long numBytes, - int flags); + private native NativeCodeAndBufferResult nativeMap( + int mojoHandle, long offset, long numBytes, int flags); private native int nativeUnmap(ByteBuffer buffer); - private native AsyncWaiterCancellableImpl nativeAsyncWait(int mojoHandle, int signals, - long deadline, AsyncWaiter.Callback callback); + private native AsyncWaiterCancellableImpl nativeAsyncWait( + int mojoHandle, int signals, long deadline, AsyncWaiter.Callback callback); private native void nativeCancelAsyncWait(long mId, long dataPtr); } diff --git a/mojo/android/system/src/org/chromium/mojo/system/impl/HandleBase.java b/mojo/android/system/src/org/chromium/mojo/system/impl/HandleBase.java index 81bfac72..a8870a8 100644 --- a/mojo/android/system/src/org/chromium/mojo/system/impl/HandleBase.java +++ b/mojo/android/system/src/org/chromium/mojo/system/impl/HandleBase.java @@ -8,6 +8,7 @@ import android.util.Log; import org.chromium.mojo.system.Core; import org.chromium.mojo.system.Core.HandleSignals; +import org.chromium.mojo.system.Core.WaitResult; import org.chromium.mojo.system.Handle; import org.chromium.mojo.system.UntypedHandle; @@ -65,7 +66,7 @@ abstract class HandleBase implements Handle { * @see org.chromium.mojo.system.Handle#wait(HandleSignals, long) */ @Override - public int wait(HandleSignals signals, long deadline) { + public WaitResult wait(HandleSignals signals, long deadline) { return mCore.wait(this, signals, deadline); } |