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 3 // found in the LICENSE file. 4 5 package org.chromium.mojo.system; 6 7 import java.util.List; 8 9 /** 10 * Core mojo interface giving access to the base operations. See |src/mojo/public/c/system/core.h| 11 * for the underlying api. 12 */ 13 public interface Core { 14 15 /** 16 * Used to indicate an infinite deadline (timeout). 17 */ 18 public static final long DEADLINE_INFINITE = -1; 19 20 /** 21 * Flags for the wait operations on handles. 22 */ 23 public static class WaitFlags extends Flags<WaitFlags> { 24 /** 25 * Constructor. 26 * 27 * @param flags the serialized flags. 28 */ WaitFlags(int flags)29 private WaitFlags(int flags) { 30 super(flags); 31 } 32 33 private static final int FLAG_NONE = 0; 34 private static final int FLAG_READABLE = 1 << 0; 35 private static final int FLAG_WRITABLE = 1 << 1; 36 private static final int FLAG_ALL = ~0; 37 38 /** 39 * Immutable flags. 40 */ 41 public static final WaitFlags NONE = WaitFlags.none().immutable(); 42 public static final WaitFlags READABLE = WaitFlags.none().setReadable(true).immutable(); 43 public static final WaitFlags WRITABLE = WaitFlags.none().setWritable(true).immutable(); 44 public static final WaitFlags ALL = WaitFlags.all().immutable(); 45 46 /** 47 * Change the readable bit of this flag. 48 * 49 * @param readable the new value of the readable bit. 50 * @return this. 51 */ setReadable(boolean readable)52 public WaitFlags setReadable(boolean readable) { 53 return setFlag(FLAG_READABLE, readable); 54 } 55 56 /** 57 * Change the writable bit of this flag. 58 * 59 * @param writable the new value of the writable bit. 60 * @return this. 61 */ setWritable(boolean writable)62 public WaitFlags setWritable(boolean writable) { 63 return setFlag(FLAG_WRITABLE, writable); 64 } 65 66 /** 67 * @return a flag with no bit set. 68 */ none()69 public static WaitFlags none() { 70 return new WaitFlags(FLAG_NONE); 71 } 72 73 /** 74 * @return a flag with all bits set. 75 */ all()76 public static WaitFlags all() { 77 return new WaitFlags(FLAG_ALL); 78 } 79 } 80 81 /** 82 * @return a platform-dependent monotonically increasing tick count representing "right now." 83 */ getTimeTicksNow()84 public long getTimeTicksNow(); 85 86 /** 87 * Waits on the given |handle| until the state indicated by |flags| is satisfied or until 88 * |deadline| has passed. 89 * 90 * @return |MojoResult.OK| if some flag in |flags| was satisfied (or is already satisfied). 91 * <p> 92 * |MojoResult.DEADLINE_EXCEEDED| if the deadline has passed without any of the flags 93 * begin satisfied. 94 * <p> 95 * |MojoResult.CANCELLED| if |handle| is closed concurrently by another thread. 96 * <p> 97 * |MojoResult.FAILED_PRECONDITION| if it is or becomes impossible that any flag in 98 * |flags| will ever be satisfied (for example, if the other endpoint is close). 99 */ wait(Handle handle, WaitFlags flags, long deadline)100 public int wait(Handle handle, WaitFlags flags, long deadline); 101 102 /** 103 * Result for the |waitMany| method. 104 */ 105 public static class WaitManyResult { 106 107 /** 108 * See |wait| for the different possible values. 109 */ 110 private int mMojoResult; 111 /** 112 * If |mojoResult| is |MojoResult.OK|, |handleIndex| is the index of the handle for which 113 * some flag was satisfied (or is already satisfied). If |mojoResult| is 114 * |MojoResult.CANCELLED| or |MojoResult.FAILED_PRECONDITION|, |handleIndex| is the index of 115 * the handle for which the issue occurred. 116 */ 117 private int mHandleIndex; 118 119 /** 120 * @return the mojoResult 121 */ getMojoResult()122 public int getMojoResult() { 123 return mMojoResult; 124 } 125 126 /** 127 * @param mojoResult the mojoResult to set 128 */ setMojoResult(int mojoResult)129 public void setMojoResult(int mojoResult) { 130 mMojoResult = mojoResult; 131 } 132 133 /** 134 * @return the handleIndex 135 */ getHandleIndex()136 public int getHandleIndex() { 137 return mHandleIndex; 138 } 139 140 /** 141 * @param handleIndex the handleIndex to set 142 */ setHandleIndex(int handleIndex)143 public void setHandleIndex(int handleIndex) { 144 mHandleIndex = handleIndex; 145 } 146 } 147 148 /** 149 * Waits on handle in |handles| for at least one of them to satisfy the associated |WaitFlags|, 150 * or until |deadline| has passed. 151 * 152 * @returns a |WaitManyResult|. 153 */ waitMany(List<Pair<Handle, WaitFlags>> handles, long deadline)154 public WaitManyResult waitMany(List<Pair<Handle, WaitFlags>> handles, long deadline); 155 156 /** 157 * Creates a message pipe, which is a bidirectional communication channel for framed data (i.e., 158 * messages). Messages can contain plain data and/or Mojo handles. 159 * 160 * @return the set of handles for the two endpoints (ports) of the message pipe. 161 */ createMessagePipe()162 public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe(); 163 164 /** 165 * Creates a data pipe, which is a unidirectional communication channel for unframed data, with 166 * the given options. Data is unframed, but must come as (multiples of) discrete elements, of 167 * the size given in |options|. See |DataPipe.CreateOptions| for a description of the different 168 * options available for data pipes. |options| may be set to null for a data pipe with the 169 * default options (which will have an element size of one byte and have some system-dependent 170 * capacity). 171 * 172 * @return the set of handles for the two endpoints of the data pipe. 173 */ createDataPipe( DataPipe.CreateOptions options)174 public Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> createDataPipe( 175 DataPipe.CreateOptions options); 176 177 /** 178 * Creates a buffer that can be shared between applications (by duplicating the handle -- see 179 * |SharedBufferHandle.duplicate()| -- and passing it over a message pipe). To access the 180 * buffer, one must call |SharedBufferHandle.map|. 181 * 182 * @return the new |SharedBufferHandle|. 183 */ createSharedBuffer(SharedBufferHandle.CreateOptions options, long numBytes)184 public SharedBufferHandle createSharedBuffer(SharedBufferHandle.CreateOptions options, 185 long numBytes); 186 187 /** 188 * Returns a default implementation of {@link AsyncWaiter}. 189 */ getDefaultAsyncWaiter()190 public AsyncWaiter getDefaultAsyncWaiter(); 191 192 } 193