mirror of
				https://github.com/ossrs/srs.git
				synced 2025-03-09 15:49:59 +00:00 
			
		
		
		
	Cover more ST Coroutine code
This commit is contained in:
		
							parent
							
								
									1ce040cc2c
								
							
						
					
					
						commit
						7037f6a197
					
				
					 3 changed files with 37 additions and 12 deletions
				
			
		| 
						 | 
					@ -79,6 +79,8 @@ int SrsDummyCoroutine::cid()
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_ST_THREAD_CREATE_PFN _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)st_thread_create;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SrsSTCoroutine::SrsSTCoroutine(const string& n, ISrsCoroutineHandler* h, int cid)
 | 
					SrsSTCoroutine::SrsSTCoroutine(const string& n, ISrsCoroutineHandler* h, int cid)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    name = n;
 | 
					    name = n;
 | 
				
			||||||
| 
						 | 
					@ -114,7 +116,7 @@ srs_error_t SrsSTCoroutine::start()
 | 
				
			||||||
        return err;
 | 
					        return err;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if ((trd = (srs_thread_t)st_thread_create(pfn, this, 1, 0)) == NULL) {
 | 
					    if ((trd = (srs_thread_t)_pfn_st_thread_create(pfn, this, 1, 0)) == NULL) {
 | 
				
			||||||
        err = srs_error_new(ERROR_ST_CREATE_CYCLE_THREAD, "create failed");
 | 
					        err = srs_error_new(ERROR_ST_CREATE_CYCLE_THREAD, "create failed");
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        srs_freep(trd_err);
 | 
					        srs_freep(trd_err);
 | 
				
			||||||
| 
						 | 
					@ -137,20 +139,18 @@ void SrsSTCoroutine::stop()
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    interrupt();
 | 
					    interrupt();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void* res = NULL;
 | 
					 | 
				
			||||||
    // When not started, the rd is NULL.
 | 
					    // When not started, the rd is NULL.
 | 
				
			||||||
    if (trd) {
 | 
					    if (trd) {
 | 
				
			||||||
 | 
					        void* res = NULL;
 | 
				
			||||||
        int r0 = st_thread_join((st_thread_t)trd, &res);
 | 
					        int r0 = st_thread_join((st_thread_t)trd, &res);
 | 
				
			||||||
        srs_assert(!r0);
 | 
					        srs_assert(!r0);
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Always override the error by the error from worker.
 | 
					        srs_error_t err_res = (srs_error_t)res;
 | 
				
			||||||
    srs_error_t err_res = (srs_error_t)res;
 | 
					        if (err_res != srs_success) {
 | 
				
			||||||
    if (err_res != srs_success && trd_err != err_res) {
 | 
					            // When worker cycle done, the error has already been overrided,
 | 
				
			||||||
        srs_freep(trd_err);
 | 
					            // so the trd_err should be equal to err_res.
 | 
				
			||||||
        // It's ok to directly use it, because it's returned by st_thread_join.
 | 
					            srs_assert(trd_err == err_res);
 | 
				
			||||||
        trd_err = err_res;
 | 
					        }
 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // If there's no error occur from worker, try to set to terminated error.
 | 
					    // If there's no error occur from worker, try to set to terminated error.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -106,6 +106,10 @@ public:
 | 
				
			||||||
    virtual int cid();
 | 
					    virtual int cid();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// For utest to mock the thread create.
 | 
				
			||||||
 | 
					typedef void* (*_ST_THREAD_CREATE_PFN)(void *(*start)(void *arg), void *arg, int joinable, int stack_size);
 | 
				
			||||||
 | 
					extern _ST_THREAD_CREATE_PFN _pfn_st_thread_create;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * A ST-coroutine is a lightweight thread, just like the goroutine.
 | 
					 * A ST-coroutine is a lightweight thread, just like the goroutine.
 | 
				
			||||||
 * But the goroutine maybe run on different thread, while ST-coroutine only
 | 
					 * But the goroutine maybe run on different thread, while ST-coroutine only
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -285,4 +285,25 @@ VOID TEST(AppCoroutineTest, Cycle)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void* mock_st_thread_create(void *(*/*start*/)(void *arg), void */*arg*/, int /*joinable*/, int /*stack_size*/) {
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					VOID TEST(AppCoroutineTest, StartThread)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    MockCoroutineHandler ch;
 | 
				
			||||||
 | 
					    SrsSTCoroutine sc("test", &ch);
 | 
				
			||||||
 | 
					    ch.trd = ≻
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _ST_THREAD_CREATE_PFN ov = _pfn_st_thread_create;
 | 
				
			||||||
 | 
					    _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)mock_st_thread_create;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    srs_error_t err = sc.start();
 | 
				
			||||||
 | 
					    _pfn_st_thread_create = ov;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    EXPECT_TRUE(srs_success != err);
 | 
				
			||||||
 | 
					    EXPECT_TRUE(ERROR_ST_CREATE_CYCLE_THREAD == srs_error_code(err));
 | 
				
			||||||
 | 
					    srs_freep(err);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue