1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

research st, expand MD_INIT_CONTEXT macro, the thread start.

This commit is contained in:
winlin 2014-11-10 13:52:17 +08:00
parent 8da8c49f39
commit 2c01f7e943
5 changed files with 73 additions and 44 deletions

View file

@ -410,15 +410,6 @@ extern _st_eventsys_t *_st_eventsys;
MD_LONGJMP((_thread)->context, 1); \
ST_END_MACRO
/*
* Initialize the thread context preparing it to execute _main
*/
#ifdef MD_INIT_CONTEXT
#define _ST_INIT_CONTEXT MD_INIT_CONTEXT
#else
#error Unknown OS
#endif
/*
* Number of bytes reserved under the stack "bottom"
*/

View file

@ -107,13 +107,6 @@
#if defined(__mips__)
#define MD_STACK_GROWS_DOWN
#define MD_INIT_CONTEXT(_thread, _sp, _main) \
ST_BEGIN_MACRO \
MD_SETJMP((_thread)->context); \
_thread->context[0].__jmpbuf[0].__pc = (__ptr_t) _main; \
_thread->context[0].__jmpbuf[0].__sp = _sp; \
ST_END_MACRO
#else /* Not or mips */
/*
* On linux, there are a few styles of jmpbuf format. These vary based
@ -166,13 +159,6 @@
#else
#error "Unknown CPU architecture"
#endif /* Cases with common MD_INIT_CONTEXT and different SP locations */
#define MD_INIT_CONTEXT(_thread, _sp, _main) \
ST_BEGIN_MACRO \
if (MD_SETJMP((_thread)->context)) \
_main(); \
MD_GET_SP(_thread) = (long) (_sp); \
ST_END_MACRO
#endif /* Cases with different MD_INIT_CONTEXT */
#if defined(MD_USE_BUILTIN_SETJMP) && !defined(USE_LIBC_SETJMP)

View file

@ -553,6 +553,20 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
* grows downward. Both stacks start in the middle and grow outward
* from each other.
*/
/**
The below comments is by winlin:
The Stack public structure:
+--------------------------------------------------------------+
| stack |
+--------------------------------------------------------------+
bottom top
The code bellow use the stack as:
+-----------------+-----------------+-------------+------------+
| stack of thread |pad+align(128B+) |thread(336B) | keys(128B) |
+-----------------+-----------------+-------------+------------+
bottom sp trd ptds top
(context[0].__jmpbuf.sp) (private_data)
*/
sp = sp - (ST_KEYS_MAX * sizeof(void *));
ptds = (void **) sp;
sp = sp - sizeof(_st_thread_t);
@ -575,8 +589,19 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
trd->stack = stack;
trd->start = start;
trd->arg = arg;
_ST_INIT_CONTEXT(trd, stack->sp, _st_thread_main);
// by winlin, expand macro MD_INIT_CONTEXT
#if defined(__mips__)
MD_SETJMP((trd)->context);
trd->context[0].__jmpbuf[0].__pc = (__ptr_t) _st_thread_main;
trd->context[0].__jmpbuf[0].__sp = stack->sp;
#else
int ret_setjmp = 0;
if ((ret_setjmp = MD_SETJMP((trd)->context)) != 0) {
_st_thread_main();
}
MD_GET_SP(trd) = (long) (stack->sp);
#endif
/* If thread is joinable, allocate a termination condition variable */
if (joinable) {

View file

@ -46,6 +46,21 @@ int huge_stack_test()
return 0;
}
int sleep_test()
{
srs_trace("===================================================");
srs_trace("sleep test: start");
srs_trace("1. sleep...");
st_usleep(sleep_ms * 1000);
srs_trace("2. sleep ok");
srs_trace("sleep test: end");
return 0;
}
void* thread_func(void* arg)
{
srs_trace("1. thread run");
@ -68,8 +83,10 @@ int thread_test()
st_thread_join(trd, NULL);
srs_trace("3. thread joined");
st_thread_exit(NULL);
srs_trace("4. all thread completed");
srs_trace("thread test: end");
exit(0);
return 0;
}
@ -342,6 +359,11 @@ int main(int argc, char** argv)
return -1;
}
if (sleep_test() < 0) {
srs_trace("sleep_test failed");
return -1;
}
if (thread_test() < 0) {
srs_trace("thread_test failed");
return -1;