本文整理匯總了Golang中sync/atomic.SwapPointer函數的典型用法代碼示例。如果您正苦於以下問題:Golang SwapPointer函數的具體用法?Golang SwapPointer怎麽用?Golang SwapPointer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SwapPointer函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: OpenErrorLog
func OpenErrorLog(error_log_path string) (success bool, err error) {
lock.Lock()
defer lock.Unlock()
success = false
if ErrorLog == nil {
if ErrorLog, ErrorFd, err = openLog(error_log_path); err == nil {
success = true
}
} else {
// start swap exist logger and new logger, and Close the older fd in later, if it is Stdout, leave it
error_log, error_file, e := openLog(error_log_path)
err = e
if err == nil {
success = true
error_log = (*log.Logger)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&ErrorLog)), unsafe.Pointer(error_log)))
error_file = (*os.File)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&ErrorFd)), unsafe.Pointer(error_file)))
if e = error_file.Close(); e != nil {
log.Println("close the old errorlog fd failure with, ", e)
}
} else {
log.Println("open " + error_log_path + " failed: " + err.Error())
}
}
return
}
示例2: Attach
func (self *MmuxConnection) Attach(conn Connection) {
self.Mutex.Lock()
defer self.Mutex.Unlock()
var container Connection
container = conn
old := atomic.SwapPointer((*unsafe.Pointer)((unsafe.Pointer)(&self.Connection)), unsafe.Pointer(&container))
if old != nil {
// 1.If the ClientId represents a Client already connected to the Server
// then the Server MUST disconnect the existing Client [MQTT-3.1.4-2].
(*(*Connection)(old)).Close()
log.Debug("close existing connection")
}
self.CleanSession = conn.ShouldCleanSession()
if conn.ShouldCleanSession() {
self.OfflineQueue = self.OfflineQueue[:0]
self.SubscribeMap = make(map[string]bool)
self.SubscribedTopics = make(map[string]*SubscribeSet)
// Should I remove remaining QoS1, QoS2 message at this time?
self.OutGoingTable.Clean()
} else {
if len(self.OfflineQueue) > 0 {
log.Info("Process Offline Queue: Playback: %d", len(self.OfflineQueue))
for i := 0; i < len(self.OfflineQueue); i++ {
self.writeMessageQueue(self.OfflineQueue[i])
}
self.OfflineQueue = self.OfflineQueue[:0]
}
}
}
示例3: Cancel
func (req *memdQRequest) Cancel() bool {
queue := (*memdQueue)(atomic.SwapPointer(&req.queuedWith, nil))
if queue == nil {
return false
}
return true
}
示例4: QueueRequest
func (s *memdQueue) QueueRequest(req *memdQRequest) bool {
s.lock.RLock()
if s.isDrained {
s.lock.RUnlock()
return false
}
oldSP := atomic.SwapPointer(&req.queuedWith, unsafe.Pointer(s))
if oldSP != nil {
panic("Request was dispatched while already queued somewhere.")
}
logDebugf("Writing request to queue!")
// Try to write the request to the queue, if the queue is full,
// we immediately fail the request with a queueOverflow error.
select {
case s.reqsCh <- req:
s.lock.RUnlock()
return true
default:
s.lock.RUnlock()
// As long as we have not lost ownership, dispatch a queue overflow error.
if atomic.CompareAndSwapPointer(&req.queuedWith, unsafe.Pointer(s), nil) {
req.Callback(nil, overloadError{})
}
return true
}
}
示例5: Dispose
func (wrapper *FileJournalChunkWrapper) Dispose() error {
chunk := (*FileJournalChunk)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&wrapper.chunk)), nil))
if chunk == nil {
return errors.New("already disposed")
}
return wrapper.journal.deleteRef((*FileJournalChunk)(chunk))
}
示例6: clear
func (ptr *routeDataPtr) clear() *routeData {
val := atomic.SwapPointer(&ptr.data, nil)
if val == nil {
panic("Attempted to clear a nil routeDataPtr")
}
return (*routeData)(val)
}
示例7: setNewData
func (c *NodeCache) setNewData(newData *ChildData) {
previousData := (*ChildData)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.data)), unsafe.Pointer(newData)))
if !reflect.DeepEqual(previousData, newData) {
c.listeners.ForEach(func(listener interface{}) {
listener.(NodeCacheListener).NodeChanged()
})
}
}
示例8: Dispose
func (this *_SimpleLruCache) Dispose() {
ptr := atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&this.qpsCounter)), nil)
if ptr != nil {
(*_SimpleQpsCounter)(ptr).Stop()
}
if this.cleanupTicker != nil {
this.cleanupTicker.Stop()
this.cleanupTicker = nil
}
}
示例9: Set
// Set updates the value from a string representation in a thread-safe manner.
// This operation may return an error if the provided `input` doesn't parse, or the resulting value doesn't pass an
// optional validator.
// If a notifier is set on the value, it will be invoked in a separate go-routine.
func (d *DynStringValue) Set(val string) error {
if d.validator != nil {
if err := d.validator(val); err != nil {
return err
}
}
oldPtr := atomic.SwapPointer(&d.ptr, unsafe.Pointer(&val))
if d.notifier != nil {
go d.notifier(*(*string)(oldPtr), val)
}
return nil
}
示例10: storeDb
func (mc *mCache) storeDb(p, bucket int, db *database) {
ptr := (*unsafe.Pointer)(unsafe.Pointer(&mc.dbRings[p][bucket]))
if odb := (*database)(atomic.SwapPointer(ptr, unsafe.Pointer(db))); odb != nil {
if db != nil {
log.Printf("FORCED eviction of db for %d (p%d bucket %d)", odb.time.Unix(), p, bucket)
}
if odb.counter() > 0 {
log.Printf("LEAKED DATABASE STRUCT %d p%d!!!!!!!!", odb.time.Unix(), p)
mc.dbLeak = append(mc.dbLeak, odb)
}
}
}
示例11: update
func (ptr *routeDataPtr) update(old, new *routeData) bool {
if new == nil {
panic("Attempted to update to nil routeData")
}
if old != nil {
return atomic.CompareAndSwapPointer(&ptr.data, unsafe.Pointer(old), unsafe.Pointer(new))
} else {
if atomic.SwapPointer(&ptr.data, unsafe.Pointer(new)) != nil {
panic("Updated from nil attempted on initialized routeDataPtr")
}
return true
}
}
示例12: storeIndex
func (mc *mCache) storeIndex(bucket int, idx *index) {
ptr := (*unsafe.Pointer)(unsafe.Pointer(&mc.idxRing[bucket]))
if oidx := (*index)(atomic.SwapPointer(ptr, unsafe.Pointer(idx))); oidx != nil {
if idx != nil {
log.Printf("FORCED eviction of idx for %d (bucket %d)", oidx.time.Unix(), bucket)
}
if oidx.counter() > 0 {
log.Printf("LEAKED INDEX STRUCT %d!!!!!!!!", oidx.time.Unix())
mc.idxLeak = append(mc.idxLeak, oidx)
}
}
}
示例13: connect
// connect attempts a single connection attempt. On success, updates `c.conn`.
func (c *Client) connect() error {
conn, err := tlsDialHTTP(c.addr.NetworkField, c.addr.StringField, c.tlsConfig)
if err != nil {
return err
}
if oldConn := (*internalConn)(atomic.SwapPointer(&c.conn, unsafe.Pointer(&internalConn{
conn: conn,
client: rpc.NewClientWithCodec(codec.NewClientCodec(conn)),
}))); oldConn != nil {
oldConn.conn.Close()
}
return nil
}
示例14: Set
// Set updates the value from a string representation in a thread-safe manner.
// This operation may return an error if the provided `input` doesn't parse, or the resulting value doesn't pass an
// optional validator.
// If a notifier is set on the value, it will be invoked in a separate go-routine.
func (d *DynStringSliceValue) Set(val string) error {
v, err := csv.NewReader(strings.NewReader(val)).Read()
if err != nil {
return err
}
if d.validator != nil {
if err := d.validator(v); err != nil {
return err
}
}
oldPtr := atomic.SwapPointer(&d.ptr, unsafe.Pointer(&v))
if d.notifier != nil {
go d.notifier(*(*[]string)(oldPtr), v)
}
return nil
}
示例15: Set
// Set updates the value from a string representation in a thread-safe manner.
// This operation may return an error if the provided `input` doesn't parse, or the resulting value doesn't pass an
// optional validator.
// If a notifier is set on the value, it will be invoked in a separate go-routine.
func (d *DynFloat64Value) Set(input string) error {
val, err := strconv.ParseFloat(input, 64)
if err != nil {
return err
}
if d.validator != nil {
if err := d.validator(val); err != nil {
return err
}
}
oldPtr := atomic.SwapPointer(&d.ptr, unsafe.Pointer(&val))
if d.notifier != nil {
go d.notifier(*(*float64)(oldPtr), val)
}
return nil
}