本文整理匯總了Golang中sync/atomic.SwapUint32函數的典型用法代碼示例。如果您正苦於以下問題:Golang SwapUint32函數的具體用法?Golang SwapUint32怎麽用?Golang SwapUint32使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SwapUint32函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetAndResetMessageCount
// GetAndResetMessageCount returns the current message counters and resets them
// to 0. This function is threadsafe.
func GetAndResetMessageCount() (messages, dropped, discarded, filtered, noroute uint32) {
return atomic.SwapUint32(&messageCount, 0),
atomic.SwapUint32(&droppedCount, 0),
atomic.SwapUint32(&discardedCount, 0),
atomic.SwapUint32(&filteredCount, 0),
atomic.SwapUint32(&noRouteCount, 0)
}
示例2: setRunning
func (pool *WorkPool) setRunning(running bool) {
if running {
atomic.SwapUint32(&pool.running, 1)
} else {
atomic.SwapUint32(&pool.running, 0)
}
}
示例3: loop_count
func (sc *SpeedCounter) loop_count() {
for !sc.s.closed {
sc.readbps = atomic.SwapUint32(&sc.readcnt, 0) / SHRINK_TIME
sc.writebps = atomic.SwapUint32(&sc.writecnt, 0) / SHRINK_TIME
time.Sleep(SHRINK_TIME * time.Second)
}
}
示例4: redeployServiceRunOnceAtATime
func redeployServiceRunOnceAtATime(serviceNameToRedeploy string, haproxyServiceName string) (alreadyRunning bool, err error) {
if RedeployServiceIsNotRunningThenSet() {
defer atomic.SwapUint32(&redeployServiceIsRunning, 0)
err := redeployService(serviceNameToRedeploy, haproxyServiceName)
atomic.SwapUint32(&redeployServiceIsRunning, 0)
return false, err
} else {
// is already running
return true, nil
}
}
示例5: pushMessage
func (prod *Websocket) pushMessage(msg core.Message) {
messageText, _ := prod.ProducerBase.Format(msg)
if prod.clientIdx&0x7FFFFFFF > 0 {
// There are new clients available
currentIdx := prod.clientIdx >> 31
activeIdx := (currentIdx + 1) & 1
// Store away the current client list and reset it
activeConns := &prod.clients[activeIdx]
oldConns := activeConns.conns
activeConns.conns = activeConns.conns[:]
activeConns.doneCount = 0
// Switch new and current client list
if currentIdx == 0 {
currentIdx = atomic.SwapUint32(&prod.clientIdx, 1<<31)
} else {
currentIdx = atomic.SwapUint32(&prod.clientIdx, 0)
}
// Wait for new list writer to finish
count := currentIdx & 0x7FFFFFFF
currentIdx = currentIdx >> 31
spin := shared.NewSpinner(shared.SpinPriorityHigh)
for prod.clients[currentIdx].doneCount != count {
spin.Yield()
}
// Add new connections to old connections
newConns := &prod.clients[currentIdx]
newConns.conns = append(oldConns, newConns.conns...)
}
// Process the active connections
activeIdx := ((prod.clientIdx >> 31) + 1) & 1
activeConns := &prod.clients[activeIdx]
for i := 0; i < len(activeConns.conns); i++ {
client := activeConns.conns[i]
if _, err := client.Write(messageText); err != nil {
activeConns.conns = append(activeConns.conns[:i], activeConns.conns[i+1:]...)
if closeErr := client.Close(); closeErr == nil {
Log.Error.Print("Websocket: ", err)
}
i--
}
}
}
示例6: atomics
func atomics() {
_ = atomic.LoadUint32(&x) // ERROR "intrinsic substitution for LoadUint32"
atomic.StoreUint32(&x, 1) // ERROR "intrinsic substitution for StoreUint32"
atomic.AddUint32(&x, 1) // ERROR "intrinsic substitution for AddUint32"
atomic.SwapUint32(&x, 1) // ERROR "intrinsic substitution for SwapUint32"
atomic.CompareAndSwapUint32(&x, 1, 2) // ERROR "intrinsic substitution for CompareAndSwapUint32"
}
示例7: Flush
// Flush writes the content of the buffer to a given resource and resets the
// internal state, i.e. the buffer is empty after a call to Flush.
// Writing will be done in a separate go routine to be non-blocking.
//
// The validate callback will be called after messages have been successfully
// written to the io.Writer.
// If validate returns false the buffer will not be resetted (automatic retry).
// If validate is nil a return value of true is assumed (buffer reset).
//
// The onError callback will be called if the io.Writer returned an error.
// If onError returns false the buffer will not be resetted (automatic retry).
// If onError is nil a return value of true is assumed (buffer reset).
func (batch *MessageBatch) Flush(assemble AssemblyFunc) {
if batch.IsEmpty() {
return // ### return, nothing to do ###
}
// Only one flush at a time
batch.flushing.IncWhenDone()
// Switch the buffers so writers can go on writing
flushSet := atomic.SwapUint32(&batch.activeSet, (batch.activeSet&messageBatchIndexMask)^messageBatchIndexMask)
flushIdx := flushSet >> messageBatchIndexShift
writerCount := flushSet & messageBatchCountMask
flushQueue := &batch.queue[flushIdx]
spin := shared.NewSpinner(shared.SpinPriorityHigh)
// Wait for remaining writers to finish
for writerCount != atomic.LoadUint32(&flushQueue.doneCount) {
spin.Yield()
}
// Write data and reset buffer asynchronously
go shared.DontPanic(func() {
defer batch.flushing.Done()
messageCount := shared.MinI(int(writerCount), len(flushQueue.messages))
assemble(flushQueue.messages[:messageCount])
atomic.StoreUint32(&flushQueue.doneCount, 0)
batch.Touch()
})
}
示例8: maybeClose
func (c *conn) maybeClose(reuse bool) {
const halfReused = 1
const halfClosed = 2
// As far as the caller is concerned, can the connection be kept alive
// and reused for another round-trip?
var next uint32
if reuse {
next = halfReused
} else {
next = halfClosed
}
// Use an atomic swap to make sure we only close the connection once.
prev := atomic.SwapUint32(&c.state, next)
// Don't do anything unless we're the latter of the two "ends" (reading
// and writing) to finish with the connection.
if prev == 0 {
return
}
// Either reuse or close the connection.
if reuse && prev == halfReused {
c.raw.SetReadDeadline(time.Time{})
c.state = 0
c.t.putIdle(c)
} else {
c.Close()
}
}
示例9: Flush
// Flush writes the content of the buffer to a given resource and resets the
// internal state, i.e. the buffer is empty after a call to Flush.
// Writing will be done in a separate go routine to be non-blocking.
//
// The validate callback will be called after messages have been successfully
// written to the io.Writer.
// If validate returns false the buffer will not be resetted (automatic retry).
// If validate is nil a return value of true is assumed (buffer reset).
//
// The onError callback will be called if the io.Writer returned an error.
// If onError returns false the buffer will not be resetted (automatic retry).
// If onError is nil a return value of true is assumed (buffer reset).
func (batch *MessageBatch) Flush(resource io.Writer, validate func() bool, onError func(error) bool) {
if batch.IsEmpty() {
return // ### return, nothing to do ###
}
// Only one flush at a time
batch.flushing.Lock()
// Switch the buffers so writers can go on writing
// If a previous flush failed we need to continue where we stopped
var flushSet uint32
if batch.activeSet&0x80000000 != 0 {
flushSet = atomic.SwapUint32(&batch.activeSet, 0|batch.queue[0].doneCount)
} else {
flushSet = atomic.SwapUint32(&batch.activeSet, 0x80000000|batch.queue[1].doneCount)
}
flushIdx := flushSet >> 31
writerCount := flushSet & 0x7FFFFFFF
flushQueue := &batch.queue[flushIdx]
// Wait for remaining writers to finish
for writerCount != flushQueue.doneCount {
runtime.Gosched()
}
// Write data and reset buffer asynchronously
go func() {
defer shared.RecoverShutdown()
defer batch.flushing.Unlock()
_, err := resource.Write(flushQueue.buffer[:flushQueue.contentLen])
if err == nil {
if validate == nil || validate() {
flushQueue.reset()
}
} else {
if onError == nil || onError(err) {
flushQueue.reset()
}
}
batch.Touch()
}()
}
示例10: Stop
// Stop will block until the consumer has stopped consuming
// messages.
func (s *subscriber) Stop() error {
if s.isStopped() {
return errors.New("sqs subscriber is already stopped")
}
exit := make(chan error)
s.stop <- exit
atomic.SwapUint32(&s.stopped, uint32(1))
return <-exit
}
示例11: Close
func (c *limitedConn) Close() (err error) {
if atomic.SwapUint32(&c.closed, 1) == 1 {
return errors.New("network connection already closed")
}
// Substract 1 by adding the two-complement of -1
numConns := atomic.AddUint64(&c.listener.numConns, ^uint64(0))
log.Tracef("Closed a connection and left %v remaining", numConns)
return c.Conn.Close()
}
示例12: startup
func (p *Peer) startup() {
if atomic.SwapUint32(&p.started, 1) == 1 {
return
}
p.wg.Add(3)
go p.goSend()
go p.goReceive()
go p.goProcess()
}
示例13: flush
func (batch *scribeMessageBatch) flush(scribe *scribe.ScribeClient, onError func(error)) {
if batch.isEmpty() {
return // ### return, nothing to do ###
}
// Only one flush at a time
batch.flushing.Lock()
// Switch the buffers so writers can go on writing
var flushSet uint32
if batch.activeSet&0x80000000 != 0 {
flushSet = atomic.SwapUint32(&batch.activeSet, 0)
} else {
flushSet = atomic.SwapUint32(&batch.activeSet, 0x80000000)
}
flushIdx := flushSet >> 31
writerCount := flushSet & 0x7FFFFFFF
flushQueue := &batch.queue[flushIdx]
// Wait for remaining writers to finish
for writerCount != flushQueue.doneCount {
// Spin
}
go func() {
defer batch.flushing.Unlock()
_, err := scribe.Log(flushQueue.buffer[:writerCount])
flushQueue.contentLen = 0
flushQueue.doneCount = 0
batch.touch()
if err != nil {
onError(err)
}
}()
}
示例14: pushVersion
func (p *Peer) pushVersion() {
if atomic.SwapUint32(&p.sent, 1) == 1 {
return
}
msg := wire.NewMsgVersion(p.me, p.you, p.nonce, 0)
msg.AddUserAgent(agentName, agentVersion)
msg.AddrYou.Services = wire.SFNodeNetwork
msg.Services = wire.SFNodeNetwork
msg.ProtocolVersion = int32(wire.RejectVersion)
p.sendQ <- msg
}
示例15: startReporter
func (s *Scheme) startReporter() {
s.done.Add(1)
go func() {
defer s.done.Done()
ticker := time.NewTicker(s.reportCycle)
for {
<-ticker.C
if s.isClosed() {
break
}
report := Report{}
report.MessageCount = atomic.SwapUint32(&s.messageCount, 0)
report.ByteCount = atomic.SwapUint64(&s.byteCount, 0)
report.ErrorCount = atomic.SwapUint32(&s.errorCount, 0)
s.reporter.Report(report)
}
}()
}