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

Update source codes.

This commit is contained in:
winlin 2022-03-15 13:49:50 +08:00
parent e8e7dc285f
commit 461b0f6366
39 changed files with 0 additions and 10851 deletions

View file

@ -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

View file

@ -1,14 +0,0 @@
DARWIN_*_DBG
LINUX_*_DBG
CYGWIN64_*_DBG
obj
st.pc
.idea
gtest*
googletest-*
*.gcda
*.gcno
coverage
codecov
*.dSYM

View file

@ -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.

View file

@ -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=<n>
#
# To start with more than the default 64 initial pollfd slots
# (but the table grows dynamically anyway):
# DEFINES += -DST_MIN_POLLFDS_SIZE=<n>
#
# 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 <target>
#
# (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 <target>
#
# or to disable default epoll(4) support:
#
# make EXTRA_CFLAGS=-UMD_HAVE_EPOLL <target>
#
# 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) $(<F) $@
##########################
# Target rules:
darwin-debug:
$(MAKE) OS="DARWIN" BUILD="DBG"
darwin-optimized:
$(MAKE) OS="DARWIN" BUILD="OPT"
linux-debug:
$(MAKE) OS="LINUX" BUILD="DBG"
linux-optimized:
$(MAKE) OS="LINUX" BUILD="OPT"
cygwin64-debug:
$(MAKE) OS="CYGWIN64" BUILD="DBG"
darwin-debug-utest:
@echo "Build utest for state-threads"
$(MAKE) OS="DARWIN" BUILD="DBG"
cd utest && $(MAKE)
linux-debug-utest:
@echo "Build utest for state-threads"
$(MAKE) OS="LINUX" BUILD="DBG"
cd utest && $(MAKE)
cygwin64-debug-utest:
@echo "Build utest for state-threads"
$(MAKE) OS="CYGWIN64" BUILD="DBG"
cd utest && $(MAKE) UTEST_FLAGS="-std=gnu++0x" # @see https://www.codenong.com/18784112/
darwin-debug-gcov:
@echo "Build utest with gcov for state-threads"
$(MAKE) OS="DARWIN" BUILD="DBG" UTEST_FLAGS="-fprofile-arcs -ftest-coverage" STATIC_ONLY=yes
cd utest && $(MAKE) UTEST_FLAGS="-fprofile-arcs -ftest-coverage"
linux-debug-gcov:
@echo "Build utest with gcov for state-threads"
$(MAKE) OS="LINUX" BUILD="DBG" UTEST_FLAGS="-fprofile-arcs -ftest-coverage" STATIC_ONLY=yes
cd utest && $(MAKE) UTEST_FLAGS="-fprofile-arcs -ftest-coverage"
##########################

View file

@ -1,394 +0,0 @@
WELCOME!
The State Threads Library is a small application library which provides
a foundation for writing fast and highly scalable Internet applications
(such as web servers, proxy servers, mail transfer agents, and so on,
really any network-data-driven application) 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. In other words, this library offers a threading API for
structuring an Internet application as a state machine. For more
details, please see the library documentation in the "docs" directory or
on-line at
http://state-threads.sourceforge.net/docs/
The State Threads Project is an open source project for maintaining and
enhancing the State Threads Library. For more information about this
project, please see
http://state-threads.sourceforge.net/
BUILDING
To build the library by hand, use the GNU make utility. Run the make
command (e.g., `gmake') with no arguments to display all supported
targets.
To build more or less automatically, first set the CONFIG_GUESS_PATH
variable in either osguess.sh or your environment then run "make
default" which guesses your OS and builds. Requires the "config.guess"
utility from GNU autoconf (not included with ST). You can use one from
a larger "main" software project or just use any config.guess available
on your system. You can also get it directly from GNU:
ftp://ftp.gnu.org/gnu/autoconf/
To build rpms (RedHat Linux 6.2 or later, Linux/Mandrake, Solaris with
gnome, etc.):
download the latest st-x.y.tar.gz
# rpm -ta st-x.y.tar.gz
The .rpms will land in /usr/src/RPMS/<arch>. 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 <your-favourite-debian-mirror> 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.

View file

@ -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=<yes|no> [default: yes]`, Controls whether Memcheck reports uses of undefined value errors. Set this to no if you don't want to see undefined value errors. It also has the side effect of speeding up Memcheck somewhat.
1. `--leak-check=<no|summary|yes|full> [default: summary]`, When enabled, search for memory leaks when the client program finishes. If set to summary, it says how many leaks occurred. If set to full or yes, each individual leak will be shown in detail and/or counted as an error, as specified by the options `--show-leak-kinds` and `--errors-for-leak-kinds`.
1. `--track-origins=<yes|no> [default: no]`, Controls whether Memcheck tracks the origin of uninitialised values. By default, it does not, which means that although it can tell you that an uninitialised value is being used in a dangerous way, it cannot tell you where the uninitialised value came from. This often makes it difficult to track down the root problem.
1. `--show-reachable=<yes|no> , --show-possibly-lost=<yes|no>`, 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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 <stddef.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <setjmp.h>
/* Enable assertions only if DEBUG is defined */
#ifndef DEBUG
#define NDEBUG
#endif
#include <assert.h>
#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__ */

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.2 KiB

View file

@ -1,434 +0,0 @@
<HTML>
<HEAD>
<TITLE>State Threads Library Programming Notes</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H2>Programming Notes</H2>
<P>
<B>
<UL>
<LI><A HREF=#porting>Porting</A></LI>
<LI><A HREF=#signals>Signals</A></LI>
<LI><A HREF=#intra>Intra-Process Synchronization</A></LI>
<LI><A HREF=#inter>Inter-Process Synchronization</A></LI>
<LI><A HREF=#nonnet>Non-Network I/O</A></LI>
<LI><A HREF=#timeouts>Timeouts</A></LI>
</UL>
</B>
<P>
<HR>
<P>
<A NAME="porting">
<H3>Porting</H3>
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:
<P>
<UL>
<LI><I>Thread context initialization</I>: Two ingredients of the
<TT>jmp_buf</TT>
data structure (the program counter and the stack pointer) have to be
manually set in the thread creation routine. The <TT>jmp_buf</TT> data
structure is defined in the <TT>setjmp.h</TT> header file and differs from
platform to platform. Usually the program counter is a structure member
with <TT>PC</TT> in the name and the stack pointer is a structure member
with <TT>SP</TT> in the name. One can also look in the
<A HREF="http://www.mozilla.org/source.html">Netscape's NSPR library source</A>
which already has this code for many UNIX-like platforms
(<TT>mozilla/nsprpub/pr/include/md/*.h</TT> files).
<P>
Note that on some BSD-derived platforms <TT>_setjmp(3)/_longjmp(3)</TT>
calls should be used instead of <TT>setjmp(3)/longjmp(3)</TT> (that is
the calls that manipulate only the stack and registers and do <I>not</I>
save and restore the process's signal mask).</LI>
<P>
Starting with glibc 2.4 on Linux the opacity of the <TT>jmp_buf</TT> data
structure is enforced by <TT>setjmp(3)/longjmp(3)</TT> so the
<TT>jmp_buf</TT> 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 <TT>setjmp/longjmp</TT> replacement functions for
all Intel CPU architectures. Other CPU architectures can also be easily
supported (the <TT>setjmp/longjmp</TT> source code is widely available for
many CPU architectures).
<P>
<LI><I>High resolution time function</I>: 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 <TT>gettimeofday(3)</TT> function can be used
(though on some platforms it involves a system call).
<P>
<LI><I>The stack growth direction</I>: 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.</LI>
<P>
<LI><I>Non-blocking attribute inheritance</I>: On some platforms (e.g. IRIX)
the socket created as a result of the <TT>accept(2)</TT> 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.</LI>
<P>
<LI><I>Anonymous memory mapping</I>: The library allocates memory segments
for thread stacks by doing anonymous memory mapping (<TT>mmap(2)</TT>). This
mapping is somewhat different on SVR4 and BSD4.3 derived platforms.
<P>
The memory mapping can be avoided altogether by using <TT>malloc(3)</TT> for
stack allocation. In this case the <TT>MALLOC_STACK</TT> macro should be
defined.</LI>
</UL>
<P>
All machine-dependent feature test macros should be defined in the
<TT>md.h</TT> header file. The assembly code for <TT>setjmp/longjmp</TT>
replacement functions for all CPU architectures should be placed in
the <TT>md.S</TT> file.
<P>
The current version of the library is ported to:
<UL>
<LI>IRIX 6.x (both 32 and 64 bit)</LI>
<LI>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)</LI>
<LI>Solaris 2.x (SunOS 5.x) on x86, AMD64, SPARC, and SPARC-64</LI>
<LI>AIX 4.x</LI>
<LI>HP-UX 11 (both 32 and 64 bit)</LI>
<LI>Tru64/OSF1</LI>
<LI>FreeBSD on x86, AMD64, and Alpha</LI>
<LI>OpenBSD on x86, AMD64, Alpha, and SPARC</LI>
<LI>NetBSD on x86, Alpha, SPARC, and VAX</LI>
<LI>MacOS X (Darwin) on PowerPC (32 bit) and Intel (both 32 and 64 bit) [universal]</LI>
<LI>Cygwin</LI>
</UL>
<P>
<A NAME="signals">
<H3>Signals</H3>
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):
<PRE>
/* 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);
.
.
.
}
</PRE>
<P>
Note that if multiple processes are used (see below), the signal pipe should
be initialized after the <TT>fork(2)</TT> call so that each process has its
own private pipe.
<P>
<A NAME="intra">
<H3>Intra-Process Synchronization</H3>
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":<TT> </TT>I/O functions (<TT>st_read(), st_write(), </TT>etc.),
sleep functions (<TT>st_sleep(), </TT>etc.), and thread synchronization
functions (<TT>st_thread_join(), st_cond_wait(), </TT>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.
<P>
<A NAME="inter">
<H3>Inter-Process Synchronization</H3>
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 (<B>N</B> of <B>M:1</B> mappings rather than one <B>M:N</B>
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.<P>
There are several reasons for creating multiple processes:
<P>
<UL>
<LI>To take advantage of multiple hardware entities (CPUs, disks, etc.)
available in the system (hardware parallelism).</LI>
<P>
<LI>To reduce risk of losing a large number of user connections when one of
the processes crashes. For example, if <B>C</B> user connections (threads)
are multiplexed onto <B>P</B> processes and one of the processes crashes,
only a fraction (<B>C/P</B>) of all connections will be lost.</LI>
<P>
<LI>To overcome per-process resource limitations imposed by the OS. For
example, if <TT>select(2)</TT> is used for event polling, the number of
simultaneous connections (threads) per process is
limited by the <TT>FD_SETSIZE</TT> parameter (see <TT>select(2)</TT>).
If <TT>FD_SETSIZE</TT> is equal to 1024 and each connection needs one file
descriptor, then an application should create 10 processes to support 10,000
simultaneous connections.</LI>
</UL>
<P>
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:
<PRE>
#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;
}
</PRE>
<P>
Generally, the following steps should be followed when writing an application
using the State Threads library:
<P>
<OL>
<LI>Initialize the library (<TT>st_init()</TT>).</LI>
<P>
<LI>Create resources that will be shared among different processes:
create and bind listening sockets, create shared memory segments, IPC
channels, synchronization primitives, etc.</LI>
<P>
<LI>Create several processes (<TT>fork(2)</TT>). 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.).</LI>
<P>
<LI>In each child process create a pool of threads
(<TT>st_thread_create()</TT>) to handle user connections.</LI>
</OL>
<P>
<A NAME="nonnet">
<H3>Non-Network I/O</H3>
The State Threads architecture uses non-blocking I/O on
<TT>st_netfd_t</TT> 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 <I>disk</I> 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 <I>reads</I>; disk <I>writes</I> 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:
<P>
<UL>
<LI>Create several identical main processes as described above (symmetric
architecture). This will improve CPU utilization and thus improve the
overall throughput of the system.</LI>
<P>
<LI>Create multiple "helper" processes in addition to the main process that
will handle blocking I/O operations (asymmetric architecture).
This approach was suggested for Web servers in a
<A HREF="http://www.cs.rice.edu/~vivek/flash99/">paper</A> by Peter
Druschel et al. In this architecture the main process communicates with
a helper process via an IPC channel (<TT>pipe(2), socketpair(2)</TT>).
The main process instructs a helper to perform the potentially blocking
operation. Once the operation completes, the helper returns a
notification via IPC.
</UL>
<P>
<A NAME="timeouts">
<H3>Timeouts</H3>
The <TT>timeout</TT> parameter to <TT>st_cond_timedwait()</TT> and the
I/O functions, and the arguments to <TT>st_sleep()</TT> and
<TT>st_usleep()</TT> specify a maximum time to wait <I>since the last
context switch</I> not since the beginning of the function call.
<P>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.
<P>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,
<TT>select()</TT>'s timeout is an <I>upper</I> bound on the amount of
time elapsed before select returns.) Similarly, if 12 ms have passed
already, the function may return immediately.
<P>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.
<P>The largest valid timeout value is platform-dependent and may be
significantly less than <TT>INT_MAX</TT> seconds for <TT>select()</TT>
or <TT>INT_MAX</TT> milliseconds for <TT>poll()</TT>. Generally, you
should not use timeouts exceeding several hours. Use
<tt>ST_UTIME_NO_TIMEOUT</tt> (<tt>-1</tt>) as a special value to
indicate infinite timeout or indefinite sleep. Use
<tt>ST_UTIME_NO_WAIT</tt> (<tt>0</tt>) to indicate no waiting at all.
<P>
<HR>
<P>
</BODY>
</HTML>

File diff suppressed because it is too large Load diff

View file

@ -1,504 +0,0 @@
<HTML>
<HEAD>
<TITLE>State Threads for Internet Applications</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H2>State Threads for Internet Applications</H2>
<H3>Introduction</H3>
<P>
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.</P>
<H3>1. Definitions</H3>
<P>
<A NAME="IA">
<H4>1.1 Internet Applications</H4>
</A>
<P>
An <I>Internet Application</I> (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 <I>data-driven</I> 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.</P>
<P>
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).
<P>
<A NAME="PS">
<H4>1.2 Performance and Scalability</H4>
</A>
<P>
The <I>performance</I> 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
<A HREF="http://www.spec.org/osg/web96/">SPECweb96</A>,
<A HREF="http://www.mindcraft.com/webstone/">WebStone</A>,
<A HREF="http://www.zdnet.com/zdbop/webbench/">WebBench</A>).
Although there is no common definition for <I>scalability</I>, 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: <I>load
scalability</I> and <I>system scalability</I>, respectively.
<P>
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.
<P>
We say that an application has a good <I>load scalability</I> if it can
sustain its throughput over a wide range of loads.
Interestingly, the <A HREF="http://www.spec.org/osg/web99/">SPECweb99</A>
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 <I>capacity</I>).
This is unlike <A HREF="http://www.spec.org/osg/web96/">SPECweb96</A> and
other benchmarks that use the throughput as their main metric (see the figure
below).
<P>
<CENTER><IMG SRC="fig.gif" ALT="Figure: Throughput vs. Number of clients">
</CENTER>
<P>
<I>System scalability</I> 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.
<P>
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).
<P>
<A NAME="CONC">
<H4>1.3 Concurrency</H4>
</A>
<P>
Concurrency reflects the parallelism in a system. The two unrelated types
are <I>virtual</I> concurrency and <I>real</I> concurrency.
<UL>
<LI>Virtual (or apparent) concurrency is the number of simultaneous
connections that a system supports.
<BR><BR>
<LI>Real concurrency is the number of hardware devices, including
CPUs, network cards, and disks, that actually allow a system to perform
tasks in parallel.
</UL>
<P>
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
<I>kernel execution vehicles</I>. 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.
<P>
<H3>2. Existing Architectures</H3>
<P>
There are a few different architectures that are commonly used by IAs.
These include the <I>Multi-Process</I>,
<I>Multi-Threaded</I>, and <I>Event-Driven State Machine</I>
architectures.
<P>
<A NAME="MP">
<H4>2.1 Multi-Process Architecture</H4>
</A>
<P>
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.
<P>
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.
<P>
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.
<P>
The Apache Web Server 1.x (<A HREF=#refs1>[Reference 1]</A>) uses the MP
architecture on UNIX systems.
<P>
<A NAME="MT">
<H4>2.2 Multi-Threaded Architecture</H4>
</A>
<P>
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.
<P>
Many modern UNIX operating systems implement a <I>many-to-few</I> 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 <I>virtual processors</I>. 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.
<P>
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 <I>one-to-one</I> 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.
<P>
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
(<TT>malloc(3)</TT>, <TT>free(3)</TT>, 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 <TT>open(2)</TT>, <TT>close(2)</TT>, 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.
<P>
In order to improve load scalability, some applications employ a different
type of MT architecture: they create one or more thread(s) <I>per task</I>
rather than one thread <I>per connection</I>. 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.
<P>
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.
<P>
<A NAME="EDSM">
<H4>2.3 Event-Driven State Machine Architecture</H4>
</A>
<P>
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
<A HREF=#refs2>[Reference 2]</A>.
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.
<P>
In the idle state the EDSM calls <TT>select(2)</TT> or <TT>poll(2)</TT> 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
<A HREF=#refs3>[Reference 3]</A>.
<P>
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
(<A HREF=#refs4>[Reference 4]</A>). 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 <A HREF=#refs5>[Reference 5]</A>.
<P>
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.
<P>
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.
<P>
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.
<P>
<A NAME="ST">
<H3>3. State Threads Library</H3>
</A>
<P>
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.
<P>
<H4>3.1 State Changes and Scheduling</H4>
<P>
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 <I>co-routine</I> 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.
<P>
Only two types of external events are handled by the library's
scheduler, because only these events can be detected by
<TT>select(2)</TT> or <TT>poll(2)</TT>: 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
(<TT>write(2)</TT> is reentrant/asynchronous-safe), thus converting a signal
event to an I/O event.
<P>
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.
<P>
There are several general-purpose threading libraries that implement a
<I>many-to-one</I> 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 (<A HREF=#refs6>[Reference 6]</A>). Because they are
general-purpose, these libraries have different objectives than the State
Threads library. The State Threads library is <I>not</I> 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.
<P>
<H4>3.2 Scalability</H4>
<P>
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.
<P>
On multiprocessor systems an application should create multiple processes
to take advantage of hardware parallelism. Using multiple separate processes
is the <I>only</I> 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.
<P>
<H4>3.3 Performance</H4>
<P>
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).
<P>
<H4>3.4 Portability</H4>
<P>
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.
<P>
<H4>3.5 State Threads and NSPR</H4>
<P>
The State Threads library is a derivative of the Netscape Portable
Runtime library (NSPR) <A HREF=#refs7>[Reference 7]</A>. 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.
<P>
<TABLE CELLPADDING=3>
<TR>
<TD></TD>
<TH>NSPR</TH>
<TH>State Threads</TH>
</TR>
<TR>
<TD><B>Lines of code</B></TD>
<TD ALIGN=RIGHT>~150,000</TD>
<TD ALIGN=RIGHT>~3000</TD>
</TR>
<TR>
<TD><B>Dynamic library size&nbsp;&nbsp;<BR>(debug version)</B></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD>IRIX</TD>
<TD ALIGN=RIGHT>~700 KB</TD>
<TD ALIGN=RIGHT>~60 KB</TD>
</TR>
<TR>
<TD>Linux</TD>
<TD ALIGN=RIGHT>~900 KB</TD>
<TD ALIGN=RIGHT>~70 KB</TD>
</TR>
</TABLE>
<P>
<H3>Conclusion</H3>
<P>
State Threads is an application library which provides a foundation for
writing <A HREF=#IA>Internet Applications</A>. To summarize, it has the
following <I>advantages</I>:
<P>
<UL>
<LI>It allows the design of fast and highly scalable applications. An
application will scale well with both load and number of CPUs.
<P>
<LI>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.
</UL>
<P>
The library's main <I>limitation</I>:
<P>
<UL>
<LI>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.
</UL>
<P>
<H3>References</H3>
<OL>
<A NAME="refs1">
<LI> Apache Software Foundation,
<A HREF="http://www.apache.org">http://www.apache.org</A>.
<A NAME="refs2">
<LI> Douglas E. Comer, David L. Stevens, <I>Internetworking With TCP/IP,
Vol. III: Client-Server Programming And Applications</I>, Second Edition,
Ch. 8, 12.
<A NAME="refs3">
<LI> W. Richard Stevens, <I>UNIX Network Programming</I>, Second Edition,
Vol. 1, Ch. 15.
<A NAME="refs4">
<LI> Zeus Technology Limited,
<A HREF="http://www.zeus.co.uk/">http://www.zeus.co.uk</A>.
<A NAME="refs5">
<LI> Peter Druschel, Vivek S. Pai, Willy Zwaenepoel,
<A HREF="http://www.cs.rice.edu/~druschel/usenix99flash.ps.gz">
Flash: An Efficient and Portable Web Server</A>. In <I>Proceedings of the
USENIX 1999 Annual Technical Conference</I>, Monterey, CA, June 1999.
<A NAME="refs6">
<LI> GNU Portable Threads,
<A HREF="http://www.gnu.org/software/pth/">http://www.gnu.org/software/pth/</A>.
<A NAME="refs7">
<LI> Netscape Portable Runtime,
<A HREF="http://www.mozilla.org/docs/refList/refNSPR/">http://www.mozilla.org/docs/refList/refNSPR/</A>.
</OL>
<H3>Other resources covering various architectural issues in IAs</H3>
<OL START=8>
<LI> Dan Kegel, <I>The C10K problem</I>,
<A HREF="http://www.kegel.com/c10k.html">http://www.kegel.com/c10k.html</A>.
</LI>
<LI> James C. Hu, Douglas C. Schmidt, Irfan Pyarali, <I>JAWS: Understanding
High Performance Web Systems</I>,
<A HREF="http://www.cs.wustl.edu/~jxh/research/research.html">http://www.cs.wustl.edu/~jxh/research/research.html</A>.</LI>
</OL>
<P>
<HR>
<P>
<CENTER><FONT SIZE=-1>Portions created by SGI are Copyright &copy; 2000
Silicon Graphics, Inc. All rights reserved.</FONT></CENTER>
<P>
</BODY>
</HTML>

View file

@ -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.

File diff suppressed because it is too large Load diff

View file

@ -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 <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#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;
}

View file

@ -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 <stdlib.h>
#include <errno.h>
#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;
}
}
}

View file

@ -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__ */

View file

@ -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

View file

@ -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 R12R15, 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

View file

@ -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

View file

@ -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 <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <time.h>
#include <errno.h>
#include <poll.h>
#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__ */

View file

@ -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 <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "common.h"
/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */
#ifndef NVALGRIND
#include <valgrind/valgrind.h>
#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 */

View file

@ -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}

View file

@ -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 <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#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;
}

