diff options
Diffstat (limited to 'mojo/public/go')
-rw-r--r-- | mojo/public/go/system/core.go | 88 | ||||
-rw-r--r-- | mojo/public/go/system/impl/core_impl.go | 129 | ||||
-rw-r--r-- | mojo/public/go/system/impl/mojo_types.go | 241 |
3 files changed, 0 insertions, 458 deletions
diff --git a/mojo/public/go/system/core.go b/mojo/public/go/system/core.go deleted file mode 100644 index ff6ffc1..0000000 --- a/mojo/public/go/system/core.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package system - -import ( - "unsafe" - - t "mojo/public/go/system/impl" -) - -// Core is an interface that defines the set of Mojo system APIs. -type Core interface { - // GetTimeTicksNow returns a monotonically increasing platform - // dependent tick count representing "right now". Resolution - // depends on the system configuration. - GetTimeTicksNow() t.MojoTimeTicks - - // Close closes the given handle. - Close(handle t.MojoHandle) (result t.MojoResult) - - // Wait waits on the given handle until a signal indicated by signals - // is satisfied or it becomes known that no signal indicated by - // signals will ever be satisfied or until deadline has passed. - // Notes about return values: - // |state| can be nil if the signal array could not be returned. This can - // happen with errors such as MOJO_RESULT_INVALID_ARGUMENT. - Wait(handle t.MojoHandle, signal t.MojoHandleSignals, deadline t.MojoDeadline) (result t.MojoResult, state *t.MojoHandleSignalsState) - - // WaitMany behaves as if Wait were called on each handle/signal pair - // simultaneously and completing when the first Wait would complete. - // Notes about return values: - // |index| can be nil if the error returned was not caused by a - // particular handle. For example, the error MOJO_RESULT_DEADLINE_EXCEEDED - // is not related to a particular handle. - // |state| can be nil if the signal array could not be returned. This can - // happen with errors such as MOJO_RESULT_INVALID_ARGUMENT. - WaitMany(handles []t.MojoHandle, signals []t.MojoHandleSignals, deadline t.MojoDeadline) (result t.MojoResult, index *uint32, state []t.MojoHandleSignalsState) - - // CreateMessagePipe creates a message pipe which is a bidirectional - // communication channel for framed data (i.e., messages). Messages - // can contain plain data and/or Mojo handles. On success, it returns - // handles to the two endpoints of the message pipe. - CreateMessagePipe(opts *t.MessagePipeOptions) (result t.MojoResult, handle0 t.MojoHandle, handle1 t.MojoHandle) - - // WriteMessage writes message data and optional attached handles to - // the message pipe endpoint given by handle. On success the attached - // handles will no longer be valid (ie: the receiver will receive - // equivalent but logically different handles). - WriteMessage(handle t.MojoHandle, msg []byte, attached []t.MojoHandle, flags t.MojoWriteMessageFlags) (result t.MojoResult) - - // ReadMessage reads a message from the message pipe endpoint given - // by handle with the specified flags. Returns the message data and - // attached handles that were received and the number of bytes and - // attached handles in the "next" message. - ReadMessage(handle t.MojoHandle, flags t.MojoReadMessageFlags) (result t.MojoResult, msg []byte, attached []t.MojoHandle, numBytes uint32, numHandles uint32) - - // CreateDataPipe creates a data pipe which is a unidirectional - // communication channel for unframed data. On success, returns a - // handle to the producer and consumer of the data pipe. - CreateDataPipe(opts *t.DataPipeOptions) (result t.MojoResult, producer t.MojoHandle, consumer t.MojoHandle) - - // WriteData writes data to the data pipe producer handle with the - // given flags. On success, returns the number of bytes that were - // actually written. - WriteData(producer t.MojoHandle, data []byte, flags t.MojoWriteDataFlags) (result t.MojoResult, numBytes uint32) - - // ReadData reads data from the data pipe consumer handle with the - // given flags. On success, returns the data that was read. - ReadData(consumer t.MojoHandle, flags t.MojoReadDataFlags) (result t.MojoResult, data []byte) - - // CreateSharedBuffer creates a buffer of size numBytes that can be - // shared between applications. One must call MapBuffer to access - // the buffer. - CreateSharedBuffer(opts *t.SharedBufferOptions, numBytes uint64) (result t.MojoResult, handle t.MojoHandle) - - // DuplicateBufferHandle duplicates the handle to a buffer. - DuplicateBufferHandle(handle t.MojoHandle, opts *t.DuplicateBufferHandleOptions) (result t.MojoResult, duplicate t.MojoHandle) - - // MapBuffer maps the requested part of the shared buffer given by - // handle into memory with specified flags. On success, it returns - // a pointer to the requested shared buffer. - MapBuffer(handle t.MojoHandle, offset uint64, numBytes uint64, flags t.MojoMapBufferFlags) (result t.MojoResult, buffer unsafe.Pointer) - - // UnmapBuffer unmaps a buffer pointer that was returned by MapBuffer. - UnmapBuffer(buffer unsafe.Pointer) (result t.MojoResult) -} diff --git a/mojo/public/go/system/impl/core_impl.go b/mojo/public/go/system/impl/core_impl.go deleted file mode 100644 index 2cce24f..0000000 --- a/mojo/public/go/system/impl/core_impl.go +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package impl - -//#include "mojo/public/platform/native/system_thunks.h" -//#include "mojo/public/c/system/main.h" -import "C" -import "unsafe" - -var core *CoreImpl - -func init() { - core = &CoreImpl{} -} - -// CoreImpl is an implementation of the Mojo system APIs. -type CoreImpl struct { -} - -func GetCore() *CoreImpl { - return core -} - -func (c *CoreImpl) GetTimeTicksNow() MojoTimeTicks { - return (MojoTimeTicks)(C.MojoGetTimeTicksNow()) -} - -func (c *CoreImpl) Close(handle MojoHandle) MojoResult { - return (MojoResult)(C.MojoClose(handle.cType())) -} - -func (c *CoreImpl) Wait(handle MojoHandle, signal MojoHandleSignals, deadline MojoDeadline) (MojoResult, *MojoHandleSignalsState) { - var signal_states C.struct_MojoHandleSignalsState - result := C.MojoNewWait(handle.cType(), signal.cType(), deadline.cType(), &signal_states) - return MojoResult(result), &MojoHandleSignalsState{MojoHandleSignals(signal_states.satisfied_signals), MojoHandleSignals(signal_states.satisfiable_signals)} -} - -func (c *CoreImpl) WaitMany(handles []MojoHandle, signals []MojoHandleSignals, deadline MojoDeadline) (result MojoResult, index *uint32, state []MojoHandleSignalsState) { - // Set "-1" using the instructions from http://blog.golang.org/constants - cindex := ^C.uint32_t(0) - cstate := make([]C.struct_MojoHandleSignalsState, len(handles)) - - result = (MojoResult)(C.MojoNewWaitMany(cArrayMojoHandle(handles), cArrayMojoHandleSignals(signals), (C.uint32_t)(len(handles)), deadline.cType(), &cindex, &cstate[0])) - - if uint32(cindex) < uint32(len(handles)) { - temp := uint32(cindex) - index = &temp - } - - if result != MOJO_RESULT_INVALID_ARGUMENT && result != MOJO_RESULT_RESOURCE_EXHAUSTED { - state = make([]MojoHandleSignalsState, len(cstate)) - for i, value := range cstate { - state[i] = NewMojoHandleSignalsState(value) - } - } - - return -} - -func (c *CoreImpl) CreateMessagePipe(opts *MessagePipeOptions) (MojoResult, MojoHandle, MojoHandle) { - var handle0, handle1 C.MojoHandle - result := C.MojoCreateMessagePipe(opts.cType(), &handle0, &handle1) - return (MojoResult)(result), (MojoHandle)(handle0), (MojoHandle)(handle1) -} - -func (c *CoreImpl) WriteMessage(handle MojoHandle, msg []byte, attached []MojoHandle, flags MojoWriteMessageFlags) MojoResult { - return (MojoResult)(C.MojoWriteMessage(handle.cType(), cArrayBytes(msg), (C.uint32_t)(len(msg)), cArrayMojoHandle(attached), (C.uint32_t)(len(attached)), flags.cType())) -} - -func (c *CoreImpl) ReadMessage(handle MojoHandle, flags MojoReadMessageFlags) (MojoResult, []byte, []MojoHandle, uint32, uint32) { - var num_bytes, num_handles C.uint32_t - if result := C.MojoReadMessage(handle.cType(), nil, &num_bytes, nil, &num_handles, flags.cType()); result != C.MOJO_RESULT_RESOURCE_EXHAUSTED { - return (MojoResult)(result), nil, nil, 0, 0 - } - msg := make([]byte, (uint32)(num_bytes)) - attached := make([]MojoHandle, (uint32)(num_handles)) - result := C.MojoReadMessage(handle.cType(), cArrayBytes(msg), &num_bytes, cArrayMojoHandle(attached), &num_handles, (C.MojoReadMessageFlags)(flags)) - return (MojoResult)(result), msg, attached, (uint32)(num_bytes), (uint32)(num_handles) -} - -func (c *CoreImpl) CreateDataPipe(opts *DataPipeOptions) (MojoResult, MojoHandle, MojoHandle) { - var producer, consumer C.MojoHandle - result := C.MojoCreateDataPipe(opts.cType(), &producer, &consumer) - return (MojoResult)(result), (MojoHandle)(producer), (MojoHandle)(consumer) -} - -func (c *CoreImpl) WriteData(producer MojoHandle, data []byte, flags MojoWriteDataFlags) (MojoResult, uint32) { - num_bytes := (C.uint32_t)(len(data)) - result := C.MojoWriteData(producer.cType(), cArrayBytes(data), &num_bytes, flags.cType()) - return (MojoResult)(result), (uint32)(num_bytes) -} - -func (c *CoreImpl) ReadData(consumer MojoHandle, flags MojoReadDataFlags) (MojoResult, []byte) { - var num_bytes C.uint32_t - var result C.MojoResult - if result = C.MojoReadData(consumer.cType(), nil, &num_bytes, C.MOJO_READ_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { - return (MojoResult)(result), nil - } - data := make([]byte, (uint32)(num_bytes)) - result = C.MojoReadData(consumer.cType(), cArrayBytes(data), &num_bytes, flags.cType()) - return (MojoResult)(result), data -} - -func (c *CoreImpl) CreateSharedBuffer(opts *SharedBufferOptions, numBytes uint64) (MojoResult, MojoHandle) { - var handle C.MojoHandle - result := C.MojoCreateSharedBuffer(opts.cType(), (C.uint64_t)(numBytes), &handle) - return (MojoResult)(result), (MojoHandle)(handle) -} - -func (c *CoreImpl) DuplicateBufferHandle(handle MojoHandle, opts *DuplicateBufferHandleOptions) (MojoResult, MojoHandle) { - var duplicate C.MojoHandle - result := C.MojoDuplicateBufferHandle(handle.cType(), opts.cType(), &duplicate) - return (MojoResult)(result), (MojoHandle)(duplicate) -} - -func (c *CoreImpl) MapBuffer(handle MojoHandle, offset uint64, numBytes uint64, flags MojoMapBufferFlags) (MojoResult, unsafe.Pointer) { - var bufPtr unsafe.Pointer - result := C.MojoMapBuffer(handle.cType(), (C.uint64_t)(offset), (C.uint64_t)(numBytes), &bufPtr, flags.cType()) - if result != C.MOJO_RESULT_OK { - return (MojoResult)(result), nil - } - return MOJO_RESULT_OK, bufPtr -} - -func (c *CoreImpl) UnmapBuffer(buffer unsafe.Pointer) MojoResult { - return (MojoResult)(C.MojoUnmapBuffer(buffer)) -} diff --git a/mojo/public/go/system/impl/mojo_types.go b/mojo/public/go/system/impl/mojo_types.go deleted file mode 100644 index 0218a33..0000000 --- a/mojo/public/go/system/impl/mojo_types.go +++ /dev/null @@ -1,241 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package impl - -//#include "mojo/public/platform/native/system_thunks.h" -//#include "mojo/public/c/system/main.h" -import "C" -import ( - "math" - "unsafe" -) - -// Go equivalent definitions of the various system types defined in Mojo. -// mojo/public/c/system/types.h -// mojo/public/c/system/data_pipe.h -// mojo/public/c/system/message_pipe.h -// -type MojoTimeTicks int64 -type MojoHandle uint32 -type MojoResult int32 -type MojoDeadline uint64 -type MojoHandleSignals uint32 -type MojoWriteMessageFlags uint32 -type MojoReadMessageFlags uint32 -type MojoWriteDataFlags uint32 -type MojoReadDataFlags uint32 -type MojoCreateDataPipeOptionsFlags uint32 -type MojoCreateMessagePipeOptionsFlags uint32 -type MojoCreateSharedBufferOptionsFlags uint32 -type MojoDuplicateBufferHandleOptionsFlags uint32 -type MojoMapBufferFlags uint32 - -const ( - MOJO_DEADLINE_INDEFINITE MojoDeadline = math.MaxUint64 - MOJO_HANDLE_INVALID MojoHandle = 0 - MOJO_RESULT_OK MojoResult = 0 - MOJO_RESULT_CANCELLED = -1 - MOJO_RESULT_UNKNOWN = -2 - MOJO_RESULT_INVALID_ARGUMENT = -3 - MOJO_RESULT_DEADLINE_EXCEEDED = -4 - MOJO_RESULT_NOT_FOUND = -5 - MOJO_RESULT_ALREADY_EXISTS = -6 - MOJO_RESULT_PERMISSION_DENIED = -7 - MOJO_RESULT_RESOURCE_EXHAUSTED = -8 - MOJO_RESULT_FAILED_PRECONDITION = -9 - MOJO_RESULT_ABORTED = -10 - MOJO_RESULT_OUT_OF_RANGE = -11 - MOJO_RESULT_UNIMPLEMENTED = -12 - MOJO_RESULT_INTERNAL = -13 - MOJO_RESULT_UNAVAILABLE = -14 - MOJO_RESULT_DATA_LOSS = -15 - MOJO_RESULT_BUSY = -16 - MOJO_RESULT_SHOULD_WAIT = -17 - - MOJO_HANDLE_SIGNAL_NONE MojoHandleSignals = 0 - MOJO_HANDLE_SIGNAL_READABLE = 1 << 0 - MOJO_HANDLE_SIGNAL_WRITABLE = 1 << 1 - MOJO_HANDLE_SIGNAL_PEER_CLOSED = 1 << 2 - - MOJO_WRITE_MESSAGE_FLAG_NONE MojoWriteMessageFlags = 0 - MOJO_READ_MESSAGE_FLAG_NONE MojoReadMessageFlags = 0 - MOJO_READ_MESSAGE_FLAG_MAY_DISCARD = 1 << 0 - - MOJO_READ_DATA_FLAG_NONE MojoReadDataFlags = 0 - MOJO_READ_DATA_FLAG_ALL_OR_NONE = 1 << 0 - MOJO_READ_DATA_FLAG_DISCARD = 1 << 1 - MOJO_READ_DATA_FLAG_QUERY = 1 << 2 - MOJO_READ_DATA_FLAG_PEEK = 1 << 3 - MOJO_WRITE_DATA_FLAG_NONE MojoWriteDataFlags = 0 - MOJO_WRITE_DATA_FLAG_ALL_OR_NONE MojoWriteDataFlags = 1 << 0 - - MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE MojoCreateDataPipeOptionsFlags = 0 - MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD = 1 << 0 - MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE MojoCreateMessagePipeOptionsFlags = 0 - - MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE MojoCreateSharedBufferOptionsFlags = 0 - MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE MojoDuplicateBufferHandleOptionsFlags = 0 - MOJO_MAP_BUFFER_FLAG_NONE MojoMapBufferFlags = 0 -) - -// DataPipeOptions is used to specify creation parameters for a data pipe. -type DataPipeOptions struct { - flags MojoCreateDataPipeOptionsFlags - // The size of an element in bytes. All transactions and buffers will - // be an integral number of elements. - elemSize uint32 - // The capacity of the data pipe in bytes. Must be a multiple of elemSize. - capacity uint32 -} - -type MojoHandleSignalsState struct { - SatisfiedSignals MojoHandleSignals - SatisfiableSignals MojoHandleSignals -} - -func NewMojoHandleSignalsState(cstate C.struct_MojoHandleSignalsState) MojoHandleSignalsState { - return MojoHandleSignalsState{ - MojoHandleSignals(cstate.satisfied_signals), - MojoHandleSignals(cstate.satisfiable_signals), - } -} - -func (opts *DataPipeOptions) cType() *C.struct_MojoCreateDataPipeOptions { - if opts == nil { - return nil - } - var cOpts C.struct_MojoCreateDataPipeOptions - cOpts = C.struct_MojoCreateDataPipeOptions{ - (C.uint32_t)(unsafe.Sizeof(cOpts)), - opts.flags.cType(), - (C.uint32_t)(opts.elemSize), - (C.uint32_t)(opts.capacity), - } - return &cOpts -} - -// MessagePipeOptions is used to specify creation parameters for a message pipe. -type MessagePipeOptions struct { - flags MojoCreateMessagePipeOptionsFlags -} - -func (opts *MessagePipeOptions) cType() *C.struct_MojoCreateMessagePipeOptions { - if opts == nil { - return nil - } - var cOpts C.struct_MojoCreateMessagePipeOptions - cOpts = C.struct_MojoCreateMessagePipeOptions{ - (C.uint32_t)(unsafe.Sizeof(cOpts)), - opts.flags.cType(), - } - return &cOpts -} - -// SharedBufferOptions is used to specify creation parameters for a -// shared buffer. -type SharedBufferOptions struct { - flags MojoCreateSharedBufferOptionsFlags -} - -func (opts *SharedBufferOptions) cType() *C.struct_MojoCreateSharedBufferOptions { - if opts == nil { - return nil - } - var cOpts C.struct_MojoCreateSharedBufferOptions - cOpts = C.struct_MojoCreateSharedBufferOptions{ - (C.uint32_t)(unsafe.Sizeof(cOpts)), - opts.flags.cType(), - } - return &cOpts -} - -// DuplicateBufferHandleOptions is used to specify parameters in -// duplicating access to a shared buffer. -type DuplicateBufferHandleOptions struct { - flags MojoDuplicateBufferHandleOptionsFlags -} - -func (opts *DuplicateBufferHandleOptions) cType() *C.struct_MojoDuplicateBufferHandleOptions { - if opts == nil { - return nil - } - var cOpts C.struct_MojoDuplicateBufferHandleOptions - cOpts = C.struct_MojoDuplicateBufferHandleOptions{ - (C.uint32_t)(unsafe.Sizeof(cOpts)), - opts.flags.cType(), - } - return &cOpts -} - -func (m MojoHandleSignals) IsReadable() bool { - return (m & MOJO_HANDLE_SIGNAL_READABLE) != 0 -} -func (m MojoHandleSignals) IsWritable() bool { - return (m & MOJO_HANDLE_SIGNAL_WRITABLE) != 0 -} -func (m MojoHandleSignals) IsClosed() bool { - return (m & MOJO_HANDLE_SIGNAL_PEER_CLOSED) != 0 -} - -// Convenience functions to convert Go types to their equivalent C types. -func (m MojoHandle) cType() C.MojoHandle { - return (C.MojoHandle)(m) -} -func (m MojoDeadline) cType() C.MojoDeadline { - return (C.MojoDeadline)(m) -} -func (m MojoHandleSignals) cType() C.MojoHandleSignals { - return (C.MojoHandleSignals)(m) -} -func (m MojoHandleSignalsState) cType() C.struct_MojoHandleSignalsState { - return C.struct_MojoHandleSignalsState{m.SatisfiedSignals.cType(), m.SatisfiableSignals.cType()} -} -func (m MojoWriteMessageFlags) cType() C.MojoWriteMessageFlags { - return (C.MojoWriteMessageFlags)(m) -} -func (m MojoReadMessageFlags) cType() C.MojoReadMessageFlags { - return (C.MojoReadMessageFlags)(m) -} -func (m MojoWriteDataFlags) cType() C.MojoWriteDataFlags { - return (C.MojoWriteDataFlags)(m) -} -func (m MojoReadDataFlags) cType() C.MojoReadDataFlags { - return (C.MojoReadDataFlags)(m) -} -func (m MojoCreateDataPipeOptionsFlags) cType() C.MojoCreateDataPipeOptionsFlags { - return (C.MojoCreateDataPipeOptionsFlags)(m) -} -func (m MojoCreateMessagePipeOptionsFlags) cType() C.MojoCreateMessagePipeOptionsFlags { - return (C.MojoCreateMessagePipeOptionsFlags)(m) -} -func (m MojoCreateSharedBufferOptionsFlags) cType() C.MojoCreateSharedBufferOptionsFlags { - return (C.MojoCreateSharedBufferOptionsFlags)(m) -} -func (m MojoDuplicateBufferHandleOptionsFlags) cType() C.MojoDuplicateBufferHandleOptionsFlags { - return (C.MojoDuplicateBufferHandleOptionsFlags)(m) -} -func (m MojoMapBufferFlags) cType() C.MojoMapBufferFlags { - return (C.MojoMapBufferFlags)(m) -} - -func cArrayMojoHandle(m []MojoHandle) *C.MojoHandle { - if len(m) == 0 { - return nil - } - return (*C.MojoHandle)(&m[0]) -} -func cArrayMojoHandleSignals(m []MojoHandleSignals) *C.MojoHandleSignals { - if len(m) == 0 { - return nil - } - return (*C.MojoHandleSignals)(&m[0]) -} - -func cArrayBytes(m []byte) unsafe.Pointer { - if len(m) == 0 { - return nil - } - return unsafe.Pointer(&m[0]) -} |