github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/timer/x_event.go (about)

     1  package timer
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  )
     7  
     8  type timingWheelOperation uint8
     9  
    10  const (
    11  	unknown timingWheelOperation = iota
    12  	addTask
    13  	reAddTask
    14  	cancelTask
    15  )
    16  
    17  func (op timingWheelOperation) String() string {
    18  	switch op {
    19  	case addTask:
    20  		return "add"
    21  	case reAddTask:
    22  		return "re-add"
    23  	case cancelTask:
    24  		return "cancel"
    25  	default:
    26  		return "unknown"
    27  	}
    28  }
    29  
    30  type timingWheelEvent struct {
    31  	operation timingWheelOperation
    32  	obj       *atomic.Value // Task or JobID
    33  	hasSetup  bool
    34  }
    35  
    36  func newTimingWheelEvent(operation timingWheelOperation) *timingWheelEvent {
    37  	event := &timingWheelEvent{
    38  		operation: operation,
    39  		obj:       &atomic.Value{},
    40  	}
    41  	return event
    42  }
    43  
    44  func (e *timingWheelEvent) GetOperation() timingWheelOperation {
    45  	return e.operation
    46  }
    47  
    48  func (e *timingWheelEvent) GetTask() (Task, bool) {
    49  	if e.operation != addTask && e.operation != reAddTask {
    50  		return nil, false
    51  	}
    52  
    53  	obj := e.obj.Load()
    54  	if task, ok := obj.(Task); ok {
    55  		return task, true
    56  	}
    57  	return nil, false
    58  }
    59  
    60  func (e *timingWheelEvent) GetCancelTaskJobID() (JobID, bool) {
    61  	if e.operation != cancelTask {
    62  		return "", false
    63  	}
    64  
    65  	obj := e.obj.Load()
    66  	if jobID, ok := obj.(JobID); ok {
    67  		return jobID, true
    68  	}
    69  	return "", false
    70  }
    71  
    72  func (e *timingWheelEvent) CancelTaskJobID(jobID JobID) {
    73  	if e.hasSetup {
    74  		return
    75  	}
    76  	e.operation = cancelTask
    77  	e.obj.Store(jobID)
    78  	e.hasSetup = true
    79  }
    80  
    81  func (e *timingWheelEvent) AddTask(task Task) {
    82  	if e.hasSetup {
    83  		return
    84  	}
    85  	e.operation = addTask
    86  	e.obj.Store(task)
    87  	e.hasSetup = true
    88  }
    89  
    90  func (e *timingWheelEvent) ReAddTask(task Task) {
    91  	if e.hasSetup {
    92  		return
    93  	}
    94  	e.operation = reAddTask
    95  	e.obj.Store(task)
    96  	e.hasSetup = true
    97  }
    98  
    99  func (e *timingWheelEvent) clear() {
   100  	e.operation = unknown
   101  	e.hasSetup = false
   102  	e.obj = &atomic.Value{}
   103  }
   104  
   105  type timingWheelEventsPool struct {
   106  	pool *sync.Pool
   107  }
   108  
   109  func (p *timingWheelEventsPool) Get() *timingWheelEvent {
   110  	return p.pool.Get().(*timingWheelEvent)
   111  }
   112  
   113  func (p *timingWheelEventsPool) Put(event *timingWheelEvent) {
   114  	event.clear()
   115  	p.pool.Put(event)
   116  }
   117  
   118  func newTimingWheelEventsPool() *timingWheelEventsPool {
   119  	return &timingWheelEventsPool{
   120  		pool: &sync.Pool{
   121  			New: func() any {
   122  				return newTimingWheelEvent(unknown)
   123  			},
   124  		},
   125  	}
   126  }