github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/gohanscript/lib/atomic.go (about) 1 // Copyright (C) 2016 Juniper Networks, Inc. 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. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package lib 17 18 import ( 19 "github.com/cloudwan/gohan/util" 20 "github.com/streamrail/concurrent-map" 21 ) 22 23 //MakeMap makes thread safe map 24 func MakeMap() cmap.ConcurrentMap { 25 return cmap.New() 26 } 27 28 //MapSet set value to map 29 func MapSet(m cmap.ConcurrentMap, key string, value interface{}) { 30 m.Set(key, value) 31 } 32 33 //MapGet set value to map 34 func MapGet(m cmap.ConcurrentMap, key string) interface{} { 35 val, _ := m.Get(key) 36 return val 37 } 38 39 //MapHas key or not 40 func MapHas(m cmap.ConcurrentMap, key string) bool { 41 return m.Has(key) 42 } 43 44 //MapRemove removes key from map 45 func MapRemove(m cmap.ConcurrentMap, key string) { 46 m.Remove(key) 47 } 48 49 //MakeCounter makes thread safe counter 50 func MakeCounter(value int) *util.Counter { 51 return util.NewCounter(int64(value)) 52 } 53 54 //CounterAdd makes thread safe counter 55 func CounterAdd(counter *util.Counter, value int) { 56 counter.Add(int64(value)) 57 } 58 59 //CounterValue makes thread safe counter 60 func CounterValue(counter *util.Counter) int { 61 return int(counter.Value()) 62 }