Fork from http://sourceforge.net/projects/state-threads, patched for SRS.
For original ST without any changes, checkout the ST master branch.
state-threads is licenced under MPL or GPLv2.
Get code:
git clone -b srs https://github.com/ossrs/state-threads.gitFor Linux:
make linux-debugFor Linux aarch64, which fail with Unknown CPU architecture:
make linux-debug EXTRA_CFLAGS="-D__aarch64__"Note: For more CPU architectures, please see #22
Linux with valgrind:
make linux-debug EXTRA_CFLAGS="-DMD_VALGRIND"Remark: User must install valgrind, for instance, in centos6
sudo yum install -y valgrind valgrind-devel.
Linux with valgrind and epoll:
make linux-debug EXTRA_CFLAGS="-DMD_HAVE_EPOLL -DMD_VALGRIND"Linux with ASAN(Google Address Sanitizer):
make linux-debug EXTRA_CFLAGS="-DMD_ASAN"Get code:
git clone -b srs https://github.com/ossrs/state-threads.gitFor OSX:
make darwin-debugFor OSX, user must specifies the valgrind header files:
make darwin-debug EXTRA_CFLAGS="-DMD_HAVE_KQUEUE -DMD_VALGRIND -I/usr/local/include"Remark: M1 is unsupported by ST, please use docker to run, please read SRS#2747.
Get code:
git clone -b srs https://github.com/ossrs/state-threads.gitNative Windows x64 builds with the Visual Studio (MSVC) toolchain, cl, ml64, and lib, through GNU make
and the same Makefile. There is no Cygwin, MinGW, WSL, or POSIX emulation layer: GNU make and Git Bash only
run the build, and the output is the static library obj/libst.a built by MSVC. It needs Windows 10 1809 or
Windows Server 2019 or later, x64.
Install the build tools:
- Visual Studio 2022, or its Build Tools, with the MSVC x64 tools (the "Desktop development with C++" workload).
- Git for Windows, for Git Bash.
- GNU make, for example
winget install ezwinports.makeorchoco install make.
Open the x64 Native Tools Command Prompt for VS 2022, which runs vcvars64.bat and puts cl, ml64, and
lib on PATH, then start Git Bash in it:
"C:\Program Files\Git\bin\bash.exe"
For Windows, build the debug or the optimized library:
make win64-debug
make win64-optimizedNote: Programs that include
st.hmust include it beforewindows.h, or defineWIN32_LEAN_AND_MEAN. The library linksws2_32.libby itself. For what works differently on Windows, read the porting notes.
Windows with ASAN(Google Address Sanitizer), where cl has no -fno-omit-frame-pointer:
make win64-debug EXTRA_CFLAGS="-DMD_ASAN -fsanitize=address"Remark: Cygwin64 is deprecated, do not use it. Use native Windows, as above.
The branch srs was patched and refined. See CHANGELOG.md for the changes and versions.
Planned:
- System: Support sendmmsg for UDP, #12.
- Support nn_coroutines, show number of coroutines.
- Support show_coroutines, show all coroutines and caller function.
How to debug with gdb under valgrind, read valgrind manual.
About startup parameters, read valgrind cli.
Important cli options:
--undef-value-errors=<yes|no> [default: yes], Controls whether Memcheck reports uses of undefined value errors. Set this to no if you don't want to see undefined value errors. It also has the side effect of speeding up Memcheck somewhat.--leak-check=<no|summary|yes|full> [default: summary], When enabled, search for memory leaks when the client program finishes. If set to summary, it says how many leaks occurred. If set to full or yes, each individual leak will be shown in detail and/or counted as an error, as specified by the options--show-leak-kindsand--errors-for-leak-kinds.--track-origins=<yes|no> [default: no], Controls whether Memcheck tracks the origin of uninitialised values. By default, it does not, which means that although it can tell you that an uninitialised value is being used in a dangerous way, it cannot tell you where the uninitialised value came from. This often makes it difficult to track down the root problem.--show-reachable=<yes|no> , --show-possibly-lost=<yes|no>, to show the using memory.
Note: We use Google test in
utest/gtest-fit.
To make ST with utest and run it:
make linux-debug-utest && ./obj/st_utestNote that the gcc(4.8) of CentOS is too old, please use docker(ossrs/srs:dev-gcc7) to run:
docker run --rm -it -v $(pwd):/state-threads -w /state-threads \
registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev-gcc7 \
bash -c 'make linux-debug-utest && ./obj/st_utest'Note: We use Google test in
utest/gtest-fit.
To make ST with utest and run it:
make darwin-debug-utest && ./obj/st_utestNote: We use Google test in
utest/gtest-fit.
To make ST with utest and run it, in Git Bash with the MSVC environment (see Windows: Usage):
make win64-debug-utest && ./obj/st_utest.exeThe tests that need POSIX features (fork, signals, pipes, files) are not built on Windows, and a few are skipped with a reason, for example the write tests that need a partial send, which Winsock never does.
To debug in Visual Studio, generate a solution with CMake, which comes with Visual Studio, from the
x64 Native Tools Command Prompt for VS 2022, then open C:\st-build\st.sln:
cmake -S cmake -B C:\st-build -G "Visual Studio 17 2022" -A x64
cmake --build C:\st-build --config Debug --target st_utest
Note: Use a short build directory, because MSBuild fails when its paths are too long. The Makefile stays the main build, and CMake also builds the
helloworld,porting, andverifytools.
The integration tools in tools/ build with MSVC and run with the same runner as on Linux and macOS, in Git
Bash with the MSVC environment (see Windows: Usage). It builds the library with
make -B win64-debug, then builds each tool and runs <name>.exe with ST_TOOL_EVENTSYS=select and alt
(both are WSAPoll on Windows), and prints All tools OK. Run it in the ST default build and in the SRS
build, as CI does:
./auto/tools.sh
EXTRA_CFLAGS=-DMALLOC_STACK ./auto/tools.shThe arguments name the tools to run, for example ./auto/tools.sh tcp udp. The runner leaves the library
built with EXTRA_CFLAGS, so rebuild with make -B win64-debug-utest before the utest.
On Windows, ST is sockets only, so the tools make each pipe a loopback TCP pair and each socketpair an
AF_UNIX stream pair, or a loopback UDP pair for datagrams. The pipe tool, which needs pipes, FIFOs,
st_open files, and signals, prints SKIP pipe: <why> instead of OK. A few checks that Windows does not
support are skipped there, each with a comment: the partial writes on timeout in tcp (Winsock never sends
part of a non-blocking send), the RLIMIT_NOFILE checks in lifecycle and stress, reading back
O_NONBLOCK in poll, and the AF_UNIX datagram echo and the ENOENT of a missing path in unix.
The exception tool is C++: it throws and catches C++ exceptions on coroutine stacks, and on Windows also
raises and catches SEH exceptions there. Windows builds C++ exceptions on SEH, which rejects frames outside
the stack bounds in the TIB, so this works only because ST switches those bounds with each coroutine stack.
Note: We use Google test in
utest/gtest-fit.
To make ST with utest and run it:
make linux-debug-gcov && ./obj/st_utestNote that the gcc(4.8) of CentOS is too old, please use docker(ossrs/srs:dev-gcc7) to run:
docker run --rm -it -v $(pwd):/state-threads -w /state-threads \
registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev-gcc7 \
bash -c 'make linux-debug-gcov && ./obj/st_utest'Then, install gcovr for coverage:
yum install -y python2-pip &&
pip install lxml && pip install gcovrFinally, run test and get the report:
bash auto/coverage.shNote: We use Google test in
utest/gtest-fit.
To make ST with utest and run it:
make darwin-debug-gcov && ./obj/st_utestThen, install gcovr for coverage:
pip install gcovrFinally, run test and get the report:
bash auto/coverage.sh-
Introduction: http://ossrs.github.io/state-threads/docs/st.html
-
API reference: http://ossrs.github.io/state-threads/docs/reference.html
-
Programming notes: http://ossrs.github.io/state-threads/docs/notes.html
-
About setjmp and longjmp, read setjmp.
-
About the stack structure, read stack
-
About asm code comments, read #91d530e.
-
About the scheduler, read #13-scheduler.
-
About the IO event system, read #13-IO.
-
Code analysis, please read #15.
Winlin 2016
