本文整理匯總了Golang中sync/atomic.CompareAndSwapUint32函數的典型用法代碼示例。如果您正苦於以下問題:Golang CompareAndSwapUint32函數的具體用法?Golang CompareAndSwapUint32怎麽用?Golang CompareAndSwapUint32使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CompareAndSwapUint32函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: doconnect
func (client *_Client) doconnect() {
if !atomic.CompareAndSwapUint32(&client.state, uint32(StateDisconnect), uint32(StateConnecting)) {
return
}
go func() {
conn, err := client.f()
if err != nil {
if !atomic.CompareAndSwapUint32(&client.state, uint32(StateConnecting), uint32(StateDisconnect)) {
return
}
client.E("%s connect server error:%s", client.name, gserrors.New(err))
if client.reconnectTimeout != 0 {
time.AfterFunc(client.reconnectTimeout, func() {
client.doconnect()
})
}
return
}
client.connected(conn)
}()
}
示例2: ListenAndServe
// ListenAndServe starts listening for connections, recording them and proxying
// to the target URL.
func (p *Proxy) ListenAndServe() (err error) {
if atomic.CompareAndSwapUint32(&p.isrun, 0, 1) {
defer func() {
// Ignore "use of closed network connection" comming from closed
// net.Listener when p was explicitely stopped.
if !atomic.CompareAndSwapUint32(&p.isrun, 1, 0) {
err = nil
}
}()
p.m.Lock()
var l net.Listener
if l, err = net.Listen("tcp", p.addr); err != nil {
p.m.Unlock()
return
}
var src *net.TCPAddr
src, err = urltotcpaddr(p.targ)
if err != nil {
p.m.Unlock()
return
}
if p.rl, err = newRecListener(l, src, p.Record); err != nil {
p.m.Unlock()
return
}
p.wgr.Done()
p.m.Unlock()
err = p.srv.Serve(p.rl)
return
}
return ErrAlreadyRunning
}
示例3: handleError
// 內部訪問接口
// 錯誤處理
func (chn *Channel) handleError(err error) {
defer func() {
if r := recover(); r != nil {
mylog.GetErrorLogger().Println("Channel handleError Panic")
}
}()
if err != nil {
mylog.GetErrorLogger().Println("handleError", err.Error())
}
if atomic.CompareAndSwapUint32(&chn.valid, 0, 0) {
return
}
atomic.CompareAndSwapUint32(&chn.valid, 1, 0)
stat.GetLocalStatistInst().Off()
func() {
chn.resMutex.Lock()
defer chn.resMutex.Unlock()
for _, r := range chn.openRes {
stat.GetLocalStatistInst().CloseRes()
mylog.GetErrorLogger().Println(" release chn res ", r.GetID())
clientDataID := new(ResourceClient)
clientDataID.ClientInf = chn
r.Close(clientDataID, "", true)
ReleaseResourcer(r)
}
for _, v := range chn.registerRes {
v.Unregister()
ReleaseResourcer(v)
}
}()
func() {
fmt.Println("Chn Close")
chn.chnMutex.Lock()
defer chn.chnMutex.Unlock()
chn.connSocket.Close()
close(chn.rwChn)
close(chn.notifyChn)
}()
}
示例4: handleError
func (pu *PUDevice) handleError(err error) {
mylog.GetErrorLogger().Println(err)
if atomic.CompareAndSwapUint32(&pu.Valid, 0, 0) {
return
}
atomic.CompareAndSwapUint32(&pu.Valid, 1, 0)
pu.connSocket.Close()
stat.GetLocalStatistInst().Off()
if atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 1) {
stat.GetLocalStatistInst().CloseRes()
}
close(pu.rwChan)
go pu.ReRun(pu.SN)
}
示例5: Reset
// Implements Service
func (bs *BaseService) Reset() (bool, error) {
if atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
// whether or not we've started, we can reset
atomic.CompareAndSwapUint32(&bs.started, 1, 0)
return true, bs.impl.OnReset()
} else {
if bs.log != nil {
bs.log.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
}
return false, nil
}
// never happens
return false, nil
}
示例6: Stop
func (a *AddrBook) Stop() {
if atomic.CompareAndSwapUint32(&a.stopped, 0, 1) {
log.Info("Stopping AddrBook")
close(a.quit)
a.wg.Wait()
}
}
示例7: resetWith
func (s *Stream) resetWith(errorCode frame.ErrorCode, resetErr error) {
// only ever send one reset
if !atomic.CompareAndSwapUint32(&s.sentRst, 0, 1) {
return
}
// close the stream
s.closeWithAndRemoveLater(resetErr)
// make the reset frame
rst := frame.NewWStreamRst()
if err := rst.Set(s.id, errorCode); err != nil {
s.die(frame.InternalError, err)
}
// need write lock to make sure no data frames get sent after we send the reset
s.writer.Lock()
// send it
if err := s.session.writeFrame(rst, zeroTime); err != nil {
s.writer.Unlock()
s.die(frame.InternalError, err)
}
s.writer.Unlock()
}
示例8: OpenStream
// OpenStream is used to create a new stream
func (s *Session) OpenStream() (*Stream, error) {
if s.IsClosed() {
return nil, ErrSessionShutdown
}
if atomic.LoadInt32(&s.remoteGoAway) == 1 {
return nil, ErrRemoteGoAway
}
GET_ID:
// Get and ID, and check for stream exhaustion
id := atomic.LoadUint32(&s.nextStreamID)
if id >= math.MaxUint32-1 {
return nil, ErrStreamsExhausted
}
if !atomic.CompareAndSwapUint32(&s.nextStreamID, id, id+2) {
goto GET_ID
}
// Register the stream
stream := newStream(s, id, streamInit)
s.streamLock.Lock()
s.streams[id] = stream
s.streamLock.Unlock()
// Send the window update to create
return stream, stream.sendWindowUpdate()
}
示例9: attrHelper
func (c *ConcurrentSolver) attrHelper(G *graphs.Graph, removed []bool, tmpMap []int32, flags []uint32, ch chan int, i int, node int, wg *sync.WaitGroup) {
for _, v0 := range G.Nodes[node].Inc {
if !removed[v0] {
flag := G.Nodes[v0].Player == i
if atomic.CompareAndSwapUint32(&flags[v0], 0, 1) {
if flag {
ch <- v0
atomic.AddInt32(&tmpMap[v0], 1)
} else {
adj_counter := 0
for _, x := range G.Nodes[v0].Adj {
if !removed[x] {
adj_counter += 1
}
}
atomic.AddInt32(&tmpMap[v0], int32(adj_counter))
if adj_counter == 1 {
ch <- v0
}
}
} else if !flag {
if atomic.AddInt32(&tmpMap[v0], -1) == 1 {
ch <- v0
}
}
}
}
wg.Done()
}
示例10: Release
func (o *emptyObject) Release() {
if !atomic.CompareAndSwapUint32(&o.once, 0, 1) {
return
}
o.node.evict()
o.node = nil
}
示例11: Stop
// Implements Reactor
func (bcR *BlockchainReactor) Stop() {
if atomic.CompareAndSwapUint32(&bcR.running, 1, 0) {
log.Info("Stopping BlockchainReactor")
close(bcR.quit)
bcR.pool.Stop()
}
}
示例12: Resume
// Resume resumes a TBucket in a paused state and begins adding new tokens to
// the bucket again.
//
// Resume returns true if the TBucket has been resumed, or false if the TBucket
// is not in a paused state.
func (tb *TBucket) Resume() bool {
if tb.IsClosed() || !atomic.CompareAndSwapUint32(&tb.paused, 1, 0) {
return false
}
tb.prch <- struct{}{}
return true
}
示例13: Close
// Close stops the internal ticker that adds tokens. The TBucket instance is now
// permanently closed and cannpt be reopened. When the TBucket will no longer be
// used, this function must be called to stop the internal timer from continuing
// to fire.
//
// It returns true if the TBucket has been closed, or false if the TBucket has
// already been closed.
func (tb *TBucket) Close() bool {
if !atomic.CompareAndSwapUint32(&tb.closed, 0, 1) {
return false
}
tb.cch <- struct{}{}
return true
}
示例14: Close
func (p *Proxy) Close() error {
if atomic.CompareAndSwapUint32(&p.closed, 0, 1) {
return p.listener.Close()
}
return nil
}
示例15: TakeDbOffline
func (dc *DatabaseContext) TakeDbOffline(reason string) error {
base.LogTo("CRUD", "Taking Database : %v, offline", dc.Name)
dbState := atomic.LoadUint32(&dc.State)
//If the DB is already trasitioning to: offline or is offline silently return
if dbState == DBOffline || dbState == DBResyncing || dbState == DBStopping {
return nil
}
if atomic.CompareAndSwapUint32(&dc.State, DBOnline, DBStopping) {
//notify all active _changes feeds to close
close(dc.ExitChanges)
base.LogTo("CRUD", "Waiting for all active calls to complete on Database : %v", dc.Name)
//Block until all current calls have returned, including _changes feeds
dc.AccessLock.Lock()
defer dc.AccessLock.Unlock()
base.LogTo("CRUD", "Database : %v, is offline", dc.Name)
//set DB state to Offline
atomic.StoreUint32(&dc.State, DBOffline)
if dc.EventMgr.HasHandlerForEvent(DBStateChange) {
dc.EventMgr.RaiseDBStateChangeEvent(dc.Name, "offline", reason, *dc.Options.AdminInterface)
}
return nil
} else {
base.LogTo("CRUD", "Unable to take Database offline, database must be in Online state")
return base.HTTPErrorf(http.StatusServiceUnavailable, "Unable to take Database offline, database must be in Online state")
}
}