Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polling a resource / poor-mans spinlock #76

Closed
Krastanov opened this issue Sep 23, 2021 · 4 comments
Closed

Polling a resource / poor-mans spinlock #76

Krastanov opened this issue Sep 23, 2021 · 4 comments

Comments

@Krastanov
Copy link
Member

Is there a "proper" way to atomically check whether a resource is available in a non-blocking manner?

I want to write something along the lines of

while true:
    if isavailable(resource)
        @yield request(resource)
        break
    else
        @yield timeout(waittime)
    end
end

I am currently thinking about using resource.level == 0 but this seems like an internal implementation detail, not a real API.

@hdavid16
Copy link
Member

That would be the way to do it currently. You could add the function though to check the level and determine if available.

Why don't you just let it get in the queue though if it's not available? Are you checking to add it to the resource that is available (assuming you have more than one resource)?

@Krastanov
Copy link
Member Author

@hdavid16 , yes, it is because I am trying to avoid locking too many resources. However, I should admit that I am not experienced in this type of simulations, so maybe I am doing something wrong.

Basically, I have a chain of resources and I have a process for each pair of resources. If I naively do the following:

@resumable function establish_link(env, r1, r2)
    @yield request(r1) & request(r2)
    @yield timeout(longwaittime)
    release(r1)
    release(r2)
end
resources = [Resource() for i in 1:100]
for i in 1:99
    @process link(env, resources[i], resources[i+1])
end

then establishing all links will take about 99*longwaittime units of time, because each iteration of the loop needs to wait for the resources of the previous iteration. But if the request was in the form from my first post, then establishing all links will take just 2*longwaittime. Is there a better way to write this?

@hdavid16
Copy link
Member

Ok. So basically you decrease the simulation time because you end up running every other link simultaneously and then you do the ones that were skipped, also simultaneously.

Your approach of checking the availability is more robust than doing two loops, one for the odd i and the other for the even ones. Especially if your service times are not the same.

@Krastanov
Copy link
Member Author

We now have islocked and isready from #93 . Closing as complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants