diff --git a/trunk/3rdparty/st-srs/.circleci/config.yml b/trunk/3rdparty/st-srs/.circleci/config.yml deleted file mode 100644 index a50dab107..000000000 --- a/trunk/3rdparty/st-srs/.circleci/config.yml +++ /dev/null @@ -1,24 +0,0 @@ -version: 2 -jobs: - build: - docker: - - image: ossrs/srs:dev - steps: - - checkout - - run: | - make linux-debug - test: - docker: - - image: ossrs/srs:dev - steps: - - checkout - - run: | - ln -sf /usr/local/gtest utest/gtest && - make linux-debug-gcov && - ./obj/st_utest && bash auto/codecov.sh -workflows: - version: 2 - build_and_test: - jobs: - - build - - test diff --git a/trunk/3rdparty/st-srs/.gitignore b/trunk/3rdparty/st-srs/.gitignore deleted file mode 100644 index 20f61304e..000000000 --- a/trunk/3rdparty/st-srs/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -DARWIN_*_DBG -LINUX_*_DBG -CYGWIN64_*_DBG -obj -st.pc -.idea - -gtest* -googletest-* -*.gcda -*.gcno -coverage -codecov -*.dSYM diff --git a/trunk/3rdparty/st-srs/LICENSE b/trunk/3rdparty/st-srs/LICENSE deleted file mode 100644 index 04f191384..000000000 --- a/trunk/3rdparty/st-srs/LICENSE +++ /dev/null @@ -1,30 +0,0 @@ -The state-threads is provided under the terms of the MPL-1.1 or the -GPL-2.0-or-later. For more information about these licenses, please see -https://spdx.org/licenses/MPL-1.1.html and -https://spdx.org/licenses/GPL-2.0-or-later.html - -Individual files contain the following tag instead of the full license text. - - SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later - -All source code in the "tools", "utest" and "porting" directory is -distributed under the MIT style license. - - SPDX-License-Identifier: MIT - -This enables machine processing of license information based on the SPDX -License Identifiers that are here available: http://spdx.org/licenses/ - ---------------------------------------------------------------------------- -Note: https://github.com/ossrs/state-threads/blob/srs/README#L68 - -The State Threads library is a derivative of the Netscape Portable -Runtime library (NSPR). All source code in this directory is -distributed under the terms of the Mozilla Public License (MPL) version -1.1 or the GNU General Public License (GPL) version 2 or later. For -more information about these licenses please see -http://www.mozilla.org/MPL/ and http://www.gnu.org/copyleft/. - -All source code in the "examples" directory is distributed under the BSD -style license. - diff --git a/trunk/3rdparty/st-srs/Makefile b/trunk/3rdparty/st-srs/Makefile deleted file mode 100644 index 12928b0d1..000000000 --- a/trunk/3rdparty/st-srs/Makefile +++ /dev/null @@ -1,335 +0,0 @@ -# The contents of this file are subject to the Mozilla Public -# License Version 1.1 (the "License"); you may not use this file -# except in compliance with the License. You may obtain a copy of -# the License at http://www.mozilla.org/MPL/ -# -# Software distributed under the License is distributed on an "AS -# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or -# implied. See the License for the specific language governing -# rights and limitations under the License. -# -# The Original Code is the Netscape Portable Runtime library. -# -# The Initial Developer of the Original Code is Netscape -# Communications Corporation. Portions created by Netscape are -# Copyright (C) 1994-2000 Netscape Communications Corporation. All -# Rights Reserved. -# -# Contributor(s): Silicon Graphics, Inc. -# -# Portions created by SGI are Copyright (C) 2000-2001 Silicon -# Graphics, Inc. All Rights Reserved. -# -# Alternatively, the contents of this file may be used under the -# terms of the GNU General Public License Version 2 or later (the -# "GPL"), in which case the provisions of the GPL are applicable -# instead of those above. If you wish to allow use of your -# version of this file only under the terms of the GPL and not to -# allow others to use your version of this file under the MPL, -# indicate your decision by deleting the provisions above and -# replace them with the notice and other provisions required by -# the GPL. If you do not delete the provisions above, a recipient -# may use your version of this file under either the MPL or the -# GPL. - -# This is the full version of the libst library - modify carefully -VERSION = 1.9 - -########################## -# Supported OSes: -# -#OS = DARWIN -#OS = LINUX - -# Please see the "Other possible defines" section below for -# possible compilation options. -########################## - -CC = cc -AR = ar -LD = ld -RANLIB = ranlib -LN = ln -STATIC_ONLY = yes - -SHELL = /bin/sh -ECHO = /bin/echo - -BUILD = DBG -TARGETDIR = $(OS)_$(shell uname -r)_$(BUILD) - -# For Cygwin, it pass a default OS env, we ignore it. -ifeq ($(OS), Windows_NT) -OS = -endif - -# For cygwin/windows, the 'uname -r' generate path with parentheses, -# which cause the make fails, so we use 'uname -s' instead. -ifeq ($(OS), CYGWIN64) -TARGETDIR = $(OS)_$(shell uname -s)_$(BUILD) -endif - -DEFINES = -D$(OS) -CFLAGS = -SFLAGS = -ARFLAGS = -r -LNFLAGS = -s -DSO_SUFFIX = so - -MAJOR = $(shell echo $(VERSION) | sed 's/^\([^\.]*\).*/\1/') -DESC = st.pc - -########################## -# Platform section. -# Possible targets: - -TARGETS = darwin-debug darwin-optimized \ - linux-debug linux-optimized \ - cygwin64-debug - -UTEST_TARGETS = darwin-debug-utest linux-debug-utest \ - darwin-debug-gcov linux-debug-gcov \ - cygwin64-debug-utest - -# -# Platform specifics -# - -ifeq ($(OS), DARWIN) -EXTRA_OBJS = $(TARGETDIR)/md_darwin.o -LD = cc -SFLAGS = -fPIC -fno-common -DSO_SUFFIX = dylib -CFLAGS += -arch x86_64 -LDFLAGS += -arch x86_64 -LDFLAGS += -dynamiclib -install_name /sw/lib/libst.$(MAJOR).$(DSO_SUFFIX) -compatibility_version $(MAJOR) -current_version $(VERSION) -OTHER_FLAGS = -Wall -DEFINES += -DMD_HAVE_KQUEUE -DMD_HAVE_SELECT -endif - -ifeq ($(OS), LINUX) -EXTRA_OBJS = $(TARGETDIR)/md_linux.o -SFLAGS = -fPIC -LDFLAGS = -shared -soname=$(SONAME) -lc -OTHER_FLAGS = -Wall -DEFINES += -DMD_HAVE_EPOLL -DMD_HAVE_SELECT -endif - -ifeq ($(OS), CYGWIN64) -EXTRA_OBJS = $(TARGETDIR)/md_cygwin64.o -SFLAGS = -fPIC -DSO_SUFFIX = dll -LDFLAGS = -shared -soname=$(SONAME) -lc -OTHER_FLAGS = -Wall -DEFINES += -DMD_HAVE_SELECT -endif - -# -# End of platform section. -########################## - - -ifeq ($(BUILD), OPT) -OTHER_FLAGS += -O2 -else -OTHER_FLAGS += -g -O0 -DEFINES += -DDEBUG -endif - -########################## -# Other possible defines: -# To use poll(2) instead of select(2) for events checking: -# DEFINES += -DUSE_POLL -# You may prefer to use select for applications that have many threads -# using one file descriptor, and poll for applications that have many -# different file descriptors. With USE_POLL poll() is called with at -# least one pollfd per I/O-blocked thread, so 1000 threads sharing one -# descriptor will poll 1000 identical pollfds and select would be more -# efficient. But if the threads all use different descriptors poll() -# may be better depending on your operating system's implementation of -# poll and select. Really, it's up to you. Oh, and on some platforms -# poll() fails with more than a few dozen descriptors. -# -# Some platforms allow to define FD_SETSIZE (if select() is used), e.g.: -# DEFINES += -DFD_SETSIZE=4096 -# -# To use malloc(3) instead of mmap(2) for stack allocation: -# DEFINES += -DMALLOC_STACK -# -# To provision more than the default 16 thread-specific-data keys -# (but not too many!): -# DEFINES += -DST_KEYS_MAX= -# -# To start with more than the default 64 initial pollfd slots -# (but the table grows dynamically anyway): -# DEFINES += -DST_MIN_POLLFDS_SIZE= -# -# Note that you can also add these defines by specifying them as -# make/gmake arguments (without editing this Makefile). For example: -# -# make EXTRA_CFLAGS=-DUSE_POLL -# -# (replace make with gmake if needed). -# -# You can also modify the default selection of an alternative event -# notification mechanism. E.g., to enable kqueue(2) support (if it's not -# enabled by default): -# -# gmake EXTRA_CFLAGS=-DMD_HAVE_KQUEUE -# -# or to disable default epoll(4) support: -# -# make EXTRA_CFLAGS=-UMD_HAVE_EPOLL -# -# or to enable sendmmsg(2) support: -# -# make EXTRA_CFLAGS="-DMD_HAVE_SENDMMSG -D_GNU_SOURCE" -# -# or to enable stats for ST: -# -# make EXTRA_CFLAGS=-DDEBUG_STATS -# -# or enable the coverage for utest: -# make UTEST_FLAGS="-fprofile-arcs -ftest-coverage" -# -########################## - -CFLAGS += $(DEFINES) $(OTHER_FLAGS) $(EXTRA_CFLAGS) -CFLAGS += $(UTEST_FLAGS) - -OBJS = $(TARGETDIR)/sched.o \ - $(TARGETDIR)/stk.o \ - $(TARGETDIR)/sync.o \ - $(TARGETDIR)/key.o \ - $(TARGETDIR)/io.o \ - $(TARGETDIR)/event.o -OBJS += $(EXTRA_OBJS) -HEADER = $(TARGETDIR)/st.h -SLIBRARY = $(TARGETDIR)/libst.a -DLIBRARY = $(TARGETDIR)/libst.$(DSO_SUFFIX).$(VERSION) - -LINKNAME = libst.$(DSO_SUFFIX) -SONAME = libst.$(DSO_SUFFIX).$(MAJOR) -FULLNAME = libst.$(DSO_SUFFIX).$(VERSION) - -ifeq ($(OS), DARWIN) -LINKNAME = libst.$(DSO_SUFFIX) -SONAME = libst.$(MAJOR).$(DSO_SUFFIX) -FULLNAME = libst.$(VERSION).$(DSO_SUFFIX) -endif - -ifeq ($(STATIC_ONLY), yes) -LIBRARIES = $(SLIBRARY) -else -LIBRARIES = $(SLIBRARY) $(DLIBRARY) -endif - -ifeq ($(OS),) -ST_ALL = unknown -else -ST_ALL = $(TARGETDIR) $(LIBRARIES) $(HEADER) $(DESC) -endif - -all: $(ST_ALL) - -unknown: - @echo - @echo "Please specify one of the following targets:" - @echo - @for target in $(TARGETS); do echo $$target; done - @echo - @for target in $(UTEST_TARGETS); do echo $$target; done - @echo - -st.pc: st.pc.in - sed "s/@VERSION@/${VERSION}/g" < $< > $@ - -$(TARGETDIR): - if [ ! -d $(TARGETDIR) ]; then mkdir $(TARGETDIR); fi - -$(SLIBRARY): $(OBJS) - $(AR) $(ARFLAGS) $@ $(OBJS) - $(RANLIB) $@ - rm -f obj; $(LN) $(LNFLAGS) $(TARGETDIR) obj - -$(DLIBRARY): $(OBJS:%.o=%-pic.o) - $(LD) $(LDFLAGS) $^ -o $@ - if test "$(LINKNAME)"; then \ - cd $(TARGETDIR); \ - rm -f $(SONAME) $(LINKNAME); \ - $(LN) $(LNFLAGS) $(FULLNAME) $(SONAME); \ - $(LN) $(LNFLAGS) $(FULLNAME) $(LINKNAME); \ - fi - -$(HEADER): public.h - rm -f $@ - cp public.h $@ - -$(TARGETDIR)/md_linux.o: md_linux.S - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGETDIR)/md_darwin.o: md_darwin.S - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGETDIR)/md_cygwin64.o: md_cygwin64.S - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGETDIR)/%.o: %.c common.h md.h - $(CC) $(CFLAGS) -c $< -o $@ - -clean: - rm -rf *_OPT *_DBG obj st.pc - -########################## -# Pattern rules: - -ifneq ($(SFLAGS),) -# Compile with shared library options if it's a C file -$(TARGETDIR)/%-pic.o: %.c common.h md.h - $(CC) $(CFLAGS) $(SFLAGS) -c $< -o $@ -endif - -# Compile assembly as normal or C as normal if no SFLAGS -%-pic.o: %.o - rm -f $@; $(LN) $(LNFLAGS) $(. Install them with: - # rpm -i libst*.rpm -Requires GNU automake and rpm 3.0.3 or later. - -Debian users: - If you run potato, please upgrade to woody. - If you run woody, "apt-get install libst-dev" will get you v1.3. - If you run testing/unstable, you will get the newest available version. - If you *must* have the newest libst in woody, you may follow these - not-recommended instructions: - 1. Add "deb-src unstable main" to your - /etc/apt/sources.list - 2. apt-get update - 3. apt-get source st - 4. cd st-1.4 (or whatever version you got) - 5. debuild - 6. dpkg -i ../*.deb - -If your application uses autoconf to search for dependencies and you -want to search for a given version of libst, you can simply add - PKG_CHECK_MODULES(MYAPP, st >= 1.3 mumble >= 0.2.23) -to your configure.ac/in. This will define @MYAPP_LIBS@ and -@MYAPP_CFLAGS@ which you may then use in your Makefile.am/in files to -link against mumble and st. - - -LICENSE - -The State Threads library is a derivative of the Netscape Portable -Runtime library (NSPR). All source code in this directory is -distributed under the terms of the Mozilla Public License (MPL) version -1.1 or the GNU General Public License (GPL) version 2 or later. For -more information about these licenses please see -http://www.mozilla.org/MPL/ and http://www.gnu.org/copyleft/. - -All source code in the "examples" directory is distributed under the BSD -style license. - - -PLATFORMS - -Please see the "docs/notes.html" file for the list of currently -supported platforms. - - -DEBUGGER SUPPORT - -It's almost impossible to print SP and PC in a portable way. The only -way to see thread's stack platform-independently is to actually jump to -the saved context. That's what the _st_iterate_threads() function does. -Do the following to iterate over all threads: - -- set the _st_iterate_threads_flag to 1 in debugger -- set breakpoint at the _st_show_thread_stack() function - (which does nothing) -- call the _st_iterate_threads() function which jumps to the - next thread -- at each break you can explore thread's stack -- continue -- when iteration is complete, you return to the original - point (you can see thread id and a message as arguments of - the _st_show_thread_stack() function). - -You can call _st_iterate_threads() in three ways: - -- Insert it into your source code at the point you want to - go over threads. -- Just run application and this function will be called at - the first context switch. -- Call it directly from the debugger at any point. - -This works with gdb and dbx. - -Example using gdb: - -(gdb) set _st_iterate_threads_flag = 1 -(gdb) b _st_show_thread_stack -... -(gdb) call _st_iterate_threads() -... -(gdb) bt -... -(gdb) c -... -(gdb) bt -... -(gdb) c -... -and so on... - -_st_iterate_threads_flag will be set to 0 automatically -after iteration is over or you can set it to 0 at any time -to stop iteration. - -Sometimes gdb complains about SIGSEGV when you call a function -directly at gdb command-line. It can be ignored -- just call the -same function right away again, it works just fine. For example: - -(gdb) set _st_iterate_threads_flag = 1 -(gdb) b _st_show_thread_stack -Breakpoint 1 at 0x809bbbb: file sched.c, line 856. -(gdb) call _st_iterate_threads() -Program received signal SIGSEGV, Segmentation fault. -.... -(gdb) # just call the function again: -(gdb) call _st_iterate_threads() -Breakpoint 1, _st_show_thread_stack (thread=0x4017aee4, messg=0x80ae7a2 -"Iteration started") at sched.c:856 -856 } -.... - -You can use simple gdb command-line scripting to display -all threads and their stack traces at once: - -(gdb) while _st_iterate_threads_flag - >bt - >c - >end -.... - -Another script to stop at the thread with the specific thread id -(e.g., 0x40252ee4): - -(gdb) # set the flag again: -(gdb) set _st_iterate_threads_flag = 1 -(gdb) call _st_iterate_threads() -Breakpoint 1, _st_show_thread_stack (thread=0x4017aee4, messg=0x80ae7a2 -"Iteration started") at sched.c:856 -856 } -.... -(gdb) while thread != 0x40252ee4 - >c - >end -.... -.... -Breakpoint 1, _st_show_thread_stack (thread=0x40252ee4, messg=0x0) at -sched.c:856 -856 } -(gdb) bt -.... -(gdb) # don't want to continue iteration, unset the flag: -(gdb) set _st_iterate_threads_flag = 0 -(gdb) c -Continuing. -Breakpoint 1, _st_show_thread_stack (thread=0x0, messg=0x80ae78e "Iteration -completed") - at sched.c:856 -856 } -(gdb) c -Continuing. -(gdb) return -Make selected stack frame return now? (y or n) y -#0 0x4011254e in __select () - from /lib/libc.so.6 -(gdb) detach - - -CHANGE LOG - -Changes from 1.8 to 1.9. ------------------------- -o Support 32-bit and 64-bit Intel Macs. - -o Added ST_VERSION string, and ST_VERSION_MAJOR and ST_VERSION_MINOR - [bug 1796801]. - -o Fixed some compiler warnings, based on a patch from Brian Wellington - [bug 1932741]. - - -Changes from 1.7 to 1.8. --------------------------- -o Added support for kqueue and epoll on platforms that support them. - Added ability to choose the event notification system at program - startup. - -o Long-overdue public definitions of ST_UTIME_NO_TIMEOUT (-1ULL) and - ST_UTIME_NO_WAIT (0) [bug 1514436]. - -o Documentation patch for st_utime() [bug 1514484]. - -o Documentation patch for st_timecache_set() [bug 1514486]. - -o Documentation patch for st_netfd_serialize_accept() [bug 1514494]. - -o Added st_writev_resid() [rfe 1538344]. - -o Added st_readv_resid() [rfe 1538768] and, for symmetry, st_readv(). - - -Changes from 1.6 to 1.7. ------------------------- -o Support glibc 2.4, which breaks programs that manipulate jump buffers. - Replaced Linux IA64 special cases with new md.S that covers all - Linux. - - -Changes from 1.5.2 to 1.6. --------------------------- -none - - -Changes from 1.5.1 to 1.5.2. ----------------------------- -o Alfred Perlstein's context switch callback feature. - -o Claus Assmann's st_recvmsg/st_sendmsg wrappers. - -o Extra stack padding for platforms that need it. - -o Ron Arts's timeout clarifications in the reference manual. - -o Raymond Bero and Anton Berezin's AMD64 FreeBSD port. - -o Claus Assmann's AMD64 SunOS 5.10 port. - -o Claus Assmann's AMD64 OpenBSD port. - -o Michael Abd-El-Malek's Mac OS X port. - -o Michael Abd-El-Malek's stack printing patch. - - -Changes from 1.5.0 to 1.5.1. ----------------------------- -o Andreas Gustafsson's USE_POLL fix. - -o Gene's st_set_utime_function() enhancement. - - -Changes from 1.4 to 1.5.0. --------------------------- -o Andreas Gustafsson's performance patch. - -o New extensions: Improved DNS resolver, generic LRU cache, in-process - DNS cache, and a program to test the resolver and cache. - -o Support for AMD Opteron 64-bit CPUs under Linux. - -o Support for SPARC-64 under Solaris. - -o Andreas Gustafsson's support for VAX under NetBSD. - -o Changed unportable #warning directives in md.h to #error. - - -Changes from 1.3 to 1.4. ------------------------- -o Andreas Gustafsson's NetBSD port. - -o Wesley W. Terpstra's Darwin (MacOS X) port. - -o Support for many CPU architectures under Linux and *BSD. - -o Renamed private typedefs so they don't conflict with public ones any - more. - -o common.h now includes public.h for strict prototyping. - -o Joshua Levy's recommendation to make st_connect() and st_sendto() - accept const struct sockaddr pointers, as the originals do. - -o Clarified the documentation regarding blocking vs. non-blocking I/O. - -o Cygwin support. - -o Created the extensions directory. - -o Fixed warnings from ia64asm.S. - - -Changes from 1.2 to 1.3. ------------------------- -o Added st_read_resid() and st_write_resid() to allow the caller to know - how much data was transferred before an error occurred. Updated - documentation. - -o Updated project link, copyrights, and documentation regarding - timeouts. Added comment to st_connect(). - -o Optimized the _st_add_sleep_q() function in sched.c. Now we walk the - sleep queue *backward* when inserting a thread into it. When you - have lots (hundreds) of threads and several timeout values, it takes - a while to insert a thread at the appropriate point in the sleep - queue. The idea is that often this appropriate point is closer to - the end of the queue rather than the beginning. Measurements show - performance improves with this change. In any case this change - should do no harm. - -o Added a hint of when to define USE_POLL and when not to, to the - Makefile. - -o Added debugging support (files common.h and sched.c). See above. - -o Decreased the number of reallocations of _ST_POLLFDS in sched.c. - Inspired by Lev Walkin. - -o Fixed st_usleep(-1) and st_sleep(-1), and added a warning to the - documentation about too-large timeouts. - -o Linux/*BSD Alpha port. - -o Wesley W. Terpstra modernized the build process: - - properly build relocatable libraries under bsd and linux - - use library versioning - - added rpm spec file - - added debian/ files - See above for build instructions. - - -Changes from 1.1 to 1.2. ------------------------- -o Added st_randomize_stacks(). - -o Added a patch contributed by Sascha Schumann. - - -Changes from 1.0 to 1.1. ------------------------- -o Relicensed under dual MPL-GPL. - -o OpenBSD port. - -o Compile-time option to use poll() instead of select() for - event polling (see Makefile). - This is useful if you want to support a large number of open - file descriptors (larger than FD_SETSIZE) within a single - process. - -o Linux IA-64 port. - Two issues make IA-64 different from other platforms: - - - Besides the traditional call stack in memory, IA-64 uses the - general register stack. Thus each thread needs a backing store - for the register stack in addition to the memory stack. - - - Current implementation of setjmp()/longjmp() can not be used - for thread context-switching since it assumes that only one - register stack exists. Using special assembly functions for - context-switching is unavoidable. - -o Thread stack capping on IRIX. - This allows some profiling tools (such as SpeedShop) to know when - to stop unwinding the stack. Without this libexc, used by SpeedShop, - traces right off the stack and crashes. - -o Miscellaneous documentation additions. - - -COPYRIGHTS - -Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. -All Rights Reserved. diff --git a/trunk/3rdparty/st-srs/README.md b/trunk/3rdparty/st-srs/README.md deleted file mode 100644 index 551a02b7c..000000000 --- a/trunk/3rdparty/st-srs/README.md +++ /dev/null @@ -1,170 +0,0 @@ -# state-threads - -![](http://ossrs.net:8000/gif/v1/sls.gif?site=github.com&path=/srs/srsst) -[![](https://circleci.com/gh/ossrs/state-threads/tree/srs.svg?style=svg&circle-token=1ef1d5b5b0cde6c8c282ed856a18199f9e8f85a9)](https://circleci.com/gh/ossrs/state-threads/tree/srs) -[![](https://codecov.io/gh/ossrs/state-threads/branch/srs/graph/badge.svg)](https://codecov.io/gh/ossrs/state-threads/branch/srs) -[![](https://cloud.githubusercontent.com/assets/2777660/22814959/c51cbe72-ef92-11e6-81cc-32b657b285d5.png)](https://github.com/ossrs/srs/wiki/v1_CN_Contact#wechat) - -Fork from http://sourceforge.net/projects/state-threads, patched for [SRS](https://github.com/ossrs/srs/tree/2.0release). - -> See: https://github.com/ossrs/state-threads/blob/srs/README - -For original ST without any changes, checkout the [ST master branch](https://github.com/ossrs/state-threads/tree/master). - -## LICENSE - -[state-threads](https://github.com/ossrs/state-threads/blob/srs/README#L68) is licenced under [MPL or GPLv2](https://github.com/ossrs/srs/wiki/LicenseMixing#state-threads). - -## Usage - -Get code: - -```bash -git clone -b srs https://github.com/ossrs/state-threads.git -``` - -For Linux: - -```bash -make linux-debug -``` - -For OSX: - -```bash -make darwin-debug -``` - -For Cygwin(Windows): - -``` -make cygwin64-debug -``` - -For Linux aarch64, which fail with `Unknown CPU architecture`: - -```bash -make linux-debug EXTRA_CFLAGS="-D__aarch64__" -``` - -Linux with valgrind: - -```bash -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: - -```bash -make linux-debug EXTRA_CFLAGS="-DMD_HAVE_EPOLL -DMD_VALGRIND" -``` - -For OSX, user must specifies the valgrind header files: - -```bash -make darwin-debug EXTRA_CFLAGS="-DMD_HAVE_KQUEUE -DMD_VALGRIND -I/usr/local/include" -``` - -> Remark: Latest OSX does not support ST, please use docker to run ST. - -## Branch SRS - -The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patched the following patches: - -- [x] ARM: Patch [st.arm.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/1.st.arm.patch), for ARM. -- [x] OSX: Patch [st.osx.kqueue.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/3.st.osx.kqueue.patch), for osx. -- [x] Linux: Patch [st.disable.examples.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/4.st.disable.examples.patch), for ubuntu. -- [x] System: [Refine TAB of code](https://github.com/ossrs/state-threads/compare/c2001d30ca58f55d72a6cc6b9b6c70391eaf14db...d2101b26988b0e0db0aabc53ddf452068c1e2cbc). -- [x] ARM: Merge from [michaeltalyansky](https://github.com/michaeltalyansky/state-threads) and [xzh3836598](https://github.com/ossrs/state-threads/commit/9a17dec8f9c2814d93761665df7c5575a4d2d8a3), support [ARM](https://github.com/ossrs/state-threads/issues/1). -- [x] Valgrind: Merge from [toffaletti](https://github.com/toffaletti/state-threads), support [valgrind](https://github.com/ossrs/state-threads/issues/2) for ST. -- [x] OSX: Patch [st.osx10.14.build.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/6.st.osx10.14.build.patch), for osx 10.14 build. -- [x] ARM: Support macro `MD_ST_NO_ASM` to disable ASM, [#8](https://github.com/ossrs/state-threads/issues/8). -- [x] AARCH64: Merge patch [srs#1282](https://github.com/ossrs/srs/issues/1282#issuecomment-445539513) to support aarch64, [#9](https://github.com/ossrs/state-threads/issues/9). -- [x] OSX: Support OSX for Apple Darwin, macOS, [#11](https://github.com/ossrs/state-threads/issues/11). -- [x] System: Refine performance for sleep or epoll_wait(0), [#17](https://github.com/ossrs/state-threads/issues/17). -- [x] System: Support utest by gtest and coverage by gcov/gocvr. -- [x] System: Only support for Linux and Darwin. [#19](https://github.com/ossrs/state-threads/issues/19), [srs#2188](https://github.com/ossrs/srs/issues/2188). -- [x] System: Improve the performance of timer. [9fe8cfe5b](https://github.com/ossrs/state-threads/commit/9fe8cfe5b1c9741a2e671a46215184f267fba400), [7879c2b](https://github.com/ossrs/state-threads/commit/7879c2b), [387cddb](https://github.com/ossrs/state-threads/commit/387cddb) -- [x] Windows: Support Windows 64bits. [#20](https://github.com/ossrs/state-threads/issues/20). -- [x] MIPS: Support Linux/MIPS for OpenWRT, [#21](https://github.com/ossrs/state-threads/issues/21). -- [ ] System: Support Multiple Threads for Linux and Darwin. [#19](https://github.com/ossrs/state-threads/issues/19), [srs#2188](https://github.com/ossrs/srs/issues/2188). -- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12). - -## GDB Tools - -- [x] Support [nn_coroutines](https://github.com/ossrs/state-threads/issues/15#issuecomment-742218041), show number of coroutines. -- [x] Support [show_coroutines](https://github.com/ossrs/state-threads/issues/15#issuecomment-742218612), show all coroutines and caller function. - -## Valgrind - -How to debug with gdb under valgrind, read [valgrind manual](http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-simple). - -About startup parameters, read [valgrind cli](http://valgrind.org/docs/manual/mc-manual.html#mc-manual.options). - -Important cli options: - -1. `--undef-value-errors= [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. -1. `--leak-check= [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-kinds` and `--errors-for-leak-kinds`. -1. `--track-origins= [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. -1. `--show-reachable= , --show-possibly-lost=`, to show the using memory. - -## UTest and Coverage - -First of all, download [google test](https://github.com/google/googletest/releases/tag/release-1.6.0) to `utest/gtest`, check by: - -```bash -ls -lh utest/gtest/include/gtest/gtest.h >/dev/null && echo yes -``` - -To make ST with utest and run it: - -```bash -make linux-debug-gcov && ./obj/st_utest -``` - -> For macOS: `make darwin-debug-gcov && ./obj/st_utest` - -> Run utest without coverage: `make darwin-debug-utest && ./obj/st_utest` - -Then, install [gcovr](https://gcovr.com/en/stable/guide.html) for coverage: - -```bash -yum install -y python2-pip && -pip install lxml && pip install gcovr -``` - -> For macOS: `pip3 install gcovr` - -Finally, run test and get the report - -```bash -mkdir -p coverage && -gcovr -r . -e LINUX -e DARWIN -e examples --html --html-details -o coverage/st.html && -open coverage/st.html -``` - -> Note: We ignore `LINUX*` and `DARWIN*` which is `obj` actually. - -Or just run locally: - -```bash -bash auto/coverage.sh -``` - -## Docs & Analysis - -* 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 - -* [How to porting ST to other OS/CPU?](https://github.com/ossrs/state-threads/issues/22) -* About setjmp and longjmp, read [setjmp](https://ossrs.net/wiki/images/st-setjmp.jpg). -* About the stack structure, read [stack](https://ossrs.net/wiki/images/st-stack.jpg) -* About asm code comments, read [#91d530e](https://github.com/ossrs/state-threads/commit/91d530e#diff-ed9428b14ff6afda0e9ab04cc91d4445R25). -* About the scheduler, read [#13-scheduler](https://github.com/ossrs/state-threads/issues/13#issuecomment-616025527). -* About the IO event system, read [#13-IO](https://github.com/ossrs/state-threads/issues/13#issuecomment-616096568). -* Code analysis, please read [#15](https://github.com/ossrs/state-threads/issues/15). - -Winlin 2016 diff --git a/trunk/3rdparty/st-srs/auto/codecov.sh b/trunk/3rdparty/st-srs/auto/codecov.sh deleted file mode 100755 index 5f964b772..000000000 --- a/trunk/3rdparty/st-srs/auto/codecov.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -# In .circleci/config.yml, generate *.gcno with -# ./configure --gcov --without-research --without-librtmp && make -# and generate *.gcda by -# ./objs/srs_utest - -# Workdir is objs/cover. -workdir=`pwd`/codecov && rm -rf $workdir - -# Tool git is required to map the right path. -git --version >/dev/null 2>&1 -ret=$?; if [[ $ret -ne 0 ]]; then echo "Tool git is required, ret=$ret"; exit $ret; fi - -# Create trunk under workdir. -mkdir -p $workdir && cd $workdir -ret=$?; if [[ $ret -ne 0 ]]; then echo "Enter workdir failed, ret=$ret"; exit $ret; fi - -# Collect all *.gcno and *.gcda to objs/cover. -cd $workdir && for file in $(cd .. && ls *.c); do - cp ../$file $file && echo "Copy $file" && - if [[ -f ../obj/${file%.*}.gcno ]]; then - cp ../obj/${file%.*}.* . - fi -done -ret=$?; if [[ $ret -ne 0 ]]; then echo "Collect *.gcno and *.gcda failed, ret=$ret"; exit $ret; fi - -# Generate *.gcov for coverage. -cd $workdir && -for file in $(ls *.c); do - gcov $file -o `dirname $file` - ret=$?; if [[ $ret -ne 0 ]]; then echo "Collect $file failed, ret=$ret"; exit $ret; fi -done - -# Filter the gcov files, remove utest or gtest. -cd $workdir && -rm -f *gtest*.gcov *utest*.gcov -ret=$?; if [[ $ret -ne 0 ]]; then echo "Cook gcov files failed, ret=$ret"; exit $ret; fi - -# Upload report with *.gcov -# Remark: The file codecov.yml is not neccessary. It literally depends on git. -# Note: The right path is like: -# https://codecov.io/gh/ossrs/srs/src/3.0release/trunk/src/protocol/srs_rtmp_stack.cpp -# https://codecov.io/gh/ossrs/srs/src/20fbb4466fdc8ba5d810b8570df6004063212838/trunk/src/protocol/srs_rtmp_stack.cpp -# Remark: It takes a few minutes to sync with github, so it might not available when CircleCI is done. -# https://circleci.com/gh/ossrs/srs/tree/3.0release -# -# Note: Use '-X gcov' to avoid generate the gcov files again. -cd $workdir && -export CODECOV_TOKEN="0d616496-f781-4e7c-b285-d1f70a1cdf24" && -bash <(curl -s https://codecov.io/bash) -X gcov && -echo "Done" && exit 0 diff --git a/trunk/3rdparty/st-srs/auto/coverage.sh b/trunk/3rdparty/st-srs/auto/coverage.sh deleted file mode 100755 index 93f717c85..000000000 --- a/trunk/3rdparty/st-srs/auto/coverage.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - -if [[ ! -f utest/gtest/include/gtest/gtest.h ]]; then - ( - cd utest && rm -rf gtest && - curl https://github.com/google/googletest/archive/release-1.6.0.tar.gz -L -o googletest-release-1.6.0.tar.gz && - tar xf googletest-release-1.6.0.tar.gz && - ln -sf googletest-release-1.6.0 gtest && - echo "Setup gtest ok" - ) -fi -if [[ ! -f utest/gtest/include/gtest/gtest.h ]]; then - echo "No utest/gtest, please download from https://github.com/google/googletest/releases/tag/release-1.6.0" - exit -1 -else - echo "Check utest/gtest ok" -fi - -if [[ $(gcovr --version >/dev/null && echo yes) != yes ]]; then - echo "Please install gcovr: https://github.com/ossrs/state-threads/tree/srs#utest-and-coverage" - exit -1 -fi - -IS_LINUX=yes -uname -s|grep Darwin >/dev/null && IS_DARWIN=yes && IS_LINUX=no -echo "IS_LINUX: $IS_LINUX, IS_DARWIN: $IS_DARWIN" - -echo "Build and run utest" -if [[ $IS_DARWIN == yes ]]; then - make clean && make darwin-debug-gcov && ./obj/st_utest -else - make clean && make linux-debug-gcov && ./obj/st_utest -fi - -echo "Generating coverage" -mkdir -p coverage && -gcovr -r . -e LINUX -e DARWIN -e examples --html --html-details -o coverage/st.html && -echo "Coverage report at coverage/st.html" && -open coverage/st.html diff --git a/trunk/3rdparty/st-srs/auto/fast.sh b/trunk/3rdparty/st-srs/auto/fast.sh deleted file mode 100755 index 8ee39f77b..000000000 --- a/trunk/3rdparty/st-srs/auto/fast.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -PWD=$(cd `dirname $0`/.. && pwd) - -pushd $PWD -echo "Run UTest in $(pwd)" - -IS_LINUX=yes -uname -s|grep Darwin >/dev/null && IS_DARWIN=yes && IS_LINUX=no -echo "IS_LINUX: $IS_LINUX, IS_DARWIN: $IS_DARWIN" - -echo "Clean gcda files" -rm -f ./obj/*.gcda - -echo "Build and run utest" -if [[ $IS_DARWIN == yes ]]; then - make darwin-debug-gcov && ./obj/st_utest -else - make linux-debug-gcov && ./obj/st_utest -fi -ret=$?; if [[ 0 -ne $ret ]]; then echo "Make ST utest fail, ret=$ret"; exit $ret; fi - -echo "Generating coverage" -mkdir -p coverage && -gcovr -r . -e LINUX -e DARWIN -e examples --html --html-details -o coverage/st.html && -echo "Coverage report at coverage/st.html" && -open coverage/st.html - -popd -echo "UTest done, restore $(pwd)" - -cat << END > /dev/stdout - - # CLI For DARWIN - cd $PWD && rm -f ./obj/*.gcda && - make darwin-debug-gcov && ./obj/st_utest && - mkdir -p coverage && gcovr -r . -e LINUX -e DARWIN -e examples --html --html-details -o coverage/st.html && - open coverage/st.html - -END \ No newline at end of file diff --git a/trunk/3rdparty/st-srs/common.h b/trunk/3rdparty/st-srs/common.h deleted file mode 100644 index 7eef498f7..000000000 --- a/trunk/3rdparty/st-srs/common.h +++ /dev/null @@ -1,475 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#ifndef __ST_COMMON_H__ -#define __ST_COMMON_H__ - -#include -#include -#include -#include -#include - -/* Enable assertions only if DEBUG is defined */ -#ifndef DEBUG - #define NDEBUG -#endif -#include -#define ST_ASSERT(expr) assert(expr) - -#define ST_BEGIN_MACRO { -#define ST_END_MACRO } - -#ifdef DEBUG - #define ST_HIDDEN /*nothing*/ -#else - #define ST_HIDDEN static -#endif - -#include "public.h" -#include "md.h" - -/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef MD_VALGRIND - #ifndef NVALGRIND - #define NVALGRIND - #endif -#else - #undef NVALGRIND -#endif - - -/***************************************** - * Circular linked list definitions - */ - -typedef struct _st_clist { - struct _st_clist *next; - struct _st_clist *prev; -} _st_clist_t; - -/* Insert element "_e" into the list, before "_l" */ -#define ST_INSERT_BEFORE(_e,_l) \ - ST_BEGIN_MACRO \ - (_e)->next = (_l); \ - (_e)->prev = (_l)->prev; \ - (_l)->prev->next = (_e); \ - (_l)->prev = (_e); \ - ST_END_MACRO - -/* Insert element "_e" into the list, after "_l" */ -#define ST_INSERT_AFTER(_e,_l) \ - ST_BEGIN_MACRO \ - (_e)->next = (_l)->next; \ - (_e)->prev = (_l); \ - (_l)->next->prev = (_e); \ - (_l)->next = (_e); \ - ST_END_MACRO - -/* Return the element following element "_e" */ -#define ST_NEXT_LINK(_e) ((_e)->next) - -/* Append an element "_e" to the end of the list "_l" */ -#define ST_APPEND_LINK(_e,_l) ST_INSERT_BEFORE(_e,_l) - -/* Insert an element "_e" at the head of the list "_l" */ -#define ST_INSERT_LINK(_e,_l) ST_INSERT_AFTER(_e,_l) - -/* Return the head/tail of the list */ -#define ST_LIST_HEAD(_l) (_l)->next -#define ST_LIST_TAIL(_l) (_l)->prev - -/* Remove the element "_e" from it's circular list */ -#define ST_REMOVE_LINK(_e) \ - ST_BEGIN_MACRO \ - (_e)->prev->next = (_e)->next; \ - (_e)->next->prev = (_e)->prev; \ - ST_END_MACRO - -/* Return non-zero if the given circular list "_l" is empty, */ -/* zero if the circular list is not empty */ -#define ST_CLIST_IS_EMPTY(_l) \ - ((_l)->next == (_l)) - -/* Initialize a circular list */ -#define ST_INIT_CLIST(_l) \ - ST_BEGIN_MACRO \ - (_l)->next = (_l); \ - (_l)->prev = (_l); \ - ST_END_MACRO - -#define ST_INIT_STATIC_CLIST(_l) \ - {(_l), (_l)} - - -/***************************************** - * Basic types definitions - */ - -typedef void (*_st_destructor_t)(void *); - - -typedef struct _st_stack { - _st_clist_t links; - char *vaddr; /* Base of stack's allocated memory */ - int vaddr_size; /* Size of stack's allocated memory */ - int stk_size; /* Size of usable portion of the stack */ - char *stk_bottom; /* Lowest address of stack's usable portion */ - char *stk_top; /* Highest address of stack's usable portion */ - void *sp; /* Stack pointer from C's point of view */ - /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND - /* id returned by VALGRIND_STACK_REGISTER */ - /* http://valgrind.org/docs/manual/manual-core-adv.html */ - unsigned long valgrind_stack_id; -#endif -} _st_stack_t; - - -typedef struct _st_cond { - _st_clist_t wait_q; /* Condition variable wait queue */ -} _st_cond_t; - - -typedef struct _st_thread _st_thread_t; - -struct _st_thread { - int state; /* Thread's state */ - int flags; /* Thread's flags */ - - void *(*start)(void *arg); /* The start function of the thread */ - void *arg; /* Argument of the start function */ - void *retval; /* Return value of the start function */ - - _st_stack_t *stack; /* Info about thread's stack */ - - _st_clist_t links; /* For putting on run/sleep/zombie queue */ - _st_clist_t wait_links; /* For putting on mutex/condvar wait queue */ -#ifdef DEBUG - _st_clist_t tlink; /* For putting on thread queue */ -#endif - - st_utime_t due; /* Wakeup time when thread is sleeping */ - _st_thread_t *left; /* For putting in timeout heap */ - _st_thread_t *right; /* -- see docs/timeout_heap.txt for details */ - int heap_index; - - void **private_data; /* Per thread private data */ - - _st_cond_t *term; /* Termination condition variable for join */ - - jmp_buf context; /* Thread's context */ -}; - - -typedef struct _st_mutex { - _st_thread_t *owner; /* Current mutex owner */ - _st_clist_t wait_q; /* Mutex wait queue */ -} _st_mutex_t; - - -typedef struct _st_pollq { - _st_clist_t links; /* For putting on io queue */ - _st_thread_t *thread; /* Polling thread */ - struct pollfd *pds; /* Array of poll descriptors */ - int npds; /* Length of the array */ - int on_ioq; /* Is it on ioq? */ -} _st_pollq_t; - - -typedef struct _st_eventsys_ops { - const char *name; /* Name of this event system */ - int val; /* Type of this event system */ - int (*init)(void); /* Initialization */ - void (*dispatch)(void); /* Dispatch function */ - int (*pollset_add)(struct pollfd *, int); /* Add descriptor set */ - void (*pollset_del)(struct pollfd *, int); /* Delete descriptor set */ - int (*fd_new)(int); /* New descriptor allocated */ - int (*fd_close)(int); /* Descriptor closed */ - int (*fd_getlimit)(void); /* Descriptor hard limit */ -} _st_eventsys_t; - - -typedef struct _st_vp { - _st_thread_t *idle_thread; /* Idle thread for this vp */ - st_utime_t last_clock; /* The last time we went into vp_check_clock() */ - - _st_clist_t run_q; /* run queue for this vp */ - _st_clist_t io_q; /* io queue for this vp */ - _st_clist_t zombie_q; /* zombie queue for this vp */ -#ifdef DEBUG - _st_clist_t thread_q; /* all threads of this vp */ -#endif - int pagesize; - - _st_thread_t *sleep_q; /* sleep queue for this vp */ - int sleepq_size; /* number of threads on sleep queue */ - -#ifdef ST_SWITCH_CB - st_switch_cb_t switch_out_cb; /* called when a thread is switched out */ - st_switch_cb_t switch_in_cb; /* called when a thread is switched in */ -#endif -} _st_vp_t; - - -typedef struct _st_netfd { - int osfd; /* Underlying OS file descriptor */ - int inuse; /* In-use flag */ - void *private_data; /* Per descriptor private data */ - _st_destructor_t destructor; /* Private data destructor function */ - void *aux_data; /* Auxiliary data for internal use */ - struct _st_netfd *next; /* For putting on the free list */ -} _st_netfd_t; - - -/***************************************** - * Current vp, thread, and event system - */ - -extern _st_vp_t _st_this_vp; -extern _st_thread_t *_st_this_thread; -extern _st_eventsys_t *_st_eventsys; - -#define _ST_CURRENT_THREAD() (_st_this_thread) -#define _ST_SET_CURRENT_THREAD(_thread) (_st_this_thread = (_thread)) - -#define _ST_LAST_CLOCK (_st_this_vp.last_clock) - -#define _ST_RUNQ (_st_this_vp.run_q) -#define _ST_IOQ (_st_this_vp.io_q) -#define _ST_ZOMBIEQ (_st_this_vp.zombie_q) -#ifdef DEBUG - #define _ST_THREADQ (_st_this_vp.thread_q) -#endif - -#define _ST_PAGE_SIZE (_st_this_vp.pagesize) - -#define _ST_SLEEPQ (_st_this_vp.sleep_q) -#define _ST_SLEEPQ_SIZE (_st_this_vp.sleepq_size) - -#define _ST_VP_IDLE() (*_st_eventsys->dispatch)() - - -/***************************************** - * vp queues operations - */ - -#define _ST_ADD_IOQ(_pq) ST_APPEND_LINK(&_pq.links, &_ST_IOQ) -#define _ST_DEL_IOQ(_pq) ST_REMOVE_LINK(&_pq.links) - -#define _ST_ADD_RUNQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_RUNQ) -#define _ST_INSERT_RUNQ(_thr) ST_INSERT_LINK(&(_thr)->links, &_ST_RUNQ) -#define _ST_DEL_RUNQ(_thr) ST_REMOVE_LINK(&(_thr)->links) - -#define _ST_ADD_SLEEPQ(_thr, _timeout) _st_add_sleep_q(_thr, _timeout) -#define _ST_DEL_SLEEPQ(_thr) _st_del_sleep_q(_thr) - -#define _ST_ADD_ZOMBIEQ(_thr) ST_APPEND_LINK(&(_thr)->links, &_ST_ZOMBIEQ) -#define _ST_DEL_ZOMBIEQ(_thr) ST_REMOVE_LINK(&(_thr)->links) - -#ifdef DEBUG - #define _ST_ADD_THREADQ(_thr) ST_APPEND_LINK(&(_thr)->tlink, &_ST_THREADQ) - #define _ST_DEL_THREADQ(_thr) ST_REMOVE_LINK(&(_thr)->tlink) -#endif - - -/***************************************** - * Thread states and flags - */ - -#define _ST_ST_RUNNING 0 -#define _ST_ST_RUNNABLE 1 -#define _ST_ST_IO_WAIT 2 -#define _ST_ST_LOCK_WAIT 3 -#define _ST_ST_COND_WAIT 4 -#define _ST_ST_SLEEPING 5 -#define _ST_ST_ZOMBIE 6 -#define _ST_ST_SUSPENDED 7 - -#define _ST_FL_PRIMORDIAL 0x01 -#define _ST_FL_IDLE_THREAD 0x02 -#define _ST_FL_ON_SLEEPQ 0x04 -#define _ST_FL_INTERRUPT 0x08 -#define _ST_FL_TIMEDOUT 0x10 - - -/***************************************** - * Pointer conversion - */ - -#ifndef offsetof - #define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier)) -#endif - -#define _ST_THREAD_PTR(_qp) \ - ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, links))) - -#define _ST_THREAD_WAITQ_PTR(_qp) \ - ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, wait_links))) - -#define _ST_THREAD_STACK_PTR(_qp) \ - ((_st_stack_t *)((char*)(_qp) - offsetof(_st_stack_t, links))) - -#define _ST_POLLQUEUE_PTR(_qp) \ - ((_st_pollq_t *)((char *)(_qp) - offsetof(_st_pollq_t, links))) - -#ifdef DEBUG - #define _ST_THREAD_THREADQ_PTR(_qp) \ - ((_st_thread_t *)((char *)(_qp) - offsetof(_st_thread_t, tlink))) -#endif - - -/***************************************** - * Constants - */ - -#ifndef ST_UTIME_NO_TIMEOUT - #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) -#endif - -#define ST_DEFAULT_STACK_SIZE (128*1024) /* Includes register stack size */ - -#ifndef ST_KEYS_MAX - #define ST_KEYS_MAX 16 -#endif - -#ifndef ST_MIN_POLLFDS_SIZE - #define ST_MIN_POLLFDS_SIZE 64 -#endif - - -/***************************************** - * Threads context switching - */ - -#ifdef DEBUG - void _st_iterate_threads(void); - #define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads() -#else - #define ST_DEBUG_ITERATE_THREADS() -#endif - -#ifdef ST_SWITCH_CB - #define ST_SWITCH_OUT_CB(_thread) \ - if (_st_this_vp.switch_out_cb != NULL && \ - _thread != _st_this_vp.idle_thread && \ - _thread->state != _ST_ST_ZOMBIE) { \ - _st_this_vp.switch_out_cb(); \ - } - #define ST_SWITCH_IN_CB(_thread) \ - if (_st_this_vp.switch_in_cb != NULL && \ - _thread != _st_this_vp.idle_thread && \ - _thread->state != _ST_ST_ZOMBIE) { \ - _st_this_vp.switch_in_cb(); \ - } -#else - #define ST_SWITCH_OUT_CB(_thread) - #define ST_SWITCH_IN_CB(_thread) -#endif - -/* - * Switch away from the current thread context by saving its state and - * calling the thread scheduler - */ -#define _ST_SWITCH_CONTEXT(_thread) \ - ST_BEGIN_MACRO \ - ST_SWITCH_OUT_CB(_thread); \ - if (!MD_SETJMP((_thread)->context)) { \ - _st_vp_schedule(); \ - } \ - ST_DEBUG_ITERATE_THREADS(); \ - ST_SWITCH_IN_CB(_thread); \ - ST_END_MACRO - -/* - * Restore a thread context that was saved by _ST_SWITCH_CONTEXT or - * initialized by _ST_INIT_CONTEXT - */ -#define _ST_RESTORE_CONTEXT(_thread) \ - ST_BEGIN_MACRO \ - _ST_SET_CURRENT_THREAD(_thread); \ - 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" - */ -#define _ST_STACK_PAD_SIZE MD_STACK_PAD_SIZE - - -/***************************************** - * Forward declarations - */ - -void _st_vp_schedule(void); -void _st_vp_check_clock(void); -void *_st_idle_thread_start(void *arg); -void _st_thread_main(void); -void _st_thread_cleanup(_st_thread_t *thread); -void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout); -void _st_del_sleep_q(_st_thread_t *thread); -_st_stack_t *_st_stack_new(int stack_size); -void _st_stack_free(_st_stack_t *ts); -int _st_io_init(void); - -st_utime_t st_utime(void); -_st_cond_t *st_cond_new(void); -int st_cond_destroy(_st_cond_t *cvar); -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout); -int st_cond_signal(_st_cond_t *cvar); -ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout); -ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout); -int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size); - -#endif /* !__ST_COMMON_H__ */ - diff --git a/trunk/3rdparty/st-srs/docs/fig.gif b/trunk/3rdparty/st-srs/docs/fig.gif deleted file mode 100644 index 7265a05db..000000000 Binary files a/trunk/3rdparty/st-srs/docs/fig.gif and /dev/null differ diff --git a/trunk/3rdparty/st-srs/docs/notes.html b/trunk/3rdparty/st-srs/docs/notes.html deleted file mode 100644 index 5a24369e2..000000000 --- a/trunk/3rdparty/st-srs/docs/notes.html +++ /dev/null @@ -1,434 +0,0 @@ - - -State Threads Library Programming Notes - - -

Programming Notes

-

- -

- -

-


-

- -

Porting

-The State Threads library uses OS concepts that are available in some -form on most UNIX platforms, making the library very portable across -many flavors of UNIX. However, there are several parts of the library -that rely on platform-specific features. Here is the list of such parts: -

-

    -
  • Thread context initialization: Two ingredients of the -jmp_buf -data structure (the program counter and the stack pointer) have to be -manually set in the thread creation routine. The jmp_buf data -structure is defined in the setjmp.h header file and differs from -platform to platform. Usually the program counter is a structure member -with PC in the name and the stack pointer is a structure member -with SP in the name. One can also look in the -Netscape's NSPR library source -which already has this code for many UNIX-like platforms -(mozilla/nsprpub/pr/include/md/*.h files). -

    -Note that on some BSD-derived platforms _setjmp(3)/_longjmp(3) -calls should be used instead of setjmp(3)/longjmp(3) (that is -the calls that manipulate only the stack and registers and do not -save and restore the process's signal mask).

  • -

    -Starting with glibc 2.4 on Linux the opacity of the jmp_buf data -structure is enforced by setjmp(3)/longjmp(3) so the -jmp_buf ingredients cannot be accessed directly anymore (unless -special environmental variable LD_POINTER_GUARD is set before application -execution). To avoid dependency on custom environment, the State Threads -library provides setjmp/longjmp replacement functions for -all Intel CPU architectures. Other CPU architectures can also be easily -supported (the setjmp/longjmp source code is widely available for -many CPU architectures). -

    -

  • High resolution time function: Some platforms (IRIX, Solaris) -provide a high resolution time function based on the free running hardware -counter. This function returns the time counted since some arbitrary -moment in the past (usually machine power up time). It is not correlated in -any way to the time of day, and thus is not subject to resetting, -drifting, etc. This type of time is ideal for tasks where cheap, accurate -interval timing is required. If such a function is not available on a -particular platform, the gettimeofday(3) function can be used -(though on some platforms it involves a system call). -

    -

  • The stack growth direction: The library needs to know whether the -stack grows toward lower (down) or higher (up) memory addresses. -One can write a simple test program that detects the stack growth direction -on a particular platform.
  • -

    -

  • Non-blocking attribute inheritance: On some platforms (e.g. IRIX) -the socket created as a result of the accept(2) call inherits the -non-blocking attribute of the listening socket. One needs to consult the manual -pages or write a simple test program to see if this applies to a specific -platform.
  • -

    -

  • Anonymous memory mapping: The library allocates memory segments -for thread stacks by doing anonymous memory mapping (mmap(2)). This -mapping is somewhat different on SVR4 and BSD4.3 derived platforms. -

    -The memory mapping can be avoided altogether by using malloc(3) for -stack allocation. In this case the MALLOC_STACK macro should be -defined.

  • -
-

-All machine-dependent feature test macros should be defined in the -md.h header file. The assembly code for setjmp/longjmp -replacement functions for all CPU architectures should be placed in -the md.S file. -

-The current version of the library is ported to: -

    -
  • IRIX 6.x (both 32 and 64 bit)
  • -
  • Linux (kernel 2.x and glibc 2.x) on x86, Alpha, MIPS and MIPSEL, - SPARC, ARM, PowerPC, 68k, HPPA, S390, IA-64, and Opteron (AMD-64)
  • -
  • Solaris 2.x (SunOS 5.x) on x86, AMD64, SPARC, and SPARC-64
  • -
  • AIX 4.x
  • -
  • HP-UX 11 (both 32 and 64 bit)
  • -
  • Tru64/OSF1
  • -
  • FreeBSD on x86, AMD64, and Alpha
  • -
  • OpenBSD on x86, AMD64, Alpha, and SPARC
  • -
  • NetBSD on x86, Alpha, SPARC, and VAX
  • -
  • MacOS X (Darwin) on PowerPC (32 bit) and Intel (both 32 and 64 bit) [universal]
  • -
  • Cygwin
  • -
-

- - -

Signals

-Signal handling in an application using State Threads should be treated the -same way as in a classical UNIX process application. There is no such -thing as per-thread signal mask, all threads share the same signal handlers, -and only asynchronous-safe functions can be used in signal handlers. -However, there is a way to process signals synchronously by converting a -signal event to an I/O event: a signal catching function does a write to -a pipe which will be processed synchronously by a dedicated signal handling -thread. The following code demonstrates this technique (error handling is -omitted for clarity): -
-
-/* Per-process pipe which is used as a signal queue. */
-/* Up to PIPE_BUF/sizeof(int) signals can be queued up. */
-int sig_pipe[2];
-
-/* Signal catching function. */
-/* Converts signal event to I/O event. */
-void sig_catcher(int signo)
-{
-  int err;
-
-  /* Save errno to restore it after the write() */
-  err = errno;
-  /* write() is reentrant/async-safe */
-  write(sig_pipe[1], &signo, sizeof(int));
-  errno = err;
-}
-
-/* Signal processing function. */
-/* This is the "main" function of the signal processing thread. */
-void *sig_process(void *arg)
-{
-  st_netfd_t nfd;
-  int signo;
-
-  nfd = st_netfd_open(sig_pipe[0]);
-
-  for ( ; ; ) {
-    /* Read the next signal from the pipe */
-    st_read(nfd, &signo, sizeof(int), ST_UTIME_NO_TIMEOUT);
-
-    /* Process signal synchronously */
-    switch (signo) {
-    case SIGHUP:
-      /* do something here - reread config files, etc. */
-      break;
-    case SIGTERM:
-      /* do something here - cleanup, etc. */
-      break;
-      /*      .
-              .
-         Other signals
-              .
-              .
-      */
-    }
-  }
-
-  return NULL;
-}
-
-int main(int argc, char *argv[])
-{
-  struct sigaction sa;
-        .
-        .
-        .
-
-  /* Create signal pipe */
-  pipe(sig_pipe);
-
-  /* Create signal processing thread */
-  st_thread_create(sig_process, NULL, 0, 0);
-
-  /* Install sig_catcher() as a signal handler */
-  sa.sa_handler = sig_catcher;
-  sigemptyset(&sa.sa_mask);
-  sa.sa_flags = 0;
-  sigaction(SIGHUP, &sa, NULL);
-
-  sa.sa_handler = sig_catcher;
-  sigemptyset(&sa.sa_mask);
-  sa.sa_flags = 0;
-  sigaction(SIGTERM, &sa, NULL);
-
-        .
-        .
-        .
-      
-}
-
-
-

-Note that if multiple processes are used (see below), the signal pipe should -be initialized after the fork(2) call so that each process has its -own private pipe. -

- - -

Intra-Process Synchronization

-Due to the event-driven nature of the library scheduler, the thread context -switch (process state change) can only happen in a well-known set of -library functions. This set includes functions in which a thread may -"block": I/O functions (st_read(), st_write(), etc.), -sleep functions (st_sleep(), etc.), and thread synchronization -functions (st_thread_join(), st_cond_wait(), etc.). As a result, -process-specific global data need not to be protected by locks since a thread -cannot be rescheduled while in a critical section (and only one thread at a -time can access the same memory location). By the same token, -non thread-safe functions (in a traditional sense) can be safely used with -the State Threads. The library's mutex facilities are practically useless -for a correctly written application (no blocking functions in critical -section) and are provided mostly for completeness. This absence of locking -greatly simplifies an application design and provides a foundation for -scalability. -

- - -

Inter-Process Synchronization

-The State Threads library makes it possible to multiplex a large number -of simultaneous connections onto a much smaller number of separate -processes, where each process uses a many-to-one user-level threading -implementation (N of M:1 mappings rather than one M:N -mapping used in native threading libraries on some platforms). This design -is key to the application's scalability. One can think about it as if a -set of all threads is partitioned into separate groups (processes) where -each group has a separate pool of resources (virtual address space, file -descriptors, etc.). An application designer has full control of how many -groups (processes) an application creates and what resources, if any, -are shared among different groups via standard UNIX inter-process -communication (IPC) facilities.

-There are several reasons for creating multiple processes: -

-

    -
  • To take advantage of multiple hardware entities (CPUs, disks, etc.) -available in the system (hardware parallelism).
  • -

    -

  • To reduce risk of losing a large number of user connections when one of -the processes crashes. For example, if C user connections (threads) -are multiplexed onto P processes and one of the processes crashes, -only a fraction (C/P) of all connections will be lost.
  • -

    -

  • To overcome per-process resource limitations imposed by the OS. For -example, if select(2) is used for event polling, the number of -simultaneous connections (threads) per process is -limited by the FD_SETSIZE parameter (see select(2)). -If FD_SETSIZE is equal to 1024 and each connection needs one file -descriptor, then an application should create 10 processes to support 10,000 -simultaneous connections.
  • -
-

-Ideally all user sessions are completely independent, so there is no need for -inter-process communication. It is always better to have several separate -smaller process-specific resources (e.g., data caches) than to have one large -resource shared (and modified) by all processes. Sometimes, however, there -is a need to share a common resource among different processes. In that case, -standard UNIX IPC facilities can be used. In addition to that, there is a way -to synchronize different processes so that only the thread accessing the -shared resource will be suspended (but not the entire process) if that resource -is unavailable. In the following code fragment a pipe is used as a counting -semaphore for inter-process synchronization: -

-#ifndef PIPE_BUF
-#define PIPE_BUF 512  /* POSIX */
-#endif
-
-/* Semaphore data structure */
-typedef struct ipc_sem {
-  st_netfd_t rdfd;  /* read descriptor */
-  st_netfd_t wrfd;  /* write descriptor */
-} ipc_sem_t;
-
-/* Create and initialize the semaphore. Should be called before fork(2). */
-/* 'value' must be less than PIPE_BUF. */
-/* If 'value' is 1, the semaphore works as mutex. */
-ipc_sem_t *ipc_sem_create(int value)
-{
-  ipc_sem_t *sem;
-  int p[2];
-  char b[PIPE_BUF];
-
-  /* Error checking is omitted for clarity */
-  sem = malloc(sizeof(ipc_sem_t));
-
-  /* Create the pipe */
-  pipe(p);
-  sem->rdfd = st_netfd_open(p[0]);
-  sem->wrfd = st_netfd_open(p[1]);
-
-  /* Initialize the semaphore: put 'value' bytes into the pipe */
-  write(p[1], b, value);
-
-  return sem;
-}
-
-/* Try to decrement the "value" of the semaphore. */
-/* If "value" is 0, the calling thread blocks on the semaphore. */
-int ipc_sem_wait(ipc_sem_t *sem)
-{
-  char c;
-
-  /* Read one byte from the pipe */
-  if (st_read(sem->rdfd, &c, 1, ST_UTIME_NO_TIMEOUT) != 1)
-    return -1;
-
-  return 0;
-}
-
-/* Increment the "value" of the semaphore. */
-int ipc_sem_post(ipc_sem_t *sem)
-{
-  char c;
-
-  if (st_write(sem->wrfd, &c, 1, ST_UTIME_NO_TIMEOUT) != 1)
-    return -1;
-
-  return 0;
-}
-
-
-

- -Generally, the following steps should be followed when writing an application -using the State Threads library: -

-

    -
  1. Initialize the library (st_init()).
  2. -

    -

  3. Create resources that will be shared among different processes: - create and bind listening sockets, create shared memory segments, IPC - channels, synchronization primitives, etc.
  4. -

    -

  5. Create several processes (fork(2)). The parent process should - either exit or become a "watchdog" (e.g., it starts a new process when - an existing one crashes, does a cleanup upon application termination, - etc.).
  6. -

    -

  7. In each child process create a pool of threads - (st_thread_create()) to handle user connections.
  8. -
-

- - -

Non-Network I/O

- -The State Threads architecture uses non-blocking I/O on -st_netfd_t objects for concurrent processing of multiple user -connections. This architecture has a drawback: the entire process and -all its threads may block for the duration of a disk or other -non-network I/O operation, whether through State Threads I/O functions, -direct system calls, or standard I/O functions. (This is applicable -mostly to disk reads; disk writes are usually performed -asynchronously -- data goes to the buffer cache to be written to disk -later.) Fortunately, disk I/O (unlike network I/O) usually takes a -finite and predictable amount of time, but this may not be true for -special devices or user input devices (including stdin). Nevertheless, -such I/O reduces throughput of the system and increases response times. -There are several ways to design an application to overcome this -drawback: - -

-

-

- - -

Timeouts

- -The timeout parameter to st_cond_timedwait() and the -I/O functions, and the arguments to st_sleep() and -st_usleep() specify a maximum time to wait since the last -context switch not since the beginning of the function call. - -

The State Threads' time resolution is actually the time interval -between context switches. That time interval may be large in some -situations, for example, when a single thread does a lot of work -continuously. Note that a steady, uninterrupted stream of network I/O -qualifies for this description; a context switch occurs only when a -thread blocks. - -

If a specified I/O timeout is less than the time interval between -context switches the function may return with a timeout error before -that amount of time has elapsed since the beginning of the function -call. For example, if eight milliseconds have passed since the last -context switch and an I/O function with a timeout of 10 milliseconds -blocks, causing a switch, the call may return with a timeout error as -little as two milliseconds after it was called. (On Linux, -select()'s timeout is an upper bound on the amount of -time elapsed before select returns.) Similarly, if 12 ms have passed -already, the function may return immediately. - -

In almost all cases I/O timeouts should be used only for detecting a -broken network connection or for preventing a peer from holding an idle -connection for too long. Therefore for most applications realistic I/O -timeouts should be on the order of seconds. Furthermore, there's -probably no point in retrying operations that time out. Rather than -retrying simply use a larger timeout in the first place. - -

The largest valid timeout value is platform-dependent and may be -significantly less than INT_MAX seconds for select() -or INT_MAX milliseconds for poll(). Generally, you -should not use timeouts exceeding several hours. Use -ST_UTIME_NO_TIMEOUT (-1) as a special value to -indicate infinite timeout or indefinite sleep. Use -ST_UTIME_NO_WAIT (0) to indicate no waiting at all. - -

-


-

- - - diff --git a/trunk/3rdparty/st-srs/docs/reference.html b/trunk/3rdparty/st-srs/docs/reference.html deleted file mode 100644 index 3c9c7bd78..000000000 --- a/trunk/3rdparty/st-srs/docs/reference.html +++ /dev/null @@ -1,3120 +0,0 @@ - - -State Threads Library Reference - - - -

State Threads Library Reference

- -
-
Types
-
st_thread_t
-
st_cond_t
-
st_mutex_t
-
st_utime_t
-
st_netfd_t
-
st_switch_cb_t
-

-

Error Handling
-

-

Library Initialization
-

-

st_init()
-
st_getfdlimit()
-
st_set_eventsys()
-
st_get_eventsys()
-
st_get_eventsys_name()
-
st_set_utime_function()
-
st_timecache_set()
-
st_randomize_stacks()
-

-

st_switch_cb_t type
-
st_set_switch_in_cb()
-
st_set_switch_out_cb()
-

-

Thread Control and Identification
-

-

st_thread_t type
-
st_thread_create()
-
st_thread_exit()
-
st_thread_join()
-
st_thread_self()
-
st_thread_interrupt()
-
st_sleep()
-
st_usleep()
-
st_randomize_stacks()
-

-

Per-Thread Private Data
-

-

st_key_create()
-
st_key_getlimit()
-
st_thread_setspecific()
-
st_thread_getspecific()
-

-

Synchronization
-

-

st_cond_t type
-
st_cond_new()
-
st_cond_destroy()
-
st_cond_wait()
-
st_cond_timedwait()
-
st_cond_signal()
-
st_cond_broadcast()
-

-

st_mutex_t type
-
st_mutex_new()
-
st_mutex_destroy()
-
st_mutex_lock()
-
st_mutex_trylock()
-
st_mutex_unlock()
-

-

Timing
-

-

st_utime_t type
-
st_utime()
-
st_set_utime_function()
-
st_timecache_set()
-
st_time()
-

-

I/O Functions
-

-

st_netfd_t type
-
st_netfd_open()
-
st_netfd_open_socket()
-
st_netfd_free()
-
st_netfd_close()
-
st_netfd_fileno()
-
st_netfd_setspecific()
-
st_netfd_getspecific()
-
st_netfd_serialize_accept()
-
-
st_netfd_poll()
-

-

st_accept()
-
st_connect()
-
st_read()
-
st_read_fully()
-
st_read_resid()
-
st_readv()
-
st_readv_resid()
-
st_write()
-
st_write_resid()
-
st_writev()
-
st_writev_resid()
-
st_recvfrom()
-
st_sendto()
-
st_recvmsg()
-
st_sendmsg()
-

-

st_open()
-
st_poll()
-

-

Program Structure
-

-

List of Blocking Functions
-

-

-

-


-

- - - -

Types

- -The State Thread library defines the following types in the st.h -header file: -

-

-
st_thread_t
-
st_cond_t
-
st_mutex_t
-
st_utime_t
-
st_netfd_t
-
-

-


-

- - -

st_thread_t

- -Thread type. -

-

Syntax
- -
-#include <st.h>
-
-typedef void *  st_thread_t;
-
-

-

Description
- -A thread is represented and identified by a pointer to an opaque data -structure. This pointer is a required parameter for most of the functions -that operate on threads. -

-The thread identifier remains valid until the thread returns from its root -function and, if the thread was created joinable, is joined. -

-


-

- - -

st_cond_t

- -Condition variable type. -

-

Syntax
- -
-#include <st.h>
-
-typedef void *  st_cond_t;
-
-

-

Description
- -A condition variable is an opaque object identified by a pointer. -Condition variables provide synchronization primitives to wait for or wake -up threads waiting for certain conditions to be satisfied. -

-In the State Threads library there is no need to lock a mutex before -waiting on a condition variable. -

-


-

- - -

st_mutex_t

- -Mutex type. -

-

Syntax
- -
-#include <st.h>
-
-typedef void *  st_mutex_t;
-
-

-

Description
- -A mutex is an opaque object identified by a pointer. -Mutual exclusion locks (mutexes) are used to serialize the execution of -threads through critical sections of code. -

-If application using the State Threads library is written with no -I/O or control yielding in critical sections (that is no -blocking functions in critical sections), then there is -no need for mutexes.

-These mutexes can only be used for intra-process thread synchronization. -They cannot be used for inter-process synchronization. -

-


-

- - -

st_utime_t

- -High resolution time type ("u" stands for "micro"). -

-

Syntax
- -
-#include <st.h>
-
-typedef unsigned long long  st_utime_t;
-
-

-

Description
- -This datatype (unsigned 64-bit integer) represents high-resolution real time -expressed in microseconds since some arbitrary time in the past. It is not -correlated in any way to the time of day. -

-


-

- - -

st_netfd_t

- -File descriptor type. -

-

Syntax
- -
-#include <st.h>
-
-typedef void *  st_netfd_t;
-
-

-

Description
- -This datatype typically represents any open end point of network -communication (socket, end point of a pipe, FIFO, etc.) but can -encapsulate any open file descriptor. Objects of this type are -identified by a pointer to an opaque data structure. - -

-


-

- - -

st_switch_cb_t

- -Context switch callback function type. -

-

Syntax
- -
-#include <st.h>
-
-typedef void (*st_switch_cb_t)(void);
-
-

-

Description
- -This datatype is a convenience type for describing a pointer -to a function that will be called when a thread is set to stop -or set to run. -This feature is available only when ST_SWITCH_CB is defined -in <st.h>. - -

-


-

- - -

Error Handling

- - -All State Threads library non-void functions return on success either a -non-negative integer or a pointer to a newly created object (constructor-type -functions). On failure they return either -1 or a NULL -pointer respectively and set global errno to indicate the error. -It is safe to use errno because it is set right before the function -return and only one thread at a time can modify its value.

-The perror(3) function can be used to produce an error message on the -standard error output. -

-


-

- - -

Library Initialization

- -

-

-
st_init()
-
st_getfdlimit()
-
st_set_eventsys()
-
st_get_eventsys()
-
st_get_eventsys_name()
-

-These functions operate on a callback function of type -st_switch_cb_t: -

st_set_switch_in_cb()
-
st_set_switch_out_cb()
-
-

-


-

- - -

st_init()

- -Initializes the runtime. -

-

Syntax
- -
-#include <st.h>
-
-int st_init(void);
-
-

-

Parameters
-None. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error. -

-

Description
-This function initializes the library runtime. It should be called near -the beginning of the application's main() function before any other -State Threads library function is called.

-Among other things, this function limits the number of open file descriptors -to the OS imposed per-process maximum number or, if select(2) is -used, to FD_SETSIZE, whichever is less (getrlimit(2)). -This limit can be -retrieved by st_getfdlimit(). It also sets the -disposition of the SIGPIPE signal to SIG_IGN (to be ignored) -(signal(5)). -

-Unlike POSIX threads, a new process created by the fork(2) system -call is an exact copy of the calling process and all state threads -which are running in the parent do exist in the child. That means that -st_init() may be called either before or after multiple processes -are created by fork(2). -

-If the library runtime is not properly initialized (e.g., st_init() -is accidentally omitted), then the process will receive either an arithmetic -exception (SIGFPE or SIGTRAP) or segmentation fault (SIGSEGV) signal upon -new thread creation or the first context switch, respectively. -

-


-

- -

st_getfdlimit()

- -Returns the maximum number of file descriptors that the calling process -can open. -

-

Syntax
- -
-#include <st.h>
-
-int st_getfdlimit(void);
-
-

-

Parameters
-None. -

-

Returns
-The maximum number of file descriptors that the calling process can open. -If this function is called before the library is successfully initialized by -st_init(), a value of -1 is returned. -

-

Description
-This function returns the limit on the number of open file descriptors which -is set by the st_init() function. -

-


-

- - -

st_set_eventsys()

- -Sets event notification mechanism. -

-

Syntax
- -
-#include <st.h>
-
-int st_set_eventsys(int eventsys);
-
-

-

Parameters
-st_set_eventsys() has the following parameter:

-eventsys

-An integer value identifying selected event notification mechanism. The -following values are defined in the st.h header file: -

- - - - - - - - - - - - - - - -
ST_EVENTSYS_DEFAULTUse default event notification mechanism. Usually it's select(2) -but if the library was compiled with the USE_POLL macro defined -then the default is poll(2).
ST_EVENTSYS_SELECTUse select(2) as an event notification mechanism.
ST_EVENTSYS_POLLUse poll(2) as an event notification mechanism.
ST_EVENTSYS_ALTUse an alternative event notification mechanism. The actual -mechanism selected depends on OS support. For example, epoll(4) -will be used on Linux if supported and kqueue(2) will be used -on FreeBSD/OpenBSD. If the OS supports no alternative event -notification mechanism, setting ST_EVENTSYS_ALT has no effect -and the ST_EVENTSYS_DEFAULT mechanism will be used.
-

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - - - -
EINVAL -The supplied eventsys parameter has an invalid value. -
EBUSY -The event notification mechanism has already been set. -
-

-

Description
-This function sets the event notification mechanism that will be used by -the State Threads library. To have any effect, it must be called -before the st_init() function which performs -the actual initialization. If st_set_eventsys() is not called, -st_init() will set the ST_EVENTSYS_DEFAULT -mechanism. The mechanism cannot be changed once set. -

-There are no strict rules for selecting an event notification -mechanism. The "best" one depends on how your application behaves. -Try a few to see which one works best for you. As a rule of -thumb, you should use the ST_EVENTSYS_ALT mechanism if your -application deals with a very large number of network connections of -which only a few are active at once. -

-


-

- -

st_get_eventsys()

- -Returns the integer value identifying the event notification mechanism -being used by the State Threads library. -

-

Syntax
- -
-#include <st.h>
-
-int st_get_eventsys(void);
-
-

-

Parameters
-None. -

-

Returns
-The integer value identifying the current event notification mechanism. -This value can be one of the following (see st_set_eventsys()): -ST_EVENTSYS_SELECT, ST_EVENTSYS_POLL, or -ST_EVENTSYS_ALT. Future versions of the library may return other -values. If a mechanism hasn't been set yet, a value of -1 is returned. -

-

Description
-This function returns the integer value identifying the event notification -mechanism which is actually being used by the State Threads library. -

-


-

- -

st_get_eventsys_name()

- -Returns the name of the event notification mechanism being used by the -State Threads library. -

-

Syntax
- -
-#include <st.h>
-
-const char *st_get_eventsys_name(void);
-
-

-

Parameters
-None. -

-

Returns
-The string identifying the current event notification mechanism. If a -mechanism hasn't been set yet (see st_set_eventsys()), an empty string is -returned. Possible return values are "select", -"poll", "kqueue", or "epoll". Future versions -of the library may return other values. -

-

Description
-This function returns the string identifying the event notification -mechanism which is actually being used by the State Threads library. -

-


-

- - -

st_set_switch_in_cb()

- - -

st_set_switch_out_cb()

-
-Set the optional callback function for thread switches. -

-

Syntax
- -
-#include <st.h>
-
-st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb);
-st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb);
-
-

-

Parameters
-st_set_switch_in_cb() and st_set_switch_out_cb() have the -following parameter:

-cb

-A function to be called when a thread is resumed and stopped respectively.

-

Returns
-The previous callback function pointer. -

-

Description
-These functions set the callback for when a thread is resumed and stopped -respectively. After being called any thread switch will call the callback. -Use a NULL pointer to disable the callback (this is the default). -Use st_thread_self() or thread -specific data to differentiate between threads.

-These functions can be called at any time.

-This feature is available only when ST_SWITCH_CB is defined -in <st.h>. -

-


-

- - -

Thread Control and Identification

- -

-These functions operate on a thread object of type -st_thread_t. -

-

-
st_thread_create()
-
st_thread_exit()
-
st_thread_join()
-
st_thread_self()
-
st_thread_interrupt()
-
st_sleep()
-
st_usleep()
-
st_randomize_stacks()
-
-

-


-

- -

st_thread_create()

- -Creates a new thread. -

-

Syntax
- -
-#include <st.h>
-
-st_thread_t st_thread_create(void *(*start)(void *arg), void *arg,
-                             int joinable, int stack_size);
-
-
-

-

Parameters
-st_thread_create() has the following parameters:

-start

-A pointer to the thread's start function, which is called as the root of the -new thread. Return from this function terminates a thread.

-arg

-A pointer to the root function's only parameter.

-joinable

-Specifies whether the thread is joinable or unjoinable. If this parameter -is zero, the thread is unjoinable. Otherwise, it is joinable. -See also st_thread_join().

-stack_size

-Specifies your preference for the size of the stack, in bytes, associated -with the newly created thread. If you pass zero in this parameter, the -default stack size will be used. The default stack size is 128 KB on IA-64 -and 64 KB on all other platforms. On IA-64 only a half of stack_size -bytes is used for the memory stack. The other half is used for the register -stack backing store. -

-

Returns
-Upon successful completion, a new thread identifier is returned (this -identifier remains valid until the thread returns from its start function). -Otherwise, NULL is returned and errno is set -to indicate the error. -

-

Description
-This function creates a new thread. Note that the total number of threads -created by the application is limited by the amount of swap space available. -Upon thread creation, stack_size bytes are reserved on the swap -space. The stack pages are not actually used (valid) until touched by the -application. -

-


-

- -

st_thread_exit()

- -Terminates the calling thread. -

-

Syntax
- -
-#include <st.h>
-
-void st_thread_exit(void *retval);
-
-

-

Parameters
-st_thread_exit() has the following parameters:

-retval

-If the thread is joinable, then the value retval may be retrieved -by st_thread_join(). If a thread returns from its -start function, it acts as if it had called st_thread_exit() with -retval as the value returned. -

-

Returns
-Nothing. -

-

Description
-This function terminates the calling thread. When a thread exits, per-thread -private data is destroyed by invoking the destructor function for any -non-NULL thread specific values associated with active keys (see -st_key_create()). This function is implicitly called -when a thread returns from its start function.

-When the last thread terminates the process exits with a zero status value. -

-


-

- -

st_thread_join()

- -Blocks the calling thread until a specified thread terminates. -

-

Syntax
- -
-#include <st.h>
-
-int st_thread_join(st_thread_t thread, void **retvalp);
-
-

-

Parameters
-st_thread_join() has the following parameters:

-thread

-A valid identifier for the thread that is to be joined.

-retvalp

-If this parameter is not NULL, then the exit value of the -thread will be placed in the location referenced by this parameter -(see st_thread_exit()). -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - - - -
EINVALTarget thread is unjoinable.
EINVALOther thread already waits on the same -joinable thread.
EDEADLKTarget thread is the same as the -calling thread.
EINTRCurrent thread was interrupted by -st_thread_interrupt().
-

-

Description
-This function is used to synchronize the termination of a thread and possibly -retrieve its exit value. Several threads cannot wait for the same thread -to complete - one of the calling threads operates successfully, and the others -terminate with the error. The calling thread is not blocked if the target -thread has already terminated. -

-


-

- -

st_thread_self()

- -Identifies the calling thread. -

-

Syntax
- -
-#include <st.h>
-
-st_thread_t st_thread_self(void);
-
-

-

Parameters
-None. -

-

Returns
-Always returns a valid reference to the calling thread - a self-identity. -

-

Description
-This function identifies the calling thread. This is the same identifier -that the creating thread obtains from -st_thread_create(). -

-


-

- -

st_thread_interrupt()

- -Interrupts a target thread. -

-

Syntax
- -
-#include <st.h>
-
-void st_thread_interrupt(st_thread_t thread);
-
-

-

Parameters
-st_thread_interrupt() has the following parameters:

-thread

-A valid identifier for the thread being interrupted. -

-

Returns
-Nothing. -

-

Description
-This function interrupts (unblocks) a target thread that is blocked in one -of the blocking functions. A function that was interrupted -returns an error and sets errno to EINTR. It is up to -the target thread to act upon an interrupt (e.g., it may exit or just -abort the current transaction).

-Note: State Threads library functions are never interrupted by a -caught signal. A blocking library function returns an error and sets -errno to EINTR only if the current thread was -interrupted via st_thread_interrupt(). -

-If a target thread is already runnable or running (e.g., it is a newly -created thread or calling thread itself), this function will prevent it -from subsequent blocking. In other words, the interrupt will be "delivered" -only when a target thread is about to block. -

-


-

- -

st_sleep(), st_usleep()

- -Suspends current thread for a specified amount of time. -

-

Syntax
- -
-#include <st.h>
-
-int st_sleep(int secs);
-
-int st_usleep(st_utime_t usecs);
-
-

-

Parameters
-st_sleep() has the following parameters:

-secs

-The number of seconds you want the thread to sleep for. -

-st_usleep() has the following parameters:

-usecs

-The number of microseconds you want the thread to sleep for. This parameter -is a variable of type st_utime_t. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
-

-

Description
-These functions suspend the calling thread from execution for a specified -number of seconds (st_sleep()) or microseconds (st_usleep()). -

- -If zero is passed as a parameter to st_sleep(), or -ST_UTIME_NO_WAIT (0) is passed to -st_usleep(), the calling thread yields, thus potentially -allowing another thread to run. - -

- -If -1 is passed as a parameter to st_sleep(), or -ST_UTIME_NO_TIMEOUT (-1) is passed to -st_usleep(), the calling thread will be suspended permanently. -It can be resumed again by interrupting it via st_thread_interrupt(). - -

-


-

- -

st_randomize_stacks()

- -Turns stack base address randomization on or off. -

-

Syntax
- -
-#include <st.h>
-
-int st_randomize_stacks(int on);
-
-

-

Parameters
-st_randomize_stacks() has the following parameters:

-on

-If this parameter has a non-zero value, the State Threads library -randomizes the base addresses of stacks allocated for threads created -after this call. Otherwise new threads' stacks are typically page -aligned. -

-

Returns
-The previous state of stack randomization (a value of 0 if it -was off and a non-zero value otherwise). -

-

Description
-Randomizing state threads' stack bases may improve cache performance on -some systems when large numbers of state threads all perform roughly the -same work, as when they all start from the same root function. On many -modern systems the performance increase is negligible. You should -compare your application's performance with this feature on and off to -see if you really need it. -

-When randomization is enabled, new stacks are allocated one page larger -to accomodate the randomization. -

-This call affects only threads created afterward. It has no effect on -existing threads. -

-


-

- - -

Per-Thread Private Data

- -These functions allow to associate private data with each of the threads in -a process. -

-

-
st_key_create()
-
st_key_getlimit()
-
st_thread_setspecific()
-
st_thread_getspecific()
-
-

-


-

- -

st_key_create()

- -Creates a key (non-negative integer) that can be used by all -threads in the process to get and set thread-specific data. -

-

Syntax
- -
-#include <st.h>
-
-int st_key_create(int *keyp, void (*destructor)(void *));
-
-

-

Parameters
-st_key_create() has the following parameters:

-keyp

-The newly created key is returned in the memory pointed to by this parameter. -The new key can be used with -st_thread_setspecific() and -st_thread_getspecific().

-destructor

-Specifies an optional destructor function for the private data associated -with the key. This function can be specified as NULL. -Upon thread exit (see st_thread_exit()), if a key -has a non-NULL destructor and has a non-NULL value -associated with that key, then the destructor function will be -called with the associated value. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - -
EAGAINThe limit on the total number of keys per -process has been exceeded (see st_key_getlimit()). -
-

-

Description
-If this function is successful, every thread in the same process is capable -of associating private data with the new key. After a new key is created, all -active threads have the value NULL associated with that key. -After a new thread is created, the value NULL is associated with -all keys for that thread. If a non-NULL destructor function is -registered with a new key, it will be called at one of two times, as long as -the private data is not NULL: - -

-The key maintains independent data values for each binding thread. A thread -can get access only to its own thread-specific data. There is no way to -deallocate a private data key once it is allocated. -

-


-

- -

st_key_getlimit()

- -Returns the key limit. -

-

Syntax
- -
-#include <st.h>
-
-int st_key_getlimit(void);
-
-

-

Parameters
-None. -

-

Returns
-The limit on the total number of keys per process. -

-

Description
-This function can be used to obtain the limit on the total number of keys -per process (see st_key_create()). -

-


-

- -

st_thread_setspecific()

- -Sets per-thread private data. -

-

Syntax
- -
-#include <st.h>
-
-int st_thread_setspecific(int key, void *value);
-
-

-

Parameters
-st_thread_setspecific() has the following parameters:

-key

-This parameter represents a key with which thread-specific data is associated. -

-value

-The per-thread private data, or more likely, a pointer to the data which is -associated with key. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - -
EINVALThe specified key is invalid.
-

-

Description
-This function associates a thread-specific value with key. -Different threads may bind different values to the same key.

-If the thread already has non-NULL private data associated with -key, and if the destructor function for that key is not -NULL, this destructor function will be called before setting the -new data value. -

-


-

- -

st_thread_getspecific()

- -Retrieves the per-thread private data for the current thread. -

-

Syntax
- -
-#include <st.h>
-
-void *st_thread_getspecific(int key);
-
-

-

Parameters
-st_thread_getspecific() has the following parameters:

-key

-This parameter represents a key with which thread-specific data is associated. -

-

Returns
-The thread-specific data associated with key. If no data is -associated with key, then NULL is returned. -

-

Description
-This function returns the calling thread's value that is bound to the -specified key (see -st_thread_setspecific()). -

-


-

- - -

Synchronization

- -

-These functions operate on condition variables -and mutual exclusion locks (mutexes).

-Functions are provided to wait on a condition variable and to wake up -(signal) threads that are waiting on the condition variable. -

-

-
st_cond_new()
-
st_cond_destroy()
-
st_cond_wait()
-
st_cond_timedwait()
-
st_cond_signal()
-
st_cond_broadcast()
-

-

st_mutex_new()
-
st_mutex_destroy()
-
st_mutex_lock()
-
st_mutex_trylock()
-
st_mutex_unlock()
-
-

-


-

- -

st_cond_new()

- -Creates a new condition variable. -

-

Syntax
- -
-#include <st.h>
-
-st_cond_t st_cond_new(void);
-
-

-

Parameters
-None. -

-

Returns
-Upon successful completion, a new condition variable identifier is returned. -Otherwise, NULL is returned and errno is set -to indicate the error. -

-

Description
-This function creates a new condition variable. -

-


-

- -

st_cond_destroy()

- -Destroys a condition variable. -

-

Syntax
- -
-#include <st.h>
-
-int st_cond_destroy(st_cond_t cvar);
-
-

-

Parameters
-st_cond_destroy() has the following parameters:

-cvar

-An identifier of the condition variable object to be destroyed. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - -
EBUSYThe condition variable is currently being -used by one or more threads.
-

-

Description
-This function destroys a condition variable. The caller is responsible for -ensuring that the condition variable is no longer in use. -

-


-

- -

st_cond_wait()

- -Waits on a condition. -

-

Syntax
- -
-#include <st.h>
-
-int st_cond_wait(st_cond_t cvar);
-
-

-

Parameters
-st_cond_wait() has the following parameters:

-cvar

-The condition variable on which to wait. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
-

-

Description
-This function is used to block on a condition variable. A return from this -function does not guarantee that the condition or event for which the caller -was waiting actually occurred. It is the responsibility of the caller -to recheck the condition wait predicate before proceeding.

-Note: The State Threads library scheduling guarantees that the -condition cannot change between the checking and blocking, therefore there -is no need for mutex protection. You must not call any -blocking functions between the condition checking and -the st_cond_wait() call. -

-


-

- -

st_cond_timedwait()

- -Waits on a condition. -

-

Syntax
- -
-#include <st.h>
-
-int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout);
-
-

-

Parameters
-st_cond_timedwait() has the following parameters:

-cvar

-The condition variable on which to wait.

-timeout

-If the number of microseconds specified by this parameter passes before the -waiting thread is signalled, an error is returned. This parameter is a -variable of type st_utime_t. Note that this -time value is a time delta; it is not an absolute time. -Also note that timeouts are measured since -the last context switch. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred before the thread was -awakened by st_cond_signal() or -st_cond_broadcast().
-

-

Description
-This function works the same way as st_cond_wait(), -except that an error is returned if the number of microseconds specified by -timeout passes before the waiting thread is signalled. -

-


-

- -

st_cond_signal()

- -Unblocks a thread waiting on a condition variable. -

-

Syntax
- -
-#include <st.h>
-
-int st_cond_signal(st_cond_t cvar);
-
-

-

Parameters
-st_cond_signal() has the following parameters:

-cvar

-The condition variable to signal. -

-

Returns
-Always zero. -

-

Description
-This function unblocks (signals) one of the threads that are blocked on -cvar at the time of the call. If no thread is waiting on the -condition variable, the signal operation is a no-op. -

-


-

- -

st_cond_broadcast()

- -Unblocks all threads waiting on a condition variable. -

-

Syntax
- -
-#include <st.h>
-
-int st_cond_broadcast(st_cond_t cvar);
-
-

-

Parameters
-st_cond_broadcast() has the following parameters:

-cvar

-The condition variable to broadcast. -

-

Returns
-Always zero. -

-

Description
-This function unblocks all threads blocked on the specified condition -variable at the time of the call. If no threads are waiting, this operation -is a no-op. -

-


-

- - -

st_mutex_new()

- -Creates a new mutual exclusion lock (mutex). -

-

Syntax
- -
-#include <st.h>
-
-st_mutex_t st_mutex_new(void);
-
-

-

Parameters
-None. -

-

Returns
-Upon successful completion, a new mutex identifier is returned. -Otherwise, NULL is returned and errno is set to -indicate the error. -

-

Description
-This function creates a new opaque mutual exclusion lock (see -st_mutex_t). -

-


-

- -

st_mutex_destroy()

- -Destroys a specified mutex object. -

-

Syntax
- -
-#include <st.h>
-
-int st_mutex_destroy(st_mutex_t lock);
-
-

-

Parameters
-st_mutex_destroy() has the following parameters:

-lock

-An identifier of the mutex object to be destroyed. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - -
EBUSYThe mutex is currently being used by -other threads.
-

-

Description
-This function destroys a mutex. The caller is responsible for ensuring -that the mutex is no longer in use. -

-


-

- -

st_mutex_lock()

- -Locks a specified mutex object. -

-

Syntax
- -
-#include <st.h>
-
-int st_mutex_lock(st_mutex_t lock);
-
-

-

Parameters
-st_mutex_lock() has the following parameters:

-lock

-An identifier of the mutex object to be locked. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - - -
EDEADLKThe current thread already owns the mutex. -
EINTRThe current thread was interrupted by -st_thread_interrupt().
-

-

Description
-A thread that calls this function will block until it can gain exclusive -ownership of a mutex, and retains ownership until it calls -st_mutex_unlock(). -

-


-

- -

st_mutex_trylock()

- -Attempts to acquire a mutex. -

-

Syntax
- -
-#include <st.h>
-
-int st_mutex_trylock(st_mutex_t lock);
-
-

-

Parameters
-st_mutex_trylock() has the following parameters:

-lock

-An identifier of the mutex object to be locked. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - -
EBUSYThe mutex is currently held by another -thread.
-

-

Description
-This function attempts to acquire a mutex. If the mutex object is locked -(by any thread, including the current thread), the call returns immediately -with an error. -

-


-

- -

st_mutex_unlock()

- -Releases a specified mutex object. -

-

Syntax
- -
-#include <st.h>
-
-int st_mutex_unlock(st_mutex_t lock);
-
-

-

Parameters
-st_mutex_unlock() has the following parameters:

-lock

-An identifier of the mutex object to be unlocked. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error:

- - -
EPERMThe current thread does not own the mutex. -
-

-

Description
-This function releases a specified mutex object previously acquired by -st_mutex_lock() or -st_mutex_trylock(). Only the thread that locked -a mutex should unlock it. -

-


-

- - -

Timing

- -

-

-
st_utime()
-
st_set_utime_function()
-
st_timecache_set()
-
st_time()
-
-

-


-

- -

st_utime()

- -Returns current high-resolution time. -

-

Syntax
- -
-#include <st.h>
-
-st_utime_t st_utime(void);
-
-

-

Parameters
-None. -

-

Returns
-Current high-resolution time value of type -st_utime_t. -

-

Description
-This function returns the current high-resolution time. Time is -expressed as microseconds since some arbitrary time in the past. It is -not correlated in any way to the time of day. See also st_utime_t and st_time(). -

-


-

- -

st_set_utime_function()

- -Set high-resolution time function. -

-

Syntax
- -
-#include <st.h>
-
-int st_set_utime_function(st_utime_t (*func)(void));
-
-

-

Parameters
-st_set_utime_function() has the following parameters:

-func

-This function will be called to get high-resolution time instead of the -default st_utime() function. It must return -number of microseconds since some arbitrary time in the past. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to EINVAL to indicate the error. -

-

Description
-This function may be called to replace the default implementation of the -st_utime() function. It must be called before the ST -library has been initialized (see st_init()). -The user-provided function func will be invoked whenever -st_utime() is called to obtain current high-resolution time. -Replacing default implementation may be useful, for example, for taking -advantage of high performance CPU cycle counters. -

-


-

- -

st_timecache_set()

- -Turns the time caching on or off. -

-

Syntax
- -
-#include <st.h>
-
-int st_timecache_set(int on);
-
-

-

Parameters
-st_timecache_set() has the following parameters:

-on

-If this parameter has a non-zero value, the time caching is turned on -(enabled). Otherwise, the time caching is turned off (disabled). -By default time caching is disabled. -

-

Returns
-The previous state of time caching (a value of 0 if it was off and -a value of 1 otherwise). -

-

Description
-The State Threads library has the ability to "cache" the time value that is -reported by the time(2) system call. If the time caching is enabled -by calling this function with a non-zero argument, then the result value -of time(2) will be stored and updated at most once per second. The -cached time can be retrieved by st_time(). -By default time caching is disabled. -You may enable or disable time caching at any time but generally -you enable it once (if desired) during program initialization.

-Note: There are some pathological cases (e.g., very heavy loads during -application benchmarking) when a single thread runs for a long time without -giving up control and the cached time value is not updated properly. If you -always need "real-time" time values, don't enable the time caching. -

-


-

- -

st_time()

- -Returns the value of time in seconds since 00:00:00 UTC, January 1, 1970. -

-

Syntax
- -
-#include <st.h>
-
-time_t st_time(void);
-
-

-

Parameters
-None. -

-

Returns
-The value of time in seconds since 00:00:00 UTC, January 1, 1970 as reported -by the time(2) system call. -

-

Description
-If the time caching was enabled by -st_timecache_set(), then this function returns -the cached result. Otherwise, it just calls time(2). -

-


-

- - -

I/O Functions

- -

-Most State Threads library I/O functions look like corresponding C library -functions with two exceptions: -

    -
  • They operate on file descriptor objects of type -st_netfd_t.
  • -
  • They take an additional argument of type -st_utime_t which represents an inactivity -timeout: if no I/O is possible during this amount of time, I/O functions -return an error code and set errno to ETIME. - -The boundary values ST_UTIME_NO_WAIT (0) and -ST_UTIME_NO_TIMEOUT (-1) for this argument indicate -that the thread should wait no time (function returns immediately) or -wait forever (never time out), respectively. - -Note that timeouts are measured since the -last context switch. -
  • -
-

-

-
st_netfd_open()
-
st_netfd_open_socket()
-
st_netfd_free()
-
st_netfd_close()
-
st_netfd_fileno()
-
st_netfd_setspecific()
-
st_netfd_getspecific()
-
st_netfd_serialize_accept()
-
st_netfd_poll()
-

-

st_accept()
-
st_connect()
-
st_read()
-
st_read_fully()
-
st_read_resid()
-
st_readv()
-
st_read_resid()
-
st_write()
-
st_write_resid()
-
st_writev()
-
st_writev_resid()
-
st_recvfrom()
-
st_sendto()
-
st_recvmsg()
-
st_sendmsg()
-
st_open()
-
st_poll()
-
-

-


-

- -

st_netfd_open()

- -Creates a new file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-st_netfd_t st_netfd_open(int osfd);
-
-

-

Parameters
-st_netfd_open() has the following parameters:

-osfd

- -Any open OS file descriptor; can be obtained from calls to -functions including, but not restricted to, pipe(2), socket(3), -socketpair(3), fcntl(2), dup(2), etc. - - -

-

Returns
-Upon successful completion, a new file descriptor object identifier is -returned. Otherwise, NULL is returned and errno is set -to indicate the error. -

-

Description
-This function creates a new file descriptor object of type -st_netfd_t.

- -Note: Among other things, this function sets a non-blocking -flag on the underlying OS file descriptor. You should not modify this -flag directly. Also, once an st_netfd_t -has been created with a given file descriptor, you should avoid -passing that descriptor to normal I/O or stdio functions. Since the -O_NONBLOCK flag is shared across dup(2), this applies to -dup()'ed file descriptors as well - for instance, if you pass -standard output or standard input to st_netfd_open(), then -you should use st_write() instead of write -or fprintf when writing to standard error as well - since all -three descriptors could point to the same terminal. If necessary, you -can still use write directly if you remember to check -errno for EAGAIN, but fprintf and other -stdio functions should be avoided completely because, at least on -Linux, the stdio library cannot be made to work reliably with -non-blocking files. (This only applies to file descriptors which are -passed to st_netfd_open() or st_netfd_open_socket(), or which are -related to such descriptors through dup(); other file -descriptors are untouched by State Threads.) -

-


-

- -

st_netfd_open_socket()

- -Creates a new file descriptor object from a socket. -

-

Syntax
- -
-#include <st.h>
-
-st_netfd_t st_netfd_open_socket(int osfd);
-
-

-

Parameters
-st_netfd_open_socket() has the following parameters:

-osfd

-An open OS file descriptor which is a socket initially obtained from a -socket(3) or socketpair(3) call. -

-

Returns
-Upon successful completion, a new file descriptor object identifier is -returned. Otherwise, NULL is returned and errno is set -to indicate the error. -

-

Description
-This function creates a new file descriptor object of type -st_netfd_t which represents an open end -point of network communication.

-Unlike the st_netfd_open() function which may be used -on OS file descriptors of any origin, st_netfd_open_socket() must -be used only on sockets. It is slightly more efficient than -st_netfd_open().

-Note: Among other things, this function sets a non-blocking flag -on the underlying OS socket. You should not modify this flag directly. -See st_netfd_open(). -

-


-

- -

st_netfd_free()

- -Frees a file descriptor object without closing the underlying OS file -descriptor. -

-

Syntax
- -
-#include <st.h>
-
-void st_netfd_free(st_netfd_t fd);
-
-

-

Parameters
-st_netfd_free() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t). -

-

Returns
-Nothing. -

-

Description
-This function frees the memory and other resources identified by the -fd parameter without closing the underlying OS file descriptor. -Any non-NULL descriptor-specific data is destroyed by invoking -the specified destructor function (see st_netfd_setspecific()).

A thread should -not free file descriptor objects that are in use by other threads -because it may lead to unpredictable results (e.g., a freed file -descriptor may be reused without other threads knowing that). -

-


-

- -

st_netfd_close()

- -Closes a file descriptor. -

-

Syntax
- -
-#include <st.h>
-
-int st_netfd_close(st_netfd_t fd);
-
-

-

Parameters
-st_netfd_close() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t). -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error. -

-

Description
-This function closes the underlying OS file descriptor, frees the memory and -other resources identified by the fd parameter. Any non-NULL -descriptor-specific data is destroyed by invoking the specified destructor -function (see st_netfd_setspecific()).

-A thread should not close file descriptor objects that are in use by other -threads because it may lead to unpredictable results (e.g., a closed -file descriptor may be reused without other threads knowing that). -

-


-

- -

st_netfd_fileno()

- -Returns an underlying OS file descriptor. -

-

Syntax
- -
-#include <st.h>
-
-int st_netfd_fileno(st_netfd_t fd);
-
-

-

Parameters
-st_netfd_fileno() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t). -

-

Returns
-An underlying OS file descriptor. -

-

Description
-This function returns the integer OS file descriptor associated with the named -file descriptor object. -

-


-

- -

st_netfd_setspecific()

- -Sets per-descriptor private data. -

-

Syntax
- -
-#include <st.h>
-
-void st_netfd_setspecific(st_netfd_t fd, void *value,
-                          void (*destructor)(void *));
-
-

-

Parameters
-st_netfd_setspecific() has the following parameters:

-fd

-A valid file descriptor object identifier (see -st_netfd_t). -

-value

-The per-descriptor private data, or more likely, a pointer to the data which -is being associated with the named file descriptor object. -

-destructor

-Specifies an optional destructor function for the private data associated -with fd. This function can be specified as NULL. -If value is not NULL, then this destructor function will -be called with value as an argument upon freeing the file descriptor -object (see st_netfd_free() and -st_netfd_close()). -

-

Returns
-Nothing. -

-

Description
-This function allows to associate any data with the specified file -descriptor object (network connection). If a non-NULL destructor -function is registered, it will be called at one of two times, as long as -the associated data is not NULL: -
    -
  • when private data is replaced by calling -st_netfd_setspecific() again -
  • upon freeing the file descriptor object (see -st_netfd_free() and -st_netfd_close()) -
-

-


-

- -

st_netfd_getspecific()

- -Retrieves the per-descriptor private data. -

-

Syntax
- -
-#include <st.h>
-
-void *st_netfd_getspecific(st_netfd_t fd);
-
-

-

Parameters
-st_netfd_getspecific() has the following parameters:

-fd

-A valid file descriptor object identifier (see -st_netfd_t). -

-

Returns
-The data associated with the named file descriptor object. If no data is -associated with fd, then NULL is returned. -

-

Description
-This function allows to retrieve the data that was associated with the -specified file descriptor object (see -st_netfd_setspecific()). -

-


-

- -

st_netfd_serialize_accept()

- -Serializes all subsequent accept(3) calls on a specified file -descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_netfd_serialize_accept(st_netfd_t fd);
-
-

-

Parameters
-st_netfd_serialize_accept() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) which has been successfully created -from a valid listening socket by st_netfd_open() or -st_netfd_open_socket(). -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error. -

-

Description
-On some platforms (e.g., Solaris 2.5 and possibly other SVR4 implementations) -accept(3) calls from different processes on -the same listening socket (see bind(3), listen(3)) must be -serialized. This function causes all subsequent accept(3) calls -made by st_accept() on the specified file descriptor -object to be serialized. -

-st_netfd_serialize_accept() must be called before -creating multiple server processes via fork(2). If the application -does not create multiple processes to accept network connections on -the same listening socket, there is no need to call this function. -

-Deciding whether or not to serialize accepts is tricky. On some -platforms (IRIX, Linux) it's not needed at all and -st_netfd_serialize_accept() is a no-op. On other platforms -it depends on the version of the OS (Solaris 2.6 doesn't need it but -earlier versions do). Serializing accepts does incur a slight -performance penalty so you want to enable it only if necessary. Read -your system's manual pages for accept(2) and select(2) -to see if accept serialization is necessary on your system. -

-st_netfd_serialize_accept() allocates resources that are -freed upon freeing of the specified file descriptor object (see -st_netfd_free() and -st_netfd_close()). -

-


-

- -

st_netfd_poll()

- -Waits for I/O on a single file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout);
-
-

-

Parameters
-st_netfd_poll() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t). -

-how

-Specifies I/O events of interest. This parameter can be constructed by -OR-ing any combination of the following event flags which are defined -in the poll.h header file:

- - - - - -
POLLINfd is readable.
POLLOUTfd is is writable.
POLLPRIfd has an exception condition.
-

-timeout

-Amount of time in microseconds the call will block waiting for I/O -to become ready. This parameter is a variable of type -st_utime_t. If this time expires without any -I/O becoming ready, st_netfd_poll() returns an error and sets -errno to ETIME. -Note that timeouts are measured since the -last context switch. -

-

Returns
-If the named file descriptor object is ready for I/O within the specified -amount of time, a value of 0 is returned. Otherwise, a value -of -1 is returned and errno is set to indicate the error: -

- - - - -
EBADFThe underlying OS file descriptor is invalid. -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred without any I/O -becoming ready.
-

-

Description
-This function returns as soon as I/O is ready on the named file -descriptor object or the specified amount of time expires. The -how parameter should be set to the I/O events (readable, -writable, exception, or some combination) that the caller is interested -in. If the value of timeout is ST_UTIME_NO_TIMEOUT -(-1), this function blocks until a requested I/O event occurs -or until the call is interrupted by st_thread_interrupt().

-Despite having an interface like poll(2), this function uses -the same event notification mechanism as the rest of the library. For -instance if an alternative event nofication mechanism was set using st_set_eventsys(), this function uses that -mechanism to check for events.

-Note: if kqueue(2) is used as an alternative event -notification mechanism (see st_set_eventsys()), the POLLPRI -event flag is not supported and st_netfd_poll() will return an error -if it's set (errno will be set to EINVAL). -

-


-

- -

st_accept()

- -Accepts a connection on a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen,
-                     st_utime_t timeout);
-
-

-

Parameters
-st_accept() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing the rendezvous socket -on which the caller is willing to accept new connections. This object has been -created from a valid listening socket by -st_netfd_open() or -st_netfd_open_socket().

-addr

-If this value is non-zero, it is a result parameter that is filled -in with the address of the connecting entity, as known to the communications -layer (see accept(3)).

-addrlen

-This parameter should initially contain the amount of space pointed to by -addr; on return it will contain the actual length (in bytes) of the -address returned (see accept(3)).

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the accept operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-Upon successful completion, a new file descriptor object identifier -representing the newly accepted connection is returned. Otherwise, -NULL is returned and errno is set to indicate the error. -Possible errno values are the same as set by the accept(3) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no pending -connection was accepted.
-

-

Description
-This function accepts the first connection from the queue of pending -connections and creates a new file descriptor object for the newly -accepted connection. The rendezvous socket can still be used to accept -more connections.

-st_accept() blocks the calling thread until either a new connection -is successfully accepted or an error occurs. If no pending connection can -be accepted before the time limit, this function returns NULL -and sets errno to ETIME. -

-


-

- -

st_connect()

- -Initiates a connection on a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_connect(st_netfd_t fd, struct sockaddr *addr, int addrlen,
-               st_utime_t timeout);
-
-

-

Parameters
-st_connect() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing a socket.

-addr

-A pointer to the address of the peer to which the socket is to be connected. -

-addrlen

-This parameter specifies the amount of space pointed to by addr. -

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the connect operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-Upon successful completion, a value of 0 is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error. Possible errno values are the same as set -by the connect(3) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and connection setup -was not completed.
-

-

Description
-This function is usually invoked on a file descriptor object representing -a TCP socket. Upon completion it establishes a TCP connection to the peer. -If the underlying OS socket is not bound, it will be bound to an arbitrary -local address (see connect(3)).

-st_connect() blocks the calling thread until either the connection -is successfully established or an error occurs. If the connection setup -cannot complete before the specified time limit, this function fails with -errno set to ETIME. -

-


-

- -

st_read()

- -Reads data from a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout);
-
-

-

Parameters
-st_read() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-buf

-A pointer to a buffer to hold the data read in. On output the buffer -contains the data.

-nbyte

-The size of buf in bytes.

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the read operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes actually -read is returned (a value of 0 means the network connection is -closed or end of file is reached). Otherwise, a value of -1 is -returned and errno is set to indicate the error. -Possible errno values are the same as set by the read(2) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was read. -
-

-

Description
-This function blocks the calling thread until it encounters an end-of-stream -indication, some positive number of bytes (but no more than nbyte -bytes) are read in, a timeout occurs, or an error occurs. -

-


-

- -

st_read_fully()

- -Reads the specified amount of data in full from a file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte,
-                      st_utime_t timeout);
-
-

-

Parameters
-st_read_fully() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-buf

-A pointer to a buffer to hold the data read in. On output the buffer -contains the data.

-nbyte

-The amount of data to be read in full (in bytes). It must not exceed the -size of buf.

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes actually -read is returned (a value less than nbyte means the network -connection is closed or end of file is reached). Otherwise, a value of --1 is returned and errno is set to indicate the error. -Possible errno values are the same as set by the read(2) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-

Description
-This function blocks the calling thread until the specified amount of data -is read in full, it encounters an end-of-stream indication, a timeout occurs, -or an error occurs. -

-


-

- -

st_read_resid()

- -Reads the specified amount of data in full from a file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_read_resid(st_netfd_t fd, void *buf, size_t *resid,
-		  st_utime_t timeout);
-
-

-

Parameters
-st_read_resid() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-buf

-A pointer to a buffer to hold the data read in. On output the buffer -contains the data.

-resid

-A pointer to a number of bytes. -On entry, the amount of data to be read in full. -It must not exceed the size of buf. -On return, the amount of data remaining to be read. -(A non-zero returned value means some but not all of the data was read.)

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success, zero is returned. *resid may be zero, indicating -a complete read, or non-zero, indicating the network -connection is closed or end of file is reached. -

-Otherwise, a value of -1 is returned, *resid is non-zero, -and errno is set to indicate the error. -Possible errno values are the same as set by the read(2) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-

Description
-This function blocks the calling thread until the specified amount of data -is read in full, it encounters an end-of-stream indication, a timeout occurs, -or an error occurs. It differs from st_read_fully() only in that -it allows the caller to know how many bytes were transferred before an error -occurred. -

-


-

- -

st_readv()

- -Reads data from a specified file descriptor object into multiple buffers. -

-

Syntax
- -
-#include <st.h>
-
-ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size,
-		 st_utime_t timeout);
-
-

-

Parameters
-st_readv() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-iov

-An array of iovec structures that identify the buffers for holding -the data read in. -On return the buffers contain the data.

-iov_size

-The number of iovec structures in the iov array.

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the read operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes actually -read is returned (a value of 0 means the network connection is -closed or end of file is reached). Otherwise, a value of -1 is -returned and errno is set to indicate the error. -Possible errno values are the same as set by the readv(2) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was read. -
-

-

Description
-This function blocks the calling thread until it encounters an end-of-stream -indication, some positive number of bytes (but no more than fit in the buffers) -are read in, a timeout occurs, or an error occurs. -

-


-

- -

st_readv_resid()

- -Reads the specified amount of data in full from a file descriptor object -into multiple buffers. -

-

Syntax
- -
-#include <st.h>
-
-int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
-		   st_utime_t timeout);
-
-

-

Parameters
-st_readv_resid() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-iov

-A pointer to an array of iovec structures. -On entry, the iovecs identify the buffers for holding the data read in. -On return, the incomplete iovecs. -This function modifies both the pointer and the array to which it points.

-iov_size

-A pointer to a number of iovec structures. -On entry, the number of iovec structures pointed to by *iov. -On return, the number of incomplete or unused iovec structures. -(A non-zero returned value means some but not all of the data was read.)

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success, zero is returned. *iov_size may be zero, indicating -a complete read, or non-zero, indicating the network connection is -closed or end of file is reached. *iov points to the first -iovec after the end of the original array on a complete read, or to the -first incomplete iovec on an incomplete read. -

-Otherwise, a value of -1 is returned, *iov_size is non-zero, -and errno is set to indicate the error. *iov points to the -first unused iovec. -Possible errno values are the same as set by the readv(2) -call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

All of the iovecs before *iov are modified such that -iov_base points to the end of the original buffer and -iov_len is zero. -

-

Description
-This function blocks the calling thread until the specified amount of data -is read in full, it encounters an end-of-stream indication, a timeout occurs, -or an error occurs. Like st_read_resid() it blocks the thread until -all of the requested data is read or an error occurs. Use -st_readv() to read up to the requested amount of data. -

-


-

- -

st_write()

- -Writes a buffer of data to a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte,
-                 st_utime_t timeout);
-
-

-

Parameters
-st_write() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-buf

-A pointer to the buffer holding the data to be written.

-nbyte

-The amount of data in bytes to be written from the buffer.

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer equal to nbyte is returned. -Otherwise, a value of -1 is returned and errno is set -to indicate the error. Possible errno values are the same as set -by the write(2) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-

Description
-This function blocks the calling thread until all the data is written, -a timeout occurs, or the write operation fails. The return value is equal to -either nbyte (on success) or -1 (on failure). Note that if -st_write() returns -1, some data (less than nbyte -bytes) may have been written before an error occurred. -

-


-

- -

st_write_resid()

- -Writes a buffer of data to a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid,
-                   st_utime_t timeout);
-
-

-

Parameters
-st_write_resid() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-buf

-A pointer to the buffer holding the data to be written.

-resid

-A pointer to a number of bytes. -On entry, the amount of data to be written from the buffer. -On return, the amount of data remaining to be written. -(A non-zero returned value means some but not all of the data was written.)

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success, zero is returned and *resid is zero. -Otherwise, a value of -1 is returned, *resid is non-zero, -and errno is set -to indicate the error. Possible errno values are the same as set -by the write(2) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-

Description
-This function blocks the calling thread until all the data is written, -a timeout occurs, or the write operation fails. It differs from -st_write() only in that it allows the caller to know how many bytes -were transferred before an error occurred. -

-


-

- -

st_writev()

- -Writes data to a specified file descriptor object from multiple buffers. -

-

Syntax
- -
-#include <st.h>
-
-ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size,
-                  st_utime_t timeout);
-
-

-

Parameters
-st_writev() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-iov

-An array of iovec structures that describe the buffers to write -from (see writev(2)).

-iov_size

-Number of iovec structures in the iov array.

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer equal to the sum of all the buffer lengths -is returned. Otherwise, a value of -1 is returned and errno -is set to indicate the error. Possible errno values are the same as -set by the writev(2) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-

Description
-This function blocks the calling thread until all the data is written, -a timeout occurs, or the write operation fails. The return value is equal to -either the sum of all the buffer lengths (on success) or -1 (on -failure). Note that if st_writev() returns -1, part of the -data may have been written before an error occurred. -

-


-

- -

st_writev_resid()

- -Writes multiple buffers of data to a specified file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size,
-		    st_utime_t timeout);
-
-

-

Parameters
-st_writev_resid() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t).

-iov

-A pointer to an array of iovec structures. -On entry, the iovecs identify the buffers holding the data to write. -On return, the incomplete iovecs. -This function modifies both the pointer and the array to which it points.

-iov_size

-A pointer to a number of iovec structures. -On entry, the number of iovec structures pointed to by *iov. -On return, the number of incomplete or unused iovec structures. -(A non-zero returned value means some but not all of the data was written.)

-timeout

-A value of type st_utime_t specifying the -inactivity timeout (in microseconds). -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success, zero is returned, *iov_size is zero, and *iov -points to the first iovec after the end of the original array. -Otherwise, a value of -1 is returned, *iov_size is non-zero, -*iov points to the first incomplete iovec, and errno is set -to indicate the error. Possible errno values are the same as set -by the writev(2) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred. -
-

-All of the iovecs before *iov are modified such that -iov_base points to the end of the original buffer and -iov_len is zero. -

-

Description
-This function blocks the calling thread until all the data is written, -a timeout occurs, or the write operation fails. It differs from -st_writev() only in that it allows the caller to know how many bytes -were transferred before an error occurred. -

-


-

- -

st_recvfrom()

- -Receives bytes from a file descriptor object and stores the sending peer's -address. -

-

Syntax
- -
-#include <st.h>
-
-int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from,
-                int *fromlen, st_utime_t timeout);
-
-

-

Parameters
-st_recvfrom() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing a UDP socket.

-buf

-A pointer to a buffer to hold the data received.

-len

-The size of buf in bytes.

-from

-If this parameter is not a NULL pointer, the source address of the -message is filled in (see recvfrom(3)).

-fromlen

-This is a value-result parameter, initialized to the size of the buffer -associated with from, and modified on return to indicate the actual -size of the address stored there.

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the receive operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the length of the received -message in bytes is returned. Otherwise, a value of -1 is returned -and errno is set to indicate the error. Possible errno -values are the same as set by the recvfrom(3) call with two -exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was received. -
-

-

Description
-This function receives up to a specified number of bytes from the specified -file descriptor object representing a UDP socket.

-st_recvfrom() blocks the calling thread until one or more bytes are -transferred, a timeout has occurred, or there is an error. No more than -len bytes will be transferred. -

-


-

- -

st_sendto()

- -Sends bytes to a specified destination. -

-

Syntax
- -
-#include <st.h>
-
-int st_sendto(st_netfd_t fd, const void *msg, int len, struct sockaddr *to,
-              int tolen, st_utime_t timeout);
-
-

-

Parameters
-st_sendto() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing a UDP socket.

-msg

-A pointer to a buffer containing the message to be sent.

-len

-The length of the message to be sent (in bytes).

-to

-A pointer to the address of the destination (see sendto(3)).

-tolen

-This parameter specifies the size of the destination address.

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the send operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes sent is -returned. Otherwise, a value of -1 is returned and errno is -set to indicate the error. Possible errno values are the same as -set by the sendto(3) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was sent. -
-

-

Description
-This function sends a specified number of bytes from a file descriptor -object representing a UDP socket to the specified destination address. -If no buffer space is available at the underlying OS socket to hold the -message to be transmitted, then st_sendto() blocks the calling -thread until the space becomes available, a timeout occurs, or an error -occurs. -

-


-

- -

st_recvmsg()

- -Receives a message from a file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags,
-               st_utime_t timeout);
-
-

-

Parameters
-st_recvmsg() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing a UDP socket.

-msg

-A pointer to a msghdr structure to describe the data received.

-flags

-Control flags for recvmsg(3).

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the receive operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes received -is returned. Otherwise, a value of -1 is returned -and errno is set to indicate the error. Possible errno -values are the same as set by the recvmsg(3) call with two -exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was received. -
-

-

Description
-This function receives bytes from the specified file descriptor object -representing a UDP socket. The operation is controlled by the in/out -msg parameter.

-st_recvmsg() blocks the calling thread until one or more bytes are -transferred, a timeout has occurred, or there is an error. -

-


-

- -

st_sendmsg()

- -Sends a message to a file descriptor object. -

-

Syntax
- -
-#include <st.h>
-
-int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags,
-               st_utime_t timeout);
-
-

-

Parameters
-st_sendmsg() has the following parameters:

-fd

-A file descriptor object identifier (see -st_netfd_t) representing a UDP socket.

-msg

-A pointer to a msghdr structure describing the message to be sent.

-flags

-Control flags for sendmsg(3).

-timeout

-A value of type st_utime_t specifying the time -limit in microseconds for completion of the send operation. -Note that timeouts are measured since the -last context switch. -

-

Returns
-On success a non-negative integer indicating the number of bytes sent is -returned. Otherwise, a value of -1 is returned and errno is -set to indicate the error. Possible errno values are the same as -set by the sendmsg(3) call with two exceptions:

- - - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
ETIMEThe timeout occurred and no data was sent. -
-

-

Description
-This function sends bytes to a file descriptor object representing a UDP -socket. The operation is controlled by the msg parameter. -If no buffer space is available at the underlying OS socket to hold the -message to be transmitted, then st_sendmsg() blocks the calling -thread until the space becomes available, a timeout occurs, or an error -occurs. -

-


-

- -

st_open()

- -Opens a file for reading, writing, or both. -

-

Syntax
- -
-#include <st.h>
-
-st_netfd_t st_open(const char *path, int oflags, mode_t mode);
-
-

-

Parameters
-st_open() has the following parameters:

-path

-The pathname of the file to be opened.

-oflags

-File status flags. These are the same flags that are used by the -open(2) system call.

-mode

-Access permission bits of the file mode, if the file is created when -O_CREAT is set in oflags (see open(2)). -

-

Returns
-Upon successful completion, a new file descriptor object identifier is -returned. Otherwise, NULL is returned and errno is set -to indicate the error. -

-

Description
-This function creates a new file descriptor object of type -st_netfd_t for the file with the pathname -path. This object can be freed by -st_netfd_free() or -st_netfd_close().

-The primary purpose of this function is to open FIFOs (named pipes) or -other special files in order to create an end point of communication. -However, it can be used on regular files as well.

-Among other things, this function always sets a non-blocking flag on the -underlying OS file descriptor, so there is no need to include that flag in -oflags. -

-


-

- -

st_poll()

- -Detects when I/O is ready for a set of OS file descriptors. -

-

Syntax
- -
-#include <st.h>
-
-int st_poll(struct pollfd *pds, int npds, st_utime_t timeout);
-
-

-

Parameters
-st_poll() has the following parameters:

-pds

-A pointer to an array of pollfd structures (see poll(2)). -

-npds

-The number of elements in the pds array.

-timeout

-A value of type st_utime_t specifying the -amount of time in microseconds the call will block waiting for I/O -to become ready. If this time expires without any I/O becoming ready, -st_poll() returns zero. -Note that timeouts are measured since the -last context switch. -

-

Returns
-Upon successful completion, a non-negative value is returned. A positive -value indicates the total number of OS file descriptors in pds -that have events. A value of 0 indicates that the call timed out. -Upon failure, a value of -1 is returned and errno is set -to indicate the error:

- - -
EINTRThe current thread was interrupted by -st_thread_interrupt().
-

-If an alternative event notification mechanism has been set by -st_set_eventsys(), other values of -errno could be set upon failure as well. The values -depend on the specific mechanism in use. -

-

Description
-This function returns as soon as I/O is ready on one or more of the specified -OS file descriptors. A count of the number of ready descriptors is returned -unless a timeout occurs, in which case zero is returned.

-The pollfd structure is defined in the poll.h header file -and contains the following members:

-

-    int fd;             /* OS file descriptor */
-    short events;       /* requested events   */
-    short revents;      /* returned events    */
-
-The events field should be set to the I/O events (readable, -writable, exception, or some combination) that the caller is interested in. -On return, the revents field is set to indicate what kind of I/O -is ready on the respective descriptor.

-The events and revents fields are constructed by OR-ing -any combination of the following event flags (defined in poll.h): -

- - - - - - -
POLLINfd is readable.
POLLOUTfd is is writable.
POLLPRIfd has an exception condition.
POLLNVALfd is bad.
-

-The POLLNVAL flag is only valid in the revents field; -it is not used in the events field.

-Despite having an interface like poll(2), this function uses -the same event notification mechanism as the rest of the library. For -instance if an alternative event nofication mechanism was set using st_set_eventsys(), this function uses that -mechanism to check for events.

-Note that unlike the poll(2) call, this function has the -timeout parameter expressed in microseconds. If the value of -timeout is ST_UTIME_NO_TIMEOUT -(-1), this function blocks until a requested I/O -event occurs or until the call is interrupted by -st_thread_interrupt(). -

-Note: if kqueue(2) is used as an alternative event -notification mechanism (see st_set_eventsys()), the POLLPRI -event flag is not supported and st_poll() will return an error -if it's set (errno will be set to EINVAL). -

-


-

- - -

Program Structure

- -

-Generally, the following steps should be followed when writing an application -using the State Threads library: -

-

    -
  1. Configure the library by calling these pre-init functions, if desired. - -
  2. -

    -

  3. Initialize the library by calling st_init().
  4. -

    -

  5. Configure the library by calling these post-init functions, if desired. - -
  6. -

    -

  7. Create resources that will be shared among different processes: - create and bind listening sockets (see socket(3), - bind(3), listen(3), - st_netfd_open_socket(), and possibly - st_netfd_serialize_accept()), - create shared memory segments, inter-process communication (IPC) - channels and synchronization primitives (if any).
  8. -

    -

  9. Create several processes via fork(2). The parent process should - either exit or become a "watchdog" (e.g., it starts a new process when - an existing one crashes, does a cleanup upon application termination, - etc.).
  10. -

    -

  11. In each child process create a pool of threads (see - st_thread_create()) to handle user - connections. Each thread in the pool may accept client connections - (st_accept()), connect to other servers - (st_connect()), perform various network I/O - (st_read(), st_write(), etc.).
  12. -
-

-Note that only State Threads library I/O functions should -be used for a network I/O: any other I/O calls may block the calling process -indefinitely. For example, standard I/O functions (fgets(3), -fread(3), fwrite(3), fprintf(3), etc.) call -read(2) and write(2) directly and therefore should not be -used on sockets or pipes. -

-Also note that for short timeouts to work the program -should do context switches (for example by calling -st_usleep()) on a regular basis. -

-


-

- - -

List of Blocking Functions

- -

-The thread context switch (process state change) can only happen -in a well-known set of blocking functions. -Only the following functions can block the calling thread: -

-

-
st_thread_join()
-
st_sleep()
-
st_usleep()
-
st_cond_wait()
-
st_cond_timedwait()
-
st_mutex_lock()
-
st_netfd_poll()
-
st_accept()
-
st_connect()
-
st_read()
-
st_read_fully()
-
st_read_resid()
-
st_readv()
-
st_readv_resid()
-
st_write()
-
st_write_resid()
-
st_writev()
-
st_writev_resid()
-
st_recvfrom()
-
st_sendto()
-
st_recvmsg()
-
st_sendmsg()
-
st_poll()
-
-

-


-

- - - - diff --git a/trunk/3rdparty/st-srs/docs/st.html b/trunk/3rdparty/st-srs/docs/st.html deleted file mode 100644 index a6b932a81..000000000 --- a/trunk/3rdparty/st-srs/docs/st.html +++ /dev/null @@ -1,504 +0,0 @@ - - -State Threads for Internet Applications - - -

State Threads for Internet Applications

-

Introduction

-

-State Threads is an application library which provides a -foundation for writing fast and highly scalable Internet Applications -on UNIX-like platforms. It combines the simplicity of the multithreaded -programming paradigm, in which one thread supports each simultaneous -connection, with the performance and scalability of an event-driven -state machine architecture.

- -

1. Definitions

-

- -

1.1 Internet Applications

- -

-An Internet Application (IA) is either a server or client network -application that accepts connections from clients and may or may not -connect to servers. In an IA the arrival or departure of network data -often controls processing (that is, IA is a data-driven application). -For each connection, an IA does some finite amount of work -involving data exchange with its peer, where its peer may be either -a client or a server. -The typical transaction steps of an IA are to accept a connection, -read a request, do some finite and predictable amount of work to -process the request, then write a response to the peer that sent the -request. One example of an IA is a Web server; -the most general example of an IA is a proxy server, because it both -accepts connections from clients and connects to other servers.

-

-We assume that the performance of an IA is constrained by available CPU -cycles rather than network bandwidth or disk I/O (that is, CPU -is a bottleneck resource). -

- - -

1.2 Performance and Scalability

- -

-The performance of an IA is usually evaluated as its -throughput measured in transactions per second or bytes per second (one -can be converted to the other, given the average transaction size). There are -several benchmarks that can be used to measure throughput of Web serving -applications for specific workloads (such as -SPECweb96, -WebStone, -WebBench). -Although there is no common definition for scalability, in general it -expresses the ability of an application to sustain its performance when some -external condition changes. For IAs this external condition is either the -number of clients (also known as "users," "simultaneous connections," or "load -generators") or the underlying hardware system size (number of CPUs, memory -size, and so on). Thus there are two types of scalability: load -scalability and system scalability, respectively. -

-The figure below shows how the throughput of an idealized IA changes with -the increasing number of clients (solid blue line). Initially the throughput -grows linearly (the slope represents the maximal throughput that one client -can provide). Within this initial range, the IA is underutilized and CPUs are -partially idle. Further increase in the number of clients leads to a system -saturation, and the throughput gradually stops growing as all CPUs become fully -utilized. After that point, the throughput stays flat because there are no -more CPU cycles available. -In the real world, however, each simultaneous connection -consumes some computational and memory resources, even when idle, and this -overhead grows with the number of clients. Therefore, the throughput of the -real world IA starts dropping after some point (dashed blue line in the figure -below). The rate at which the throughput drops depends, among other things, on -application design. -

-We say that an application has a good load scalability if it can -sustain its throughput over a wide range of loads. -Interestingly, the SPECweb99 -benchmark somewhat reflects the Web server's load scalability because it -measures the number of clients (load generators) given a mandatory minimal -throughput per client (that is, it measures the server's capacity). -This is unlike SPECweb96 and -other benchmarks that use the throughput as their main metric (see the figure -below). -

-

Figure: Throughput vs. Number of clients -
-

-System scalability is the ability of an application to sustain its -performance per hardware unit (such as a CPU) with the increasing number of -these units. In other words, good system scalability means that doubling the -number of processors will roughly double the application's throughput (dashed -green line). We assume here that the underlying operating system also scales -well. Good system scalability allows you to initially run an application on -the smallest system possible, while retaining the ability to move that -application to a larger system if necessary, without excessive effort or -expense. That is, an application need not be rewritten or even undergo a -major porting effort when changing system size. -

-Although scalability and performance are more important in the case of server -IAs, they should also be considered for some client applications (such as -benchmark load generators). -

- - -

1.3 Concurrency

- -

-Concurrency reflects the parallelism in a system. The two unrelated types -are virtual concurrency and real concurrency. -

    -
  • Virtual (or apparent) concurrency is the number of simultaneous -connections that a system supports. -

    -
  • Real concurrency is the number of hardware devices, including -CPUs, network cards, and disks, that actually allow a system to perform -tasks in parallel. -
-

-An IA must provide virtual concurrency in order to serve many users -simultaneously. -To achieve maximum performance and scalability in doing so, the number of -programming entities than an IA creates to be scheduled by the OS kernel -should be -kept close to (within an order of magnitude of) the real concurrency found on -the system. These programming entities scheduled by the kernel are known as -kernel execution vehicles. Examples of kernel execution vehicles -include Solaris lightweight processes and IRIX kernel threads. -In other words, the number of kernel execution vehicles should be dictated by -the system size and not by the number of simultaneous connections. -

- -

2. Existing Architectures

-

-There are a few different architectures that are commonly used by IAs. -These include the Multi-Process, -Multi-Threaded, and Event-Driven State Machine -architectures. -

- -

2.1 Multi-Process Architecture

- -

-In the Multi-Process (MP) architecture, an individual process is -dedicated to each simultaneous connection. -A process performs all of a transaction's initialization steps -and services a connection completely before moving on to service -a new connection. -

-User sessions in IAs are relatively independent; therefore, no -synchronization between processes handling different connections is -necessary. Because each process has its own private address space, -this architecture is very robust. If a process serving one of the connections -crashes, the other sessions will not be affected. However, to serve many -concurrent connections, an equal number of processes must be employed. -Because processes are kernel entities (and are in fact the heaviest ones), -the number of kernel entities will be at least as large as the number of -concurrent sessions. On most systems, good performance will not be achieved -when more than a few hundred processes are created because of the high -context-switching overhead. In other words, MP applications have poor load -scalability. -

-On the other hand, MP applications have very good system scalability, because -no resources are shared among different processes and there is no -synchronization overhead. -

-The Apache Web Server 1.x ([Reference 1]) uses the MP -architecture on UNIX systems. -

- -

2.2 Multi-Threaded Architecture

- -

-In the Multi-Threaded (MT) architecture, multiple independent threads -of control are employed within a single shared address space. Like a -process in the MP architecture, each thread performs all of a -transaction's initialization steps and services a connection completely -before moving on to service a new connection. -

-Many modern UNIX operating systems implement a many-to-few model when -mapping user-level threads to kernel entities. In this model, an -arbitrarily large number of user-level threads is multiplexed onto a -lesser number of kernel execution vehicles. Kernel execution -vehicles are also known as virtual processors. Whenever a user-level -thread makes a blocking system call, the kernel execution vehicle it is using -will become blocked in the kernel. If there are no other non-blocked kernel -execution vehicles and there are other runnable user-level threads, a new -kernel execution vehicle will be created automatically. This prevents the -application from blocking when it can continue to make useful forward -progress. -

-Because IAs are by nature network I/O driven, all concurrent sessions block on -network I/O at various points. As a result, the number of virtual processors -created in the kernel grows close to the number of user-level threads -(or simultaneous connections). When this occurs, the many-to-few model -effectively degenerates to a one-to-one model. Again, like in -the MP architecture, the number of kernel execution vehicles is dictated by -the number of simultaneous connections rather than by number of CPUs. This -reduces an application's load scalability. However, because kernel threads -(lightweight processes) use fewer resources and are more light-weight than -traditional UNIX processes, an MT application should scale better with load -than an MP application. -

-Unexpectedly, the small number of virtual processors sharing the same address -space in the MT architecture destroys an application's system scalability -because of contention among the threads on various locks. Even if an -application itself is carefully -optimized to avoid lock contention around its own global data (a non-trivial -task), there are still standard library functions and system calls -that use common resources hidden from the application. For example, -on many platforms thread safety of memory allocation routines -(malloc(3), free(3), and so on) is achieved by using a single -global lock. Another example is a per-process file descriptor table. -This common resource table is shared by all kernel execution vehicles within -the same process and must be protected when one modifies it via -certain system calls (such as open(2), close(2), and so on). -In addition to that, maintaining the caches coherent -among CPUs on multiprocessor systems hurts performance when different threads -running on different CPUs modify data items on the same cache line. -

-In order to improve load scalability, some applications employ a different -type of MT architecture: they create one or more thread(s) per task -rather than one thread per connection. For example, one small group -of threads may be responsible for accepting client connections, another -for request processing, and yet another for serving responses. The main -advantage of this architecture is that it eliminates the tight coupling -between the number of threads and number of simultaneous connections. However, -in this architecture, different task-specific thread groups must share common -work queues that must be protected by mutual exclusion locks (a typical -producer-consumer problem). This adds synchronization overhead that causes an -application to perform badly on multiprocessor systems. In other words, in -this architecture, the application's system scalability is sacrificed for the -sake of load scalability. -

-Of course, the usual nightmares of threaded programming, including data -corruption, deadlocks, and race conditions, also make MT architecture (in any -form) non-simplistic to use. -

- - -

2.3 Event-Driven State Machine Architecture

- -

-In the Event-Driven State Machine (EDSM) architecture, a single process -is employed to concurrently process multiple connections. The basics of this -architecture are described in Comer and Stevens -[Reference 2]. -The EDSM architecture performs one basic data-driven step associated with -a particular connection at a time, thus multiplexing many concurrent -connections. The process operates as a state machine that receives an event -and then reacts to it. -

-In the idle state the EDSM calls select(2) or poll(2) to -wait for network I/O events. When a particular file descriptor is ready for -I/O, the EDSM completes the corresponding basic step (usually by invoking a -handler function) and starts the next one. This architecture uses -non-blocking system calls to perform asynchronous network I/O operations. -For more details on non-blocking I/O see Stevens -[Reference 3]. -

-To take advantage of hardware parallelism (real concurrency), multiple -identical processes may be created. This is called Symmetric Multi-Process -EDSM and is used, for example, in the Zeus Web Server -([Reference 4]). To more efficiently multiplex disk I/O, -special "helper" processes may be created. This is called Asymmetric -Multi-Process EDSM and was proposed for Web servers by Druschel -and others [Reference 5]. -

-EDSM is probably the most scalable architecture for IAs. -Because the number of simultaneous connections (virtual concurrency) is -completely decoupled from the number of kernel execution vehicles (processes), -this architecture has very good load scalability. It requires only minimal -user-level resources to create and maintain additional connection. -

-Like MP applications, Multi-Process EDSM has very good system scalability -because no resources are shared among different processes and there is no -synchronization overhead. -

-Unfortunately, the EDSM architecture is monolithic rather than based on the -concept of threads, so new applications generally need to be implemented from -the ground up. In effect, the EDSM architecture simulates threads and their -stacks the hard way. -

- - -

3. State Threads Library

- -

-The State Threads library combines the advantages of all of the above -architectures. The interface preserves the programming simplicity of thread -abstraction, allowing each simultaneous connection to be treated as a separate -thread of execution within a single process. The underlying implementation is -close to the EDSM architecture as the state of each particular concurrent -session is saved in a separate memory segment. -

- -

3.1 State Changes and Scheduling

-

-The state of each concurrent session includes its stack environment -(stack pointer, program counter, CPU registers) and its stack. Conceptually, -a thread context switch can be viewed as a process changing its state. There -are no kernel entities involved other than processes. -Unlike other general-purpose threading libraries, the State Threads library -is fully deterministic. The thread context switch (process state change) can -only happen in a well-known set of functions (at I/O points or at explicit -synchronization points). As a result, process-specific global data does not -have to be protected by mutual exclusion locks in most cases. The entire -application is free to use all the static variables and non-reentrant library -functions it wants, greatly simplifying programming and debugging while -increasing performance. This is somewhat similar to a co-routine model -(co-operatively multitasked threads), except that no explicit yield is needed --- -sooner or later, a thread performs a blocking I/O operation and thus surrenders -control. All threads of execution (simultaneous connections) have the -same priority, so scheduling is non-preemptive, like in the EDSM architecture. -Because IAs are data-driven (processing is limited by the size of network -buffers and data arrival rates), scheduling is non-time-slicing. -

-Only two types of external events are handled by the library's -scheduler, because only these events can be detected by -select(2) or poll(2): I/O events (a file descriptor is ready -for I/O) and time events -(some timeout has expired). However, other types of events (such as -a signal sent to a process) can also be handled by converting them to I/O -events. For example, a signal handling function can perform a write to a pipe -(write(2) is reentrant/asynchronous-safe), thus converting a signal -event to an I/O event. -

-To take advantage of hardware parallelism, as in the EDSM architecture, -multiple processes can be created in either a symmetric or asymmetric manner. -Process management is not in the library's scope but instead is left up to the -application. -

-There are several general-purpose threading libraries that implement a -many-to-one model (many user-level threads to one kernel execution -vehicle), using the same basic techniques as the State Threads library -(non-blocking I/O, event-driven scheduler, and so on). For an example, see GNU -Portable Threads ([Reference 6]). Because they are -general-purpose, these libraries have different objectives than the State -Threads library. The State Threads library is not a general-purpose -threading library, -but rather an application library that targets only certain types of -applications (IAs) in order to achieve the highest possible performance and -scalability for those applications. -

- -

3.2 Scalability

-

-State threads are very lightweight user-level entities, and therefore creating -and maintaining user connections requires minimal resources. An application -using the State Threads library scales very well with the increasing number -of connections. -

-On multiprocessor systems an application should create multiple processes -to take advantage of hardware parallelism. Using multiple separate processes -is the only way to achieve the highest possible system scalability. -This is because duplicating per-process resources is the only way to avoid -significant synchronization overhead on multiprocessor systems. Creating -separate UNIX processes naturally offers resource duplication. Again, -as in the EDSM architecture, there is no connection between the number of -simultaneous connections (which may be very large and changes within a wide -range) and the number of kernel entities (which is usually small and constant). -In other words, the State Threads library makes it possible to multiplex a -large number of simultaneous connections onto a much smaller number of -separate processes, thus allowing an application to scale well with both -the load and system size. -

- -

3.3 Performance

-

-Performance is one of the library's main objectives. The State Threads -library is implemented to minimize the number of system calls and -to make thread creation and context switching as fast as possible. -For example, per-thread signal mask does not exist (unlike -POSIX threads), so there is no need to save and restore a process's -signal mask on every thread context switch. This eliminates two system -calls per context switch. Signal events can be handled much more -efficiently by converting them to I/O events (see above). -

- -

3.4 Portability

-

-The library uses the same general, underlying concepts as the EDSM -architecture, including non-blocking I/O, file descriptors, and -I/O multiplexing. These concepts are available in some form on most -UNIX platforms, making the library very portable across many -flavors of UNIX. There are only a few platform-dependent sections in the -source. -

- -

3.5 State Threads and NSPR

-

-The State Threads library is a derivative of the Netscape Portable -Runtime library (NSPR) [Reference 7]. The primary goal of -NSPR is to provide a platform-independent layer for system facilities, -where system facilities include threads, thread synchronization, and I/O. -Performance and scalability are not the main concern of NSPR. The -State Threads library addresses performance and scalability while -remaining much smaller than NSPR. It is contained in 8 source files -as opposed to more than 400, but provides all the functionality that -is needed to write efficient IAs on UNIX-like platforms. -

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
NSPRState Threads
Lines of code~150,000~3000
Dynamic library size  
(debug version)
IRIX~700 KB~60 KB
Linux~900 KB~70 KB
-

- -

Conclusion

-

-State Threads is an application library which provides a foundation for -writing Internet Applications. To summarize, it has the -following advantages: -

-

    -
  • It allows the design of fast and highly scalable applications. An -application will scale well with both load and number of CPUs. -

    -

  • It greatly simplifies application programming and debugging because, as a -rule, no mutual exclusion locking is necessary and the entire application is -free to use static variables and non-reentrant library functions. -
-

-The library's main limitation: -

-

    -
  • All I/O operations on sockets must use the State Thread library's I/O -functions because only those functions perform thread scheduling and prevent -the application's processes from blocking. -
-

- -

References

-
    - -
  1. Apache Software Foundation, -http://www.apache.org. - -
  2. Douglas E. Comer, David L. Stevens, Internetworking With TCP/IP, -Vol. III: Client-Server Programming And Applications, Second Edition, -Ch. 8, 12. - -
  3. W. Richard Stevens, UNIX Network Programming, Second Edition, -Vol. 1, Ch. 15. - -
  4. Zeus Technology Limited, -http://www.zeus.co.uk. - -
  5. Peter Druschel, Vivek S. Pai, Willy Zwaenepoel, - -Flash: An Efficient and Portable Web Server. In Proceedings of the -USENIX 1999 Annual Technical Conference, Monterey, CA, June 1999. - -
  6. GNU Portable Threads, -http://www.gnu.org/software/pth/. - -
  7. Netscape Portable Runtime, -http://www.mozilla.org/docs/refList/refNSPR/. -
- -

Other resources covering various architectural issues in IAs

-
    -
  1. Dan Kegel, The C10K problem, -http://www.kegel.com/c10k.html. -
  2. -
  3. James C. Hu, Douglas C. Schmidt, Irfan Pyarali, JAWS: Understanding -High Performance Web Systems, -http://www.cs.wustl.edu/~jxh/research/research.html.
  4. -
-

-


-

- -

Portions created by SGI are Copyright © 2000 -Silicon Graphics, Inc. All rights reserved.
-

- - - - diff --git a/trunk/3rdparty/st-srs/docs/timeout_heap.txt b/trunk/3rdparty/st-srs/docs/timeout_heap.txt deleted file mode 100644 index 1582dc129..000000000 --- a/trunk/3rdparty/st-srs/docs/timeout_heap.txt +++ /dev/null @@ -1,60 +0,0 @@ -How the timeout heap works - -As of version 1.5, the State Threads Library represents the queue of -sleeping threads using a heap data structure rather than a sorted -linked list. This improves performance when there is a large number -of sleeping threads, since insertion into a heap takes O(log N) time -while insertion into a sorted list takes O(N) time. For example, in -one test 1000 threads were created, each thread called st_usleep() -with a random time interval, and then all the threads where -immediately interrupted and joined before the sleeps had a chance to -finish. The whole process was repeated 1000 times, for a total of a -million sleep queue insertions and removals. With the old list-based -sleep queue, this test took 100 seconds; now it takes only 12 seconds. - -Heap data structures are typically based on dynamically resized -arrays. However, since the existing ST code base was very nicely -structured around linking the thread objects into pointer-based lists -without the need for any auxiliary data structures, implementing the -heap using a similar nodes-and-pointers based approach seemed more -appropriate for ST than introducing a separate array. - -Thus, the new ST timeout heap works by organizing the existing -_st_thread_t objects in a balanced binary tree, just as they were -previously organized into a doubly-linked, sorted list. The global -_ST_SLEEPQ variable, formerly a linked list head, is now simply a -pointer to the root of this tree, and the root node of the tree is the -thread with the earliest timeout. Each thread object has two child -pointers, "left" and "right", pointing to threads with later timeouts. - -Each node in the tree is numbered with an integer index, corresponding -to the array index in an array-based heap, and the tree is kept fully -balanced and left-adjusted at all times. In other words, the tree -consists of any number of fully populated top levels, followed by a -single bottom level which may be partially populated, such that any -existing nodes form a contiguous block to the left and the spaces for -missing nodes form a contiguous block to the right. For example, if -there are nine threads waiting for a timeout, they are numbered and -arranged in a tree exactly as follows: - - 1 - / \ - 2 3 - / \ / \ - 4 5 6 7 - / \ - 8 9 - -Each node has either no children, only a left child, or both a left -and a right child. Children always time out later than their parents -(this is called the "heap invariant"), but when a node has two -children, their mutual order is unspecified - the left child may time -out before or after the right child. If a node is numbered N, its -left child is numbered 2N, and its right child is numbered 2N+1. - -There is no pointer from a child to its parent; all pointers point -downward. Additions and deletions both work by starting at the root -and traversing the tree towards the leaves, going left or right -according to the binary digits forming the index of the destination -node. As nodes are added or deleted, existing nodes are rearranged to -maintain the heap invariant. diff --git a/trunk/3rdparty/st-srs/event.c b/trunk/3rdparty/st-srs/event.c deleted file mode 100644 index 4fea5986e..000000000 --- a/trunk/3rdparty/st-srs/event.c +++ /dev/null @@ -1,1253 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * Yahoo! Inc. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -#ifdef MD_HAVE_KQUEUE -#include -#endif -#ifdef MD_HAVE_EPOLL -#include -#endif - -// Global stat. -#if defined(DEBUG) && defined(DEBUG_STATS) -unsigned long long _st_stat_epoll = 0; -unsigned long long _st_stat_epoll_zero = 0; -unsigned long long _st_stat_epoll_shake = 0; -unsigned long long _st_stat_epoll_spin = 0; -#endif - -#if !defined(MD_HAVE_KQUEUE) && !defined(MD_HAVE_EPOLL) && !defined(MD_HAVE_SELECT) - #error Only support epoll(for Linux), kqueue(for Darwin) or select(for Cygwin) -#endif - - -#ifdef MD_HAVE_SELECT -static struct _st_seldata { - fd_set fd_read_set, fd_write_set, fd_exception_set; - int fd_ref_cnts[FD_SETSIZE][3]; - int maxfd; -} *_st_select_data; - -#define _ST_SELECT_MAX_OSFD (_st_select_data->maxfd) -#define _ST_SELECT_READ_SET (_st_select_data->fd_read_set) -#define _ST_SELECT_WRITE_SET (_st_select_data->fd_write_set) -#define _ST_SELECT_EXCEP_SET (_st_select_data->fd_exception_set) -#define _ST_SELECT_READ_CNT(fd) (_st_select_data->fd_ref_cnts[fd][0]) -#define _ST_SELECT_WRITE_CNT(fd) (_st_select_data->fd_ref_cnts[fd][1]) -#define _ST_SELECT_EXCEP_CNT(fd) (_st_select_data->fd_ref_cnts[fd][2]) -#endif - - -#ifdef MD_HAVE_KQUEUE -typedef struct _kq_fd_data { - int rd_ref_cnt; - int wr_ref_cnt; - int revents; -} _kq_fd_data_t; - -static struct _st_kqdata { - _kq_fd_data_t *fd_data; - struct kevent *evtlist; - struct kevent *addlist; - struct kevent *dellist; - int fd_data_size; - int evtlist_size; - int addlist_size; - int addlist_cnt; - int dellist_size; - int dellist_cnt; - int kq; - pid_t pid; -} *_st_kq_data; - -#ifndef ST_KQ_MIN_EVTLIST_SIZE -#define ST_KQ_MIN_EVTLIST_SIZE 64 -#endif - -#define _ST_KQ_READ_CNT(fd) (_st_kq_data->fd_data[fd].rd_ref_cnt) -#define _ST_KQ_WRITE_CNT(fd) (_st_kq_data->fd_data[fd].wr_ref_cnt) -#define _ST_KQ_REVENTS(fd) (_st_kq_data->fd_data[fd].revents) -#endif /* MD_HAVE_KQUEUE */ - - -#ifdef MD_HAVE_EPOLL -typedef struct _epoll_fd_data { - int rd_ref_cnt; - int wr_ref_cnt; - int ex_ref_cnt; - int revents; -} _epoll_fd_data_t; - -static struct _st_epolldata { - _epoll_fd_data_t *fd_data; - struct epoll_event *evtlist; - int fd_data_size; - int evtlist_size; - int evtlist_cnt; - int fd_hint; - int epfd; -} *_st_epoll_data; - -#ifndef ST_EPOLL_EVTLIST_SIZE - /* Not a limit, just a hint */ - #define ST_EPOLL_EVTLIST_SIZE 4096 -#endif - -#define _ST_EPOLL_READ_CNT(fd) (_st_epoll_data->fd_data[fd].rd_ref_cnt) -#define _ST_EPOLL_WRITE_CNT(fd) (_st_epoll_data->fd_data[fd].wr_ref_cnt) -#define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt) -#define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents) - -#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0) -#define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0) -#define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0) -#define _ST_EPOLL_EVENTS(fd) \ - (_ST_EPOLL_READ_BIT(fd)|_ST_EPOLL_WRITE_BIT(fd)|_ST_EPOLL_EXCEP_BIT(fd)) - -#endif /* MD_HAVE_EPOLL */ - -_st_eventsys_t *_st_eventsys = NULL; - - -#ifdef MD_HAVE_SELECT -/***************************************** - * select event system - */ - -ST_HIDDEN int _st_select_init(void) -{ - _st_select_data = (struct _st_seldata *) malloc(sizeof(*_st_select_data)); - if (!_st_select_data) - return -1; - - memset(_st_select_data, 0, sizeof(*_st_select_data)); - _st_select_data->maxfd = -1; - - return 0; -} - -ST_HIDDEN int _st_select_pollset_add(struct pollfd *pds, int npds) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* Do checks up front */ - for (pd = pds; pd < epd; pd++) { - if (pd->fd < 0 || pd->fd >= FD_SETSIZE || !pd->events || - (pd->events & ~(POLLIN | POLLOUT | POLLPRI))) { - errno = EINVAL; - return -1; - } - } - - for (pd = pds; pd < epd; pd++) { - if (pd->events & POLLIN) { - FD_SET(pd->fd, &_ST_SELECT_READ_SET); - _ST_SELECT_READ_CNT(pd->fd)++; - } - if (pd->events & POLLOUT) { - FD_SET(pd->fd, &_ST_SELECT_WRITE_SET); - _ST_SELECT_WRITE_CNT(pd->fd)++; - } - if (pd->events & POLLPRI) { - FD_SET(pd->fd, &_ST_SELECT_EXCEP_SET); - _ST_SELECT_EXCEP_CNT(pd->fd)++; - } - if (_ST_SELECT_MAX_OSFD < pd->fd) - _ST_SELECT_MAX_OSFD = pd->fd; - } - - return 0; -} - -ST_HIDDEN void _st_select_pollset_del(struct pollfd *pds, int npds) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - for (pd = pds; pd < epd; pd++) { - if (pd->events & POLLIN) { - if (--_ST_SELECT_READ_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_READ_SET); - } - if (pd->events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_WRITE_SET); - } - if (pd->events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_EXCEP_SET); - } - } -} - -ST_HIDDEN void _st_select_find_bad_fd(void) -{ - _st_clist_t *q; - _st_pollq_t *pq; - int notify; - struct pollfd *pds, *epds; - int pq_max_osfd, osfd; - short events; - - _ST_SELECT_MAX_OSFD = -1; - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - pq_max_osfd = -1; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - pds->revents = 0; - if (pds->events == 0) - continue; - if (fcntl(osfd, F_GETFL, 0) < 0) { - pds->revents = POLLNVAL; - notify = 1; - } - if (osfd > pq_max_osfd) { - pq_max_osfd = osfd; - } - } - - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Decrement the count of descriptors for each descriptor/event - * because this I/O request is being removed from the ioq - */ - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - if (events & POLLIN) { - if (--_ST_SELECT_READ_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_READ_SET); - } - } - if (events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_WRITE_SET); - } - } - if (events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); - } - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } else { - if (_ST_SELECT_MAX_OSFD < pq_max_osfd) - _ST_SELECT_MAX_OSFD = pq_max_osfd; - } - } -} - -ST_HIDDEN void _st_select_dispatch(void) -{ - struct timeval timeout, *tvp; - fd_set r, w, e; - fd_set *rp, *wp, *ep; - int nfd, pq_max_osfd, osfd; - _st_clist_t *q; - st_utime_t min_timeout; - _st_pollq_t *pq; - int notify; - struct pollfd *pds, *epds; - short events, revents; - - /* - * Assignment of fd_sets - */ - r = _ST_SELECT_READ_SET; - w = _ST_SELECT_WRITE_SET; - e = _ST_SELECT_EXCEP_SET; - - rp = &r; - wp = &w; - ep = &e; - - if (_ST_SLEEPQ == NULL) { - tvp = NULL; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : - (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout.tv_sec = (int) (min_timeout / 1000000); - timeout.tv_usec = (int) (min_timeout % 1000000); - tvp = &timeout; - } - - /* Check for I/O operations */ - nfd = select(_ST_SELECT_MAX_OSFD + 1, rp, wp, ep, tvp); - - /* Notify threads that are associated with the selected descriptors */ - if (nfd > 0) { - _ST_SELECT_MAX_OSFD = -1; - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - pq_max_osfd = -1; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - revents = 0; - if ((events & POLLIN) && FD_ISSET(osfd, rp)) { - revents |= POLLIN; - } - if ((events & POLLOUT) && FD_ISSET(osfd, wp)) { - revents |= POLLOUT; - } - if ((events & POLLPRI) && FD_ISSET(osfd, ep)) { - revents |= POLLPRI; - } - pds->revents = revents; - if (revents) { - notify = 1; - } - if (osfd > pq_max_osfd) { - pq_max_osfd = osfd; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Decrement the count of descriptors for each descriptor/event - * because this I/O request is being removed from the ioq - */ - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - if (events & POLLIN) { - if (--_ST_SELECT_READ_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_READ_SET); - } - } - if (events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_WRITE_SET); - } - } - if (events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); - } - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } else { - if (_ST_SELECT_MAX_OSFD < pq_max_osfd) - _ST_SELECT_MAX_OSFD = pq_max_osfd; - } - } - } else if (nfd < 0) { - /* - * It can happen when a thread closes file descriptor - * that is being used by some other thread -- BAD! - */ - if (errno == EBADF) - _st_select_find_bad_fd(); - } -} - -ST_HIDDEN int _st_select_fd_new(int osfd) -{ - if (osfd >= FD_SETSIZE) { - errno = EMFILE; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_select_fd_close(int osfd) -{ - if (_ST_SELECT_READ_CNT(osfd) || _ST_SELECT_WRITE_CNT(osfd) || - _ST_SELECT_EXCEP_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_select_fd_getlimit(void) -{ - return FD_SETSIZE; -} - -static _st_eventsys_t _st_select_eventsys = { - "select", - ST_EVENTSYS_SELECT, - _st_select_init, - _st_select_dispatch, - _st_select_pollset_add, - _st_select_pollset_del, - _st_select_fd_new, - _st_select_fd_close, - _st_select_fd_getlimit -}; -#endif - - -#ifdef MD_HAVE_KQUEUE -/***************************************** - * kqueue event system - */ - -ST_HIDDEN int _st_kq_init(void) -{ - int err = 0; - int rv = 0; - - _st_kq_data = (struct _st_kqdata *) calloc(1, sizeof(*_st_kq_data)); - if (!_st_kq_data) - return -1; - - if ((_st_kq_data->kq = kqueue()) < 0) { - err = errno; - rv = -1; - goto cleanup_kq; - } - fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC); - _st_kq_data->pid = getpid(); - - /* - * Allocate file descriptor data array. - * FD_SETSIZE looks like good initial size. - */ - _st_kq_data->fd_data_size = FD_SETSIZE; - _st_kq_data->fd_data = (_kq_fd_data_t *)calloc(_st_kq_data->fd_data_size, sizeof(_kq_fd_data_t)); - if (!_st_kq_data->fd_data) { - err = errno; - rv = -1; - goto cleanup_kq; - } - - /* Allocate event lists */ - _st_kq_data->evtlist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->evtlist = (struct kevent *)malloc(_st_kq_data->evtlist_size * sizeof(struct kevent)); - _st_kq_data->addlist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->addlist = (struct kevent *)malloc(_st_kq_data->addlist_size * sizeof(struct kevent)); - _st_kq_data->dellist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->dellist = (struct kevent *)malloc(_st_kq_data->dellist_size * sizeof(struct kevent)); - if (!_st_kq_data->evtlist || !_st_kq_data->addlist || - !_st_kq_data->dellist) { - err = ENOMEM; - rv = -1; - } - - cleanup_kq: - if (rv < 0) { - if (_st_kq_data->kq >= 0) - close(_st_kq_data->kq); - free(_st_kq_data->fd_data); - free(_st_kq_data->evtlist); - free(_st_kq_data->addlist); - free(_st_kq_data->dellist); - free(_st_kq_data); - _st_kq_data = NULL; - errno = err; - } - - return rv; -} - -ST_HIDDEN int _st_kq_fd_data_expand(int maxfd) -{ - _kq_fd_data_t *ptr; - int n = _st_kq_data->fd_data_size; - - while (maxfd >= n) - n <<= 1; - - ptr = (_kq_fd_data_t *)realloc(_st_kq_data->fd_data, n * sizeof(_kq_fd_data_t)); - if (!ptr) - return -1; - - memset(ptr + _st_kq_data->fd_data_size, 0, (n - _st_kq_data->fd_data_size) * sizeof(_kq_fd_data_t)); - - _st_kq_data->fd_data = ptr; - _st_kq_data->fd_data_size = n; - - return 0; -} - -ST_HIDDEN int _st_kq_addlist_expand(int avail) -{ - struct kevent *ptr; - int n = _st_kq_data->addlist_size; - - while (avail > n - _st_kq_data->addlist_cnt) - n <<= 1; - - ptr = (struct kevent *)realloc(_st_kq_data->addlist, n * sizeof(struct kevent)); - if (!ptr) - return -1; - - _st_kq_data->addlist = ptr; - _st_kq_data->addlist_size = n; - - /* - * Try to expand the result event list too - * (although we don't have to do it). - */ - ptr = (struct kevent *)realloc(_st_kq_data->evtlist, n * sizeof(struct kevent)); - if (ptr) { - _st_kq_data->evtlist = ptr; - _st_kq_data->evtlist_size = n; - } - - return 0; -} - -ST_HIDDEN void _st_kq_addlist_add(const struct kevent *kev) -{ - ST_ASSERT(_st_kq_data->addlist_cnt < _st_kq_data->addlist_size); - memcpy(_st_kq_data->addlist + _st_kq_data->addlist_cnt, kev, sizeof(struct kevent)); - _st_kq_data->addlist_cnt++; -} - -ST_HIDDEN void _st_kq_dellist_add(const struct kevent *kev) -{ - int n = _st_kq_data->dellist_size; - - if (_st_kq_data->dellist_cnt >= n) { - struct kevent *ptr; - - n <<= 1; - ptr = (struct kevent *)realloc(_st_kq_data->dellist, n * sizeof(struct kevent)); - if (!ptr) { - /* See comment in _st_kq_pollset_del() */ - return; - } - - _st_kq_data->dellist = ptr; - _st_kq_data->dellist_size = n; - } - - memcpy(_st_kq_data->dellist + _st_kq_data->dellist_cnt, kev, sizeof(struct kevent)); - _st_kq_data->dellist_cnt++; -} - -ST_HIDDEN int _st_kq_pollset_add(struct pollfd *pds, int npds) -{ - struct kevent kev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* - * Pollset adding is "atomic". That is, either it succeeded for - * all descriptors in the set or it failed. It means that we - * need to do all the checks up front so we don't have to - * "unwind" if adding of one of the descriptors failed. - */ - for (pd = pds; pd < epd; pd++) { - /* POLLIN and/or POLLOUT must be set, but nothing else */ - if (pd->fd < 0 || !pd->events || (pd->events & ~(POLLIN | POLLOUT))) { - errno = EINVAL; - return -1; - } - if (pd->fd >= _st_kq_data->fd_data_size && - _st_kq_fd_data_expand(pd->fd) < 0) - return -1; - } - - /* - * Make sure we have enough room in the addlist for twice as many - * descriptors as in the pollset (for both READ and WRITE filters). - */ - npds <<= 1; - if (npds > _st_kq_data->addlist_size - _st_kq_data->addlist_cnt && _st_kq_addlist_expand(npds) < 0) - return -1; - - for (pd = pds; pd < epd; pd++) { - if ((pd->events & POLLIN) && (_ST_KQ_READ_CNT(pd->fd)++ == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_READ; -#ifdef NOTE_EOF - /* Make it behave like select() and poll() */ - kev.fflags = NOTE_EOF; -#endif - kev.flags = (EV_ADD | EV_ONESHOT); - _st_kq_addlist_add(&kev); - } - if ((pd->events & POLLOUT) && (_ST_KQ_WRITE_CNT(pd->fd)++ == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_WRITE; - kev.flags = (EV_ADD | EV_ONESHOT); - _st_kq_addlist_add(&kev); - } - } - - return 0; -} - -ST_HIDDEN void _st_kq_pollset_del(struct pollfd *pds, int npds) -{ - struct kevent kev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* - * It's OK if deleting fails because a descriptor will either be - * closed or fire only once (we set EV_ONESHOT flag). - */ - _st_kq_data->dellist_cnt = 0; - for (pd = pds; pd < epd; pd++) { - if ((pd->events & POLLIN) && (--_ST_KQ_READ_CNT(pd->fd) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_READ; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - if ((pd->events & POLLOUT) && (--_ST_KQ_WRITE_CNT(pd->fd) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_WRITE; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - } - - if (_st_kq_data->dellist_cnt > 0) { - /* - * We do "synchronous" kqueue deletes to avoid deleting - * closed descriptors and other possible problems. - */ - int rv; - do { - /* This kevent() won't block since result list size is 0 */ - rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL); - } while (rv < 0 && errno == EINTR); - } -} - -ST_HIDDEN void _st_kq_dispatch(void) -{ - struct timespec timeout, *tsp; - struct kevent kev; - st_utime_t min_timeout; - _st_clist_t *q; - _st_pollq_t *pq; - struct pollfd *pds, *epds; - int nfd, i, osfd, notify, filter; - short events, revents; - - if (_ST_SLEEPQ == NULL) { - tsp = NULL; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout.tv_sec = (time_t) (min_timeout / 1000000); - timeout.tv_nsec = (long) ((min_timeout % 1000000) * 1000); - tsp = &timeout; - } - - retry_kevent: - /* Check for I/O operations */ - nfd = kevent(_st_kq_data->kq, - _st_kq_data->addlist, _st_kq_data->addlist_cnt, - _st_kq_data->evtlist, _st_kq_data->evtlist_size, tsp); - - _st_kq_data->addlist_cnt = 0; - - if (nfd > 0) { - for (i = 0; i < nfd; i++) { - osfd = _st_kq_data->evtlist[i].ident; - filter = _st_kq_data->evtlist[i].filter; - - if (filter == EVFILT_READ) { - _ST_KQ_REVENTS(osfd) |= POLLIN; - } else if (filter == EVFILT_WRITE) { - _ST_KQ_REVENTS(osfd) |= POLLOUT; - } - if (_st_kq_data->evtlist[i].flags & EV_ERROR) { - if (_st_kq_data->evtlist[i].data == EBADF) { - _ST_KQ_REVENTS(osfd) |= POLLNVAL; - } else { - _ST_KQ_REVENTS(osfd) |= POLLERR; - } - } - } - - _st_kq_data->dellist_cnt = 0; - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - revents = (short)(_ST_KQ_REVENTS(osfd) & ~(POLLIN | POLLOUT)); - if ((events & POLLIN) && (_ST_KQ_REVENTS(osfd) & POLLIN)) { - revents |= POLLIN; - } - if ((events & POLLOUT) && (_ST_KQ_REVENTS(osfd) & POLLOUT)) { - revents |= POLLOUT; - } - pds->revents = revents; - if (revents) { - notify = 1; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - /* - * We set EV_ONESHOT flag so we only need to delete - * descriptor if it didn't fire. - */ - if ((events & POLLIN) && (--_ST_KQ_READ_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLIN) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = osfd; - kev.filter = EVFILT_READ; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - if ((events & POLLOUT) && (--_ST_KQ_WRITE_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLOUT) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = osfd; - kev.filter = EVFILT_WRITE; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } - } - - if (_st_kq_data->dellist_cnt > 0) { - int rv; - do { - /* This kevent() won't block since result list size is 0 */ - rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL); - } while (rv < 0 && errno == EINTR); - } - - for (i = 0; i < nfd; i++) { - osfd = _st_kq_data->evtlist[i].ident; - _ST_KQ_REVENTS(osfd) = 0; - } - } else if (nfd < 0) { - if (errno == EBADF && _st_kq_data->pid != getpid()) { - /* We probably forked, reinitialize kqueue */ - if ((_st_kq_data->kq = kqueue()) < 0) { - /* There is nothing we can do here, will retry later */ - return; - } - fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC); - _st_kq_data->pid = getpid(); - /* Re-register all descriptors on ioq with new kqueue */ - memset(_st_kq_data->fd_data, 0, _st_kq_data->fd_data_size * sizeof(_kq_fd_data_t)); - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - _st_kq_pollset_add(pq->pds, pq->npds); - } - goto retry_kevent; - } - } -} - -ST_HIDDEN int _st_kq_fd_new(int osfd) -{ - if (osfd >= _st_kq_data->fd_data_size && _st_kq_fd_data_expand(osfd) < 0) - return -1; - - return 0; -} - -ST_HIDDEN int _st_kq_fd_close(int osfd) -{ - if (_ST_KQ_READ_CNT(osfd) || _ST_KQ_WRITE_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_kq_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -static _st_eventsys_t _st_kq_eventsys = { - "kqueue", - ST_EVENTSYS_ALT, - _st_kq_init, - _st_kq_dispatch, - _st_kq_pollset_add, - _st_kq_pollset_del, - _st_kq_fd_new, - _st_kq_fd_close, - _st_kq_fd_getlimit -}; -#endif /* MD_HAVE_KQUEUE */ - - -#ifdef MD_HAVE_EPOLL -/***************************************** - * epoll event system - */ - -ST_HIDDEN int _st_epoll_init(void) -{ - int fdlim; - int err = 0; - int rv = 0; - - _st_epoll_data = (struct _st_epolldata *) calloc(1, sizeof(*_st_epoll_data)); - if (!_st_epoll_data) - return -1; - - fdlim = st_getfdlimit(); - _st_epoll_data->fd_hint = (fdlim > 0 && fdlim < ST_EPOLL_EVTLIST_SIZE) ? fdlim : ST_EPOLL_EVTLIST_SIZE; - - if ((_st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint)) < 0) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC); - - /* Allocate file descriptor data array */ - _st_epoll_data->fd_data_size = _st_epoll_data->fd_hint; - _st_epoll_data->fd_data = (_epoll_fd_data_t *)calloc(_st_epoll_data->fd_data_size, sizeof(_epoll_fd_data_t)); - if (!_st_epoll_data->fd_data) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - - /* Allocate event lists */ - _st_epoll_data->evtlist_size = _st_epoll_data->fd_hint; - _st_epoll_data->evtlist = (struct epoll_event *)malloc(_st_epoll_data->evtlist_size * sizeof(struct epoll_event)); - if (!_st_epoll_data->evtlist) { - err = errno; - rv = -1; - } - - cleanup_epoll: - if (rv < 0) { - if (_st_epoll_data->epfd >= 0) - close(_st_epoll_data->epfd); - free(_st_epoll_data->fd_data); - free(_st_epoll_data->evtlist); - free(_st_epoll_data); - _st_epoll_data = NULL; - errno = err; - } - - return rv; -} - -ST_HIDDEN int _st_epoll_fd_data_expand(int maxfd) -{ - _epoll_fd_data_t *ptr; - int n = _st_epoll_data->fd_data_size; - - while (maxfd >= n) - n <<= 1; - - ptr = (_epoll_fd_data_t *)realloc(_st_epoll_data->fd_data, n * sizeof(_epoll_fd_data_t)); - if (!ptr) - return -1; - - memset(ptr + _st_epoll_data->fd_data_size, 0, (n - _st_epoll_data->fd_data_size) * sizeof(_epoll_fd_data_t)); - - _st_epoll_data->fd_data = ptr; - _st_epoll_data->fd_data_size = n; - - return 0; -} - -ST_HIDDEN void _st_epoll_evtlist_expand(void) -{ - struct epoll_event *ptr; - int n = _st_epoll_data->evtlist_size; - - while (_st_epoll_data->evtlist_cnt > n) - n <<= 1; - - ptr = (struct epoll_event *)realloc(_st_epoll_data->evtlist, n * sizeof(struct epoll_event)); - if (ptr) { - _st_epoll_data->evtlist = ptr; - _st_epoll_data->evtlist_size = n; - } -} - -ST_HIDDEN void _st_epoll_pollset_del(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - int old_events, events, op; - - /* - * It's more or less OK if deleting fails because a descriptor - * will either be closed or deleted in dispatch function after - * it fires. - */ - for (pd = pds; pd < epd; pd++) { - old_events = _ST_EPOLL_EVENTS(pd->fd); - - if (pd->events & POLLIN) - _ST_EPOLL_READ_CNT(pd->fd)--; - if (pd->events & POLLOUT) - _ST_EPOLL_WRITE_CNT(pd->fd)--; - if (pd->events & POLLPRI) - _ST_EPOLL_EXCEP_CNT(pd->fd)--; - - events = _ST_EPOLL_EVENTS(pd->fd); - /* - * The _ST_EPOLL_REVENTS check below is needed so we can use - * this function inside dispatch(). Outside of dispatch() - * _ST_EPOLL_REVENTS is always zero for all descriptors. - */ - if (events != old_events && _ST_EPOLL_REVENTS(pd->fd) == 0) { - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = pd->fd; - if (epoll_ctl(_st_epoll_data->epfd, op, pd->fd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - int i, fd; - int old_events, events, op; - - /* Do as many checks as possible up front */ - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - if (fd < 0 || !pds[i].events || - (pds[i].events & ~(POLLIN | POLLOUT | POLLPRI))) { - errno = EINVAL; - return -1; - } - if (fd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(fd) < 0) - return -1; - } - - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - old_events = _ST_EPOLL_EVENTS(fd); - - if (pds[i].events & POLLIN) - _ST_EPOLL_READ_CNT(fd)++; - if (pds[i].events & POLLOUT) - _ST_EPOLL_WRITE_CNT(fd)++; - if (pds[i].events & POLLPRI) - _ST_EPOLL_EXCEP_CNT(fd)++; - - events = _ST_EPOLL_EVENTS(fd); - if (events != old_events) { - op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; - ev.events = events; - ev.data.fd = fd; - if (epoll_ctl(_st_epoll_data->epfd, op, fd, &ev) < 0 && (op != EPOLL_CTL_ADD || errno != EEXIST)) - break; - if (op == EPOLL_CTL_ADD) { - _st_epoll_data->evtlist_cnt++; - if (_st_epoll_data->evtlist_cnt > _st_epoll_data->evtlist_size) - _st_epoll_evtlist_expand(); - } - } - } - - if (i < npds) { - /* Error */ - int err = errno; - /* Unroll the state */ - _st_epoll_pollset_del(pds, i + 1); - errno = err; - return -1; - } - - return 0; -} - -ST_HIDDEN void _st_epoll_dispatch(void) -{ - st_utime_t min_timeout; - _st_clist_t *q; - _st_pollq_t *pq; - struct pollfd *pds, *epds; - struct epoll_event ev; - int timeout, nfd, i, osfd, notify; - int events, op; - short revents; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_epoll; - #endif - - if (_ST_SLEEPQ == NULL) { - timeout = -1; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout = (int) (min_timeout / 1000); - - // At least wait 1ms when <1ms, to avoid epoll_wait spin loop. - if (timeout == 0) { - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_epoll_zero; - #endif - - if (min_timeout > 0) { - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_epoll_shake; - #endif - - timeout = 1; - } - } - } - - /* Check for I/O operations */ - nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout); - - #if defined(DEBUG) && defined(DEBUG_STATS) - if (nfd <= 0) { - ++_st_stat_epoll_spin; - } - #endif - - if (nfd > 0) { - for (i = 0; i < nfd; i++) { - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = _st_epoll_data->evtlist[i].events; - if (_ST_EPOLL_REVENTS(osfd) & (EPOLLERR | EPOLLHUP)) { - /* Also set I/O bits on error */ - _ST_EPOLL_REVENTS(osfd) |= _ST_EPOLL_EVENTS(osfd); - } - } - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - - for (pds = pq->pds; pds < epds; pds++) { - if (_ST_EPOLL_REVENTS(pds->fd) == 0) { - pds->revents = 0; - continue; - } - osfd = pds->fd; - events = pds->events; - revents = 0; - if ((events & POLLIN) && (_ST_EPOLL_REVENTS(osfd) & EPOLLIN)) - revents |= POLLIN; - if ((events & POLLOUT) && (_ST_EPOLL_REVENTS(osfd) & EPOLLOUT)) - revents |= POLLOUT; - if ((events & POLLPRI) && (_ST_EPOLL_REVENTS(osfd) & EPOLLPRI)) - revents |= POLLPRI; - if (_ST_EPOLL_REVENTS(osfd) & EPOLLERR) - revents |= POLLERR; - if (_ST_EPOLL_REVENTS(osfd) & EPOLLHUP) - revents |= POLLHUP; - - pds->revents = revents; - if (revents) { - notify = 1; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Here we will only delete/modify descriptors that - * didn't fire (see comments in _st_epoll_pollset_del()). - */ - _st_epoll_pollset_del(pq->pds, pq->npds); - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } - } - - for (i = 0; i < nfd; i++) { - /* Delete/modify descriptors that fired */ - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = 0; - events = _ST_EPOLL_EVENTS(osfd); - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = osfd; - if (epoll_ctl(_st_epoll_data->epfd, op, osfd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_fd_new(int osfd) -{ - if (osfd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(osfd) < 0) - return -1; - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_close(int osfd) -{ - if (_ST_EPOLL_READ_CNT(osfd) || _ST_EPOLL_WRITE_CNT(osfd) || _ST_EPOLL_EXCEP_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -/* - * Check if epoll functions are just stubs. - */ -ST_HIDDEN int _st_epoll_is_supported(void) -{ - struct epoll_event ev; - - ev.events = EPOLLIN; - ev.data.ptr = NULL; - /* Guaranteed to fail */ - epoll_ctl(-1, EPOLL_CTL_ADD, -1, &ev); - - return (errno != ENOSYS); -} - -static _st_eventsys_t _st_epoll_eventsys = { - "epoll", - ST_EVENTSYS_ALT, - _st_epoll_init, - _st_epoll_dispatch, - _st_epoll_pollset_add, - _st_epoll_pollset_del, - _st_epoll_fd_new, - _st_epoll_fd_close, - _st_epoll_fd_getlimit -}; -#endif /* MD_HAVE_EPOLL */ - - -/***************************************** - * Public functions - */ - -int st_set_eventsys(int eventsys) -{ - if (_st_eventsys) { - errno = EBUSY; - return -1; - } - - if (eventsys == ST_EVENTSYS_SELECT || eventsys == ST_EVENTSYS_DEFAULT) { -#if defined (MD_HAVE_SELECT) - _st_eventsys = &_st_select_eventsys; - return 0; -#endif - } - - if (eventsys == ST_EVENTSYS_ALT) { -#if defined (MD_HAVE_KQUEUE) - _st_eventsys = &_st_kq_eventsys; - return 0; -#elif defined (MD_HAVE_EPOLL) - if (_st_epoll_is_supported()) { - _st_eventsys = &_st_epoll_eventsys; - return 0; - } -#endif - } - - errno = EINVAL; - return -1; -} - -int st_get_eventsys(void) -{ - return _st_eventsys ? _st_eventsys->val : -1; -} - -const char *st_get_eventsys_name(void) -{ - return _st_eventsys ? _st_eventsys->name : ""; -} - diff --git a/trunk/3rdparty/st-srs/io.c b/trunk/3rdparty/st-srs/io.c deleted file mode 100644 index 22998b05c..000000000 --- a/trunk/3rdparty/st-srs/io.c +++ /dev/null @@ -1,752 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - -// Global stat. -#if defined(DEBUG) && defined(DEBUG_STATS) -unsigned long long _st_stat_recvfrom = 0; -unsigned long long _st_stat_recvfrom_eagain = 0; -unsigned long long _st_stat_sendto = 0; -unsigned long long _st_stat_sendto_eagain = 0; -unsigned long long _st_stat_read = 0; -unsigned long long _st_stat_read_eagain = 0; -unsigned long long _st_stat_readv = 0; -unsigned long long _st_stat_readv_eagain = 0; -unsigned long long _st_stat_writev = 0; -unsigned long long _st_stat_writev_eagain = 0; -unsigned long long _st_stat_recvmsg = 0; -unsigned long long _st_stat_recvmsg_eagain = 0; -unsigned long long _st_stat_sendmsg = 0; -unsigned long long _st_stat_sendmsg_eagain = 0; -#endif - -#if EAGAIN != EWOULDBLOCK - #define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK)) -#else - #define _IO_NOT_READY_ERROR (errno == EAGAIN) -#endif - -#define _LOCAL_MAXIOV 16 - -/* File descriptor object free list */ -static _st_netfd_t *_st_netfd_freelist = NULL; -/* Maximum number of file descriptors that the process can open */ -static int _st_osfd_limit = -1; - -static void _st_netfd_free_aux_data(_st_netfd_t *fd); - -int _st_io_init(void) -{ - struct sigaction sigact; - struct rlimit rlim; - int fdlim; - - /* Ignore SIGPIPE */ - sigact.sa_handler = SIG_IGN; - sigemptyset(&sigact.sa_mask); - sigact.sa_flags = 0; - if (sigaction(SIGPIPE, &sigact, NULL) < 0) - return -1; - - /* Set maximum number of open file descriptors */ - if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) - return -1; - - fdlim = (*_st_eventsys->fd_getlimit)(); - if (fdlim > 0 && rlim.rlim_max > (rlim_t) fdlim) { - rlim.rlim_max = fdlim; - } - - /** - * by SRS, for osx. - * when rlimit max is negative, for example, osx, use cur directly. - * @see https://github.com/ossrs/srs/issues/336 - */ - if ((int)rlim.rlim_max < 0) { - _st_osfd_limit = (int)(fdlim > 0? fdlim : rlim.rlim_cur); - return 0; - } - - rlim.rlim_cur = rlim.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) - return -1; - _st_osfd_limit = (int) rlim.rlim_max; - - return 0; -} - - -int st_getfdlimit(void) -{ - return _st_osfd_limit; -} - - -void st_netfd_free(_st_netfd_t *fd) -{ - if (!fd->inuse) - return; - - fd->inuse = 0; - if (fd->aux_data) - _st_netfd_free_aux_data(fd); - if (fd->private_data && fd->destructor) - (*(fd->destructor))(fd->private_data); - fd->private_data = NULL; - fd->destructor = NULL; - fd->next = _st_netfd_freelist; - _st_netfd_freelist = fd; -} - - -static _st_netfd_t *_st_netfd_new(int osfd, int nonblock, int is_socket) -{ - _st_netfd_t *fd; - int flags = 1; - - if ((*_st_eventsys->fd_new)(osfd) < 0) - return NULL; - - if (_st_netfd_freelist) { - fd = _st_netfd_freelist; - _st_netfd_freelist = _st_netfd_freelist->next; - } else { - fd = calloc(1, sizeof(_st_netfd_t)); - if (!fd) - return NULL; - } - - fd->osfd = osfd; - fd->inuse = 1; - fd->next = NULL; - - if (nonblock) { - /* Use just one system call */ - if (is_socket && ioctl(osfd, FIONBIO, &flags) != -1) - return fd; - /* Do it the Posix way */ - if ((flags = fcntl(osfd, F_GETFL, 0)) < 0 || - fcntl(osfd, F_SETFL, flags | O_NONBLOCK) < 0) { - st_netfd_free(fd); - return NULL; - } - } - - return fd; -} - - -_st_netfd_t *st_netfd_open(int osfd) -{ - return _st_netfd_new(osfd, 1, 0); -} - - -_st_netfd_t *st_netfd_open_socket(int osfd) -{ - return _st_netfd_new(osfd, 1, 1); -} - - -int st_netfd_close(_st_netfd_t *fd) -{ - if ((*_st_eventsys->fd_close)(fd->osfd) < 0) - return -1; - - st_netfd_free(fd); - return close(fd->osfd); -} - - -int st_netfd_fileno(_st_netfd_t *fd) -{ - return (fd->osfd); -} - - -void st_netfd_setspecific(_st_netfd_t *fd, void *value, _st_destructor_t destructor) -{ - if (value != fd->private_data) { - /* Free up previously set non-NULL data value */ - if (fd->private_data && fd->destructor) - (*(fd->destructor))(fd->private_data); - } - fd->private_data = value; - fd->destructor = destructor; -} - - -void *st_netfd_getspecific(_st_netfd_t *fd) -{ - return (fd->private_data); -} - - -/* - * Wait for I/O on a single descriptor. - */ -int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout) -{ - struct pollfd pd; - int n; - - pd.fd = fd->osfd; - pd.events = (short) how; - pd.revents = 0; - - if ((n = st_poll(&pd, 1, timeout)) < 0) - return -1; - if (n == 0) { - /* Timed out */ - errno = ETIME; - return -1; - } - if (pd.revents & POLLNVAL) { - errno = EBADF; - return -1; - } - - return 0; -} - - -/* No-op */ -int st_netfd_serialize_accept(_st_netfd_t *fd) -{ - fd->aux_data = NULL; - return 0; -} - -/* No-op */ -static void _st_netfd_free_aux_data(_st_netfd_t *fd) -{ - fd->aux_data = NULL; -} - -_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout) -{ - int osfd, err; - _st_netfd_t *newfd; - - while ((osfd = accept(fd->osfd, addr, (socklen_t *)addrlen)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return NULL; - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return NULL; - } - - /* On some platforms the new socket created by accept() inherits */ - /* the nonblocking attribute of the listening socket */ -#if defined (MD_ACCEPT_NB_INHERITED) - newfd = _st_netfd_new(osfd, 0, 1); -#elif defined (MD_ACCEPT_NB_NOT_INHERITED) - newfd = _st_netfd_new(osfd, 1, 1); -#else - #error Unknown OS -#endif - - if (!newfd) { - err = errno; - close(osfd); - errno = err; - } - - return newfd; -} - - -int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout) -{ - int n, err = 0; - - while (connect(fd->osfd, addr, addrlen) < 0) { - if (errno != EINTR) { - /* - * On some platforms, if connect() is interrupted (errno == EINTR) - * after the kernel binds the socket, a subsequent connect() - * attempt will fail with errno == EADDRINUSE. Ignore EADDRINUSE - * iff connect() was previously interrupted. See Rich Stevens' - * "UNIX Network Programming," Vol. 1, 2nd edition, p. 413 - * ("Interrupted connect"). - */ - if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0)) - return -1; - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) - return -1; - /* Try to find out whether the connection setup succeeded or failed */ - n = sizeof(int); - if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err, (socklen_t *)&n) < 0) - return -1; - if (err) { - errno = err; - return -1; - } - break; - } - err = 1; - } - - return 0; -} - - -ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout) -{ - ssize_t n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_read; - #endif - - while ((n = read(fd->osfd, buf, nbyte)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_read_eagain; - #endif - - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return -1; - } - - return n; -} - - -int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, st_utime_t timeout) -{ - struct iovec iov, *riov; - int riov_size, rv; - - iov.iov_base = buf; - iov.iov_len = *resid; - riov = &iov; - riov_size = 1; - rv = st_readv_resid(fd, &riov, &riov_size, timeout); - *resid = iov.iov_len; - return rv; -} - - -ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout) -{ - ssize_t n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_readv; - #endif - - while ((n = readv(fd->osfd, iov, iov_size)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_readv_eagain; - #endif - - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return -1; - } - - return n; -} - -int st_readv_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout) -{ - ssize_t n; - - while (*iov_size > 0) { - if (*iov_size == 1) - n = read(fd->osfd, (*iov)->iov_base, (*iov)->iov_len); - else - n = readv(fd->osfd, *iov, *iov_size); - if (n < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - } else if (n == 0) - break; - else { - while ((size_t) n >= (*iov)->iov_len) { - n -= (*iov)->iov_len; - (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len; - (*iov)->iov_len = 0; - (*iov)++; - (*iov_size)--; - if (n == 0) - break; - } - if (*iov_size == 0) - break; - (*iov)->iov_base = (char *) (*iov)->iov_base + n; - (*iov)->iov_len -= n; - } - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return -1; - } - - return 0; -} - - -ssize_t st_read_fully(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout) -{ - size_t resid = nbyte; - return st_read_resid(fd, buf, &resid, timeout) == 0 ? - (ssize_t) (nbyte - resid) : -1; -} - - -int st_write_resid(_st_netfd_t *fd, const void *buf, size_t *resid, st_utime_t timeout) -{ - struct iovec iov, *riov; - int riov_size, rv; - - iov.iov_base = (void *) buf; /* we promise not to modify buf */ - iov.iov_len = *resid; - riov = &iov; - riov_size = 1; - rv = st_writev_resid(fd, &riov, &riov_size, timeout); - *resid = iov.iov_len; - return rv; -} - - -ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout) -{ - size_t resid = nbyte; - return st_write_resid(fd, buf, &resid, timeout) == 0 ? - (ssize_t) (nbyte - resid) : -1; -} - - -ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout) -{ - ssize_t n, rv; - size_t nleft, nbyte; - int index, iov_cnt; - struct iovec *tmp_iov; - struct iovec local_iov[_LOCAL_MAXIOV]; - - /* Calculate the total number of bytes to be sent */ - nbyte = 0; - for (index = 0; index < iov_size; index++) - nbyte += iov[index].iov_len; - - rv = (ssize_t)nbyte; - nleft = nbyte; - tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */ - iov_cnt = iov_size; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_writev; - #endif - - while (nleft > 0) { - if (iov_cnt == 1) { - if (st_write(fd, tmp_iov[0].iov_base, nleft, timeout) != (ssize_t) nleft) - rv = -1; - break; - } - if ((n = writev(fd->osfd, tmp_iov, iov_cnt)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) { - rv = -1; - break; - } - } else { - if ((size_t) n == nleft) - break; - nleft -= n; - /* Find the next unwritten vector */ - n = (ssize_t)(nbyte - nleft); - for (index = 0; (size_t) n >= iov[index].iov_len; index++) - n -= iov[index].iov_len; - - if (tmp_iov == iov) { - /* Must copy iov's around */ - if (iov_size - index <= _LOCAL_MAXIOV) { - tmp_iov = local_iov; - } else { - tmp_iov = calloc(1, (iov_size - index) * sizeof(struct iovec)); - if (tmp_iov == NULL) - return -1; - } - } - - /* Fill in the first partial read */ - tmp_iov[0].iov_base = &(((char *)iov[index].iov_base)[n]); - tmp_iov[0].iov_len = iov[index].iov_len - n; - index++; - /* Copy the remaining vectors */ - for (iov_cnt = 1; index < iov_size; iov_cnt++, index++) { - tmp_iov[iov_cnt].iov_base = iov[index].iov_base; - tmp_iov[iov_cnt].iov_len = iov[index].iov_len; - } - } - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_writev_eagain; - #endif - - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) { - rv = -1; - break; - } - } - - if (tmp_iov != iov && tmp_iov != local_iov) - free(tmp_iov); - - return rv; -} - - -int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout) -{ - ssize_t n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_writev; - #endif - - while (*iov_size > 0) { - if (*iov_size == 1) - n = write(fd->osfd, (*iov)->iov_base, (*iov)->iov_len); - else - n = writev(fd->osfd, *iov, *iov_size); - if (n < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - } else { - while ((size_t) n >= (*iov)->iov_len) { - n -= (*iov)->iov_len; - (*iov)->iov_base = (char *) (*iov)->iov_base + (*iov)->iov_len; - (*iov)->iov_len = 0; - (*iov)++; - (*iov_size)--; - if (n == 0) - break; - } - if (*iov_size == 0) - break; - (*iov)->iov_base = (char *) (*iov)->iov_base + n; - (*iov)->iov_len -= n; - } - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_writev_eagain; - #endif - - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) - return -1; - } - - return 0; -} - - -/* - * Simple I/O functions for UDP. - */ -int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout) -{ - int n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_recvfrom; - #endif - - while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_recvfrom_eagain; - #endif - - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return -1; - } - - return n; -} - - -int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout) -{ - int n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_sendto; - #endif - - while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_sendto_eagain; - #endif - - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) - return -1; - } - - return n; -} - - -int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout) -{ - int n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_recvmsg; - #endif - - while ((n = recvmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_recvmsg_eagain; - #endif - - /* Wait until the socket becomes readable */ - if (st_netfd_poll(fd, POLLIN, timeout) < 0) - return -1; - } - - return n; -} - - -int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout) -{ - int n; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_sendmsg; - #endif - - while ((n = sendmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_sendmsg_eagain; - #endif - - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) - return -1; - } - - return n; -} - - -/* - * To open FIFOs or other special files. - */ -_st_netfd_t *st_open(const char *path, int oflags, mode_t mode) -{ - int osfd, err; - _st_netfd_t *newfd; - - while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) { - if (errno != EINTR) - return NULL; - } - - newfd = _st_netfd_new(osfd, 0, 0); - if (!newfd) { - err = errno; - close(osfd); - errno = err; - } - - return newfd; -} - diff --git a/trunk/3rdparty/st-srs/key.c b/trunk/3rdparty/st-srs/key.c deleted file mode 100644 index 03f73a73a..000000000 --- a/trunk/3rdparty/st-srs/key.c +++ /dev/null @@ -1,123 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include "common.h" - - -/* - * Destructor table for per-thread private data - */ -static _st_destructor_t _st_destructors[ST_KEYS_MAX]; -static int key_max = 0; - - -/* - * Return a key to be used for thread specific data - */ -int st_key_create(int *keyp, _st_destructor_t destructor) -{ - if (key_max >= ST_KEYS_MAX) { - errno = EAGAIN; - return -1; - } - - *keyp = key_max++; - _st_destructors[*keyp] = destructor; - - return 0; -} - - -int st_key_getlimit(void) -{ - return ST_KEYS_MAX; -} - - -int st_thread_setspecific(int key, void *value) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (key < 0 || key >= key_max) { - errno = EINVAL; - return -1; - } - - if (value != me->private_data[key]) { - /* free up previously set non-NULL data value */ - if (me->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(me->private_data[key]); - } - me->private_data[key] = value; - } - - return 0; -} - - -void *st_thread_getspecific(int key) -{ - if (key < 0 || key >= key_max) - return NULL; - - return ((_ST_CURRENT_THREAD())->private_data[key]); -} - - -/* - * Free up all per-thread private data - */ -void _st_thread_cleanup(_st_thread_t *thread) -{ - int key; - - for (key = 0; key < key_max; key++) { - if (thread->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(thread->private_data[key]); - thread->private_data[key] = NULL; - } - } -} - diff --git a/trunk/3rdparty/st-srs/md.h b/trunk/3rdparty/st-srs/md.h deleted file mode 100644 index 51e8b2575..000000000 --- a/trunk/3rdparty/st-srs/md.h +++ /dev/null @@ -1,272 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#ifndef __ST_MD_H__ -#define __ST_MD_H__ - -#if defined(ETIMEDOUT) && !defined(ETIME) - #define ETIME ETIMEDOUT -#endif - -#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) - #define MAP_ANON MAP_ANONYMOUS -#endif - -#ifndef MAP_FAILED - #define MAP_FAILED -1 -#endif - -/***************************************** - * Platform specifics - */ - -#if defined (DARWIN) - - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_HAVE_SOCKLEN_T - - #define MD_USE_BUILTIN_SETJMP - - #if defined(__amd64__) || defined(__x86_64__) - #define JB_SP 12 /* The jmpbuf is int(4B) array, while MD_GET_SP covert to long(8B) pointer, so the JB_SP should be 12 which is 6*sizeof(long)/sizeof(int) */ - #define MD_GET_SP(_t) *((long *)&((_t)->context[JB_SP])) - #else - #error Unknown CPU architecture - #endif - - #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 - - #if defined(MD_USE_BUILTIN_SETJMP) - #define MD_SETJMP(env) _st_md_cxt_save(env) - #define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val) - - extern int _st_md_cxt_save(jmp_buf env); - extern void _st_md_cxt_restore(jmp_buf env, int val); - #endif - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (LINUX) - - /* - * These are properties of the linux kernel and are the same on every - * flavor and architecture. - */ - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_NOT_INHERITED - /* - * Modern GNU/Linux is Posix.1g compliant. - */ - #define MD_HAVE_SOCKLEN_T - - /* - * All architectures and flavors of linux have the gettimeofday - * function but if you know of a faster way, use it. - */ - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - - #if 1 - - /* - * On linux, there are a few styles of jmpbuf format. These vary based - * on architecture/glibc combination. - * - * Most of the glibc based toggles were lifted from: - * mozilla/nsprpub/pr/include/md/_linux.h - */ - - /* - * Starting with glibc 2.4, JB_SP definitions are not public anymore. - * They, however, can still be found in glibc source tree in - * architecture-specific "jmpbuf-offsets.h" files. - * Most importantly, the content of jmp_buf is mangled by setjmp to make - * it completely opaque (the mangling can be disabled by setting the - * LD_POINTER_GUARD environment variable before application execution). - * Therefore we will use built-in _st_md_cxt_save/_st_md_cxt_restore - * functions as a setjmp/longjmp replacement wherever they are available - * unless USE_LIBC_SETJMP is defined. - */ - - #if defined(__i386__) - #define MD_USE_BUILTIN_SETJMP - - #if defined(__GLIBC__) && __GLIBC__ >= 2 - #ifndef JB_SP - #define JB_SP 4 - #endif - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_SP] - #else - /* not an error but certainly cause for caution */ - #error "Untested use of old glibc on i386" - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp - #endif - - #elif defined(__amd64__) || defined(__x86_64__) - #define MD_USE_BUILTIN_SETJMP - - #ifndef JB_RSP - #define JB_RSP 6 - #endif - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_RSP] - - #elif defined(__aarch64__) - /* https://github.com/ossrs/state-threads/issues/9 */ - #define MD_USE_BUILTIN_SETJMP - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[13] - - #elif defined(__arm__) - /* https://github.com/ossrs/state-threads/issues/1#issuecomment-244648573 */ - #define MD_USE_BUILTIN_SETJMP - - /* force to use glibc solution, hack the guard jmpbuf from michaeltalyansky */ - #ifdef USE_LIBC_SETJMP - #undef MD_USE_BUILTIN_SETJMP - #endif - - #if defined(__GLIBC__) && __GLIBC__ >= 2 - /* Merge from https://github.com/michaeltalyansky/state-threads/commit/56554a5c425aee8e7a73782eae23d74d83c4120a */ - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[8] - #else - #error "ARM/Linux pre-glibc2 not supported yet" - #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */ - - #elif defined(__mips__) - /* https://github.com/ossrs/state-threads/issues/21 */ - #define MD_USE_BUILTIN_SETJMP - #define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jb[0])) - - #elif defined(__loongarch__) - /* https://github.com/ossrs/state-threads/issues/24 */ - #define MD_USE_BUILTIN_SETJMP - #define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jmpbuf[0])) - - #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) - #define MD_SETJMP(env) _st_md_cxt_save(env) - #define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val) - - extern int _st_md_cxt_save(jmp_buf env); - extern void _st_md_cxt_restore(jmp_buf env, int val); - #else - #define MD_SETJMP(env) setjmp(env) - #define MD_LONGJMP(env, val) longjmp(env, val) - #endif - -#elif defined (CYGWIN64) - - // For CYGWIN64, build SRS on Windows. - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_HAVE_SOCKLEN_T - - #define MD_USE_BUILTIN_SETJMP - - #if defined(__amd64__) || defined(__x86_64__) - #define JB_SP 6 // The context is long(32) array, @see https://github.com/ossrs/state-threads/issues/20#issuecomment-887569093 - #define MD_GET_SP(_t) *((long *)&((_t)->context[JB_SP])) - #else - #error Unknown CPU architecture - #endif - - #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 - - #if defined(MD_USE_BUILTIN_SETJMP) - #define MD_SETJMP(env) _st_md_cxt_save(env) - #define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val) - - extern int _st_md_cxt_save(jmp_buf env); - extern void _st_md_cxt_restore(jmp_buf env, int val); - #endif - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#else - #error Unknown OS -#endif /* OS */ - -#ifndef MD_STACK_PAD_SIZE - #define MD_STACK_PAD_SIZE 128 -#endif - -#if !defined(MD_HAVE_SOCKLEN_T) && !defined(socklen_t) - #define socklen_t int -#endif - -#ifndef MD_CAP_STACK - #define MD_CAP_STACK(var_addr) -#endif - -#endif /* !__ST_MD_H__ */ - diff --git a/trunk/3rdparty/st-srs/md_cygwin64.S b/trunk/3rdparty/st-srs/md_cygwin64.S deleted file mode 100644 index 332ef3384..000000000 --- a/trunk/3rdparty/st-srs/md_cygwin64.S +++ /dev/null @@ -1,77 +0,0 @@ - -/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */ -#if !defined(MD_ST_NO_ASM) - -#if defined(__amd64__) || defined(__x86_64__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_RBX 0 - #define JB_RBP 1 - #define JB_R12 2 /* R12:R15 Nonvolatile Must be preserved by callee */ - #define JB_R13 3 /* @see https://docs.microsoft.com/en-us/cpp/build/x64-software-conventions?view=msvc-160#register-usage */ - #define JB_R14 4 /* RBX, RBP, RDI, RSI, R12, R14, R14, and R15 must be saved in any function using them. */ - #define JB_R15 5 /* @see https://software.intel.com/content/www/us/en/develop/articles/introduction-to-x64-assembly.html */ - #define JB_RSP 6 - #define JB_PC 7 - - .file "md_cygwin64.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ /* The env is rcx, https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160 */ - .globl _st_md_cxt_save - .align 16 - _st_md_cxt_save: - /* - * Save registers. - */ - movq %rbx, (JB_RBX*8)(%rcx) /* Save rbx to env[0], *(int64_t*)(rcx+0)=rbx */ - movq %rbp, (JB_RBP*8)(%rcx) /* Save rbp to env[1], *(int64_t*)(rcx+1)=rbp */ - movq %r12, (JB_R12*8)(%rcx) /* Save r12 to env[2], *(int64_t*)(rcx+2)=r12 */ - movq %r13, (JB_R13*8)(%rcx) /* Save r13 to env[3], *(int64_t*)(rcx+3)=r13 */ - movq %r14, (JB_R14*8)(%rcx) /* Save r14 to env[4], *(int64_t*)(rcx+4)=r14 */ - movq %r15, (JB_R15*8)(%rcx) /* Save r15 to env[5], *(int64_t*)(rcx+5)=r15 */ - /* Save SP */ - leaq 8(%rsp), %r8 /* Save *(int64_t*)(rsp+8) to r8, https://github.com/ossrs/state-threads/issues/20#issuecomment-887569093 */ - movq %r8, (JB_RSP*8)(%rcx) /* Save r8(rsp) to env[6], *(int64_t*)(rcx+6)=r8 */ - /* Save PC we are returning to */ - movq (%rsp), %r9 /* Save PC(parent function address) %(rsp) to r9, https://github.com/ossrs/state-threads/issues/20#issuecomment-887569093 */ - movq %r9, (JB_PC*8)(%rcx) /* Save r9(PC) to env[7], *(int64_t*)(rcx+7)=r9 */ - xorq %rax, %rax /* Reset rax(return value) to 0 */ - ret - - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rcx, val is edx/rdx, https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160 */ - .globl _st_md_cxt_restore - .align 16 - _st_md_cxt_restore: - /* - * Restore registers. - */ - movq (JB_RBX*8)(%rcx), %rbx /* Load rbx from env[0] */ - movq (JB_RBP*8)(%rcx), %rbp /* Load rbp from env[1] */ - movq (JB_R12*8)(%rcx), %r12 /* Load r12 from env[2] */ - movq (JB_R13*8)(%rcx), %r13 /* Load r13 from env[3] */ - movq (JB_R14*8)(%rcx), %r14 /* Load r14 from env[4] */ - movq (JB_R15*8)(%rcx), %r15 /* Load r15 from env[5] */ - /* Set return value */ /* The edx is param1 val, the eax is return value */ - test %edx, %edx /* if (!val) { */ - mov $01, %eax /* val=1; */ - cmove %eax, %edx /* } */ - mov %edx, %eax /* return val; */ - /* Restore PC and RSP */ - movq (JB_PC*8)(%rcx), %r8 /* Load r8(PC) from env[7], https://github.com/ossrs/state-threads/issues/20#issuecomment-887569093 */ - movq (JB_RSP*8)(%rcx), %rsp /* Load rsp from env[6] */ - /* Jump to saved PC */ - jmpq *%r8 /* Jump to r8(PC) */ - - /****************************************************************/ - -#endif - -#endif diff --git a/trunk/3rdparty/st-srs/md_darwin.S b/trunk/3rdparty/st-srs/md_darwin.S deleted file mode 100644 index cfad14ce7..000000000 --- a/trunk/3rdparty/st-srs/md_darwin.S +++ /dev/null @@ -1,79 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */ -#if !defined(MD_ST_NO_ASM) - -#if defined(__amd64__) || defined(__x86_64__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_RBX 0 - #define JB_RBP 1 - #define JB_R12 2 /* The first six integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX, R8, R9. */ - #define JB_R13 3 /* If the callee wishes to use registers RBX, RSP, RBP, and R12–R15, it must restore their original values before returning control to the caller. */ - #define JB_R14 4 /* @see https://en.wikipedia.org/wiki/X86_calling_conventions */ - #define JB_R15 5 /* @see https://www.cnblogs.com/Five100Miles/p/8458561.html */ - #define JB_RSP 6 - #define JB_PC 7 - - .file "md_darwin.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ /* The env is rdi, https://en.wikipedia.org/wiki/X86_calling_conventions */ - .globl __st_md_cxt_save - .align 16 - __st_md_cxt_save: - /* - * Save registers. - */ - movq %rbx, (JB_RBX*8)(%rdi) /* Save rbx to env[0], *(int64_t*)(rdi+0)=rbx */ - movq %rbp, (JB_RBP*8)(%rdi) /* Save rbp to env[1], *(int64_t*)(rdi+1)=rbp */ - movq %r12, (JB_R12*8)(%rdi) /* Save r12 to env[2], *(int64_t*)(rdi+2)=r12 */ - movq %r13, (JB_R13*8)(%rdi) /* Save r13 to env[3], *(int64_t*)(rdi+3)=r13 */ - movq %r14, (JB_R14*8)(%rdi) /* Save r14 to env[4], *(int64_t*)(rdi+4)=r14 */ - movq %r15, (JB_R15*8)(%rdi) /* Save r15 to env[5], *(int64_t*)(rdi+5)=r15 */ - /* Save SP */ - leaq 8(%rsp), %r8 /* Save *(int64_t*)(rsp+8) to r8, https://github.com/ossrs/state-threads/issues/11#issuecomment-888709759 */ - movq %r8, (JB_RSP*8)(%rdi) /* Save r8(rsp) to env[6], *(int64_t*)(rdi+6)=r8 */ - /* Save PC we are returning to */ - movq (%rsp), %r9 /* Save PC(parent function address) %(rsp) to r9 */ - movq %r9, (JB_PC*8)(%rdi) /* Save r9(PC) to env[7], *(int64_t*)(rdi+7)=r9 */ - xorq %rax, %rax /* Reset rax to 0 */ - ret - - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rdi, val is esi/rsi, https://en.wikipedia.org/wiki/X86_calling_conventions */ - .globl __st_md_cxt_restore - .align 16 - __st_md_cxt_restore: - /* - * Restore registers. - */ - movq (JB_RBX*8)(%rdi), %rbx /* Load rbx from env[0] */ - movq (JB_RBP*8)(%rdi), %rbp /* Load rbp from env[1] */ - movq (JB_R12*8)(%rdi), %r12 /* Load r12 from env[2] */ - movq (JB_R13*8)(%rdi), %r13 /* Load r13 from env[3] */ - movq (JB_R14*8)(%rdi), %r14 /* Load r14 from env[4] */ - movq (JB_R15*8)(%rdi), %r15 /* Load r15 from env[5] */ - /* Set return value */ /* The esi is param1 val, the eax is return value */ - test %esi, %esi /* if (!val) { */ - mov $01, %eax /* val=1; */ - cmove %eax, %esi /* } */ - mov %esi, %eax /* return val; */ - /* Restore PC and RSP */ - movq (JB_PC*8)(%rdi), %r8 /* Load r8(PC) from env[7] */ - movq (JB_RSP*8)(%rdi), %rsp /* Load rsp from env[6] */ - /* Jump to saved PC */ - jmpq *%r8 /* Jump to r8(PC) */ - - /****************************************************************/ - -#endif - -#endif diff --git a/trunk/3rdparty/st-srs/md_linux.S b/trunk/3rdparty/st-srs/md_linux.S deleted file mode 100644 index adcee5a18..000000000 --- a/trunk/3rdparty/st-srs/md_linux.S +++ /dev/null @@ -1,515 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */ -#if !defined(MD_ST_NO_ASM) - -/* - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - */ - -#if defined(__i386__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_BX 0 - #define JB_SI 1 - #define JB_DI 2 - #define JB_BP 3 - #define JB_SP 4 - #define JB_PC 5 - - .file "md.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, @function - .align 16 - _st_md_cxt_save: - movl 4(%esp), %eax - - /* - * Save registers. - */ - movl %ebx, (JB_BX*4)(%eax) - movl %esi, (JB_SI*4)(%eax) - movl %edi, (JB_DI*4)(%eax) - /* Save SP */ - leal 4(%esp), %ecx - movl %ecx, (JB_SP*4)(%eax) - /* Save PC we are returning to */ - movl 0(%esp), %ecx - movl %ecx, (JB_PC*4)(%eax) - /* Save caller frame pointer */ - movl %ebp, (JB_BP*4)(%eax) - xorl %eax, %eax - ret - .size _st_md_cxt_save, .-_st_md_cxt_save - - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, @function - .align 16 - _st_md_cxt_restore: - /* First argument is jmp_buf */ - movl 4(%esp), %ecx - /* Second argument is return value */ - movl 8(%esp), %eax - /* Set the return address */ - movl (JB_PC*4)(%ecx), %edx - /* - * Restore registers. - */ - movl (JB_BX*4)(%ecx), %ebx - movl (JB_SI*4)(%ecx), %esi - movl (JB_DI*4)(%ecx), %edi - movl (JB_BP*4)(%ecx), %ebp - movl (JB_SP*4)(%ecx), %esp - testl %eax, %eax - jnz 1f - incl %eax - /* Jump to saved PC */ - 1: jmp *%edx - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - - -#elif defined(__amd64__) || defined(__x86_64__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_RBX 0 - #define JB_RBP 1 - #define JB_R12 2 - #define JB_R13 3 - #define JB_R14 4 - #define JB_R15 5 - #define JB_RSP 6 - #define JB_PC 7 - - .file "md.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, @function - .align 16 - _st_md_cxt_save: - /* - * Save registers. - */ - movq %rbx, (JB_RBX*8)(%rdi) - movq %rbp, (JB_RBP*8)(%rdi) - movq %r12, (JB_R12*8)(%rdi) - movq %r13, (JB_R13*8)(%rdi) - movq %r14, (JB_R14*8)(%rdi) - movq %r15, (JB_R15*8)(%rdi) - /* Save SP */ - leaq 8(%rsp), %rdx - movq %rdx, (JB_RSP*8)(%rdi) - /* Save PC we are returning to */ - movq (%rsp), %rax - movq %rax, (JB_PC*8)(%rdi) - xorq %rax, %rax - ret - .size _st_md_cxt_save, .-_st_md_cxt_save - - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, @function - .align 16 - _st_md_cxt_restore: - /* - * Restore registers. - */ - movq (JB_RBX*8)(%rdi), %rbx - movq (JB_RBP*8)(%rdi), %rbp - movq (JB_R12*8)(%rdi), %r12 - movq (JB_R13*8)(%rdi), %r13 - movq (JB_R14*8)(%rdi), %r14 - movq (JB_R15*8)(%rdi), %r15 - /* Set return value */ - test %esi, %esi - mov $01, %eax - cmove %eax, %esi - mov %esi, %eax - movq (JB_PC*8)(%rdi), %rdx - movq (JB_RSP*8)(%rdi), %rsp - /* Jump to saved PC */ - jmpq *%rdx - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - - -#elif defined(__aarch64__) - - /****************************************************************/ - /* https://github.com/ossrs/srs/issues/1282#issuecomment-445539513 */ - - #define JB_X19 0 - #define JB_X20 1 - #define JB_X21 2 - #define JB_X22 3 - #define JB_X23 4 - #define JB_X24 5 - #define JB_X25 6 - #define JB_X26 7 - #define JB_X27 8 - #define JB_X28 9 - #define JB_X29 10 - #define JB_LR 11 - #define JB_SP 13 - - #define JB_D8 14 - #define JB_D9 15 - #define JB_D10 16 - #define JB_D11 17 - #define JB_D12 18 - #define JB_D13 19 - #define JB_D14 20 - #define JB_D15 21 - - .file "md.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, %function - .align 4 - _st_md_cxt_save: - stp x19, x20, [x0, #JB_X19<<3] - stp x21, x22, [x0, #JB_X21<<3] - stp x23, x24, [x0, #JB_X23<<3] - stp x25, x26, [x0, #JB_X25<<3] - stp x27, x28, [x0, #JB_X27<<3] - stp x29, x30, [x0, #JB_X29<<3] - - stp d8, d9, [x0, #JB_D8<<3] - stp d10, d11, [x0, #JB_D10<<3] - stp d12, d13, [x0, #JB_D12<<3] - stp d14, d15, [x0, #JB_D14<<3] - mov x2, sp - str x2, [x0, #JB_SP<<3] - - mov x0, #0 - ret - .size _st_md_cxt_save, .-_st_md_cxt_save - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, %function - .align 4 - _st_md_cxt_restore: - ldp x19, x20, [x0, #JB_X19<<3] - ldp x21, x22, [x0, #JB_X21<<3] - ldp x23, x24, [x0, #JB_X23<<3] - ldp x25, x26, [x0, #JB_X25<<3] - ldp x27, x28, [x0, #JB_X27<<3] - - ldp x29, x30, [x0, #JB_X29<<3] - - ldp d8, d9, [x0, #JB_D8<<3] - ldp d10, d11, [x0, #JB_D10<<3] - ldp d12, d13, [x0, #JB_D12<<3] - ldp d14, d15, [x0, #JB_D14<<3] - - ldr x5, [x0, #JB_SP<<3] - mov sp, x5 - - cmp x1, #0 - mov x0, #1 - csel x0, x1, x0, ne - /* Use br instead of ret because ret is guaranteed to mispredict */ - br x30 - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - - -#elif defined(__arm__) - - /****************************************************************/ - /* https://github.com/ossrs/srs/issues/1282#issuecomment-445539513 */ - - /* Register list for a ldm/stm instruction to load/store - the general registers from a __jmp_buf. */ - # define JMP_BUF_REGLIST {v1-v6, sl, fp, sp, lr} - - .file "md.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, %function - .align 2 - _st_md_cxt_save: - mov ip, r0 - - /* Save registers */ - stmia ip!, JMP_BUF_REGLIST - - #ifdef __VFP_FP__ - /* Store the VFP registers. */ - /* Following instruction is vstmia ip!, {d8-d15}. */ - stc p11, cr8, [ip], #64 - #endif - - #ifdef __IWMMXT__ - /* Save the call-preserved iWMMXt registers. */ - /* Following instructions are wstrd wr10, [ip], #8 (etc.) */ - stcl p1, cr10, [r12], #8 - stcl p1, cr11, [r12], #8 - stcl p1, cr12, [r12], #8 - stcl p1, cr13, [r12], #8 - stcl p1, cr14, [r12], #8 - stcl p1, cr15, [r12], #8 - #endif - - mov r0, #0 - bx lr - - .size _st_md_cxt_save, .-_st_md_cxt_save - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, %function - .align 2 - _st_md_cxt_restore: - mov ip, r0 - - /* Restore registers */ - ldmia ip!, JMP_BUF_REGLIST - - #ifdef __VFP_FP__ - /* Restore the VFP registers. */ - /* Following instruction is vldmia ip!, {d8-d15}. */ - ldc p11, cr8, [r12], #64 - #endif - - #ifdef __IWMMXT__ - /* Restore the call-preserved iWMMXt registers. */ - /* Following instructions are wldrd wr10, [ip], #8 (etc.) */ - ldcl p1, cr10, [r12], #8 - ldcl p1, cr11, [r12], #8 - ldcl p1, cr12, [r12], #8 - ldcl p1, cr13, [r12], #8 - ldcl p1, cr14, [r12], #8 - ldcl p1, cr15, [r12], #8 - #endif - - movs r0, r1 /* get the return value in place */ - moveq r0, #1 /* can't let setjmp() return zero! */ - bx lr - - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - - -#elif defined(__mips__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_SP 0 /* Stack pointer */ - #define JB_RA 11 /* Return address */ - #define JB_GP 1 /* Global pointer */ - #define JB_S0 3 /* S0-S7, Saved temporaries */ - #define JB_S1 4 /* S0-S7, Saved temporaries */ - #define JB_S2 5 /* S0-S7, Saved temporaries */ - #define JB_S3 6 /* S0-S7, Saved temporaries */ - #define JB_S4 7 /* S0-S7, Saved temporaries */ - #define JB_S5 8 /* S0-S7, Saved temporaries */ - #define JB_S6 9 /* S0-S7, Saved temporaries */ - #define JB_S7 10 /* S0-S7, Saved temporaries */ - #define JB_FP 2 /* FP/S8 Frame pointer */ - - .file "md_linux.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ /* The env is $a0, https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, %function - .align 2 - _st_md_cxt_save: - sw $sp, 0($a0) /* Save sp to env[0], *(long*)($a0+0) =sp */ - sw $ra, 4($a0) /* Save ra to env[1], *(long*)($a0+4)=ra, the return address, https://chortle.ccsu.edu/AssemblyTutorial/Chapter-26/ass26_4.html */ - sw $gp, 8($a0) /* Save gp to env[2], *(long*)($a0+8) =gp */ - sw $s0, 12($a0) /* Save s0 to env[3], *(long*)($a0+12)=s0 */ - sw $s1, 16($a0) /* Save s1 to env[4], *(long*)($a0+16)=s1 */ - sw $s2, 20($a0) /* Save s2 to env[5], *(long*)($a0+20)=s2 */ - sw $s3, 24($a0) /* Save s3 to env[6], *(long*)($a0+24)=s3 */ - sw $s4, 28($a0) /* Save s4 to env[7], *(long*)($a0+28)=s4 */ - sw $s5, 32($a0) /* Save s5 to env[8], *(long*)($a0+32)=s5 */ - sw $s6, 36($a0) /* Save s6 to env[9], *(long*)($a0+36)=s6 */ - sw $s7, 40($a0) /* Save s7 to env[10], *(long*)($a0+40)=s7 */ - sw $fp, 44($a0) /* Save fp to env[11], *(long*)($a0+44) =fp */ - li $v0, 0 /* Set return value to 0 */ - jr $ra /* Return */ - - .size _st_md_cxt_save, .-_st_md_cxt_save - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, %function - .align 2 - _st_md_cxt_restore: - lw $sp, 0($a0) /* Load sp from env[0], sp=*(long*)($a0+0) */ - lw $ra, 4($a0) /* Load sp from env[1], ra=*(long*)($a0+4), the saved return address */ - lw $gp, 8($a0) /* Load sp from env[2], gp=*(long*)($a0+8) */ - lw $s0, 12($a0) /* Load sp from env[3], s0=*(long*)($a0+12) */ - lw $s1, 16($a0) /* Load sp from env[4], s1=*(long*)($a0+16) */ - lw $s2, 20($a0) /* Load sp from env[5], s2=*(long*)($a0+20) */ - lw $s3, 24($a0) /* Load sp from env[6], s3=*(long*)($a0+24) */ - lw $s4, 28($a0) /* Load sp from env[7], s4=*(long*)($a0+28) */ - lw $s5, 32($a0) /* Load sp from env[8], s5=*(long*)($a0+32) */ - lw $s6, 36($a0) /* Load sp from env[9], s6=*(long*)($a0+36) */ - lw $s7, 40($a0) /* Load sp from env[10], s7=*(long*)($a0+40) */ - lw $fp, 44($a0) /* Load sp from env[2], fp=*(long*)($a0+44) */ - li $v0, 1 /* Set return value to 1 */ - jr $ra /* Return to the saved return address */ - - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - - -#elif defined(__loongarch__) - - /****************************************************************/ - - /* - * Internal __jmp_buf layout - */ - #define JB_SP 0 /* R3, SP, Stack pointer */ - #define JB_RA 1 /* R1, RA, Return address */ - #define JB_FP 2 /* FP/R22 Frame pointer */ - #define JB_S0 3 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S1 4 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S2 5 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S3 6 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S4 7 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S5 8 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S6 9 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S7 10 /* R23-R31, S0-S8, Subroutine register variable */ - #define JB_S8 11 /* R23-R31, S0-S8, Subroutine register variable */ - - .file "md_linux.S" - .text - - /* _st_md_cxt_save(__jmp_buf env) */ /* The env is $r4, https://github.com/ossrs/state-threads/issues/24#porting */ - .globl _st_md_cxt_save - .type _st_md_cxt_save, %function - .align 2 - _st_md_cxt_save: - st.d $r3, $r4, 0 /* Save sp to env[0], *(long*)($r4+0) = sp */ - st.d $r1, $r4, 8 /* Save ra to env[1], *(long*)($r4+8) = r1 */ - st.d $r22, $r4, 16 /* Save fp to env[2], *(long*)($r4+16) = r22 */ - st.d $r23, $r4, 24 /* Save r23 to env[3], *(long*)($r4+24) = r23 */ - st.d $r24, $r4, 32 /* Save r24 to env[4], *(long*)($r4+32) = r24 */ - st.d $r25, $r4, 40 /* Save r25 to env[5], *(long*)($r4+40) = r25 */ - st.d $r26, $r4, 48 /* Save r26 to env[6], *(long*)($r4+48) = r26 */ - st.d $r27, $r4, 56 /* Save r27 to env[7], *(long*)($r4+56) = r27 */ - st.d $r28, $r4, 64 /* Save r28 to env[8], *(long*)($r4+64) = r28 */ - st.d $r29, $r4, 72 /* Save r29 to env[9], *(long*)($r4+72) = r29 */ - st.d $r30, $r4, 80 /* Save r30 to env[10], *(long*)($r4+80) = r30 */ - st.d $r31, $r4, 88 /* Save r31 to env[11], *(long*)($r4+88) = r31 */ - addi.w $r12, $r0, 0 /* Set return value to 0 */ - move $r4, $r12 /* Set return value to 0 */ - jirl $r0, $r1, 0 /* Return */ - - .size _st_md_cxt_save, .-_st_md_cxt_save - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .globl _st_md_cxt_restore - .type _st_md_cxt_restore, %function - .align 2 - _st_md_cxt_restore: - ld.d $r3, $r4, 0 /* Load sp from env[0], sp=*(long*)($r4+0) */ - ld.d $r1, $r4, 8 /* Load ra from env[1], r1=*(long*)($r4+8) */ - ld.d $r22, $r4, 16 /* Load fp from env[2], r22=*(long*)($r4+16) */ - ld.d $r23, $r4, 24 /* Load r23 from env[3], r23=*(long*)($r4+24) */ - ld.d $r24, $r4, 32 /* Load r24 from env[4], r24=*(long*)($r4+32) */ - ld.d $r25, $r4, 40 /* Load r25 from env[5], r25=*(long*)($r4+40) */ - ld.d $r26, $r4, 48 /* Load r26 from env[6], r26=*(long*)($r4+48) */ - ld.d $r27, $r4, 56 /* Load r27 from env[7], r27=*(long*)($r4+56) */ - ld.d $r28, $r4, 64 /* Load r28 from env[8], r28=*(long*)($r4+64) */ - ld.d $r29, $r4, 72 /* Load r29 from env[9], r29=*(long*)($r4+72) */ - ld.d $r30, $r4, 80 /* Load r30 from env[10], r30=*(long*)($r4+80) */ - ld.d $r31, $r4, 88 /* Load r31 from env[11], r31=*(long*)($r4+88) */ - addi.w $r12, $r0, 1 /* Set return value to 1 */ - move $r4, $r12 /* Set return value to 1 */ - jirl $r0, $r1, 0 /* Return to the saved return address */ - - .size _st_md_cxt_restore, .-_st_md_cxt_restore - - /****************************************************************/ - -#endif - -#endif diff --git a/trunk/3rdparty/st-srs/public.h b/trunk/3rdparty/st-srs/public.h deleted file mode 100644 index 75f53a4c2..000000000 --- a/trunk/3rdparty/st-srs/public.h +++ /dev/null @@ -1,169 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -#ifndef __ST_THREAD_H__ -#define __ST_THREAD_H__ - -#include -#include -#include -#include -#include -#include -#include - -#define ST_VERSION "1.9" -#define ST_VERSION_MAJOR 1 -#define ST_VERSION_MINOR 9 - -/* Undefine this to remove the context switch callback feature. */ -#define ST_SWITCH_CB - -#ifndef ETIME - #define ETIME ETIMEDOUT -#endif - -#ifndef ST_UTIME_NO_TIMEOUT - #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) -#endif - -#ifndef ST_UTIME_NO_WAIT - #define ST_UTIME_NO_WAIT 0 -#endif - -#define ST_EVENTSYS_DEFAULT 0 -#define ST_EVENTSYS_SELECT 1 -#define ST_EVENTSYS_ALT 3 - -#ifdef __cplusplus -extern "C" { -#endif - -typedef unsigned long long st_utime_t; -typedef struct _st_thread * st_thread_t; -typedef struct _st_cond * st_cond_t; -typedef struct _st_mutex * st_mutex_t; -typedef struct _st_netfd * st_netfd_t; -#ifdef ST_SWITCH_CB -typedef void (*st_switch_cb_t)(void); -#endif - -extern int st_init(void); -extern int st_getfdlimit(void); - -extern int st_set_eventsys(int eventsys); -extern int st_get_eventsys(void); -extern const char *st_get_eventsys_name(void); - -#ifdef ST_SWITCH_CB -extern st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb); -extern st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb); -#endif - -extern st_thread_t st_thread_self(void); -extern void st_thread_exit(void *retval); -extern int st_thread_join(st_thread_t thread, void **retvalp); -extern void st_thread_interrupt(st_thread_t thread); -extern void st_thread_yield(); -extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); -extern int st_randomize_stacks(int on); -extern int st_set_utime_function(st_utime_t (*func)(void)); - -extern st_utime_t st_utime(void); -extern st_utime_t st_utime_last_clock(void); -extern int st_timecache_set(int on); -extern time_t st_time(void); -extern int st_usleep(st_utime_t usecs); -extern int st_sleep(int secs); -extern st_cond_t st_cond_new(void); -extern int st_cond_destroy(st_cond_t cvar); -extern int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout); -extern int st_cond_wait(st_cond_t cvar); -extern int st_cond_signal(st_cond_t cvar); -extern int st_cond_broadcast(st_cond_t cvar); -extern st_mutex_t st_mutex_new(void); -extern int st_mutex_destroy(st_mutex_t lock); -extern int st_mutex_lock(st_mutex_t lock); -extern int st_mutex_unlock(st_mutex_t lock); -extern int st_mutex_trylock(st_mutex_t lock); - -extern int st_key_create(int *keyp, void (*destructor)(void *)); -extern int st_key_getlimit(void); -extern int st_thread_setspecific(int key, void *value); -extern void *st_thread_getspecific(int key); - -extern st_netfd_t st_netfd_open(int osfd); -extern st_netfd_t st_netfd_open_socket(int osfd); -extern void st_netfd_free(st_netfd_t fd); -extern int st_netfd_close(st_netfd_t fd); -extern int st_netfd_fileno(st_netfd_t fd); -extern void st_netfd_setspecific(st_netfd_t fd, void *value, void (*destructor)(void *)); -extern void *st_netfd_getspecific(st_netfd_t fd); -extern int st_netfd_serialize_accept(st_netfd_t fd); -extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout); - -extern int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); -extern st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout); -extern int st_connect(st_netfd_t fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout); -extern ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); -extern ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); -extern int st_read_resid(st_netfd_t fd, void *buf, size_t *resid, st_utime_t timeout); -extern ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); -extern int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); -extern ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte, st_utime_t timeout); -extern int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid, st_utime_t timeout); -extern ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); -extern int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); -extern int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout); -extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout); -extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout); -extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout); - -extern st_netfd_t st_open(const char *path, int oflags, mode_t mode); - -#ifdef DEBUG -extern void _st_show_thread_stack(st_thread_t thread, const char *messg); -extern void _st_iterate_threads(void); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* !__ST_THREAD_H__ */ - diff --git a/trunk/3rdparty/st-srs/sched.c b/trunk/3rdparty/st-srs/sched.c deleted file mode 100644 index 845b7f4d7..000000000 --- a/trunk/3rdparty/st-srs/sched.c +++ /dev/null @@ -1,753 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND -#include -#endif - -// Global stat. -#if defined(DEBUG) && defined(DEBUG_STATS) -unsigned long long _st_stat_sched_15ms = 0; -unsigned long long _st_stat_sched_20ms = 0; -unsigned long long _st_stat_sched_25ms = 0; -unsigned long long _st_stat_sched_30ms = 0; -unsigned long long _st_stat_sched_35ms = 0; -unsigned long long _st_stat_sched_40ms = 0; -unsigned long long _st_stat_sched_80ms = 0; -unsigned long long _st_stat_sched_160ms = 0; -unsigned long long _st_stat_sched_s = 0; - -unsigned long long _st_stat_thread_run = 0; -unsigned long long _st_stat_thread_idle = 0; -unsigned long long _st_stat_thread_yield = 0; -unsigned long long _st_stat_thread_yield2 = 0; -#endif - - -/* Global data */ -_st_vp_t _st_this_vp; /* This VP */ -_st_thread_t *_st_this_thread; /* Current thread */ -int _st_active_count = 0; /* Active thread count */ - -time_t _st_curr_time = 0; /* Current time as returned by time(2) */ -st_utime_t _st_last_tset; /* Last time it was fetched */ - - -int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - _st_pollq_t pq; - _st_thread_t *me = _ST_CURRENT_THREAD(); - int n; - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if ((*_st_eventsys->pollset_add)(pds, npds) < 0) - return -1; - - pq.pds = pds; - pq.npds = npds; - pq.thread = me; - pq.on_ioq = 1; - _ST_ADD_IOQ(pq); - if (timeout != ST_UTIME_NO_TIMEOUT) - _ST_ADD_SLEEPQ(me, timeout); - me->state = _ST_ST_IO_WAIT; - - _ST_SWITCH_CONTEXT(me); - - n = 0; - if (pq.on_ioq) { - /* If we timed out, the pollq might still be on the ioq. Remove it */ - _ST_DEL_IOQ(pq); - (*_st_eventsys->pollset_del)(pds, npds); - } else { - /* Count the number of ready descriptors */ - for (pd = pds; pd < epd; pd++) { - if (pd->revents) - n++; - } - } - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return n; -} - - -void _st_vp_schedule(void) -{ - _st_thread_t *thread; - - if (_ST_RUNQ.next != &_ST_RUNQ) { - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_thread_run; - #endif - - /* Pull thread off of the run queue */ - thread = _ST_THREAD_PTR(_ST_RUNQ.next); - _ST_DEL_RUNQ(thread); - } else { - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_thread_idle; - #endif - - /* If there are no threads to run, switch to the idle thread */ - thread = _st_this_vp.idle_thread; - } - ST_ASSERT(thread->state == _ST_ST_RUNNABLE); - - /* Resume the thread */ - thread->state = _ST_ST_RUNNING; - _ST_RESTORE_CONTEXT(thread); -} - - -/* - * Initialize this Virtual Processor - */ -int st_init(void) -{ - _st_thread_t *thread; - - if (_st_active_count) { - /* Already initialized */ - return 0; - } - - /* We can ignore return value here */ - st_set_eventsys(ST_EVENTSYS_DEFAULT); - - if (_st_io_init() < 0) - return -1; - - memset(&_st_this_vp, 0, sizeof(_st_vp_t)); - - ST_INIT_CLIST(&_ST_RUNQ); - ST_INIT_CLIST(&_ST_IOQ); - ST_INIT_CLIST(&_ST_ZOMBIEQ); -#ifdef DEBUG - ST_INIT_CLIST(&_ST_THREADQ); -#endif - - if ((*_st_eventsys->init)() < 0) - return -1; - - _st_this_vp.pagesize = getpagesize(); - _st_this_vp.last_clock = st_utime(); - - /* - * Create idle thread - */ - _st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start, NULL, 0, 0); - if (!_st_this_vp.idle_thread) - return -1; - _st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD; - _st_active_count--; - _ST_DEL_RUNQ(_st_this_vp.idle_thread); - - /* - * Initialize primordial thread - */ - thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + (ST_KEYS_MAX * sizeof(void *))); - if (!thread) - return -1; - thread->private_data = (void **) (thread + 1); - thread->state = _ST_ST_RUNNING; - thread->flags = _ST_FL_PRIMORDIAL; - _ST_SET_CURRENT_THREAD(thread); - _st_active_count++; -#ifdef DEBUG - _ST_ADD_THREADQ(thread); -#endif - - return 0; -} - - -#ifdef ST_SWITCH_CB -st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb) -{ - st_switch_cb_t ocb = _st_this_vp.switch_in_cb; - _st_this_vp.switch_in_cb = cb; - return ocb; -} - -st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb) -{ - st_switch_cb_t ocb = _st_this_vp.switch_out_cb; - _st_this_vp.switch_out_cb = cb; - return ocb; -} -#endif - - -/* - * Start function for the idle thread - */ -/* ARGSUSED */ -void *_st_idle_thread_start(void *arg) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - while (_st_active_count > 0) { - /* Idle vp till I/O is ready or the smallest timeout expired */ - _ST_VP_IDLE(); - - /* Check sleep queue for expired threads */ - _st_vp_check_clock(); - - me->state = _ST_ST_RUNNABLE; - _ST_SWITCH_CONTEXT(me); - } - - /* No more threads */ - exit(0); - - /* NOTREACHED */ - return NULL; -} - - -void st_thread_exit(void *retval) -{ - _st_thread_t *thread = _ST_CURRENT_THREAD(); - - thread->retval = retval; - _st_thread_cleanup(thread); - _st_active_count--; - if (thread->term) { - /* Put thread on the zombie queue */ - thread->state = _ST_ST_ZOMBIE; - _ST_ADD_ZOMBIEQ(thread); - - /* Notify on our termination condition variable */ - st_cond_signal(thread->term); - - /* Switch context and come back later */ - _ST_SWITCH_CONTEXT(thread); - - /* Continue the cleanup */ - st_cond_destroy(thread->term); - thread->term = NULL; - } - -#ifdef DEBUG - _ST_DEL_THREADQ(thread); -#endif - - /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND - if (!(thread->flags & _ST_FL_PRIMORDIAL)) { - VALGRIND_STACK_DEREGISTER(thread->stack->valgrind_stack_id); - } -#endif - - if (!(thread->flags & _ST_FL_PRIMORDIAL)) - _st_stack_free(thread->stack); - - /* Find another thread to run */ - _ST_SWITCH_CONTEXT(thread); - /* Not going to land here */ -} - - -int st_thread_join(_st_thread_t *thread, void **retvalp) -{ - _st_cond_t *term = thread->term; - - /* Can't join a non-joinable thread */ - if (term == NULL) { - errno = EINVAL; - return -1; - } - if (_ST_CURRENT_THREAD() == thread) { - errno = EDEADLK; - return -1; - } - - /* Multiple threads can't wait on the same joinable thread */ - if (term->wait_q.next != &term->wait_q) { - errno = EINVAL; - return -1; - } - - while (thread->state != _ST_ST_ZOMBIE) { - if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) - return -1; - } - - if (retvalp) - *retvalp = thread->retval; - - /* - * Remove target thread from the zombie queue and make it runnable. - * When it gets scheduled later, it will do the clean up. - */ - thread->state = _ST_ST_RUNNABLE; - _ST_DEL_ZOMBIEQ(thread); - _ST_ADD_RUNQ(thread); - - return 0; -} - - -void _st_thread_main(void) -{ - _st_thread_t *thread = _ST_CURRENT_THREAD(); - - /* - * Cap the stack by zeroing out the saved return address register - * value. This allows some debugging/profiling tools to know when - * to stop unwinding the stack. It's a no-op on most platforms. - */ - MD_CAP_STACK(&thread); - - /* Run thread main */ - thread->retval = (*thread->start)(thread->arg); - - /* All done, time to go away */ - st_thread_exit(thread->retval); -} - - -/* - * Insert "thread" into the timeout heap, in the position - * specified by thread->heap_index. See docs/timeout_heap.txt - * for details about the timeout heap. - */ -static _st_thread_t **heap_insert(_st_thread_t *thread) { - int target = thread->heap_index; - int s = target; - _st_thread_t **p = &_ST_SLEEPQ; - int bits = 0; - int bit; - int index = 1; - - while (s) { - s >>= 1; - bits++; - } - for (bit = bits - 2; bit >= 0; bit--) { - if (thread->due < (*p)->due) { - _st_thread_t *t = *p; - thread->left = t->left; - thread->right = t->right; - *p = thread; - thread->heap_index = index; - thread = t; - } - index <<= 1; - if (target & (1 << bit)) { - p = &((*p)->right); - index |= 1; - } else { - p = &((*p)->left); - } - } - thread->heap_index = index; - *p = thread; - thread->left = thread->right = NULL; - return p; -} - - -/* - * Delete "thread" from the timeout heap. - */ -static void heap_delete(_st_thread_t *thread) { - _st_thread_t *t, **p; - int bits = 0; - int s, bit; - - /* First find and unlink the last heap element */ - p = &_ST_SLEEPQ; - s = _ST_SLEEPQ_SIZE; - while (s) { - s >>= 1; - bits++; - } - for (bit = bits - 2; bit >= 0; bit--) { - if (_ST_SLEEPQ_SIZE & (1 << bit)) { - p = &((*p)->right); - } else { - p = &((*p)->left); - } - } - t = *p; - *p = NULL; - --_ST_SLEEPQ_SIZE; - if (t != thread) { - /* - * Insert the unlinked last element in place of the element we are deleting - */ - t->heap_index = thread->heap_index; - p = heap_insert(t); - t = *p; - t->left = thread->left; - t->right = thread->right; - - /* - * Reestablish the heap invariant. - */ - for (;;) { - _st_thread_t *y; /* The younger child */ - int index_tmp; - if (t->left == NULL) - break; - else if (t->right == NULL) - y = t->left; - else if (t->left->due < t->right->due) - y = t->left; - else - y = t->right; - if (t->due > y->due) { - _st_thread_t *tl = y->left; - _st_thread_t *tr = y->right; - *p = y; - if (y == t->left) { - y->left = t; - y->right = t->right; - p = &y->left; - } else { - y->left = t->left; - y->right = t; - p = &y->right; - } - t->left = tl; - t->right = tr; - index_tmp = t->heap_index; - t->heap_index = y->heap_index; - y->heap_index = index_tmp; - } else { - break; - } - } - } - thread->left = thread->right = NULL; -} - - -void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) -{ - thread->due = _ST_LAST_CLOCK + timeout; - thread->flags |= _ST_FL_ON_SLEEPQ; - thread->heap_index = ++_ST_SLEEPQ_SIZE; - heap_insert(thread); -} - - -void _st_del_sleep_q(_st_thread_t *thread) -{ - heap_delete(thread); - thread->flags &= ~_ST_FL_ON_SLEEPQ; -} - - -void _st_vp_check_clock(void) -{ - _st_thread_t *thread; - st_utime_t now; -#if defined(DEBUG) && defined(DEBUG_STATS) - st_utime_t elapsed; -#endif - - now = st_utime(); -#if defined(DEBUG) && defined(DEBUG_STATS) - elapsed = now < _ST_LAST_CLOCK? 0 : now - _ST_LAST_CLOCK; // Might step back. -#endif - _ST_LAST_CLOCK = now; - - #if defined(DEBUG) && defined(DEBUG_STATS) - if (elapsed <= 10000) { - ++_st_stat_sched_15ms; - } else if (elapsed <= 21000) { - ++_st_stat_sched_20ms; - } else if (elapsed <= 25000) { - ++_st_stat_sched_25ms; - } else if (elapsed <= 30000) { - ++_st_stat_sched_30ms; - } else if (elapsed <= 35000) { - ++_st_stat_sched_35ms; - } else if (elapsed <= 40000) { - ++_st_stat_sched_40ms; - } else if (elapsed <= 80000) { - ++_st_stat_sched_80ms; - } else if (elapsed <= 160000) { - ++_st_stat_sched_160ms; - } else { - ++_st_stat_sched_s; - } - #endif - - if (_st_curr_time && now - _st_last_tset > 999000) { - _st_curr_time = time(NULL); - _st_last_tset = now; - } - - while (_ST_SLEEPQ != NULL) { - thread = _ST_SLEEPQ; - ST_ASSERT(thread->flags & _ST_FL_ON_SLEEPQ); - if (thread->due > now) - break; - _ST_DEL_SLEEPQ(thread); - - /* If thread is waiting on condition variable, set the time out flag */ - if (thread->state == _ST_ST_COND_WAIT) - thread->flags |= _ST_FL_TIMEDOUT; - - /* Make thread runnable */ - ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD)); - thread->state = _ST_ST_RUNNABLE; - // Insert at the head of RunQ, to execute timer first. - _ST_INSERT_RUNQ(thread); - } -} - - -void st_thread_yield() -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_thread_yield; - #endif - - /* Check sleep queue for expired threads */ - _st_vp_check_clock(); - - // If not thread in RunQ to yield to, ignore and continue to run. - if (_ST_RUNQ.next == &_ST_RUNQ) { - return; - } - - #if defined(DEBUG) && defined(DEBUG_STATS) - ++_st_stat_thread_yield2; - #endif - - // Append thread to the tail of RunQ, we will back after all threads executed. - me->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(me); - - // Yield to other threads in the RunQ. - _ST_SWITCH_CONTEXT(me); -} - - -void st_thread_interrupt(_st_thread_t *thread) -{ - /* If thread is already dead */ - if (thread->state == _ST_ST_ZOMBIE) - return; - - thread->flags |= _ST_FL_INTERRUPT; - - if (thread->state == _ST_ST_RUNNING || thread->state == _ST_ST_RUNNABLE) - return; - - if (thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(thread); - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); -} - - -/* Merge from https://github.com/michaeltalyansky/state-threads/commit/cce736426c2320ffec7c9820df49ee7a18ae638c */ -#if defined(__arm__) && !defined(MD_USE_BUILTIN_SETJMP) && __GLIBC_MINOR__ >= 19 - extern unsigned long __pointer_chk_guard; - #define PTR_MANGLE(var) \ - (var) = (__typeof (var)) ((unsigned long) (var) ^ __pointer_chk_guard) - #define PTR_DEMANGLE(var) PTR_MANGLE (var) -#endif - - -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size) -{ - _st_thread_t *thread; - _st_stack_t *stack; - void **ptds; - char *sp; - - /* Adjust stack size */ - if (stk_size == 0) - stk_size = ST_DEFAULT_STACK_SIZE; - stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE; - stack = _st_stack_new(stk_size); - if (!stack) - return NULL; - - /* Allocate thread object and per-thread data off the stack */ - sp = stack->stk_top; - sp = sp - (ST_KEYS_MAX * sizeof(void *)); - ptds = (void **) sp; - sp = sp - sizeof(_st_thread_t); - thread = (_st_thread_t *) sp; - - /* Make stack 64-byte aligned */ - if ((unsigned long)sp & 0x3f) - sp = sp - ((unsigned long)sp & 0x3f); - stack->sp = sp - _ST_STACK_PAD_SIZE; - - memset(thread, 0, sizeof(_st_thread_t)); - memset(ptds, 0, ST_KEYS_MAX * sizeof(void *)); - - /* Initialize thread */ - thread->private_data = ptds; - thread->stack = stack; - thread->start = start; - thread->arg = arg; - - /* Merge from https://github.com/michaeltalyansky/state-threads/commit/cce736426c2320ffec7c9820df49ee7a18ae638c */ - #if defined(__arm__) && !defined(MD_USE_BUILTIN_SETJMP) && __GLIBC_MINOR__ >= 19 - volatile void * lsp = PTR_MANGLE(stack->sp); - if (_setjmp ((thread)->context)) - _st_thread_main(); - (thread)->context[0].__jmpbuf[8] = (long) (lsp); - #else - _ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main); - #endif - - /* If thread is joinable, allocate a termination condition variable */ - if (joinable) { - thread->term = st_cond_new(); - if (thread->term == NULL) { - _st_stack_free(thread->stack); - return NULL; - } - } - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _st_active_count++; - _ST_ADD_RUNQ(thread); -#ifdef DEBUG - _ST_ADD_THREADQ(thread); -#endif - - /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND - if (!(thread->flags & _ST_FL_PRIMORDIAL)) { - thread->stack->valgrind_stack_id = VALGRIND_STACK_REGISTER(thread->stack->stk_top, thread->stack->stk_bottom); - } -#endif - - return thread; -} - - -_st_thread_t *st_thread_self(void) -{ - return _ST_CURRENT_THREAD(); -} - - -#ifdef DEBUG -/* ARGSUSED */ -void _st_show_thread_stack(_st_thread_t *thread, const char *messg) -{ - -} - -/* To be set from debugger */ -int _st_iterate_threads_flag = 0; - -void _st_iterate_threads(void) -{ - static _st_thread_t *thread = NULL; - static jmp_buf orig_jb, save_jb; - _st_clist_t *q; - - if (!_st_iterate_threads_flag) { - if (thread) { - memcpy(thread->context, save_jb, sizeof(jmp_buf)); - MD_LONGJMP(orig_jb, 1); - } - return; - } - - if (thread) { - memcpy(thread->context, save_jb, sizeof(jmp_buf)); - _st_show_thread_stack(thread, NULL); - } else { - if (MD_SETJMP(orig_jb)) { - _st_iterate_threads_flag = 0; - thread = NULL; - _st_show_thread_stack(thread, "Iteration completed"); - return; - } - thread = _ST_CURRENT_THREAD(); - _st_show_thread_stack(thread, "Iteration started"); - } - - q = thread->tlink.next; - if (q == &_ST_THREADQ) - q = q->next; - ST_ASSERT(q != &_ST_THREADQ); - thread = _ST_THREAD_THREADQ_PTR(q); - if (thread == _ST_CURRENT_THREAD()) - MD_LONGJMP(orig_jb, 1); - memcpy(save_jb, thread->context, sizeof(jmp_buf)); - MD_LONGJMP(thread->context, 1); -} -#endif /* DEBUG */ - diff --git a/trunk/3rdparty/st-srs/st.pc.in b/trunk/3rdparty/st-srs/st.pc.in deleted file mode 100644 index 46c39ec52..000000000 --- a/trunk/3rdparty/st-srs/st.pc.in +++ /dev/null @@ -1,10 +0,0 @@ -prefix=@prefix@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include - -Name: libst -Description: State Thread Library -Version: @VERSION@ -Libs: -L${libdir} -lst -Cflags: -I${includedir} diff --git a/trunk/3rdparty/st-srs/stk.c b/trunk/3rdparty/st-srs/stk.c deleted file mode 100644 index a37c3e62e..000000000 --- a/trunk/3rdparty/st-srs/stk.c +++ /dev/null @@ -1,176 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include "common.h" - - -/* How much space to leave between the stacks, at each end */ -#define REDZONE _ST_PAGE_SIZE - -_st_clist_t _st_free_stacks = ST_INIT_STATIC_CLIST(&_st_free_stacks); -int _st_num_free_stacks = 0; -int _st_randomize_stacks = 0; - -static char *_st_new_stk_segment(int size); - -_st_stack_t *_st_stack_new(int stack_size) -{ - _st_clist_t *qp; - _st_stack_t *ts; - int extra; - - for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) { - ts = _ST_THREAD_STACK_PTR(qp); - if (ts->stk_size >= stack_size) { - /* Found a stack that is big enough */ - ST_REMOVE_LINK(&ts->links); - _st_num_free_stacks--; - ts->links.next = NULL; - ts->links.prev = NULL; - return ts; - } - } - - /* Make a new thread stack object. */ - 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); - if (!ts->vaddr) { - free(ts); - return NULL; - } - ts->stk_size = stack_size; - ts->stk_bottom = ts->vaddr + REDZONE; - ts->stk_top = ts->stk_bottom + stack_size; - - /* For example, in OpenWRT, the memory at the begin minus 16B by mprotect is read-only. */ -#if defined(DEBUG) && !defined(MD_NO_PROTECT) - mprotect(ts->vaddr, REDZONE, PROT_NONE); - mprotect(ts->stk_top + extra, REDZONE, PROT_NONE); -#endif - - if (extra) { - long offset = (random() % extra) & ~0xf; - - ts->stk_bottom += offset; - ts->stk_top += offset; - } - - return ts; -} - - -/* - * Free the stack for the current thread - */ -void _st_stack_free(_st_stack_t *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 - void *vaddr = malloc(size); -#else - static int zero_fd = -1; - int mmap_flags = MAP_PRIVATE; - void *vaddr; - -#if defined (MD_USE_SYSV_ANON_MMAP) - if (zero_fd < 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) - mmap_flags |= MAP_ANON; -#else -#error Unknown OS -#endif - - vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); - if (vaddr == (void *)MAP_FAILED) - return NULL; - -#endif /* MALLOC_STACK */ - - return (char *)vaddr; -} - - -/* Not used */ -#if 0 -void _st_delete_stk_segment(char *vaddr, int size) -{ -#ifdef MALLOC_STACK - free(vaddr); -#else - (void) munmap(vaddr, size); -#endif -} -#endif - -int st_randomize_stacks(int on) -{ - int wason = _st_randomize_stacks; - - _st_randomize_stacks = on; - if (on) - srandom((unsigned int) st_utime()); - - return wason; -} diff --git a/trunk/3rdparty/st-srs/sync.c b/trunk/3rdparty/st-srs/sync.c deleted file mode 100644 index f69e22477..000000000 --- a/trunk/3rdparty/st-srs/sync.c +++ /dev/null @@ -1,370 +0,0 @@ -/* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */ - -/* - * The contents of this file are subject to the Mozilla Public - * License Version 1.1 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS - * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or - * implied. See the License for the specific language governing - * rights and limitations under the License. - * - * The Original Code is the Netscape Portable Runtime library. - * - * The Initial Developer of the Original Code is Netscape - * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1994-2000 Netscape Communications Corporation. All - * Rights Reserved. - * - * Contributor(s): Silicon Graphics, Inc. - * - * Portions created by SGI are Copyright (C) 2000-2001 Silicon - * Graphics, Inc. All Rights Reserved. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -/* - * This file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#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 - */ - -st_utime_t st_utime(void) -{ - if (_st_utime == NULL) { -#ifdef MD_GET_UTIME - MD_GET_UTIME(); -#else -#error Unknown OS -#endif - } - - return (*_st_utime)(); -} - - -int st_set_utime_function(st_utime_t (*func)(void)) -{ - if (_st_active_count) { - errno = EINVAL; - return -1; - } - - _st_utime = func; - - 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; - - if (on) { - _st_curr_time = time(NULL); - _st_last_tset = st_utime(); - } else - _st_curr_time = 0; - - return wason; -} - - -time_t st_time(void) -{ - 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(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (usecs != ST_UTIME_NO_TIMEOUT) { - me->state = _ST_ST_SLEEPING; - _ST_ADD_SLEEPQ(me, usecs); - } else - me->state = _ST_ST_SUSPENDED; - - _ST_SWITCH_CONTEXT(me); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - - -int st_sleep(int secs) -{ - 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; - - cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t)); - if (cvar) { - ST_INIT_CLIST(&cvar->wait_q); - } - - return cvar; -} - - -int st_cond_destroy(_st_cond_t *cvar) -{ - if (cvar->wait_q.next != &cvar->wait_q) { - errno = EBUSY; - return -1; - } - - free(cvar); - - return 0; -} - - -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - int rv; - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - /* Put caller thread on the condition variable's wait queue */ - me->state = _ST_ST_COND_WAIT; - ST_APPEND_LINK(&me->wait_links, &cvar->wait_q); - - if (timeout != ST_UTIME_NO_TIMEOUT) - _ST_ADD_SLEEPQ(me, timeout); - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - rv = 0; - - if (me->flags & _ST_FL_TIMEDOUT) { - me->flags &= ~_ST_FL_TIMEDOUT; - errno = ETIME; - rv = -1; - } - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - rv = -1; - } - - 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; - _st_clist_t *q; - - 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) - _ST_DEL_SLEEPQ(thread); - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - 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; - - lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t)); - if (lock) { - ST_INIT_CLIST(&lock->wait_q); - lock->owner = NULL; - } - - return lock; -} - - -int st_mutex_destroy(_st_mutex_t *lock) -{ - if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) { - errno = EBUSY; - return -1; - } - - free(lock); - - return 0; -} - - -int st_mutex_lock(_st_mutex_t *lock) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (lock->owner == NULL) { - /* Got the mutex */ - lock->owner = me; - return 0; - } - - if (lock->owner == me) { - errno = EDEADLK; - return -1; - } - - /* Put caller thread on the mutex's wait queue */ - me->state = _ST_ST_LOCK_WAIT; - ST_APPEND_LINK(&me->wait_links, &lock->wait_q); - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - - if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - - -int st_mutex_unlock(_st_mutex_t *lock) -{ - _st_thread_t *thread; - _st_clist_t *q; - - if (lock->owner != _ST_CURRENT_THREAD()) { - errno = EPERM; - return -1; - } - - for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) { - thread = _ST_THREAD_WAITQ_PTR(q); - if (thread->state == _ST_ST_LOCK_WAIT) { - lock->owner = thread; - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - return 0; - } - } - - /* No threads waiting on this mutex */ - lock->owner = NULL; - - return 0; -} - - -int st_mutex_trylock(_st_mutex_t *lock) -{ - if (lock->owner != NULL) { - errno = EBUSY; - return -1; - } - - /* Got the mutex */ - lock->owner = _ST_CURRENT_THREAD(); - - return 0; -} - diff --git a/trunk/3rdparty/st-srs/tools/helloworld/.gitignore b/trunk/3rdparty/st-srs/tools/helloworld/.gitignore deleted file mode 100644 index 31e0fce56..000000000 --- a/trunk/3rdparty/st-srs/tools/helloworld/.gitignore +++ /dev/null @@ -1 +0,0 @@ -helloworld diff --git a/trunk/3rdparty/st-srs/tools/helloworld/Makefile b/trunk/3rdparty/st-srs/tools/helloworld/Makefile deleted file mode 100644 index 2ec9a2d78..000000000 --- a/trunk/3rdparty/st-srs/tools/helloworld/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -.PHONY: clean - -LDLIBS=../../obj/libst.a -CFLAGS=-g -O0 -I../../obj - -./helloworld: helloworld.c $(LDLIBS) - $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o helloworld helloworld.c $(LDLIBS) - -clean: - rm -f helloworld - diff --git a/trunk/3rdparty/st-srs/tools/helloworld/helloworld.c b/trunk/3rdparty/st-srs/tools/helloworld/helloworld.c deleted file mode 100644 index 2f3481a5c..000000000 --- a/trunk/3rdparty/st-srs/tools/helloworld/helloworld.c +++ /dev/null @@ -1,19 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -#include - -#include - -int main(int argc, char** argv) -{ - st_init(); - - for (int i = 0; i < 10000; i++) { - printf("#%03d, Hello, state-threads world!\n", i); - st_sleep(1); - } - - return 0; -} - diff --git a/trunk/3rdparty/st-srs/tools/porting/.gitignore b/trunk/3rdparty/st-srs/tools/porting/.gitignore deleted file mode 100644 index d9bcb42a5..000000000 --- a/trunk/3rdparty/st-srs/tools/porting/.gitignore +++ /dev/null @@ -1 +0,0 @@ -porting diff --git a/trunk/3rdparty/st-srs/tools/porting/Makefile b/trunk/3rdparty/st-srs/tools/porting/Makefile deleted file mode 100644 index e31722b97..000000000 --- a/trunk/3rdparty/st-srs/tools/porting/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -.PHONY: clean - -CFLAGS=-g -O0 - -./porting: porting.c - $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS) - -clean: - rm -f porting - diff --git a/trunk/3rdparty/st-srs/tools/porting/porting.c b/trunk/3rdparty/st-srs/tools/porting/porting.c deleted file mode 100644 index 5032e89e1..000000000 --- a/trunk/3rdparty/st-srs/tools/porting/porting.c +++ /dev/null @@ -1,208 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -#include -#include - -int foo_return_zero(); -int foo_return_one(); -int foo_return_one_arg1(int r0); -extern void print_buf(unsigned char* p, int nn_jb); -extern void print_jmpbuf(); - -int main(int argc, char** argv) -{ - printf("OS specs:\n"); -#ifdef __linux__ - printf("__linux__: %d\n", __linux__); -#endif -#ifdef __APPLE__ - printf("__APPLE__: %d\n", __APPLE__); -#endif - - printf("\nCPU specs:\n"); -#ifdef __mips__ - // https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MD00565-2B-MIPS32-QRC-01.01.pdf - printf("__mips__: %d, __mips: %d, _MIPSEL: %d\n", __mips__, __mips, _MIPSEL); -#endif -#ifdef __x86_64__ - printf("__x86_64__: %d\n", __x86_64__); -#endif -#ifdef __loongarch__ - printf("__loongarch__: %d, __loongarch64 :%d\n", __loongarch__, __loongarch64); -#endif - - printf("\nCompiler specs:\n"); -#ifdef __GLIBC__ - printf("__GLIBC__: %d\n", __GLIBC__); -#endif - printf("sizeof(long)=%d\n", (int)sizeof(long)); - printf("sizeof(long long int)=%d\n", (int)sizeof(long long int)); - printf("sizeof(void*)=%d\n", (int)sizeof(void*)); -#ifdef __ptr_t - printf("sizeof(__ptr_t)=%d\n", (int)sizeof(__ptr_t)); -#endif - - printf("\nReturn value:\n"); - int r0 = foo_return_zero(); - int r1 = foo_return_one(); - int r2 = foo_return_one_arg1(r1); - printf("foo_return_zero=%d, foo_return_one=%d, foo_return_one_arg1=%d\n", r0, r1, r2); - - printf("\nCalling conventions:\n"); - print_jmpbuf(); - - return 0; -} - -int foo_return_zero() -{ - return 0; -} - -int foo_return_one() -{ - return 1; -} - -int foo_return_one_arg1(int r0) -{ - return r0 + 2; -} - -#ifdef __linux__ -#ifdef __mips__ -void print_jmpbuf() -{ - // https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions - register void* ra asm("ra"); - register void* gp asm("gp"); - register void* sp asm("sp"); - register void* fp asm("fp"); - // $s0–$s7 $16–$23 saved temporaries - register void* s0 asm("s0"); - register void* s1 asm("s1"); - register void* s2 asm("s2"); - register void* s3 asm("s3"); - register void* s4 asm("s4"); - register void* s5 asm("s5"); - register void* s6 asm("s6"); - register void* s7 asm("s7"); - - /* - typedef unsigned long long __jmp_buf[13]; - typedef struct __jmp_buf_tag { - __jmp_buf __jb; - unsigned long __fl; - unsigned long __ss[128/sizeof(long)]; - } jmp_buf[1]; - */ - jmp_buf ctx = {0}; - int r0 = setjmp(ctx); - if (!r0) { - longjmp(ctx, 1); - } - - printf("ra=%p, sp=%p, s0=%p, s1=%p, s2=%p, s3=%p, s4=%p, s5=%p, s6=%p, s7=%p, fp=%p, gp=%p\n", - ra, sp, s0, s1, s2, s3, s4, s5, s6, s7, fp, gp); - - int nn_jb = sizeof(ctx[0].__jb); - printf("sizeof(jmp_buf)=%d (unsigned long long [%d])\n", nn_jb, nn_jb/8); - - unsigned char* p = (unsigned char*)ctx[0].__jb; - print_buf(p, nn_jb); -} -#elif __loongarch__ -void print_jmpbuf() -{ - // https://github.com/ossrs/state-threads/issues/24#porting - register void* ra asm("r1"); // r1, ra, Return address - register void* sp asm("r3"); // r3, sp, Stack pointer - register void* fp asm("r22"); // r22, fp, Frame pointer - // r23-r31, s0-s8, Subroutine register variable - register void* s0 asm("r23"); - register void* s1 asm("r24"); - register void* s2 asm("r25"); - register void* s3 asm("r26"); - register void* s4 asm("r27"); - register void* s5 asm("r28"); - register void* s6 asm("r29"); - register void* s7 asm("r30"); - register void* s8 asm("r31"); - - /* - struct __jmp_buf_tag { - __jmp_buf __jmpbuf; - int __mask_was_saved; - __sigset_t __saved_mask; - }; - typedef struct __jmp_buf_tag jmp_buf[1]; - */ - jmp_buf ctx = {0}; - int r0 = setjmp(ctx); - if (!r0) { - longjmp(ctx, 1); - } - - printf("ra=%p, sp=%p, fp=%p, s0=%p, s1=%p, s2=%p, s3=%p, s4=%p, s5=%p, s6=%p, s7=%p, s7=%p\n", - ra, sp, fp, s0, s1, s2, s3, s4, s5, s6, s7, s8); - - int nn_jb = sizeof(ctx[0].__jmpbuf); - printf("sizeof(jmp_buf)=%d (unsigned long long [%d])\n", nn_jb, nn_jb/8); - - unsigned char* p = (unsigned char*)ctx[0].__jmpbuf; - print_buf(p, nn_jb); -} -#endif -#endif - -#ifdef __APPLE__ -#ifdef __x86_64__ -void print_jmpbuf() -{ - // https://courses.cs.washington.edu/courses/cse378/10au/sections/Section1_recap.pdf - void *rbx, *rbp, *r12, *r13, *r14, *r15, *rsp; - __asm__ __volatile__ ("movq %%rbx,%0": "=r"(rbx): /* No input */); - __asm__ __volatile__ ("movq %%rbp,%0": "=r"(rbp): /* No input */); - __asm__ __volatile__ ("movq %%r12,%0": "=r"(r12): /* No input */); - __asm__ __volatile__ ("movq %%r13,%0": "=r"(r13): /* No input */); - __asm__ __volatile__ ("movq %%r14,%0": "=r"(r14): /* No input */); - __asm__ __volatile__ ("movq %%r15,%0": "=r"(r15): /* No input */); - __asm__ __volatile__ ("movq %%rsp,%0": "=r"(rsp): /* No input */); - - printf("rbx=%p, rbp=%p, r12=%p, r13=%p, r14=%p, r15=%p, rsp=%p\n", - rbx, rbp, r12, r13, r14, r15, rsp); - - jmp_buf ctx = {0}; - int r0 = setjmp(ctx); - if (!r0) { - longjmp(ctx, 1); - } - - int nn_jb = sizeof(ctx); - printf("sizeof(jmp_buf)=%d (unsigned long long [%d])\n", nn_jb, nn_jb/8); - - unsigned char* p = (unsigned char*)ctx; - print_buf(p, nn_jb); -} -#endif -#endif - -void print_buf(unsigned char* p, int nn_jb) -{ - printf(" "); - - for (int i = 0; i < nn_jb; i++) { - printf("0x%02x ", (unsigned char)p[i]); - - int newline = ((i + 1) % sizeof(void*)); - if (!newline || i == nn_jb - 1) { - printf("\n"); - } - - if (!newline && i < nn_jb - 1) { - printf(" "); - } - } -} - diff --git a/trunk/3rdparty/st-srs/tools/verify/.gitignore b/trunk/3rdparty/st-srs/tools/verify/.gitignore deleted file mode 100644 index 0b5987362..000000000 --- a/trunk/3rdparty/st-srs/tools/verify/.gitignore +++ /dev/null @@ -1 +0,0 @@ -verify diff --git a/trunk/3rdparty/st-srs/tools/verify/Makefile b/trunk/3rdparty/st-srs/tools/verify/Makefile deleted file mode 100644 index 7c0107cf9..000000000 --- a/trunk/3rdparty/st-srs/tools/verify/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -.PHONY: clean - -LDLIBS=../../obj/libst.a -CFLAGS=-g -O0 - -./verify: verify.c $(LDLIBS) - $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o verify verify.c $(LDLIBS) - -clean: - rm -f verify - diff --git a/trunk/3rdparty/st-srs/tools/verify/verify.c b/trunk/3rdparty/st-srs/tools/verify/verify.c deleted file mode 100644 index 9a4264064..000000000 --- a/trunk/3rdparty/st-srs/tools/verify/verify.c +++ /dev/null @@ -1,134 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -#include - -#include -extern int _st_md_cxt_save(jmp_buf env); -extern void _st_md_cxt_restore(jmp_buf env, int val); - -void verify_jmpbuf(); -void print_buf(unsigned char* p, int nn_jb); - -int main(int argc, char** argv) -{ - verify_jmpbuf(); - return 0; -} - -#ifdef __linux__ -#ifdef __mips__ -void verify_jmpbuf() -{ - // https://en.wikipedia.org/wiki/MIPS_architecture#Calling_conventions - register void* ra asm("ra"); - register void* gp asm("gp"); - register void* sp asm("sp"); - register void* fp asm("fp"); - // $s0–$s7 $16–$23 saved temporaries - register void* s0 asm("s0"); - register void* s1 asm("s1"); - register void* s2 asm("s2"); - register void* s3 asm("s3"); - register void* s4 asm("s4"); - register void* s5 asm("s5"); - register void* s6 asm("s6"); - register void* s7 asm("s7"); - - jmp_buf ctx = {0}; - int r0 = _st_md_cxt_save(ctx); - if (!r0) { - _st_md_cxt_restore(ctx, 1); // Restore/Jump to previous line, set r0 to 1. - } - - printf("sp=%p, ra=%p, gp=%p, s0=%p, s1=%p, s2=%p, s3=%p, s4=%p, s5=%p, s6=%p, s7=%p, fp=%p\n", - sp, ra, gp, s0, s1, s2, s3, s4, s5, s6, s7, fp); - - int nn_jb = sizeof(ctx[0].__jb); - unsigned char* p = (unsigned char*)ctx[0].__jb; - print_buf(p, nn_jb); -} -#elif __loongarch__ -void verify_jmpbuf() -{ - // https://github.com/ossrs/state-threads/issues/24#porting - register void* ra asm("r1"); // r1, ra, Return address - register void* sp asm("r3"); // r3, sp, Stack pointer - register void* fp asm("r22"); // r22, fp, Frame pointer - // r23-r31, s0-s8, Subroutine register variable - register void* s0 asm("r23"); - register void* s1 asm("r24"); - register void* s2 asm("r25"); - register void* s3 asm("r26"); - register void* s4 asm("r27"); - register void* s5 asm("r28"); - register void* s6 asm("r29"); - register void* s7 asm("r30"); - register void* s8 asm("r31"); - - jmp_buf ctx = {0}; - int r0 = _st_md_cxt_save(ctx); - if (!r0) { - _st_md_cxt_restore(ctx, 1); // Restore/Jump to previous line, set r0 to 1. - } - - printf("sp=%p, ra=%p, fp=%p, s0=%p, s1=%p, s2=%p, s3=%p, s4=%p, s5=%p, s6=%p, s7=%p, s7=%p\n", - sp, ra, fp, s0, s1, s2, s3, s4, s5, s6, s7, s8); - - int nn_jb = sizeof(ctx[0].__jmpbuf); - unsigned char* p = (unsigned char*)ctx[0].__jmpbuf; - print_buf(p, nn_jb); -} -#endif -#endif - -#ifdef __APPLE__ -#ifdef __x86_64__ -void verify_jmpbuf() -{ - // https://courses.cs.washington.edu/courses/cse378/10au/sections/Section1_recap.pdf - void *rbx, *rbp, *r12, *r13, *r14, *r15, *rsp; - __asm__ __volatile__ ("movq %%rbx,%0": "=r"(rbx): /* No input */); - __asm__ __volatile__ ("movq %%rbp,%0": "=r"(rbp): /* No input */); - __asm__ __volatile__ ("movq %%r12,%0": "=r"(r12): /* No input */); - __asm__ __volatile__ ("movq %%r13,%0": "=r"(r13): /* No input */); - __asm__ __volatile__ ("movq %%r14,%0": "=r"(r14): /* No input */); - __asm__ __volatile__ ("movq %%r15,%0": "=r"(r15): /* No input */); - __asm__ __volatile__ ("movq %%rsp,%0": "=r"(rsp): /* No input */); - - printf("rbx=%p, rbp=%p, r12=%p, r13=%p, r14=%p, r15=%p, rsp=%p\n", - rbx, rbp, r12, r13, r14, r15, rsp); - - jmp_buf ctx = {0}; - int r0 = _st_md_cxt_save(ctx); - if (!r0) { - _st_md_cxt_restore(ctx, 1); // Restore/Jump to previous line, set r0 to 1. - } - - int nn_jb = sizeof(ctx); - printf("sizeof(jmp_buf)=%d (unsigned long long [%d])\n", nn_jb, nn_jb/8); - - unsigned char* p = (unsigned char*)ctx; - print_buf(p, nn_jb); -} -#endif -#endif - -void print_buf(unsigned char* p, int nn_jb) -{ - printf(" "); - - for (int i = 0; i < nn_jb; i++) { - printf("0x%02x ", (unsigned char)p[i]); - - int newline = ((i + 1) % sizeof(void*)); - if (!newline || i == nn_jb - 1) { - printf("\n"); - } - - if (!newline && i < nn_jb - 1) { - printf(" "); - } - } -} - diff --git a/trunk/3rdparty/st-srs/utest/Makefile b/trunk/3rdparty/st-srs/utest/Makefile deleted file mode 100644 index a294af7b6..000000000 --- a/trunk/3rdparty/st-srs/utest/Makefile +++ /dev/null @@ -1,60 +0,0 @@ - -# The main dir of st. -ST_DIR = .. -# The main dir of st utest. -ST_UTEST = . -# The main dir of gtest. -GTEST_DIR = $(ST_UTEST)/gtest - -# Flags passed to the C++ compiler. -CXXFLAGS += -g -O0 -std=c++11 -CXXFLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1 -# Flags for warnings. -WARNFLAGS += -Wall -Wno-deprecated-declarations -Wno-unused-private-field -Wno-unused-command-line-argument - -# House-keeping build targets. -all : $(ST_DIR)/obj/st_utest - -clean : - rm -f $(ST_DIR)/obj/st_utest* $(ST_DIR)/obj/gtest* - -# Usually you shouldn't tweak such internal variables, indicated by a -# trailing _. -GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_DIR)/include/gtest/*.h $(GTEST_DIR)/include/gtest/internal/*.h - -# For simplicity and to avoid depending on Google Test's -# implementation details, the dependencies specified below are -# conservative and not optimized. This is fine as Google Test -# compiles fast and for ordinary users its source rarely changes. -$(ST_DIR)/obj/gtest-all.o : $(GTEST_SRCS_) - $(CXX) -c $(GTEST_DIR)/src/gtest-all.cc -o $@ \ - $(CXXFLAGS) $(UTEST_FLAGS) \ - $(WARNFLAGS) \ - -I$(GTEST_DIR)/include -I$(GTEST_DIR) - -$(ST_DIR)/obj/gtest.a : $(ST_DIR)/obj/gtest-all.o - $(AR) $(ARFLAGS) $@ $^ - -##################################################################################### -##################################################################################### -# ST(state-threads) utest section -##################################################################################### -##################################################################################### - -# Depends, the depends objects -ST_UTEST_DEPS = $(ST_DIR)/obj/libst.a - -# Depends, utest header files -UTEST_DEPS = $(ST_UTEST)/st_utest.hpp - -# Objects, build each object of utest -$(ST_DIR)/obj/st_utest.o : st_utest.cpp $(ST_UTEST_DEPS) $(UTEST_DEPS) - $(CXX) -c st_utest.cpp -o $@ \ - $(CXXFLAGS) $(UTEST_FLAGS) \ - $(WARNFLAGS) \ - -I$(GTEST_DIR)/include -I$(ST_UTEST) -I$(ST_DIR) -I$(ST_DIR)/obj - -# generate the utest binary -$(ST_DIR)/obj/st_utest : $(ST_DIR)/obj/st_utest.o $(ST_DIR)/obj/gtest.a $(ST_UTEST_DEPS) - $(CXX) -o $@ $(CXXFLAGS) $(UTEST_FLAGS) \ - -lpthread -ldl $^ diff --git a/trunk/3rdparty/st-srs/utest/st_utest.cpp b/trunk/3rdparty/st-srs/utest/st_utest.cpp deleted file mode 100644 index 469b16e95..000000000 --- a/trunk/3rdparty/st-srs/utest/st_utest.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -#include - -#include -#include - -// We could do something in the main of utest. -// Copy from gtest-1.6.0/src/gtest_main.cc -GTEST_API_ int main(int argc, char **argv) { - // Select the best event system available on the OS. In Linux this is - // epoll(). On BSD it will be kqueue. On Cygwin it will be select. -#if __CYGWIN__ - assert(st_set_eventsys(ST_EVENTSYS_SELECT) != -1); -#else - assert(st_set_eventsys(ST_EVENTSYS_ALT) != -1); -#endif - - // Initialize state-threads, create idle coroutine. - assert(st_init() == 0); - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - -// basic test and samples. -VOID TEST(SampleTest, FastSampleInt64Test) -{ - EXPECT_EQ(1, (int)sizeof(int8_t)); - EXPECT_EQ(2, (int)sizeof(int16_t)); - EXPECT_EQ(4, (int)sizeof(int32_t)); - EXPECT_EQ(8, (int)sizeof(int64_t)); -} - -void* pfn_coroutine(void* /*arg*/) -{ - st_usleep(0); - return NULL; -} - -VOID TEST(SampleTest, StartCoroutine) -{ - st_thread_t trd = st_thread_create(pfn_coroutine, NULL, 1, 0); - EXPECT_TRUE(trd != NULL); - - // Wait for joinable coroutine to quit. - st_thread_join(trd, NULL); -} - -VOID TEST(SampleTest, StartCoroutineX3) -{ - st_thread_t trd0 = st_thread_create(pfn_coroutine, NULL, 1, 0); - st_thread_t trd1 = st_thread_create(pfn_coroutine, NULL, 1, 0); - st_thread_t trd2 = st_thread_create(pfn_coroutine, NULL, 1, 0); - EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL); - - // Wait for joinable coroutine to quit. - st_thread_join(trd1, NULL); - st_thread_join(trd2, NULL); - st_thread_join(trd0, NULL); -} - -void* pfn_coroutine_add(void* arg) -{ - int v = 0; - int* pi = (int*)arg; - - // Load the change of arg. - while (v != *pi) { - v = *pi; - st_usleep(0); - } - - // Add with const. - v += 100; - *pi = v; - - return NULL; -} - -VOID TEST(SampleTest, StartCoroutineAdd) -{ - int v = 0; - st_thread_t trd = st_thread_create(pfn_coroutine_add, &v, 1, 0); - EXPECT_TRUE(trd != NULL); - - // Wait for joinable coroutine to quit. - st_thread_join(trd, NULL); - - EXPECT_EQ(100, v); -} - -VOID TEST(SampleTest, StartCoroutineAddX3) -{ - int v = 0; - st_thread_t trd0 = st_thread_create(pfn_coroutine_add, &v, 1, 0); - st_thread_t trd1 = st_thread_create(pfn_coroutine_add, &v, 1, 0); - st_thread_t trd2 = st_thread_create(pfn_coroutine_add, &v, 1, 0); - EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL); - - // Wait for joinable coroutine to quit. - st_thread_join(trd0, NULL); - st_thread_join(trd1, NULL); - st_thread_join(trd2, NULL); - - EXPECT_EQ(300, v); -} - -int pfn_coroutine_params_x4(int a, int b, int c, int d) -{ - int e = 0; - - st_usleep(0); - - e += a + b + c + d; - e += 100; - return e; -} - -void* pfn_coroutine_params(void* arg) -{ - int r0 = pfn_coroutine_params_x4(1, 2, 3, 4); - *(int*)arg = r0; - return NULL; -} - -VOID TEST(SampleTest, StartCoroutineParams) -{ - int r0 = 0; - st_thread_t trd = st_thread_create(pfn_coroutine_params, &r0, 1, 0); - EXPECT_TRUE(trd != NULL); - - // Wait for joinable coroutine to quit. - st_thread_join(trd, NULL); - - EXPECT_EQ(110, r0); -} - diff --git a/trunk/3rdparty/st-srs/utest/st_utest.hpp b/trunk/3rdparty/st-srs/utest/st_utest.hpp deleted file mode 100644 index 8d39a06b2..000000000 --- a/trunk/3rdparty/st-srs/utest/st_utest.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* Copyright (c) 2021 Winlin */ - -#ifndef ST_UTEST_PUBLIC_HPP -#define ST_UTEST_PUBLIC_HPP - -// Before define the private/protected, we must include some system header files. -// Or it may fail with: -// redeclared with different access struct __xfer_bufptrs -// @see https://stackoverflow.com/questions/47839718/sstream-redeclared-with-public-access-compiler-error -#include - -#define VOID - -#endif -