mirror of
				https://github.com/ossrs/srs.git
				synced 2025-03-09 15:49:59 +00:00 
			
		
		
		
	Merge branch 'srs.master'
This commit is contained in:
		
						commit
						9d6c22d45e
					
				
					 2 changed files with 291 additions and 307 deletions
				
			
		| 
						 | 
				
			
			@ -46,7 +46,6 @@
 | 
			
		|||
#include <sys/mman.h>
 | 
			
		||||
#include "common.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* How much space to leave between the stacks, at each end */
 | 
			
		||||
#define REDZONE    _ST_PAGE_SIZE
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -75,8 +74,9 @@ _st_stack_t *_st_stack_new(int stack_size)
 | 
			
		|||
    }
 | 
			
		||||
    
 | 
			
		||||
    /* Make a new thread stack object. */
 | 
			
		||||
  if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL)
 | 
			
		||||
    if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
 | 
			
		||||
    ts->vaddr_size = stack_size + 2*REDZONE + extra;
 | 
			
		||||
    ts->vaddr = _st_new_stk_segment(ts->vaddr_size);
 | 
			
		||||
| 
						 | 
				
			
			@ -103,21 +103,20 @@ _st_stack_t *_st_stack_new(int stack_size)
 | 
			
		|||
    return ts;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Free the stack for the current thread
 | 
			
		||||
 */
 | 
			
		||||
void _st_stack_free(_st_stack_t *ts)
 | 
			
		||||
{
 | 
			
		||||
  if (!ts)
 | 
			
		||||
    if (!ts) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /* Put the stack on the free list */
 | 
			
		||||
    ST_APPEND_LINK(&ts->links, _st_free_stacks.prev);
 | 
			
		||||
    _st_num_free_stacks++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static char *_st_new_stk_segment(int size)
 | 
			
		||||
{
 | 
			
		||||
#ifdef MALLOC_STACK
 | 
			
		||||
| 
						 | 
				
			
			@ -127,28 +126,29 @@ static char *_st_new_stk_segment(int size)
 | 
			
		|||
    int mmap_flags = MAP_PRIVATE;
 | 
			
		||||
    void *vaddr;
 | 
			
		||||
    
 | 
			
		||||
#if defined (MD_USE_SYSV_ANON_MMAP)
 | 
			
		||||
    #if defined (MD_USE_SYSV_ANON_MMAP)
 | 
			
		||||
    if (zero_fd < 0) {
 | 
			
		||||
    if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0)
 | 
			
		||||
        if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
        fcntl(zero_fd, F_SETFD, FD_CLOEXEC);
 | 
			
		||||
    }
 | 
			
		||||
#elif defined (MD_USE_BSD_ANON_MMAP)
 | 
			
		||||
    #elif defined (MD_USE_BSD_ANON_MMAP)
 | 
			
		||||
    mmap_flags |= MAP_ANON;
 | 
			
		||||
#else
 | 
			
		||||
#error Unknown OS
 | 
			
		||||
#endif
 | 
			
		||||
    #else
 | 
			
		||||
        #error Unknown OS
 | 
			
		||||
    #endif
 | 
			
		||||
    
 | 
			
		||||
    vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0);
 | 
			
		||||
  if (vaddr == (void *)MAP_FAILED)
 | 
			
		||||
    if (vaddr == (void *)MAP_FAILED) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
#endif /* MALLOC_STACK */
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
    return (char *)vaddr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Not used */
 | 
			
		||||
#if 0
 | 
			
		||||
void _st_delete_stk_segment(char *vaddr, int size)
 | 
			
		||||
