Giter VIP home page Giter VIP logo

golock's Introduction

golock

A lock implement with context and multiple keys supported

1. Get the module

go get github.com/imstevez/golock

2. CtxRWMutex

package main

import (
    "context"
    "github.com/imstevez/golock"
)

type Counter struct {
    count int
    mu  golock.CtxRWMutex
}

func (c *Counter)Add(ctx context.Context, delta int) (err error) {
    err = c.mu.Lock(ctx)
    if err != nil {
        return
    }

    defer c.mu.Unlock()

    c.count += delta

    return
}

func (c *Counter)Catch(ctx context.Contex, catch func(int)) (err error) {
    err = c.mu.RLock(ctx)
    if err != nil {
        return
    }

    defer c.mu.RUnlock()

    catch(c.count)

    return
}

3. MultiCtxRWMutex

//...
type Counters struct{
    counts []int
    mus *golock.MultiCtxRWMutex
}

func NewCounters(n int) *Counters {
    return &Counters{
        counts: make([]int, n),
        mus:    golock.NewDefaultMultiCtxRWMutex(),
    }
}
    

func (cs *Counters) Add(ctx context.Context, idx int, delta int) (err error) {
    if idx < 0 || idx >= len(cs.counts) {
        err = errors.New("invalid idx")
        return
    }

    key := idx

    err = cs.mus.Lock(ctx, key)
    if err != nil {
        return
    }

    defer cs.mus.Unlock(key)

    cs.counts[idx] += delta

    return
}

func (cs *Counters) Catch(ctx context.Context, idx int, catch func(int)) (err error) {
    if idx < 0 || idx >= len(cs.counts) {
        err = errors.New("invalid idx")
        return
    }

    key := idx

    err = cs.mus.RLock(ctx, key)
    if err != nil {
        return
    }

    defer cs.mus.RUnlock(key)

    catch(cs.counts[idx])

    return
}

golock's People

Contributors

imstevez avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.