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

What is the reason to deprecate Lock in swoole 4.4 ? #2601

Closed
abbychau opened this issue May 28, 2019 · 7 comments
Closed

What is the reason to deprecate Lock in swoole 4.4 ? #2601

abbychau opened this issue May 28, 2019 · 7 comments

Comments

@abbychau
Copy link

I found 废弃Lock模块,废弃原因:在协程模式下加锁可能存在问题,可使用chan实现协程版本的锁.

What is the concrete reason? or what are the error instances?

@matyhtf
Copy link
Member

matyhtf commented May 29, 2019

Bad Case:

<?php
$lock = new Swoole\Lock();
$c = 2;

while ($c--) {
    go(function () use ($lock) {
        $lock->lock();
        Co::sleep(1);
        $lock->unlock();
    });
}

@matyhtf
Copy link
Member

matyhtf commented May 29, 2019

This will cause a deadlock

@matyhtf matyhtf closed this as completed May 29, 2019
@abbychau
Copy link
Author

abbychau commented May 29, 2019

Thanks. I try to elaborate. Please tell me if I am wrong.

I think the problem is because of Co::sleep, Locks are across the threads, a thread sleep could be more suitable. However, this will block the second creation of Coroutine, which is not the expected behavior.

after reading

static void sleep_timeout(swTimer *timer, swTimer_node *tnode)
{
    ((Coroutine *) tnode->data)->resume();
}

int System::sleep(double sec)
{
    Coroutine* co = Coroutine::get_current_safe();
    if (swTimer_add(&SwooleG.timer, (long) (sec * 1000), 0, co, sleep_timeout) == NULL)
    {
        return -1;
    }
    co->yield();
    return 0;
}

the yield may not be resumed after the next coroutine comes in.

@huanghantao
Copy link
Member

@abbychau 死锁的原因是Swoole的协程是在同一个线程里面跑,也就是说,这个线程把自己给锁住了。如果未来Swoole的协程可以在不同的线程里面跑,应该是不会死锁的。

@huanghantao
Copy link
Member

@abbychau Swoole以后应该是会提供协程锁

@abbychau
Copy link
Author

@abbychau 死锁的原因是Swoole的协程是在同一个线程里面跑,也就是说,这个线程把自己给锁住了。如果未来Swoole的协程可以在不同的线程里面跑,应该是不会死锁的。

I think so too.

@abbychau
Copy link
Author

@abbychau Swoole以后应该是会提供协程锁

This will be very cool. 😎 kotlinx implemented it.

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

No branches or pull requests

3 participants