| 
						 | 
				
			
			@ -166,8 +166,9 @@ int st_randomize_stacks(int on)
 | 
			
		|||
    int wason = _st_randomize_stacks;
 | 
			
		||||
    
 | 
			
		||||
    _st_randomize_stacks = on;
 | 
			
		||||
  if (on)
 | 
			
		||||
    if (on) {
 | 
			
		||||
        srandom((unsigned int) st_utime());
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return wason;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,14 +44,12 @@
 | 
			
		|||
#include <errno.h>
 | 
			
		||||
#include "common.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
extern time_t _st_curr_time;
 | 
			
		||||
extern st_utime_t _st_last_tset;
 | 
			
		||||
extern int _st_active_count;
 | 
			
		||||
 | 
			
		||||
static st_utime_t (*_st_utime)(void) = NULL;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 * Time functions
 | 
			
		||||
 */
 | 
			
		||||
| 
						 | 
				
			
			@ -62,14 +60,13 @@ st_utime_t st_utime(void)
 | 
			
		|||
#ifdef MD_GET_UTIME
 | 
			
		||||
        MD_GET_UTIME();
 | 
			
		||||
#else
 | 
			
		||||
#error Unknown OS
 | 
			
		||||
        #error Unknown OS
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return (*_st_utime)();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_set_utime_function(st_utime_t (*func)(void))
 | 
			
		||||
{
 | 
			
		||||
    if (_st_active_count) {
 | 
			
		||||
| 
						 | 
				
			
			@ -82,13 +79,11 @@ int st_set_utime_function(st_utime_t (*func)(void))
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
st_utime_t st_utime_last_clock(void)
 | 
			
		||||
{
 | 
			
		||||
    return _ST_LAST_CLOCK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_timecache_set(int on)
 | 
			
		||||
{
 | 
			
		||||
    int wason = (_st_curr_time) ? 1 : 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -96,22 +91,22 @@ int st_timecache_set(int on)
 | 
			
		|||
    if (on) {
 | 
			
		||||
        _st_curr_time = time(NULL);
 | 
			
		||||
        _st_last_tset = st_utime();
 | 
			
		||||
  } else
 | 
			
		||||
    } else {
 | 
			
		||||
        _st_curr_time = 0;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return wason;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
time_t st_time(void)
 | 
			
		||||
{
 | 
			
		||||
  if (_st_curr_time)
 | 
			
		||||
    if (_st_curr_time) {
 | 
			
		||||
        return _st_curr_time;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return time(NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_usleep(st_utime_t usecs)
 | 
			
		||||
{
 | 
			
		||||
    _st_thread_t *me = _ST_CURRENT_THREAD();
 | 
			
		||||
| 
						 | 
				
			
			@ -125,8 +120,9 @@ int st_usleep(st_utime_t usecs)
 | 
			
		|||
    if (usecs != ST_UTIME_NO_TIMEOUT) {
 | 
			
		||||
        me->state = _ST_ST_SLEEPING;
 | 
			
		||||
        _ST_ADD_SLEEPQ(me, usecs);
 | 
			
		||||
  } else
 | 
			
		||||
    } else {
 | 
			
		||||
        me->state = _ST_ST_SUSPENDED;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    _ST_SWITCH_CONTEXT(me);
 | 
			
		||||
    
 | 
			
		||||
| 
						 | 
				
			
			@ -139,18 +135,14 @@ int st_usleep(st_utime_t usecs)
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_sleep(int secs)
 | 
			
		||||
{
 | 
			
		||||
  return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL :
 | 
			
		||||
                 ST_UTIME_NO_TIMEOUT);
 | 
			
		||||
    return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 * Condition variable functions
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
_st_cond_t *st_cond_new(void)
 | 
			
		||||
{
 | 
			
		||||
    _st_cond_t *cvar;
 | 
			
		||||
| 
						 | 
				
			
			@ -163,7 +155,6 @@ _st_cond_t *st_cond_new(void)
 | 
			
		|||
    return cvar;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_cond_destroy(_st_cond_t *cvar)
 | 
			
		||||
{
 | 
			
		||||
    if (cvar->wait_q.next != &cvar->wait_q) {
 | 
			
		||||
| 
						 | 
				
			
			@ -176,7 +167,6 @@ int st_cond_destroy(_st_cond_t *cvar)
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
 | 
			
		||||
{
 | 
			
		||||
    _st_thread_t *me = _ST_CURRENT_THREAD();
 | 
			
		||||
| 
						 | 
				
			
			@ -192,8 +182,9 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
 | 
			
		|||
    me->state = _ST_ST_COND_WAIT;
 | 
			
		||||
    ST_APPEND_LINK(&me->wait_links, &cvar->wait_q);
 | 
			
		||||
    
 | 
			
		||||
  if (timeout != ST_UTIME_NO_TIMEOUT)
 | 
			
		||||
    if (timeout != ST_UTIME_NO_TIMEOUT) {
 | 
			
		||||
        _ST_ADD_SLEEPQ(me, timeout);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    _ST_SWITCH_CONTEXT(me);
 | 
			
		||||
    
 | 
			
		||||
| 
						 | 
				
			
			@ -214,13 +205,11 @@ int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout)
 | 
			
		|||
    return rv;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_cond_wait(_st_cond_t *cvar)
 | 
			
		||||
{
 | 
			
		||||
    return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
 | 
			
		||||
{
 | 
			
		||||
    _st_thread_t *thread;
 | 
			
		||||
| 
						 | 
				
			
			@ -229,37 +218,35 @@ static int _st_cond_signal(_st_cond_t *cvar, int broadcast)
 | 
			
		|||
    for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) {
 | 
			
		||||
        thread = _ST_THREAD_WAITQ_PTR(q);
 | 
			
		||||
        if (thread->state == _ST_ST_COND_WAIT) {
 | 
			
		||||
      if (thread->flags & _ST_FL_ON_SLEEPQ)
 | 
			
		||||
            if (thread->flags & _ST_FL_ON_SLEEPQ) {
 | 
			
		||||
                _ST_DEL_SLEEPQ(thread);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            /* Make thread runnable */
 | 
			
		||||
            thread->state = _ST_ST_RUNNABLE;
 | 
			
		||||
            _ST_ADD_RUNQ(thread);
 | 
			
		||||
      if (!broadcast)
 | 
			
		||||
            if (!broadcast) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_cond_signal(_st_cond_t *cvar)
 | 
			
		||||
{
 | 
			
		||||
    return _st_cond_signal(cvar, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_cond_broadcast(_st_cond_t *cvar)
 | 
			
		||||
{
 | 
			
		||||
    return _st_cond_signal(cvar, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 * Mutex functions
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
_st_mutex_t *st_mutex_new(void)
 | 
			
		||||
{
 | 
			
		||||
    _st_mutex_t *lock;
 | 
			
		||||
| 
						 | 
				
			
			@ -273,7 +260,6 @@ _st_mutex_t *st_mutex_new(void)
 | 
			
		|||
    return lock;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_mutex_destroy(_st_mutex_t *lock)
 | 
			
		||||
{
 | 
			
		||||
    if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) {
 | 
			
		||||
| 
						 | 
				
			
			@ -286,7 +272,6 @@ int st_mutex_destroy(_st_mutex_t *lock)
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_mutex_lock(_st_mutex_t *lock)
 | 
			
		||||
{
 | 
			
		||||
    _st_thread_t *me = _ST_CURRENT_THREAD();
 | 
			
		||||
| 
						 | 
				
			
			@ -325,7 +310,6 @@ int st_mutex_lock(_st_mutex_t *lock)
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_mutex_unlock(_st_mutex_t *lock)
 | 
			
		||||
{
 | 
			
		||||
    _st_thread_t *thread;
 | 
			
		||||
| 
						 | 
				
			
			@ -353,7 +337,6 @@ int st_mutex_unlock(_st_mutex_t *lock)
 | 
			
		|||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int st_mutex_trylock(_st_mutex_t *lock)
 | 
			
		||||
{
 | 
			
		||||
    if (lock->owner != NULL) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue