OLD | NEW |
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 package impl | 5 package impl |
6 | 6 |
7 //#include "mojo/public/platform/native/system_thunks.h" | 7 //#include "mojo/public/platform/native/system_thunks.h" |
8 //#include "mojo/public/c/system/main.h" | 8 //#include "mojo/public/c/system/main.h" |
9 import "C" | 9 import "C" |
| 10 import "unsafe" |
10 | 11 |
| 12 var core *CoreImpl |
| 13 |
| 14 func init() { |
| 15 core = &CoreImpl{} |
| 16 } |
| 17 |
| 18 // CoreImpl is an implementation of the Mojo system APIs. |
11 type CoreImpl struct { | 19 type CoreImpl struct { |
12 } | 20 } |
13 | 21 |
14 func (c *CoreImpl) GetTimeTicksNow() int64 { | 22 func GetCore() *CoreImpl { |
15 return (int64)(C.MojoGetTimeTicksNow()) | 23 » return core |
16 } | 24 } |
| 25 |
| 26 func (c *CoreImpl) GetTimeTicksNow() MojoTimeTicks { |
| 27 return (MojoTimeTicks)(C.MojoGetTimeTicksNow()) |
| 28 } |
| 29 |
| 30 func (c *CoreImpl) Close(handle MojoHandle) MojoResult { |
| 31 return (MojoResult)(C.MojoClose(handle.cType())) |
| 32 } |
| 33 |
| 34 func (c *CoreImpl) Wait(handle MojoHandle, signal MojoHandleSignals, deadline Mo
joDeadline) MojoResult { |
| 35 return (MojoResult)(C.MojoWait(handle.cType(), signal.cType(), deadline.
cType())) |
| 36 } |
| 37 |
| 38 func (c *CoreImpl) WaitMany(handles []MojoHandle, signals []MojoHandleSignals, d
eadline MojoDeadline) MojoResult { |
| 39 return (MojoResult)(C.MojoWaitMany(cArrayMojoHandle(handles), cArrayMojo
HandleSignals(signals), (C.uint32_t)(len(handles)), deadline.cType())) |
| 40 } |
| 41 |
| 42 func (c *CoreImpl) CreateMessagePipe(opts *MessagePipeOptions) (MojoResult, Mojo
Handle, MojoHandle) { |
| 43 var handle0, handle1 C.MojoHandle |
| 44 result := C.MojoCreateMessagePipe(opts.cType(), &handle0, &handle1) |
| 45 return (MojoResult)(result), (MojoHandle)(handle0), (MojoHandle)(handle1
) |
| 46 } |
| 47 |
| 48 func (c *CoreImpl) WriteMessage(handle MojoHandle, msg []byte, attached []MojoHa
ndle, flags MojoWriteMessageFlags) MojoResult { |
| 49 return (MojoResult)(C.MojoWriteMessage(handle.cType(), cArrayBytes(msg),
(C.uint32_t)(len(msg)), cArrayMojoHandle(attached), (C.uint32_t)(len(attached))
, flags.cType())) |
| 50 } |
| 51 |
| 52 func (c *CoreImpl) ReadMessage(handle MojoHandle, flags MojoReadMessageFlags) (M
ojoResult, []byte, []MojoHandle, uint32, uint32) { |
| 53 var num_bytes, num_handles C.uint32_t |
| 54 if result := C.MojoReadMessage(handle.cType(), nil, &num_bytes, nil, &nu
m_handles, flags.cType()); result != C.MOJO_RESULT_RESOURCE_EXHAUSTED { |
| 55 return (MojoResult)(result), nil, nil, 0, 0 |
| 56 } |
| 57 msg := make([]byte, (uint32)(num_bytes)) |
| 58 attached := make([]MojoHandle, (uint32)(num_handles)) |
| 59 result := C.MojoReadMessage(handle.cType(), cArrayBytes(msg), &num_bytes
, cArrayMojoHandle(attached), &num_handles, (C.MojoReadMessageFlags)(flags)) |
| 60 return (MojoResult)(result), msg, attached, (uint32)(num_bytes), (uint32
)(num_handles) |
| 61 } |
| 62 |
| 63 func (c *CoreImpl) CreateDataPipe(opts *DataPipeOptions) (MojoResult, MojoHandle
, MojoHandle) { |
| 64 var producer, consumer C.MojoHandle |
| 65 result := C.MojoCreateDataPipe(opts.cType(), &producer, &consumer) |
| 66 return (MojoResult)(result), (MojoHandle)(producer), (MojoHandle)(consum
er) |
| 67 } |
| 68 |
| 69 func (c *CoreImpl) WriteData(producer MojoHandle, data []byte, flags MojoWriteDa
taFlags) (MojoResult, uint32) { |
| 70 num_bytes := (C.uint32_t)(len(data)) |
| 71 result := C.MojoWriteData(producer.cType(), cArrayBytes(data), &num_byte
s, flags.cType()) |
| 72 return (MojoResult)(result), (uint32)(num_bytes) |
| 73 } |
| 74 |
| 75 func (c *CoreImpl) ReadData(consumer MojoHandle, flags MojoReadDataFlags) (MojoR
esult, []byte) { |
| 76 var num_bytes C.uint32_t |
| 77 var result C.MojoResult |
| 78 if result = C.MojoReadData(consumer.cType(), nil, &num_bytes, C.MOJO_REA
D_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { |
| 79 return (MojoResult)(result), nil |
| 80 } |
| 81 data := make([]byte, (uint32)(num_bytes)) |
| 82 result = C.MojoReadData(consumer.cType(), cArrayBytes(data), &num_bytes,
flags.cType()) |
| 83 return (MojoResult)(result), data |
| 84 } |
| 85 |
| 86 func (c *CoreImpl) CreateSharedBuffer(opts *SharedBufferOptions, numBytes uint64
) (MojoResult, MojoHandle) { |
| 87 var handle C.MojoHandle |
| 88 result := C.MojoCreateSharedBuffer(opts.cType(), (C.uint64_t)(numBytes),
&handle) |
| 89 return (MojoResult)(result), (MojoHandle)(handle) |
| 90 } |
| 91 |
| 92 func (c *CoreImpl) DuplicateBufferHandle(handle MojoHandle, opts *DuplicateBuffe
rHandleOptions) (MojoResult, MojoHandle) { |
| 93 var duplicate C.MojoHandle |
| 94 result := C.MojoDuplicateBufferHandle(handle.cType(), opts.cType(), &dup
licate) |
| 95 return (MojoResult)(result), (MojoHandle)(duplicate) |
| 96 } |
| 97 |
| 98 func (c *CoreImpl) MapBuffer(handle MojoHandle, offset uint64, numBytes uint64,
flags MojoMapBufferFlags) (MojoResult, unsafe.Pointer) { |
| 99 var bufPtr unsafe.Pointer |
| 100 result := C.MojoMapBuffer(handle.cType(), (C.uint64_t)(offset), (C.uint6
4_t)(numBytes), &bufPtr, flags.cType()) |
| 101 if result != C.MOJO_RESULT_OK { |
| 102 return (MojoResult)(result), nil |
| 103 } |
| 104 return MOJO_RESULT_OK, bufPtr |
| 105 } |
| 106 |
| 107 func (c *CoreImpl) UnmapBuffer(buffer unsafe.Pointer) MojoResult { |
| 108 return (MojoResult)(C.MojoUnmapBuffer(buffer)) |
| 109 } |
OLD | NEW |