cubefs
1// Copyright 2018 The CubeFS Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied. See the License for the specific language governing
13// permissions and limitations under the License.
14
15package synclist16
17import (18"container/list"19"sync"20)
21
22type SyncList struct {23list.List24mu sync.RWMutex25}
26
27func New() *SyncList {28l := new(SyncList)29l.Init()30return l31}
32
33func (l *SyncList) Init() *SyncList {34l.mu.Lock()35l.List.Init()36l.mu.Unlock()37return l38}
39
40func (l *SyncList) Remove(e *list.Element) interface{} {41l.mu.Lock()42defer l.mu.Unlock()43return l.List.Remove(e)44}
45
46func (l *SyncList) PushFront(v interface{}) *list.Element {47l.mu.Lock()48defer l.mu.Unlock()49return l.List.PushFront(v)50}
51
52func (l *SyncList) Back() *list.Element {53l.mu.RLock()54defer l.mu.RUnlock()55return l.List.Back()56}
57
58func (l *SyncList) PushBack(v interface{}) *list.Element {59l.mu.Lock()60defer l.mu.Unlock()61return l.List.PushBack(v)62}
63
64func (l *SyncList) InsertBefore(v interface{}, mark *list.Element) *list.Element {65l.mu.Lock()66defer l.mu.Unlock()67return l.List.InsertBefore(v, mark)68}
69
70func (l *SyncList) InsertAfter(v interface{}, mark *list.Element) *list.Element {71l.mu.Lock()72defer l.mu.Unlock()73return l.List.InsertAfter(v, mark)74}
75
76func (l *SyncList) Len() int {77l.mu.RLock()78defer l.mu.RUnlock()79return l.List.Len()80}
81
82func (l *SyncList) Front() *list.Element {83l.mu.RLock()84defer l.mu.RUnlock()85return l.List.Front()86}
87
88func (l *SyncList) MoveToFront(e *list.Element) {89l.mu.Lock()90l.List.MoveToFront(e)91l.mu.Unlock()92}
93
94func (l *SyncList) MoveToBack(e *list.Element) {95l.mu.Lock()96l.List.MoveToBack(e)97l.mu.Unlock()98}
99
100func (l *SyncList) MoveBefore(e, mark *list.Element) {101l.mu.Lock()102l.List.MoveBefore(e, mark)103l.mu.Unlock()104}
105
106func (l *SyncList) MoveAfter(e, mark *list.Element) {107l.mu.Lock()108l.List.MoveAfter(e, mark)109l.mu.Unlock()110}
111
112func (l *SyncList) PushBackList(other *SyncList) {113l.mu.Lock()114l.List.PushBackList(&other.List)115l.mu.Unlock()116}
117
118func (l *SyncList) PushFrontList(other *SyncList) {119l.mu.Lock()120l.List.PushFrontList(&other.List)121l.mu.Unlock()122}
123