OLD | NEW |
| (Empty) |
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 impl | |
6 | |
7 //#include "mojo/public/platform/native/system_thunks.h" | |
8 //#include "mojo/public/c/system/main.h" | |
9 import "C" | |
10 import "unsafe" | |
11 | |
12 var core *CoreImpl | |
13 | |
14 func init() { | |
15 core = &CoreImpl{} | |
16 } | |
17 | |
18 // CoreImpl is an implementation of the Mojo system APIs. | |
19 type CoreImpl struct { | |
20 } | |
21 | |
22 func GetCore() *CoreImpl { | |
23 return core | |
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, *MojoHandleSignalsState) { | |
35 var signal_states C.struct_MojoHandleSignalsState | |
36 result := C.MojoNewWait(handle.cType(), signal.cType(), deadline.cType()
, &signal_states) | |
37 return MojoResult(result), &MojoHandleSignalsState{MojoHandleSignals(sig
nal_states.satisfied_signals), MojoHandleSignals(signal_states.satisfiable_signa
ls)} | |
38 } | |
39 | |
40 func (c *CoreImpl) WaitMany(handles []MojoHandle, signals []MojoHandleSignals, d
eadline MojoDeadline) (result MojoResult, index *uint32, state []MojoHandleSigna
lsState) { | |
41 // Set "-1" using the instructions from http://blog.golang.org/constants | |
42 cindex := ^C.uint32_t(0) | |
43 cstate := make([]C.struct_MojoHandleSignalsState, len(handles)) | |
44 | |
45 result = (MojoResult)(C.MojoNewWaitMany(cArrayMojoHandle(handles), cArra
yMojoHandleSignals(signals), (C.uint32_t)(len(handles)), deadline.cType(), &cind
ex, &cstate[0])) | |
46 | |
47 if uint32(cindex) < uint32(len(handles)) { | |
48 temp := uint32(cindex) | |
49 index = &temp | |
50 } | |
51 | |
52 if result != MOJO_RESULT_INVALID_ARGUMENT && result != MOJO_RESULT_RESOU
RCE_EXHAUSTED { | |
53 state = make([]MojoHandleSignalsState, len(cstate)) | |
54 for i, value := range cstate { | |
55 state[i] = NewMojoHandleSignalsState(value) | |
56 } | |
57 } | |
58 | |
59 return | |
60 } | |
61 | |
62 func (c *CoreImpl) CreateMessagePipe(opts *MessagePipeOptions) (MojoResult, Mojo
Handle, MojoHandle) { | |
63 var handle0, handle1 C.MojoHandle | |
64 result := C.MojoCreateMessagePipe(opts.cType(), &handle0, &handle1) | |
65 return (MojoResult)(result), (MojoHandle)(handle0), (MojoHandle)(handle1
) | |
66 } | |
67 | |
68 func (c *CoreImpl) WriteMessage(handle MojoHandle, msg []byte, attached []MojoHa
ndle, flags MojoWriteMessageFlags) MojoResult { | |
69 return (MojoResult)(C.MojoWriteMessage(handle.cType(), cArrayBytes(msg),
(C.uint32_t)(len(msg)), cArrayMojoHandle(attached), (C.uint32_t)(len(attached))
, flags.cType())) | |
70 } | |
71 | |
72 func (c *CoreImpl) ReadMessage(handle MojoHandle, flags MojoReadMessageFlags) (M
ojoResult, []byte, []MojoHandle, uint32, uint32) { | |
73 var num_bytes, num_handles C.uint32_t | |
74 if result := C.MojoReadMessage(handle.cType(), nil, &num_bytes, nil, &nu
m_handles, flags.cType()); result != C.MOJO_RESULT_RESOURCE_EXHAUSTED { | |
75 return (MojoResult)(result), nil, nil, 0, 0 | |
76 } | |
77 msg := make([]byte, (uint32)(num_bytes)) | |
78 attached := make([]MojoHandle, (uint32)(num_handles)) | |
79 result := C.MojoReadMessage(handle.cType(), cArrayBytes(msg), &num_bytes
, cArrayMojoHandle(attached), &num_handles, (C.MojoReadMessageFlags)(flags)) | |
80 return (MojoResult)(result), msg, attached, (uint32)(num_bytes), (uint32
)(num_handles) | |
81 } | |
82 | |
83 func (c *CoreImpl) CreateDataPipe(opts *DataPipeOptions) (MojoResult, MojoHandle
, MojoHandle) { | |
84 var producer, consumer C.MojoHandle | |
85 result := C.MojoCreateDataPipe(opts.cType(), &producer, &consumer) | |
86 return (MojoResult)(result), (MojoHandle)(producer), (MojoHandle)(consum
er) | |
87 } | |
88 | |
89 func (c *CoreImpl) WriteData(producer MojoHandle, data []byte, flags MojoWriteDa
taFlags) (MojoResult, uint32) { | |
90 num_bytes := (C.uint32_t)(len(data)) | |
91 result := C.MojoWriteData(producer.cType(), cArrayBytes(data), &num_byte
s, flags.cType()) | |
92 return (MojoResult)(result), (uint32)(num_bytes) | |
93 } | |
94 | |
95 func (c *CoreImpl) ReadData(consumer MojoHandle, flags MojoReadDataFlags) (MojoR
esult, []byte) { | |
96 var num_bytes C.uint32_t | |
97 var result C.MojoResult | |
98 if result = C.MojoReadData(consumer.cType(), nil, &num_bytes, C.MOJO_REA
D_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { | |
99 return (MojoResult)(result), nil | |
100 } | |
101 data := make([]byte, (uint32)(num_bytes)) | |
102 result = C.MojoReadData(consumer.cType(), cArrayBytes(data), &num_bytes,
flags.cType()) | |
103 return (MojoResult)(result), data | |
104 } | |
105 | |
106 func (c *CoreImpl) CreateSharedBuffer(opts *SharedBufferOptions, numBytes uint64
) (MojoResult, MojoHandle) { | |
107 var handle C.MojoHandle | |
108 result := C.MojoCreateSharedBuffer(opts.cType(), (C.uint64_t)(numBytes),
&handle) | |
109 return (MojoResult)(result), (MojoHandle)(handle) | |
110 } | |
111 | |
112 func (c *CoreImpl) DuplicateBufferHandle(handle MojoHandle, opts *DuplicateBuffe
rHandleOptions) (MojoResult, MojoHandle) { | |
113 var duplicate C.MojoHandle | |
114 result := C.MojoDuplicateBufferHandle(handle.cType(), opts.cType(), &dup
licate) | |
115 return (MojoResult)(result), (MojoHandle)(duplicate) | |
116 } | |
117 | |
118 func (c *CoreImpl) MapBuffer(handle MojoHandle, offset uint64, numBytes uint64,
flags MojoMapBufferFlags) (MojoResult, unsafe.Pointer) { | |
119 var bufPtr unsafe.Pointer | |
120 result := C.MojoMapBuffer(handle.cType(), (C.uint64_t)(offset), (C.uint6
4_t)(numBytes), &bufPtr, flags.cType()) | |
121 if result != C.MOJO_RESULT_OK { | |
122 return (MojoResult)(result), nil | |
123 } | |
124 return MOJO_RESULT_OK, bufPtr | |
125 } | |
126 | |
127 func (c *CoreImpl) UnmapBuffer(buffer unsafe.Pointer) MojoResult { | |
128 return (MojoResult)(C.MojoUnmapBuffer(buffer)) | |
129 } | |
OLD | NEW |