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

KERNEL: Simplify the ST-thread as simple object. #906

Closed
winlinvip opened this issue May 29, 2017 · 4 comments
Closed

KERNEL: Simplify the ST-thread as simple object. #906

winlinvip opened this issue May 29, 2017 · 4 comments
Assignees
Labels
Enhancement Improvement or enhancement. TransByAI Translated by AI/GPT.
Milestone

Comments

@winlinvip
Copy link
Member

To keep object simple and no-reusable, read #786 or #288.
To simplify the thread model, read #902

@winlinvip winlinvip added the Enhancement Improvement or enhancement. label May 29, 2017
@winlinvip winlinvip added this to the srs 3.0 release milestone May 29, 2017
@winlinvip winlinvip changed the title KERNEL: Refine the ST-thread to simple object. KERNEL: Simplify the ST-thread as simple object. May 29, 2017
@winlinvip
Copy link
Member Author

winlinvip commented May 29, 2017

The first category of thread, which has its inside loop, like the SrsSignalManager, it should be writen like:

class TestThread : public ISrsCoroutineHandler
{
public:
    SrsCoroutine trd;
public:
    TestThread() : trd("test",this) {
    }
    virtual ~TestThread() {
    }
public:
    int cycle() {
        while (!trd.pull()) {
            st_sleep(3); // Do something.
        }
        return ERROR_SUCCESS;
    }
};

Another type of thread, is need to be restart, like the SrsTcpListener, we must delete the thread then restart:

class SrsTcpListener : public ISrsCoroutineHandler
{
private:
    SrsCoroutine* trd;
public:
    SrsTcpListener() {
        trd = NULL;
    }
    virtual ~SrsTcpListener() {
        srs_freep(trd);
    }
public:
    virtual int start() {
        srs_freep(trd);
        trd = new SrsCoroutine("tcp", this);
        return trd->start();
    }
    virtual void stop() {
        trd->stop();
    }
    virtual int cycle() {
        while (!trd->pull()) {
            int fd = st_accept(xxx); // Accept and process fd.
        }
    }

It's important to check the thread is running then sleep, for example:

// when error, encoder sleep for a while and retry.
#define SRS_RTMP_ENCODER_CIMS (3000)

int SrsEncoder::cycle()
{
    int ret = ERROR_SUCCESS;
    
    while (!trd->pull()) {
        if ((ret = do_cycle()) != ERROR_SUCCESS) {
            srs_warn("Encoder: Ignore error, ret=%d", ret);
        }
        
        if (!trd->pull()) {
            st_usleep(SRS_RTMP_ENCODER_CIMS * 1000);
        }
    }
    
    return ret;
}

@winlinvip
Copy link
Member Author

Please notice the pull of thread:

    /**
     * Check whether thread is terminated normally or error(stopped or termianted with error),
     * and the thread should be running if it return ERROR_SUCCESS.
     * @remark Return specified error when thread terminated normally with error.
     * @remark Return ERROR_THREAD_TERMINATED when thread terminated normally without error.
     * @remark Return ERROR_THREAD_INTERRUPED when thread is interrupted.
     */
    virtual int pull();

User should ignore ERROR_THREAD_TERMINATED and ERROR_THREAD_INTERRUPED, because they just said the tread is terminated normally(without error) or interrupted by user.

winlinvip added a commit that referenced this issue May 29, 2017
@winlinvip
Copy link
Member Author

winlinvip commented May 29, 2017

Added an SrsCoroutine object and reviewed the usage of ST threads. This time, it should be done correctly. Find time to replace all the threads with this object.

TRANS_BY_GPT3

@winlinvip
Copy link
Member Author

Refined.

@winlinvip winlinvip self-assigned this Sep 15, 2021
@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Improvement or enhancement. TransByAI Translated by AI/GPT.
Projects
None yet
Development

No branches or pull requests

1 participant