当前位置: 首页>>代码示例>>Golang>>正文


Golang atomic.SwapInt32函数代码示例

本文整理汇总了Golang中sync/atomic.SwapInt32函数的典型用法代码示例。如果您正苦于以下问题:Golang SwapInt32函数的具体用法?Golang SwapInt32怎么用?Golang SwapInt32使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SwapInt32函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: AcquireTimeout

func (s *Semaphore) AcquireTimeout(timeout time.Duration) bool {
	done := make(chan bool, 1)
	// Gate used to communicate between the threads and decide what the result
	// is. If the main thread decides, we have timed out, otherwise we succeed.
	decided := new(int32)
	go func() {
		s.Acquire()
		if atomic.SwapInt32(decided, 1) == 0 {
			done <- true
		} else {
			// If we already decided the result, and this thread did not win
			s.Release()
		}
	}()
	select {
	case <-done:
		return true
	case <-time.NewTimer(timeout).C:
		if atomic.SwapInt32(decided, 1) == 1 {
			// The other thread already decided the result
			return true
		}
		return false
	}
}
开发者ID:KosyanMedia,项目名称:burlesque,代码行数:25,代码来源:semaphore.go

示例2: watchUpdate

func watchUpdate(kv kvdb.Kvdb, data *watchData) error {
	var err error
	var kvp *kvdb.KVPair

	data.reader, data.writer = 0, 0
	atomic.AddInt32(&data.writer, 1)
	// whichKey = 1 : key
	// whichKey = 0 : otherKey
	atomic.SwapInt32(&data.whichKey, 1)
	data.action = kvdb.KVCreate
	fmt.Printf("-")
	kvp, err = kv.Create(data.key, []byte("bar"), 0)
	for i := 0; i < data.iterations && err == nil; i++ {
		fmt.Printf("-")

		for data.writer != data.reader {
			time.Sleep(time.Millisecond * 100)
		}
		atomic.AddInt32(&data.writer, 1)
		data.action = kvdb.KVSet
		kvp, err = kv.Put(data.key, []byte("bar"), 0)

		data.updateIndex = kvp.KVDBIndex
		assert.NoError(data.t, err, "Unexpected error in Put")
	}

	fmt.Printf("-")
	for data.writer != data.reader {
		time.Sleep(time.Millisecond * 100)
	}
	atomic.AddInt32(&data.writer, 1)
	// Delete key
	data.action = kvdb.KVDelete
	kv.Delete(data.key)

	fmt.Printf("-")
	for data.writer != data.reader {
		time.Sleep(time.Millisecond * 100)
	}
	atomic.AddInt32(&data.writer, 1)

	atomic.SwapInt32(&data.whichKey, 0)
	data.action = kvdb.KVDelete
	// Delete otherKey
	kv.Delete(data.otherKey)

	fmt.Printf("-")
	for data.writer != data.reader {
		time.Sleep(time.Millisecond * 100)
	}
	atomic.AddInt32(&data.writer, 1)

	atomic.SwapInt32(&data.whichKey, 1)
	data.action = kvdb.KVCreate
	_, err = kv.Create(data.key, []byte(data.stop), 0)

	return err
}
开发者ID:portworx,项目名称:kvdb,代码行数:58,代码来源:kv.go

示例3: finishPut

func (b *Buffer) finishPut() {
	atomic.AddInt32(&b.ring.wpos, 1)
	atomic.CompareAndSwapInt32(&b.ring.wpos, b.ring.size, 0)

	atomic.SwapInt32(&b.readonly, 1)
	b.lock.Unlock()
}
开发者ID:dongjun111111,项目名称:notes,代码行数:7,代码来源:ringbuffer.go

示例4: Set

func (this *AtomicBoolean) Set(val bool) bool {
	var b int32 = 0
	if val {
		b = 1
	}
	return atomic.SwapInt32((*int32)(this), b) != 0
}
开发者ID:jackwanger,项目名称:cloud-base,代码行数:7,代码来源:atomic.go

示例5: processMessages

func (mailbox *endpointWriterMailbox) processMessages() {
	//we are about to start processing messages, we can safely reset the message flag of the mailbox
	atomic.StoreInt32(&mailbox.hasMoreMessages, mailboxHasNoMessages)
	batchSize := mailbox.batchSize
	done := false

	for !done {
		if sysMsg, ok := mailbox.systemMailbox.Pop(); ok {

			first := sysMsg.(actor.SystemMessage)
			mailbox.systemInvoke(first)
		} else if userMsg, ok := mailbox.userMailbox.PopMany(batchSize); ok {

			mailbox.userInvoke(userMsg)
		} else {
			done = true
			break
		}
		runtime.Gosched()
	}

	//set mailbox to idle
	atomic.StoreInt32(&mailbox.schedulerStatus, mailboxIdle)
	//check if there are still messages to process (sent after the message loop ended)
	if atomic.SwapInt32(&mailbox.hasMoreMessages, mailboxHasNoMessages) == mailboxHasMoreMessages {
		mailbox.schedule()
	}

}
开发者ID:yonglehou,项目名称:gam,代码行数:29,代码来源:endpointwritermailbox.go

示例6: processMessages

