OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 bindings | |
6 | |
7 import ( | |
8 "sync/atomic" | |
9 | |
10 "mojo/public/go/system" | |
11 ) | |
12 | |
13 func align(size, alignment int) int { | |
14 return ((size - 1) | (alignment - 1)) + 1 | |
15 } | |
16 | |
17 // bytesForBits returns minimum number of bytes required to store provided | |
18 // number of bits. | |
19 func bytesForBits(bits uint64) int { | |
20 return int((bits + 7) / 8) | |
21 } | |
22 | |
23 // WriteMessage writes a message to a message pipe. | |
24 func WriteMessage(handle system.MessagePipeHandle, message *Message) error { | |
25 result := handle.WriteMessage(message.Bytes, message.Handles, system.MOJ
O_WRITE_MESSAGE_FLAG_NONE) | |
26 if result != system.MOJO_RESULT_OK { | |
27 return &ConnectionError{result} | |
28 } | |
29 return nil | |
30 } | |
31 | |
32 // StringPointer converts provided string to *string. | |
33 func StringPointer(s string) *string { | |
34 return &s | |
35 } | |
36 | |
37 // Counter is a simple thread-safe lock-free counter that can issue unique | |
38 // numbers starting from 1 to callers. | |
39 type Counter interface { | |
40 // Count returns next unused value, each value is returned only once. | |
41 Count() uint64 | |
42 } | |
43 | |
44 // NewCounter return a new counter that returns numbers starting from 1. | |
45 func NewCounter() Counter { | |
46 return &counterImpl{} | |
47 } | |
48 | |
49 // counterImpl implements Counter interface. | |
50 // This implementation uses atomic operations on an uint64, it should be always | |
51 // allocated separatelly to be 8-aligned in order to work correctly on ARM. | |
52 // See http://golang.org/pkg/sync/atomic/#pkg-note-BUG. | |
53 type counterImpl struct { | |
54 last uint64 | |
55 } | |
56 | |
57 func (c *counterImpl) Count() uint64 { | |
58 return atomic.AddUint64(&c.last, 1) | |
59 } | |
OLD | NEW |