View file

@ -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 <stdlib.h>
#include <time.h>
#include <errno.h>
#include "common.h"
extern time_t _st_curr_time;
extern st_utime_t _st_last_tset;
extern int _st_active_count;
static st_utime_t (*_st_utime)(void) = NULL;
/*****************************************
* Time functions
*/
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;
}

View file

@ -1 +0,0 @@
helloworld

View file

@ -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

View file

@ -1,19 +0,0 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021 Winlin */
#include <stdio.h>
#include <st.h>
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;
}

View file

@ -1 +0,0 @@
porting

View file

@ -1,10 +0,0 @@
.PHONY: clean
CFLAGS=-g -O0
./porting: porting.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)
clean:
rm -f porting

View file

@ -1,208 +0,0 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021 Winlin */
#include <stdio.h>
#include <setjmp.h>
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(" ");
}
}
}

View file

@ -1 +0,0 @@
verify

View file

@ -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

View file

@ -1,134 +0,0 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021 Winlin */
#include <stdio.h>
#include <setjmp.h>
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(" ");
}
}
}

View file

@ -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 $^

View file

@ -1,139 +0,0 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2021 Winlin */
#include <st_utest.hpp>
#include <st.h>
#include <assert.h>
// 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);
}

View file

@ -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 <gtest/gtest.h>
#define VOID
#endif