Code Monkey home page Code Monkey logo

sema's People

Contributors

tarndt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sema's Issues

Problem with cond implementation of TimeoutCountingSema

The below code only appears to work if I use the channel implementation of TimeoutCountingSema. Basically the code keeps a map of TimeoutCountingSema objects that are locked with a timeout and then unlocked.

After a minimal amount of investigation, it seems like the first lock (for a given ID) is acquired and the protected function (fnc) runs, and then the code to unlock is called and Do returns, but then subsequent attempts to acquire the lock for the same ID fail and the code hangs indefinitely. Just calling UseChannelBasedImpl() causes the code to run as expected.

type redirectJobSynchronizer struct {
    mapLock sync.RWMutex
    // Map of per-originID locks
    idToLock map[int64]sema.TimeoutCountingSema
}

func (this *redirectJobSynchronizer) Do(id int64, fnc func() error) error {
    this.mapLock.RLock()
    idSema, ok := this.idToLock[id]
    this.mapLock.RUnlock()
    if !ok {
        this.mapLock.Lock()
        if idSema, ok = this.idToLock[id]; !ok {
            idSema = sema.NewTimeoutSema(1, 10*time.Second)
            this.idTpLock[id] = idSema
        }
        this.mapLock.Unlock()
    }

    //Aquire lock
    if idSema.P() {
        defer idSema.V()
        return fnc()
    }
    return errors.New("timeout")

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.