func (mailbox *unboundedMailbox) processMessages() {
	//we are about to start processing messages, we can safely reset the message flag of the mailbox
	atomic.StoreInt32(&mailbox.hasMoreMessages, mailboxHasNoMessages)

	done := false
	for !done {
		//process x messages in sequence, then exit
		for i := 0; i < mailbox.throughput; i++ {
			if sysMsg, ok := mailbox.systemMailbox.Pop(); ok {
				sys, _ := sysMsg.(SystemMessage)
				mailbox.systemInvoke(sys)
			} else if userMsg, ok := mailbox.userMailbox.Pop(); ok {

				mailbox.userInvoke(userMsg)
			} else {
				done = true
				break
			}
		}
		runtime.Gosched()
	}

	//set mailbox to idle
	atomic.StoreInt32(&mailbox.schedulerStatus, mailboxIdle)
	//check if there are still messages to process (sent after the message loop ended)
	if atomic.SwapInt32(&mailbox.hasMoreMessages, mailboxHasNoMessages) == mailboxHasMoreMessages {
		mailbox.schedule()
	}

}
开发者ID:yonglehou,项目名称:gam,代码行数:30,代码来源:unboundedmailbox.go

示例7: Close

// Implement kanzi.InputStream interface
func (this *CompressedInputStream) Close() error {
	if atomic.SwapInt32(&this.closed, 1) == 1 {
		return nil
	}

	if _, err := this.ibs.Close(); err != nil {
		return err
	}

	// Release resources
	this.maxIdx = 0
	this.data = EMPTY_BYTE_SLICE

	for i := range this.buffers {
		this.buffers[i] = EMPTY_BYTE_SLICE
	}

	for _, c := range this.syncChan {
		if c != nil {
			close(c)
		}
	}

	close(this.resChan)
	return nil
}
开发者ID:lygstate,项目名称:kanzi,代码行数:27,代码来源:CompressedStream.go

示例8: Close

func (pc *SocketClient) Close() {
	pc.changes.Lock()
	defer pc.changes.Unlock()

	if atomic.SwapInt32(&pc.state, SocketClosed) != SocketClosed {
		close(pc.writeQueue)
	}
}
开发者ID:Auditorius,项目名称:go-socketclient,代码行数:8,代码来源:socketclient.go

示例9: monitor

// functions for monitoring internals
func monitor(mon *mylib.Mmon, boss mylib.Boss) {
	// just pick up first sender all the time, kiss
	sender := boss.Senders[0]
	ticker := time.Tick(1000 * time.Millisecond)
	last := time.Now()
	for {
		select {
		case <-ticker:
			//			log("debug", fmt.Sprintf("sending to %s..", sender.host))
			curr_time := time.Now()
			if curr_time.Unix() > last.Unix() {
				send_mon_data(atomic.SwapInt32(&mon.Send, 0), atomic.SwapInt32(&mon.Rcv, 0), atomic.SwapInt32(&mon.Conn, 0), boss.Port, curr_time, sender)
				last = curr_time
			}
		}
	}
}
开发者ID:Timka21213,项目名称:cacher,代码行数:18,代码来源:cacher.go

示例10: Close

func (t *Terminal) Close() error {
	if atomic.SwapInt32(&t.closed, 1) != 0 {
		return nil
	}
	t.stopChan <- struct{}{}
	t.wg.Wait()
	return t.ExitRawMode()
}
开发者ID:firebitsbr,项目名称:readline,代码行数:8,代码来源:terminal.go

示例11: monitor

func (c *Cache) monitor() {
	tick := time.Tick(10 * time.Second)
	start := time.Now()
	for {
		select {
		case <-tick:
			in := atomic.SwapInt32(&c.inCache, 0)
			out := atomic.SwapInt32(&c.outCache, 0)
			all := atomic.SwapInt32(&c.requestCount, 0)
			now := time.Now()
			delta := now.Sub(start).Nanoseconds()
			speed := int64(all) * 1000000000 / int64(delta)
			log.Printf("Speed %d. In: %d, Out: %d, All: %d\n", speed, in, out, all)
			start = now
		}
	}
}
开发者ID:dreyk,项目名称:dtests,代码行数:17,代码来源:TopRanker.go

示例12: lastDo

func (b *Buffer) lastDo() {
	if b.index+1 == b.ring.size {
		atomic.StoreInt32(&b.ring.rpos, 0)
	} else {
		atomic.StoreInt32(&b.ring.rpos, b.index+1)
	}

	atomic.SwapInt32(&b.readonly, 0)
}
开发者ID:dongjun111111,项目名称:notes,代码行数:9,代码来源:ringbuffer.go

示例13: Swap

func (b *AtomicBool) Swap(v bool) bool {
	var n AtomicBool

	if v {
		n = TRUE
	}

	return atomic.SwapInt32((*int32)(unsafe.Pointer(b)), int32(n)) != int32(FALSE)
}
开发者ID:sheepkiller,项目名称:curator-go,代码行数:9,代码来源:utils.go

示例14: Set

// Set sets the current node ID. If it is already set, the value must match.
func (n *NodeIDContainer) Set(ctx context.Context, val roachpb.NodeID) {
	if val <= 0 {
		log.Fatalf(ctx, "trying to set invalid NodeID: %d", val)
	}
	oldVal := atomic.SwapInt32(&n.nodeID, int32(val))
	if oldVal == 0 {
		log.Infof(ctx, "NodeID set to %d", val)
	} else if oldVal != int32(val) {
		log.Fatalf(ctx, "different NodeIDs set: %d, then %d", oldVal, val)
	}
}
开发者ID:knz,项目名称:cockroach,代码行数:12,代码来源:node_id.go

示例15: Close

func (t *Terminal) Close() error {
	if atomic.SwapInt32(&t.closed, 1) != 0 {
		return nil
	}
	if closer, ok := t.cfg.Stdin.(io.Closer); ok {
		closer.Close()
	}
	close(t.stopChan)
	t.wg.Wait()
	return t.ExitRawMode()
}
开发者ID:ryane,项目名称:terraform,代码行数:11,代码来源:terminal.go


注:本文中的sync/atomic.SwapInt32函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。