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:
parent
8da8c49f39
commit
2c01f7e943
5 changed files with 73 additions and 44 deletions
|
@ -9,15 +9,20 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
|
|
||||||
jmp_buf env_func1;
|
jmp_buf context;
|
||||||
|
|
||||||
void func1()
|
void func1()
|
||||||
{
|
{
|
||||||
#if defined(__amd64__) || defined(__x86_64__)
|
#if defined(__amd64__) || defined(__x86_64__)
|
||||||
register long int rsp0 asm("rsp");
|
register long int rsp0 asm("rsp");
|
||||||
|
|
||||||
int ret = setjmp(env_func1);
|
int ret = setjmp(context);
|
||||||
printf("setjmp func0 ret=%d, rsp=%#lx\n", ret, rsp0);
|
printf("setjmp func1 ret=%d, rsp=%#lx\n", ret, rsp0);
|
||||||
|
// enter by longjmp
|
||||||
|
if (ret != 0) {
|
||||||
|
printf("call by longjmp.\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,15 +54,15 @@ void func0()
|
||||||
// for glibc 2.4+, it's not possible to get and set the sp in jmp_buf
|
// for glibc 2.4+, it's not possible to get and set the sp in jmp_buf
|
||||||
/**
|
/**
|
||||||
for example, the following is show the jmp_buf when setjmp:
|
for example, the following is show the jmp_buf when setjmp:
|
||||||
(gdb) x /64xb env_func1[0].__jmpbuf
|
(gdb) x /64xb context[0].__jmpbuf
|
||||||
0x600ca0 <env_func1>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
0x600ca0 <context>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||||
0x600ca8 <env_func1+8>: 0xf8 0xc1 0x71 0xe5 0xa8 0x88 0xb4 0x15
|
0x600ca8 <context+8>: 0xf8 0xc1 0x71 0xe5 0xa8 0x88 0xb4 0x15
|
||||||
0x600cb0 <env_func1+16>: 0xa0 0x05 0x40 0x00 0x00 0x00 0x00 0x00
|
0x600cb0 <context+16>: 0xa0 0x05 0x40 0x00 0x00 0x00 0x00 0x00
|
||||||
0x600cb8 <env_func1+24>: 0x90 0xe4 0xff 0xff 0xff 0x7f 0x00 0x00
|
0x600cb8 <context+24>: 0x90 0xe4 0xff 0xff 0xff 0x7f 0x00 0x00
|
||||||
0x600cc0 <env_func1+32>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
0x600cc0 <context+32>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||||
0x600cc8 <env_func1+40>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
0x600cc8 <context+40>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||||
0x600cd0 <env_func1+48>: 0xf8 0xc1 0x51 0xe5 0xa8 0x88 0xb4 0x15
|
0x600cd0 <context+48>: 0xf8 0xc1 0x51 0xe5 0xa8 0x88 0xb4 0x15
|
||||||
0x600cd8 <env_func1+56>: 0xf8 0xc1 0xd9 0x2f 0xd7 0x77 0x4b 0xea
|
0x600cd8 <context+56>: 0xf8 0xc1 0xd9 0x2f 0xd7 0x77 0x4b 0xea
|
||||||
(gdb) p /x $sp
|
(gdb) p /x $sp
|
||||||
$4 = 0x7fffffffe380
|
$4 = 0x7fffffffe380
|
||||||
we cannot finger the sp out.
|
we cannot finger the sp out.
|
||||||
|
@ -65,12 +70,12 @@ void func0()
|
||||||
*/
|
*/
|
||||||
register long int rsp0 asm("rsp");
|
register long int rsp0 asm("rsp");
|
||||||
|
|
||||||
int ret = setjmp(env_func1);
|
int ret = setjmp(context);
|
||||||
printf("setjmp func0 ret=%d, rsp=%#lx\n", ret, rsp0);
|
printf("setjmp func0 ret=%d, rsp=%#lx\n", ret, rsp0);
|
||||||
|
|
||||||
printf("after setjmp: ");
|
printf("after setjmp: ");
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
printf("env[%d]=%#x, ", i, (int)env_func1[0].__jmpbuf[i]);
|
printf("env[%d]=%#x, ", i, (int)context[0].__jmpbuf[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
@ -100,7 +105,7 @@ void func0()
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
For example, on raspberry-pi, armv6 cpu:
|
For example, on raspberry-pi, armv6 cpu:
|
||||||
(gdb) x /64 env_func1[0].__jmpbuf
|
(gdb) x /64 context[0].__jmpbuf
|
||||||
v1, 0: 0x00 0x00 0x00 0x00
|
v1, 0: 0x00 0x00 0x00 0x00
|
||||||
v2, 1: 0x00 0x00 0x00 0x00
|
v2, 1: 0x00 0x00 0x00 0x00
|
||||||
v3, 2: 0x2c 0x84 0x00 0x00
|
v3, 2: 0x2c 0x84 0x00 0x00
|
||||||
|
@ -116,19 +121,18 @@ void func0()
|
||||||
(gdb) p /x $pc
|
(gdb) p /x $pc
|
||||||
$4 = 0x850c
|
$4 = 0x850c
|
||||||
*/
|
*/
|
||||||
int ret = setjmp(env_func1);
|
int ret = setjmp(context);
|
||||||
printf("setjmp func1 ret=%d\n", ret);
|
printf("setjmp func1 ret=%d\n", ret);
|
||||||
|
|
||||||
printf("after setjmp: ");
|
printf("after setjmp: ");
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
printf("env[%d]=%#x, ", i, (int)env_func1[0].__jmpbuf[i]);
|
printf("env[%d]=%#x, ", i, (int)context[0].__jmpbuf[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("func0 terminated\n");
|
printf("func0 terminated\n");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
extern uintptr_t _jmpbuf_sp (__jmp_buf regs);
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
#if defined(__amd64__) || defined(__x86_64__)
|
#if defined(__amd64__) || defined(__x86_64__)
|
||||||
printf("x86_64 sizeof(long int)=%d, sizeof(long)=%d, "
|
printf("x86_64 sizeof(long int)=%d, sizeof(long)=%d, "
|
||||||
|
@ -143,6 +147,7 @@ int main(int argc, char** argv) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
func0();
|
func0();
|
||||||
|
longjmp(context, 1);
|
||||||
|
|
||||||
printf("terminated\n");
|
printf("terminated\n");
|
||||||
|
|
||||||
|
|
|
@ -410,15 +410,6 @@ extern _st_eventsys_t *_st_eventsys;
|
||||||
MD_LONGJMP((_thread)->context, 1); \
|
MD_LONGJMP((_thread)->context, 1); \
|
||||||
ST_END_MACRO
|
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"
|
* Number of bytes reserved under the stack "bottom"
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -107,13 +107,6 @@
|
||||||
|
|
||||||
#if defined(__mips__)
|
#if defined(__mips__)
|
||||||
#define MD_STACK_GROWS_DOWN
|
#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 */
|
#else /* Not or mips */
|
||||||
/*
|
/*
|
||||||
* On linux, there are a few styles of jmpbuf format. These vary based
|
* On linux, there are a few styles of jmpbuf format. These vary based
|
||||||
|
@ -166,13 +159,6 @@
|
||||||
#else
|
#else
|
||||||
#error "Unknown CPU architecture"
|
#error "Unknown CPU architecture"
|
||||||
#endif /* Cases with common MD_INIT_CONTEXT and different SP locations */
|
#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 */
|
#endif /* Cases with different MD_INIT_CONTEXT */
|
||||||
|
|
||||||
#if defined(MD_USE_BUILTIN_SETJMP) && !defined(USE_LIBC_SETJMP)
|
#if defined(MD_USE_BUILTIN_SETJMP) && !defined(USE_LIBC_SETJMP)
|
||||||
|
|
|
@ -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
|
* grows downward. Both stacks start in the middle and grow outward
|
||||||
* from each other.
|
* 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 *));
|
sp = sp - (ST_KEYS_MAX * sizeof(void *));
|
||||||
ptds = (void **) sp;
|
ptds = (void **) sp;
|
||||||
sp = sp - sizeof(_st_thread_t);
|
sp = sp - sizeof(_st_thread_t);
|
||||||
|
@ -576,7 +590,18 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
|
||||||
trd->start = start;
|
trd->start = start;
|
||||||
trd->arg = arg;
|
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 thread is joinable, allocate a termination condition variable */
|
||||||
if (joinable) {
|
if (joinable) {
|
||||||
|
|
|
@ -46,6 +46,21 @@ int huge_stack_test()
|
||||||
return 0;
|
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)
|
void* thread_func(void* arg)
|
||||||
{
|
{
|
||||||
srs_trace("1. thread run");
|
srs_trace("1. thread run");
|
||||||
|
@ -68,8 +83,10 @@ int thread_test()
|
||||||
st_thread_join(trd, NULL);
|
st_thread_join(trd, NULL);
|
||||||
srs_trace("3. thread joined");
|
srs_trace("3. thread joined");
|
||||||
|
|
||||||
|
st_thread_exit(NULL);
|
||||||
|
srs_trace("4. all thread completed");
|
||||||
|
|
||||||
srs_trace("thread test: end");
|
srs_trace("thread test: end");
|
||||||
exit(0);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -342,6 +359,11 @@ int main(int argc, char** argv)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sleep_test() < 0) {
|
||||||
|
srs_trace("sleep_test failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (thread_test() < 0) {
|
if (thread_test() < 0) {
|
||||||
srs_trace("thread_test failed");
|
srs_trace("thread_test failed");
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue