當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。