本文整理匯總了Golang中sync/atomic.AddInt64函數的典型用法代碼示例。如果您正苦於以下問題:Golang AddInt64函數的具體用法?Golang AddInt64怎麽用?Golang AddInt64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了AddInt64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
// state will be a map
var state = make(map[int]int)
// mutex will synchronize across to state
var mutex = &sync.Mutex{}
// ops will count how many operations we perform against the state
var ops int64 = 0
// start 100 goroutines to execute repeated reads against the state
for r := 0; r < 100; r++ {
go func() {
total := 0
// for each read pick a key to access
// Lock() the mutex to ensure exclusive access to state
// read value at chosen key
// Unlock() mutex
// increment ops count
for {
key := rand.Intn(5)
mutex.Lock()
total += state[key]
mutex.Unlock()
atomic.AddInt64(&ops, 1)
// explicitly yield after each operation
// to ensure goroutine doesn't starve scheduler
runtime.Gosched()
}
}()
}
// start 10 goroutines to simulate writes
for w := 0; w < 10; w++ {
go func() {
for {
key := rand.Intn(5)
val := rand.Intn(100)
mutex.Lock()
state[key] = val
mutex.Unlock()
atomic.AddInt64(&ops, 1)
runtime.Gosched()
}
}()
}
// allow 10 goroutines to work on state and mutex for 1 second
time.Sleep(time.Second)
// take and report final ops count
opsFinal := atomic.LoadInt64(&ops)
fmt.Println("ops:", opsFinal)
// final lock state, show ending point
mutex.Lock()
fmt.Println("state:", state)
mutex.Unlock()
}
示例2: serve
func (s *Service) serve() {
// From https://collectd.org/wiki/index.php/Binary_protocol
// 1024 bytes (payload only, not including UDP / IP headers)
// In versions 4.0 through 4.7, the receive buffer has a fixed size
// of 1024 bytes. When longer packets are received, the trailing data
// is simply ignored. Since version 4.8, the buffer size can be
// configured. Version 5.0 will increase the default buffer size to
// 1452 bytes (the maximum payload size when using UDP/IPv6 over
// Ethernet).
buffer := make([]byte, 1452)
for {
select {
case <-s.done:
// We closed the connection, time to go.
return
default:
// Keep processing.
}
n, _, err := s.conn.ReadFromUDP(buffer)
if err != nil {
atomic.AddInt64(&s.stats.ReadFail, 1)
s.Logger.Printf("collectd ReadFromUDP error: %s", err)
continue
}
if n > 0 {
atomic.AddInt64(&s.stats.BytesReceived, int64(n))
s.handleMessage(buffer[:n])
}
}
}
示例3: writeMessage
func (o *S3SplitFileOutput) writeMessage(fi *SplitFileInfo, msgBytes []byte) (rotate bool, err error) {
rotate = false
atomic.AddInt64(&o.processMessageCount, 1)
file, e := o.openCurrent(fi)
if e != nil {
atomic.AddInt64(&o.processMessageFailures, 1)
return rotate, fmt.Errorf("Error getting open file %s: %s", fi.name, e)
}
n, e := file.Write(msgBytes)
atomic.AddInt64(&o.processMessageBytes, int64(n))
// Note that if these files are being written to elsewhere, the size-based
// rotation will not work as expected. A more robust approach would be to
// use something like `file.Seek(0, os.SEEK_CUR)` to get the current
// offset into the file.
fi.size += uint32(n)
if e != nil {
atomic.AddInt64(&o.processMessageFailures, 1)
return rotate, fmt.Errorf("Can't write to %s: %s", fi.name, e)
} else if n != len(msgBytes) {
return rotate, fmt.Errorf("Truncated output for %s", fi.name)
} else {
if fi.size >= o.MaxFileSize {
rotate = true
}
}
return
}
示例4: setVBMeta
func (v *VBucket) setVBMeta(newMeta *VBMeta) (err error) {
// This should only be called when holding the bucketstore
// service/apply "lock", to ensure a Flush between changes stream
// update and COLL_VBMETA update is atomic.
var j []byte
j, err = json.Marshal(newMeta)
if err != nil {
return err
}
k := []byte(fmt.Sprintf("%d", v.vbid))
i := &item{
key: nil, // A nil key means it's a VBMeta change.
cas: newMeta.MetaCas,
data: j,
}
deltaItemBytes, err := v.ps.set(i, nil)
if err != nil {
return err
}
if err = v.bs.collMeta(COLL_VBMETA).Set(k, j); err != nil {
return err
}
atomic.StorePointer(&v.meta, unsafe.Pointer(newMeta))
atomic.AddInt64(&v.stats.ItemBytes, deltaItemBytes)
atomic.AddInt64(v.bucketItemBytes, deltaItemBytes)
return nil
}
示例5: writeToShard
// writeToShards writes points to a shard.
func (w *PointsWriter) writeToShard(shard *meta.ShardInfo, database, retentionPolicy string, points []models.Point) error {
atomic.AddInt64(&w.stats.PointWriteReqLocal, int64(len(points)))
err := w.TSDBStore.WriteToShard(shard.ID, points)
if err == nil {
atomic.AddInt64(&w.stats.WriteOK, 1)
return nil
}
// If we've written to shard that should exist on the current node, but the store has
// not actually created this shard, tell it to create it and retry the write
if err == tsdb.ErrShardNotFound {
err = w.TSDBStore.CreateShard(database, retentionPolicy, shard.ID, true)
if err != nil {
w.Logger.Printf("write failed for shard %d: %v", shard.ID, err)
atomic.AddInt64(&w.stats.WriteErr, 1)
return err
}
}
err = w.TSDBStore.WriteToShard(shard.ID, points)
if err != nil {
w.Logger.Printf("write failed for shard %d: %v", shard.ID, err)
atomic.AddInt64(&w.stats.WriteErr, 1)
return err
}
atomic.AddInt64(&w.stats.WriteOK, 1)
return nil
}
示例6: Fetch
func (s *Storage) Fetch(ref blob.Ref) (file io.ReadCloser, size uint32, err error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.lru != nil {
s.lru.Get(ref.String()) // force to head
}
if s.m == nil {
err = os.ErrNotExist
return
}
b, ok := s.m[ref]
if !ok {
err = os.ErrNotExist
return
}
size = uint32(len(b))
atomic.AddInt64(&s.blobsFetched, 1)
atomic.AddInt64(&s.bytesFetched, int64(len(b)))
return struct {
*io.SectionReader
io.Closer
}{
io.NewSectionReader(bytes.NewReader(b), 0, int64(size)),
types.NopCloser,
}, size, nil
}
示例7: processEntry
// Processes the given |entry| in the specified log.
func (s *Scanner) processEntry(entry ct.LogEntry, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry)) {
atomic.AddInt64(&s.certsProcessed, 1)
switch entry.Leaf.TimestampedEntry.EntryType {
case ct.X509LogEntryType:
if s.opts.PrecertOnly {
// Only interested in precerts and this is an X.509 cert, early-out.
return
}
cert, err := x509.ParseCertificate(entry.Leaf.TimestampedEntry.X509Entry)
if err = s.handleParseEntryError(err, entry.Leaf.TimestampedEntry.EntryType, entry.Index); err != nil {
// We hit an unparseable entry, already logged inside handleParseEntryError()
return
}
if s.opts.Matcher.CertificateMatches(cert) {
entry.X509Cert = cert
foundCert(&entry)
}
case ct.PrecertLogEntryType:
c, err := x509.ParseTBSCertificate(entry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate)
if err = s.handleParseEntryError(err, entry.Leaf.TimestampedEntry.EntryType, entry.Index); err != nil {
// We hit an unparseable entry, already logged inside handleParseEntryError()
return
}
precert := &ct.Precertificate{
Raw: entry.Chain[0],
TBSCertificate: *c,
IssuerKeyHash: entry.Leaf.TimestampedEntry.PrecertEntry.IssuerKeyHash}
if s.opts.Matcher.PrecertificateMatches(precert) {
entry.Precert = precert
foundPrecert(&entry)
}
atomic.AddInt64(&s.precertsSeen, 1)
}
}
示例8: bgRead
func (p *Pumper) bgRead(ctx context.Context) {
defer p.brEnding()
for q := false; !q; {
id, m := p.readMsg()
p.pushRMsg(id, m)
atomic.AddInt64(&p.stat.InN, 1)
if p.imax > 0 && p.rQ.Len() > p.imax {
for q := false; !q && p.rQ.Len() > p.imax; {
atomic.AddInt64(&p.stat.PauseN, 1)
select {
case <-ctx.Done():
q = true
case <-time.After(readPauseTime):
}
}
}
select {
case <-ctx.Done():
q = true
default:
}
}
}
示例9: expire
// Invoked when the caller believes the item has expired. We double
// check here in case some concurrent race has mutated the item.
func (v *VBucket) expire(key []byte, now time.Time) (err error) {
var deltaItemBytes int64
var expireCas uint64
v.Apply(func() {
var i *item
i, err = v.ps.get(key)
if err != nil || i == nil {
return
}
if i.isExpired(now) {
expireCas = atomic.AddUint64(&v.Meta().LastCas, 1)
deltaItemBytes, err = v.ps.del(key, expireCas, i)
}
})
atomic.AddInt64(&v.stats.ItemBytes, deltaItemBytes)
atomic.AddInt64(v.bucketItemBytes, deltaItemBytes)
if err == nil && expireCas != 0 {
v.markStale()
v.observer.Submit(mutation{v.vbid, key, expireCas, true})
}
return err
}
示例10: waitForProgress
func (l *leaseUpdater) waitForProgress(item ovfFileItem) {
var pos, total int64
total = item.item.Size
for {
select {
case <-l.done:
return
case p, ok := <-item.ch:
// Return in case of error
if ok && p.Error() != nil {
return
}
if !ok {
// Last element on the channel, add to total
atomic.AddInt64(&l.pos, total-pos)
return
}
// Approximate progress in number of bytes
x := int64(float32(total) * (p.Percentage() / 100.0))
atomic.AddInt64(&l.pos, x-pos)
pos = x
}
}
}
示例11: Get
func (c *Client) Get(ctx context.Context, url string) (r *http.Response, err error) {
ctx = trace.Enter(ctx, "HTTP")
if maximum := c.MaximumInFlight; maximum != 0 {
defer atomic.AddInt64(&c.inflight, -1)
// too many in-flight?
if n := atomic.AddInt64(&c.inflight, 1); n >= maximum {
trace.Leave(ctx, "Errors.TooManyInFlight")
return
}
}
r, err = defaults.Client(c.Client).Get(url)
if err != nil {
atomic.AddInt64(&c.count, -1)
trace.Error(ctx, "Errors.Fail", err)
return
}
if r.StatusCode != http.StatusOK && r.StatusCode != http.StatusNoContent {
atomic.AddInt64(&c.count, -1)
trace.Error(ctx, "Errors.Status", fmt.Errorf("%s", r.Status))
return
}
trace.Leave(ctx, "Check")
return
}
示例12: SendMessages
func SendMessages(s servernode) {
for {
select {
case msg, valid := <-s.Outbox():
if !valid {
return
} else {
targetID := msg.Pid
if targetID == BROADCAST {
for i := 0; i < len(Peers); i++ {
msg.Pid = Peers[i]
msg.MsgId = s.MessageId
atomic.AddInt64(&s.MessageId, 1)
_, err := s.Peersockets[peerIdIndex[msg.Pid]].SendMessage(msg)
check_err(err)
//fmt.Println(n, "and", len(msg.Msg))
//fmt.Println("SENDING: (Src,Dst) --> (",s.ServerId,",",msg.Pid,") (Message:",*msg,")")
}
} else {
targetID = peerIdIndex[msg.Pid]
msg.MsgId = s.MessageId
atomic.AddInt64(&s.MessageId, 1)
_, err := s.Peersockets[targetID].SendMessage(msg)
//fmt.Println(n, "and", msg)
check_err(err)
//fmt.Println("SENDING: (Src,Dst) --> (",s.ServerId,",",msg.Pid,") (Message:",*msg,")")
}
}
}
}
}
示例13: TestDoWithPanic
func TestDoWithPanic(t *testing.T) {
var counter int64 = 0
cm := New(func() {})
tests := []Test{
// No panic
func(sem *Semaphore) {
defer atomic.AddInt64(&counter, 1)
sem.Ready()
},
// Panic after sem.Ready()
func(sem *Semaphore) {
defer atomic.AddInt64(&counter, 1)
sem.Ready()
panic("Panic after calling sem.Ready()")
},
// Panic before sem.Ready()
func(sem *Semaphore) {
defer atomic.AddInt64(&counter, 1)
panic("Panic before calling sem.Ready()")
},
}
for _, test := range tests {
cm.Register(test)
}
cm.Do()
// Check that all funcs in tests were called.
if int(counter) != len(tests) {
t.Errorf("Expected counter to be %v, but it was %v", len(tests), counter)
}
}
示例14: WriteMulti
// WriteMulti writes the map of keys and associated values to the cache. This function is goroutine-safe.
// It returns an error if the cache has exceeded its max size.
func (c *Cache) WriteMulti(values map[string][]Value) error {
totalSz := 0
for _, v := range values {
totalSz += Values(v).Size()
}
// Enough room in the cache?
c.mu.RLock()
newSize := c.size + uint64(totalSz)
if c.maxSize > 0 && newSize+c.snapshotSize > c.maxSize {
c.mu.RUnlock()
atomic.AddInt64(&c.stats.WriteErr, 1)
return ErrCacheMemoryExceeded
}
c.mu.RUnlock()
for k, v := range values {
c.entry(k).add(v)
}
c.mu.Lock()
c.size += uint64(totalSz)
c.mu.Unlock()
// Update the memory size stat
c.updateMemSize(int64(totalSz))
atomic.AddInt64(&c.stats.WriteOK, 1)
return nil
}
示例15: WritePoints
// WritePoints will write the raw data points and any new metadata to the index in the shard
func (s *Shard) WritePoints(points []models.Point) error {
if err := s.ready(); err != nil {
return err
}
s.mu.RLock()
defer s.mu.RUnlock()
atomic.AddInt64(&s.stats.WriteReq, 1)
fieldsToCreate, err := s.validateSeriesAndFields(points)
if err != nil {
return err
}
atomic.AddInt64(&s.stats.FieldsCreated, int64(len(fieldsToCreate)))
// add any new fields and keep track of what needs to be saved
if err := s.createFieldsAndMeasurements(fieldsToCreate); err != nil {
return err
}
// Write to the engine.
if err := s.engine.WritePoints(points); err != nil {
atomic.AddInt64(&s.stats.WritePointsFail, 1)
return fmt.Errorf("engine: %s", err)
}
atomic.AddInt64(&s.stats.WritePointsOK, int64(len(points)))
return nil
}