本文整理匯總了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
}
}
示例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
}
示例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()
}
示例4: Set
func (this *AtomicBoolean) Set(val bool) bool {
var b int32 = 0
if val {
b = 1
}
return atomic.SwapInt32((*int32)(this), b) != 0
}
示例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()
}
}
示例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()
}
}
示例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
}
示例8: Close
func (pc *SocketClient) Close() {
pc.changes.Lock()
defer pc.changes.Unlock()
if atomic.SwapInt32(&pc.state, SocketClosed) != SocketClosed {
close(pc.writeQueue)
}
}
示例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
}
}
}
}
示例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()
}
示例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
}
}
}
示例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)
}
示例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)
}
示例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)
}
}
示例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()
}