From 7c8a35aea9b6108bffdc39390cdb00db525be2ac Mon Sep 17 00:00:00 2001 From: xiaozhihong Date: Sun, 16 Feb 2020 21:07:54 +0800 Subject: [PATCH] use libco instead of state-thread(st), still have some bug --- trunk/3rdparty/libco/.gitignore | 1 + trunk/3rdparty/libco/CMakeLists.txt | 55 + trunk/3rdparty/libco/LICENSE.txt | Bin 0 -> 18962 bytes trunk/3rdparty/libco/Makefile | 84 + trunk/3rdparty/libco/co.mk | 85 + trunk/3rdparty/libco/co_closure.h | 96 + trunk/3rdparty/libco/co_epoll.cpp | 318 ++ trunk/3rdparty/libco/co_epoll.h | 93 + trunk/3rdparty/libco/co_hook_sys_call.cpp | 1003 ++++++ trunk/3rdparty/libco/co_routine.cpp | 1196 +++++++ trunk/3rdparty/libco/co_routine.h | 93 + trunk/3rdparty/libco/co_routine_inner.h | 111 + trunk/3rdparty/libco/co_routine_specific.h | 86 + trunk/3rdparty/libco/coctx.cpp | 132 + trunk/3rdparty/libco/coctx.h | 42 + trunk/3rdparty/libco/coctx_swap.S | 83 + trunk/3rdparty/libco/example_closure.cpp | 91 + trunk/3rdparty/libco/example_cond.cpp | 83 + trunk/3rdparty/libco/example_copystack.cpp | 61 + trunk/3rdparty/libco/example_echocli.cpp | 218 ++ trunk/3rdparty/libco/example_echosvr.cpp | 255 ++ trunk/3rdparty/libco/example_poll.cpp | 211 ++ trunk/3rdparty/libco/example_setenv.cpp | 89 + trunk/3rdparty/libco/example_specific.cpp | 61 + trunk/3rdparty/libco/example_thread.cpp | 56 + trunk/3rdparty/st-srs/.gitignore | 4 - trunk/3rdparty/st-srs/Makefile | 474 --- trunk/3rdparty/st-srs/README | 394 --- trunk/3rdparty/st-srs/README.md | 88 - trunk/3rdparty/st-srs/common.h | 479 --- trunk/3rdparty/st-srs/docs/fig.gif | Bin 5374 -> 0 bytes trunk/3rdparty/st-srs/docs/notes.html | 434 --- trunk/3rdparty/st-srs/docs/reference.html | 3120 ----------------- trunk/3rdparty/st-srs/docs/st.html | 504 --- trunk/3rdparty/st-srs/docs/timeout_heap.txt | 60 - trunk/3rdparty/st-srs/event.c | 1413 -------- trunk/3rdparty/st-srs/examples/Makefile | 115 - trunk/3rdparty/st-srs/examples/README | 98 - trunk/3rdparty/st-srs/examples/error.c | 168 - trunk/3rdparty/st-srs/examples/lookupdns.c | 103 - trunk/3rdparty/st-srs/examples/proxy.c | 541 --- trunk/3rdparty/st-srs/examples/res.c | 305 -- trunk/3rdparty/st-srs/examples/server.c | 1025 ------ trunk/3rdparty/st-srs/extensions/Makefile | 91 - trunk/3rdparty/st-srs/extensions/README | 42 - trunk/3rdparty/st-srs/extensions/common.h | 77 - trunk/3rdparty/st-srs/extensions/dnscache.c | 190 - trunk/3rdparty/st-srs/extensions/dnsres.c | 305 -- trunk/3rdparty/st-srs/extensions/lrucache.c | 343 -- .../st-srs/extensions/print_stk.patch | 367 -- trunk/3rdparty/st-srs/extensions/stx.h | 91 - trunk/3rdparty/st-srs/extensions/stx_fileio.c | 197 -- trunk/3rdparty/st-srs/extensions/stx_fileio.h | 52 - trunk/3rdparty/st-srs/extensions/testdns.c | 112 - trunk/3rdparty/st-srs/io.c | 769 ---- trunk/3rdparty/st-srs/key.c | 121 - trunk/3rdparty/st-srs/libst.def | 51 - trunk/3rdparty/st-srs/md.S | 644 ---- trunk/3rdparty/st-srs/md.h | 641 ---- trunk/3rdparty/st-srs/osguess.sh | 45 - trunk/3rdparty/st-srs/public.h | 166 - trunk/3rdparty/st-srs/sched.c | 705 ---- trunk/3rdparty/st-srs/st.pc.in | 10 - trunk/3rdparty/st-srs/st.spec | 79 - trunk/3rdparty/st-srs/stk.c | 173 - trunk/3rdparty/st-srs/sync.c | 368 -- trunk/auto/depends.sh | 31 +- trunk/configure | 34 +- trunk/research/st/Makefile | 100 - trunk/research/st/common.h | 445 --- trunk/research/st/event.c | 483 --- trunk/research/st/io.c | 792 ----- trunk/research/st/key.c | 116 - trunk/research/st/md.S | 151 - trunk/research/st/md.h | 193 - trunk/research/st/public.h | 164 - trunk/research/st/sched.c | 680 ---- trunk/research/st/srs.c | 497 --- trunk/research/st/st/init | 3 - trunk/research/st/st/st.upp | 18 - trunk/research/st/stk.c | 169 - trunk/research/st/sync.c | 352 -- trunk/src/app/srs_app_listener.cpp | 10 +- trunk/src/app/srs_app_server.cpp | 12 +- trunk/src/app/srs_app_st.cpp | 27 +- trunk/src/app/srs_app_st.hpp | 3 + trunk/src/service/srs_service_st.cpp | 260 +- trunk/src/service/srs_service_st.hpp | 2 +- 88 files changed, 4836 insertions(+), 19273 deletions(-) create mode 100644 trunk/3rdparty/libco/.gitignore create mode 100644 trunk/3rdparty/libco/CMakeLists.txt create mode 100644 trunk/3rdparty/libco/LICENSE.txt create mode 100644 trunk/3rdparty/libco/Makefile create mode 100644 trunk/3rdparty/libco/co.mk create mode 100644 trunk/3rdparty/libco/co_closure.h create mode 100644 trunk/3rdparty/libco/co_epoll.cpp create mode 100644 trunk/3rdparty/libco/co_epoll.h create mode 100644 trunk/3rdparty/libco/co_hook_sys_call.cpp create mode 100644 trunk/3rdparty/libco/co_routine.cpp create mode 100644 trunk/3rdparty/libco/co_routine.h create mode 100644 trunk/3rdparty/libco/co_routine_inner.h create mode 100644 trunk/3rdparty/libco/co_routine_specific.h create mode 100644 trunk/3rdparty/libco/coctx.cpp create mode 100644 trunk/3rdparty/libco/coctx.h create mode 100644 trunk/3rdparty/libco/coctx_swap.S create mode 100644 trunk/3rdparty/libco/example_closure.cpp create mode 100644 trunk/3rdparty/libco/example_cond.cpp create mode 100644 trunk/3rdparty/libco/example_copystack.cpp create mode 100644 trunk/3rdparty/libco/example_echocli.cpp create mode 100644 trunk/3rdparty/libco/example_echosvr.cpp create mode 100644 trunk/3rdparty/libco/example_poll.cpp create mode 100644 trunk/3rdparty/libco/example_setenv.cpp create mode 100644 trunk/3rdparty/libco/example_specific.cpp create mode 100644 trunk/3rdparty/libco/example_thread.cpp delete mode 100644 trunk/3rdparty/st-srs/.gitignore delete mode 100644 trunk/3rdparty/st-srs/Makefile delete mode 100644 trunk/3rdparty/st-srs/README delete mode 100644 trunk/3rdparty/st-srs/README.md delete mode 100644 trunk/3rdparty/st-srs/common.h delete mode 100644 trunk/3rdparty/st-srs/docs/fig.gif delete mode 100644 trunk/3rdparty/st-srs/docs/notes.html delete mode 100644 trunk/3rdparty/st-srs/docs/reference.html delete mode 100644 trunk/3rdparty/st-srs/docs/st.html delete mode 100644 trunk/3rdparty/st-srs/docs/timeout_heap.txt delete mode 100644 trunk/3rdparty/st-srs/event.c delete mode 100644 trunk/3rdparty/st-srs/examples/Makefile delete mode 100644 trunk/3rdparty/st-srs/examples/README delete mode 100644 trunk/3rdparty/st-srs/examples/error.c delete mode 100644 trunk/3rdparty/st-srs/examples/lookupdns.c delete mode 100644 trunk/3rdparty/st-srs/examples/proxy.c delete mode 100644 trunk/3rdparty/st-srs/examples/res.c delete mode 100644 trunk/3rdparty/st-srs/examples/server.c delete mode 100644 trunk/3rdparty/st-srs/extensions/Makefile delete mode 100644 trunk/3rdparty/st-srs/extensions/README delete mode 100644 trunk/3rdparty/st-srs/extensions/common.h delete mode 100644 trunk/3rdparty/st-srs/extensions/dnscache.c delete mode 100644 trunk/3rdparty/st-srs/extensions/dnsres.c delete mode 100644 trunk/3rdparty/st-srs/extensions/lrucache.c delete mode 100644 trunk/3rdparty/st-srs/extensions/print_stk.patch delete mode 100644 trunk/3rdparty/st-srs/extensions/stx.h delete mode 100644 trunk/3rdparty/st-srs/extensions/stx_fileio.c delete mode 100644 trunk/3rdparty/st-srs/extensions/stx_fileio.h delete mode 100644 trunk/3rdparty/st-srs/extensions/testdns.c delete mode 100644 trunk/3rdparty/st-srs/io.c delete mode 100644 trunk/3rdparty/st-srs/key.c delete mode 100644 trunk/3rdparty/st-srs/libst.def delete mode 100644 trunk/3rdparty/st-srs/md.S delete mode 100644 trunk/3rdparty/st-srs/md.h delete mode 100644 trunk/3rdparty/st-srs/osguess.sh delete mode 100644 trunk/3rdparty/st-srs/public.h delete mode 100644 trunk/3rdparty/st-srs/sched.c delete mode 100644 trunk/3rdparty/st-srs/st.pc.in delete mode 100644 trunk/3rdparty/st-srs/st.spec delete mode 100644 trunk/3rdparty/st-srs/stk.c delete mode 100644 trunk/3rdparty/st-srs/sync.c delete mode 100755 trunk/research/st/Makefile delete mode 100644 trunk/research/st/common.h delete mode 100644 trunk/research/st/event.c delete mode 100644 trunk/research/st/io.c delete mode 100644 trunk/research/st/key.c delete mode 100755 trunk/research/st/md.S delete mode 100644 trunk/research/st/md.h delete mode 100644 trunk/research/st/public.h delete mode 100755 trunk/research/st/sched.c delete mode 100644 trunk/research/st/srs.c delete mode 100644 trunk/research/st/st/init delete mode 100755 trunk/research/st/st/st.upp delete mode 100644 trunk/research/st/stk.c delete mode 100644 trunk/research/st/sync.c diff --git a/trunk/3rdparty/libco/.gitignore b/trunk/3rdparty/libco/.gitignore new file mode 100644 index 0000000000..42061c01a1 --- /dev/null +++ b/trunk/3rdparty/libco/.gitignore @@ -0,0 +1 @@ +README.md \ No newline at end of file diff --git a/trunk/3rdparty/libco/CMakeLists.txt b/trunk/3rdparty/libco/CMakeLists.txt new file mode 100644 index 0000000000..04399a55a3 --- /dev/null +++ b/trunk/3rdparty/libco/CMakeLists.txt @@ -0,0 +1,55 @@ +cmake_minimum_required(VERSION 2.8) +project(libco) + +# This for mac osx only +set(CMAKE_MACOSX_RPATH 0) + +# Set lib version +set(LIBCO_VERSION 0.5) + +# Set cflags +set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -g -fno-strict-aliasing -O2 -Wall -export-dynamic -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64) + +# Use c and asm +enable_language(C ASM) + +# Add source files +set(SOURCE_FILES + co_epoll.cpp + co_hook_sys_call.cpp + co_routine.cpp + coctx.cpp + coctx_swap.S) + +# Add static and shared library target +add_library(colib_static STATIC ${SOURCE_FILES}) +add_library(colib_shared SHARED ${SOURCE_FILES}) + +# Set library output name +set_target_properties(colib_static PROPERTIES OUTPUT_NAME colib) +set_target_properties(colib_shared PROPERTIES OUTPUT_NAME colib) + +set_target_properties(colib_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) +set_target_properties(colib_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) + +# Set shared library version, will generate libcolib.${LIBCO_VERSION}.so and a symbol link named libcolib.so +# For mac osx, the extension name will be .dylib +set_target_properties(colib_shared PROPERTIES VERSION ${LIBCO_VERSION} SOVERSION ${LIBCO_VERSION}) + + + +# Macro for add example target +macro(add_example_target EXAMPLE_TARGET) + add_executable("example_${EXAMPLE_TARGET}" "example_${EXAMPLE_TARGET}.cpp") + target_link_libraries("example_${EXAMPLE_TARGET}" colib_static pthread dl) +endmacro(add_example_target) + +add_example_target(closure) +add_example_target(cond) +add_example_target(copystack) +add_example_target(echocli) +add_example_target(echosvr) +add_example_target(poll) +add_example_target(setenv) +add_example_target(specific) +add_example_target(thread) diff --git a/trunk/3rdparty/libco/LICENSE.txt b/trunk/3rdparty/libco/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..bff49d4bc3a55637e1ed4bd7eeb9834a2b16dbd4 GIT binary patch literal 18962 zcmeI4TW=jn6@~j1B>sVs`US}&kwAtSV1_4;osfuNM|MJ#M_*#cjBj&}9mo85V1Ko| z*j3$qeC)s=9#CYTIJeOY_$U6;;3{{Fgq-<@^)<@aURcgNj4eqVGaab-8YzwZv> z@5}Bye$TtBxN{NTXK{tAAL4!=e_xd6X>lHHW~IMAexG-z@$V`|IF8mg(RQ!=K8?P= zk85Y$QCxc&-}XxDKCXX^pW_&RH?ASY<7m;gk&GwNN^)CD#yN;5X3^fVt%Yp6(N2;w zQaQ1pk#0Zhe%kS~UaWW%vLA;{KEw!pw;ET!h|e!Wq9@%~@$Gf@Jgn7sYjN$H_(Y%6 z__+)%4sNBURqIpv0Xv+94zlN3jDHe6l3-S%!PUqt`2K*D*!KJuqs zj635vqXsEg`gVM*?oY;s@ixtk)L`R**2Dl-H*aWsG?(rfQE=69*x7Qx z42Pi|6#%t@Dg-cr<-U&m_KRXYYs9B2i#prF7?&~nWziUmD2Bw0JTWi2)pAL)y|_oL zBgcOHD4G__J8ic*D&GZiu1XB9-ePQ}*ERSEBuw))zEVAa{2${7&b2&v;$_G=74P`) zQ~Y2%d|>TmPpW`(HEv7u38Vi4GCv4Z&{vsuWwtL|LR)8+=!Gz76#)( zYs*J9dRh+teuz>ck~yim`9>KaNhs5zCmg(qF@)X^)S zNk3^sOPE}9GyE@~Sjx2FYP7;G@ZWC13^jyDQRONotw(V`%uO^uMYm7!9NFL`Jaip7 zr{?sHxQ=#MkthH)eP^%F@9?{0)iVF5e2WgXMfc(_vag~An>hP`0LiYq=7qW@u_no4 z=45b)7hO%EH~mlp;VoxO7zj&yzN~zU_CkW}rVNA)uru>``WW653fc%qWLM6 zL*f>6q5*ZM7+rXgyKm@aUY^*gC7(#ggE6LK-hCFvtvI<%AtXRSb+ACK;mSP^8 z@r)3}`i~=uCpF(Zj5e&NARQKEG`AX~ZMjACEK<^29h>WMMRSOD{<)ZohcS7V&yl;f zJl|QFvMysJwHd{IyZu;qOS8D%v7cabMV>1mGWD$`nYX{E|q*$BNAR z%>wR{S3aGx2bR6yfEodbTqDNn)GnJHmd&DE|#L#zdF?^o;L?Q@D3 zMXKjA$mcn~85=LkvWw8_sGs(V{-tTw!kp_NNCIVu)4yCFTKV&}DNZK#4E=Vg+ILo?rQk)ZAtu0j1vfi)y zd>R{ATJ@bUnyWwbr<|tsDS1q*M*O}E9nAXI#g-?56zoU-AzG4SuF&JL=SPSK^kI{x4;+hrP3~dhc=8l~;anA6Lh5A(%Q6&t= zEw74Pv%+;q!B|3;c#eJ0-H;!R#%*LnXIR<@sjbXhxA)a+9sSAx+MP5yvpxOE>&tZ! z(%Y39<%8+9gN%XSEQoKyX&Q(7S`d7o!j6gL64=H?+DUqz2#}S`8RiC5S%GoZ2*?6|m2SPhQB&oDNcVjD%dB!xpygUje0B)B?PR(r?@ z&Y<|c^^K8GuNvKDA<1CLS&=6vi7V?lC7Jj|psnwmsnCK5V6MPQ4cd+o&V6=FQd@Sz zv^v7m;#1br#bxxyU+i3s_ZPI*N&ce3C9@eNwlp?opAvc4CCU5r$7t+I>;`_(k=a(b&x~8Pn%MQ z)=@_VM@C~ufKiD}p&qTwD)Ua~m6)aNmWcjIJJxwlXkYI9WrdIoK3-^s#8IhrU+d5#KS908Lm zwp>YSty^_HPhmNHWZRE@M&?Iq=S$&{2=`9wSg-wsdVym`WIRwKq`A))ajx!dS9^82 zSH@aQMXC_5lQxRk`jnC|E!IgcWv&Xoy~3eBv;srhdVb;jKeV~`2a*-;*4x%GIvF$P zgJ_nkJ+8KfW2QyeyUiVZ&7D8z;*=Fh<0|Eh1E73^fuC$mTk?2{MT+ z)^E1TIllEcf5jRM`QlUjPW;i5_Cr}=53Y|rFTY?#$x-LVWt!=kmes~1kOf$&6))kR z)J>WZ2Oue{T_ezFw~g+LDzTvKy} z`9&)Wnjd@{PqtP9cddP6wbSWpSL^ef$wl7#WwF-h%e;4A-o(p^OICXh ziuF{FDY~!(t=Ox@8=T@}H9XfGuy^Z^I)+shrM2q3dRG;j=+f9(pUQjpwH!5?Pi<&* z-spnKW}%JZL)z9J20Na;qT|@lt}L_S)+}bRznynj74h2-t_tf@t@SJryrf)UEs?C; zt%}HVsMf0X-OzsWG0f_9FITPg`Ve~K%^%~$eM%rtwEINP%(Cs>HUP)!O|g#7nv*1o zmdA|oF0RWnHmyC$J1JAHAHq9o`8n?vv=yIY1g*p}v%>nUND95IK;kFOdd&_QWvWl` zt~n2+B+}4A`9s{z-NpF+x^VMO#EQ;FTInR7=nzA_Go7@qJjYOsiEN4OA;YAa#;^o;VUb!pkKwxn=b zufniHIDKC6{*jh=nVS1zC1b*C5srvyHmygN#8U{umzxEX?p@x}K3~h0)Y|NhYxP}~ z09Aroxt0(HYwO@2&67ZTosW_O>+IV~?uvyLJg;g5zQpE3W@Gh~xUHG@CZxWKwr11Z zHvq4!7;1f%2(lI0*pdC{m$g}{B5R#&vopKpHrlcJByVvtzC`T|DE6<_^40Ea&WxSfG_occ8np05!IOkcsY`0uis-kG2o` zkjK@wl|7llqajPJ`>;1f97u&phqh=dELXg_xB0t*mW_}FdA(AvN?nq@Eok)qzup^a znR#qSv0UyvQG0k7TPy#_Np{Xy!m`4X?9!es!G}Y?4(CfC!8T4iuErSh8$P}Jj6j>S z>5mM<=iDXi_44`GX| z<|yX@p3q)YR*|$rXYjB0NQ@F!7#edbCm5&b{5m}Qw8#QlH)A%4PS46aqMLE{RL$qs#u+|*TWAl%U@i5 z6Ft8Q8F-_WtmQoI`5}xzMWUKOwq;M3T{!Ac?j?)B0?sp7R@P07Dn6L$;3v+ID~i)& z5R=P zE!h~Gt7@3?jmIN;u&2F1+u=1%P11s~b(%vxYPADg`MPA4cc~up6rP@?wyLEwhAoYD zZ;T4%UKoeCWK32Qyu*5%(5B_Mim#*X3F14Va-6O7{MGx z7KZCHRu@GC^9uZ&=S7*B>7654xAn2!=jQK0ghCV##NBTJkU8`ZPQjfrx}gBg1z&9h@o{`H!1I6VOaV! zK9$>ODSb0jXkOadlvX;YS)cd83ewtiJ0qVd=-7gsCWHOZxR^xoh zv@|-^$l7dH7iM4U>e-nQp%QV)kM~*OzG)L6*%GOwaapFG#rFBM` literal 0 HcmV?d00001 diff --git a/trunk/3rdparty/libco/Makefile b/trunk/3rdparty/libco/Makefile new file mode 100644 index 0000000000..7db3749460 --- /dev/null +++ b/trunk/3rdparty/libco/Makefile @@ -0,0 +1,84 @@ +# +# Tencent is pleased to support the open source community by making Libco available. +# +# Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +COMM_MAKE = 1 +COMM_ECHO = 1 +version=0.5 +v=debug +include co.mk + +########## options ########## +CFLAGS += -g -fno-strict-aliasing -O2 -Wall -export-dynamic \ + -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64 + +UNAME := $(shell uname -s) + +ifeq ($(UNAME), FreeBSD) +LINKS += -g -L./lib -lcolib -lpthread +else +LINKS += -g -L./lib -lcolib -lpthread -ldl +endif + +COLIB_OBJS=co_epoll.o co_routine.o co_hook_sys_call.o coctx_swap.o coctx.o +#co_swapcontext.o + +PROGS = colib example_poll example_echosvr example_echocli example_thread example_cond example_specific example_copystack example_closure + +all:$(PROGS) + +colib:libcolib.a libcolib.so + +libcolib.a: $(COLIB_OBJS) + $(ARSTATICLIB) +libcolib.so: $(COLIB_OBJS) + $(BUILDSHARELIB) + +example_echosvr:example_echosvr.o + $(BUILDEXE) +example_echocli:example_echocli.o + $(BUILDEXE) +example_thread:example_thread.o + $(BUILDEXE) +example_poll:example_poll.o + $(BUILDEXE) +example_exit:example_exit.o + $(BUILDEXE) +example_cond:example_cond.o + $(BUILDEXE) +example_specific:example_specific.o + $(BUILDEXE) +example_copystack:example_copystack.o + $(BUILDEXE) +example_setenv:example_setenv.o + $(BUILDEXE) +example_closure:example_closure.o + $(BUILDEXE) + +dist: clean libco-$(version).src.tar.gz + +libco-$(version).src.tar.gz: + @find . -type f | grep -v CVS | grep -v .svn | sed s:^./:libco-$(version)/: > MANIFEST + @(cd ..; ln -s libco_pub libco-$(version)) + (cd ..; tar cvf - `cat libco_pub/MANIFEST` | gzip > libco_pub/libco-$(version).src.tar.gz) + @(cd ..; rm libco-$(version)) + +clean: + $(CLEAN) *.o $(PROGS) + rm -fr MANIFEST lib solib libco-$(version).src.tar.gz libco-$(version) + diff --git a/trunk/3rdparty/libco/co.mk b/trunk/3rdparty/libco/co.mk new file mode 100644 index 0000000000..29658b5116 --- /dev/null +++ b/trunk/3rdparty/libco/co.mk @@ -0,0 +1,85 @@ +# +# Tencent is pleased to support the open source community by making Libco available. +# +# Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + + +##### Makefile Rules ########## +MAIL_ROOT=. +SRCROOT=. + +##define the compliers +CPP = $(CXX) +AR = ar -rc +RANLIB = ranlib + +CPPSHARE = $(CPP) -fPIC -shared -O2 -pipe -L$(SRCROOT)/solib/ -o +CSHARE = $(CC) -fPIC -shared -O2 -pipe -L$(SRCROOT)/solib/ -o + +ifeq ($v,release) +CFLAGS= -O2 $(INCLS) -fPIC -DLINUX -pipe -Wno-deprecated -c +else +CFLAGS= -g $(INCLS) -fPIC -DLINUX -pipe -c -fno-inline +endif + +ifneq ($v,release) +BFLAGS= -g +endif + +STATICLIBPATH=$(SRCROOT)/lib +DYNAMICLIBPATH=$(SRCROOT)/solib + +INCLS += -I$(SRCROOT) + +## default links +ifeq ($(LINKS_DYNAMIC), 1) +LINKS += -L$(DYNAMICLIBPATH) -L$(STATICLIBPATH) +else +LINKS += -L$(STATICLIBPATH) +endif + +CPPSRCS = $(wildcard *.cpp) +CSRCS = $(wildcard *.c) +CPPOBJS = $(patsubst %.cpp,%.o,$(CPPSRCS)) +COBJS = $(patsubst %.c,%.o,$(CSRCS)) + +SRCS = $(CPPSRCS) $(CSRCS) +OBJS = $(CPPOBJS) $(COBJS) + +CPPCOMPI=$(CPP) $(CFLAGS) -Wno-deprecated +CCCOMPI=$(CC) $(CFLAGS) + +BUILDEXE = $(CPP) $(BFLAGS) -o $@ $^ $(LINKS) +CLEAN = rm -f *.o + +CPPCOMPILE = $(CPPCOMPI) $< $(FLAGS) $(INCLS) $(MTOOL_INCL) -o $@ +CCCOMPILE = $(CCCOMPI) $< $(FLAGS) $(INCLS) $(MTOOL_INCL) -o $@ + +ARSTATICLIB = $(AR) $@.tmp $^ $(AR_FLAGS); \ + if [ $$? -ne 0 ]; then exit 1; fi; \ + test -d $(STATICLIBPATH) || mkdir -p $(STATICLIBPATH); \ + mv -f $@.tmp $(STATICLIBPATH)/$@; + +BUILDSHARELIB = $(CPPSHARE) $@.tmp $^ $(BS_FLAGS); \ + if [ $$? -ne 0 ]; then exit 1; fi; \ + test -d $(DYNAMICLIBPATH) || mkdir -p $(DYNAMICLIBPATH); \ + mv -f $@.tmp $(DYNAMICLIBPATH)/$@; + +.cpp.o: + $(CPPCOMPILE) +.c.o: + $(CCCOMPILE) diff --git a/trunk/3rdparty/libco/co_closure.h b/trunk/3rdparty/libco/co_closure.h new file mode 100644 index 0000000000..87d5c6891d --- /dev/null +++ b/trunk/3rdparty/libco/co_closure.h @@ -0,0 +1,96 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __CO_CLOSURE_H__ +#define __CO_CLOSURE_H__ +struct stCoClosure_t +{ +public: + virtual void exec() = 0; +}; + +//1.base +//-- 1.1 comac_argc + +#define comac_get_args_cnt( ... ) comac_arg_n( __VA_ARGS__ ) +#define comac_arg_n( _0,_1,_2,_3,_4,_5,_6,_7,N,...) N +#define comac_args_seqs() 7,6,5,4,3,2,1,0 +#define comac_join_1( x,y ) x##y + +#define comac_argc( ... ) comac_get_args_cnt( 0,##__VA_ARGS__,comac_args_seqs() ) +#define comac_join( x,y) comac_join_1( x,y ) + +//-- 1.2 repeat +#define repeat_0( fun,a,... ) +#define repeat_1( fun,a,... ) fun( 1,a,__VA_ARGS__ ) repeat_0( fun,__VA_ARGS__ ) +#define repeat_2( fun,a,... ) fun( 2,a,__VA_ARGS__ ) repeat_1( fun,__VA_ARGS__ ) +#define repeat_3( fun,a,... ) fun( 3,a,__VA_ARGS__ ) repeat_2( fun,__VA_ARGS__ ) +#define repeat_4( fun,a,... ) fun( 4,a,__VA_ARGS__ ) repeat_3( fun,__VA_ARGS__ ) +#define repeat_5( fun,a,... ) fun( 5,a,__VA_ARGS__ ) repeat_4( fun,__VA_ARGS__ ) +#define repeat_6( fun,a,... ) fun( 6,a,__VA_ARGS__ ) repeat_5( fun,__VA_ARGS__ ) + +#define repeat( n,fun,... ) comac_join( repeat_,n )( fun,__VA_ARGS__) + +//2.implement +#if __cplusplus <= 199711L +#define decl_typeof( i,a,... ) typedef typeof( a ) typeof_##a; +#else +#define decl_typeof( i,a,... ) typedef decltype( a ) typeof_##a; +#endif +#define impl_typeof( i,a,... ) typeof_##a & a; +#define impl_typeof_cpy( i,a,... ) typeof_##a a; +#define con_param_typeof( i,a,... ) typeof_##a & a##r, +#define param_init_typeof( i,a,... ) a(a##r), + + +//2.1 reference + +#define co_ref( name,... )\ +repeat( comac_argc(__VA_ARGS__) ,decl_typeof,__VA_ARGS__ )\ +class type_##name\ +{\ +public:\ + repeat( comac_argc(__VA_ARGS__) ,impl_typeof,__VA_ARGS__ )\ + int _member_cnt;\ + type_##name( \ + repeat( comac_argc(__VA_ARGS__),con_param_typeof,__VA_ARGS__ ) ... ): \ + repeat( comac_argc(__VA_ARGS__),param_init_typeof,__VA_ARGS__ ) _member_cnt(comac_argc(__VA_ARGS__)) \ + {}\ +} name( __VA_ARGS__ ) ; + + +//2.2 function + +#define co_func(name,...)\ +repeat( comac_argc(__VA_ARGS__) ,decl_typeof,__VA_ARGS__ )\ +class name:public stCoClosure_t\ +{\ +public:\ + repeat( comac_argc(__VA_ARGS__) ,impl_typeof_cpy,__VA_ARGS__ )\ + int _member_cnt;\ +public:\ + name( repeat( comac_argc(__VA_ARGS__),con_param_typeof,__VA_ARGS__ ) ... ): \ + repeat( comac_argc(__VA_ARGS__),param_init_typeof,__VA_ARGS__ ) _member_cnt(comac_argc(__VA_ARGS__))\ + {}\ + void exec() + +#define co_func_end } + + +#endif + diff --git a/trunk/3rdparty/libco/co_epoll.cpp b/trunk/3rdparty/libco/co_epoll.cpp new file mode 100644 index 0000000000..12726218dd --- /dev/null +++ b/trunk/3rdparty/libco/co_epoll.cpp @@ -0,0 +1,318 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_epoll.h" +#include +#include +#include +#include + +#if !defined( __APPLE__ ) && !defined( __FreeBSD__ ) + +int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout ) +{ + return epoll_wait( epfd,events->events,maxevents,timeout ); +} +int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ev ) +{ + return epoll_ctl( epfd,op,fd,ev ); +} +int co_epoll_create( int size ) +{ + return epoll_create( size ); +} + +struct co_epoll_res *co_epoll_res_alloc( int n ) +{ + struct co_epoll_res * ptr = + (struct co_epoll_res *)malloc( sizeof( struct co_epoll_res ) ); + + ptr->size = n; + ptr->events = (struct epoll_event*)calloc( 1,n * sizeof( struct epoll_event ) ); + + return ptr; + +} +void co_epoll_res_free( struct co_epoll_res * ptr ) +{ + if( !ptr ) return; + if( ptr->events ) free( ptr->events ); + free( ptr ); +} + +#else +class clsFdMap // million of fd , 1024 * 1024 +{ +private: + static const int row_size = 1024; + static const int col_size = 1024; + + void **m_pp[ 1024 ]; +public: + clsFdMap() + { + memset( m_pp,0,sizeof(m_pp) ); + } + ~clsFdMap() + { + for(int i=0;i= sizeof(m_pp)/sizeof(m_pp[0]) ) + { + assert( __LINE__ == 0 ); + return -__LINE__; + } + if( !m_pp[ idx ] ) + { + m_pp[ idx ] = (void**)calloc( 1,sizeof(void*) * col_size ); + } + m_pp[ idx ][ fd % col_size ] = (void*)ptr; + return 0; + } + inline void *get( int fd ) + { + int idx = fd / row_size; + if( idx < 0 || idx >= sizeof(m_pp)/sizeof(m_pp[0]) ) + { + return NULL; + } + void **lp = m_pp[ idx ]; + if( !lp ) return NULL; + + return lp[ fd % col_size ]; + } +}; + +__thread clsFdMap *s_fd_map = NULL; + +static inline clsFdMap *get_fd_map() +{ + if( !s_fd_map ) + { + s_fd_map = new clsFdMap(); + } + return s_fd_map; +} + +struct kevent_pair_t +{ + int fire_idx; + int events; + uint64_t u64; +}; +int co_epoll_create( int size ) +{ + return kqueue(); +} +int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout ) +{ + struct timespec t = { 0 }; + if( timeout > 0 ) + { + t.tv_sec = timeout; + } + int ret = kevent( epfd, + NULL, 0, //register null + events->eventlist, maxevents,//just retrival + ( -1 == timeout ) ? NULL : &t ); + int j = 0; + for(int i=0;ieventlist[i]; + struct kevent_pair_t *ptr = (struct kevent_pair_t*)kev.udata; + struct epoll_event *ev = events->events + i; + if( 0 == ptr->fire_idx ) + { + ptr->fire_idx = i + 1; + memset( ev,0,sizeof(*ev) ); + ++j; + } + else + { + ev = events->events + ptr->fire_idx - 1; + } + if( EVFILT_READ == kev.filter ) + { + ev->events |= EPOLLIN; + } + else if( EVFILT_WRITE == kev.filter ) + { + ev->events |= EPOLLOUT; + } + ev->data.u64 = ptr->u64; + } + for(int i=0;ieventlist[i].udata) )->fire_idx = 0; + } + return j; +} +int co_epoll_del( int epfd,int fd ) +{ + + struct timespec t = { 0 }; + struct kevent_pair_t *ptr = ( struct kevent_pair_t* )get_fd_map()->get( fd ); + if( !ptr ) return 0; + if( EPOLLIN & ptr->events ) + { + struct kevent kev = { 0 }; + kev.ident = fd; + kev.filter = EVFILT_READ; + kev.flags = EV_DELETE; + kevent( epfd,&kev,1, NULL,0,&t ); + } + if( EPOLLOUT & ptr->events ) + { + struct kevent kev = { 0 }; + kev.ident = fd; + kev.filter = EVFILT_WRITE; + kev.flags = EV_DELETE; + kevent( epfd,&kev,1, NULL,0,&t ); + } + get_fd_map()->clear( fd ); + free( ptr ); + return 0; +} +int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ev ) +{ + if( EPOLL_CTL_DEL == op ) + { + return co_epoll_del( epfd,fd ); + } + + const int flags = ( EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP ); + if( ev->events & ~flags ) + { + return -1; + } + + if( EPOLL_CTL_ADD == op && get_fd_map()->get( fd ) ) + { + errno = EEXIST; + return -1; + } + else if( EPOLL_CTL_MOD == op && !get_fd_map()->get( fd ) ) + { + errno = ENOENT; + return -1; + } + + struct kevent_pair_t *ptr = (struct kevent_pair_t*)get_fd_map()->get( fd ); + if( !ptr ) + { + ptr = (kevent_pair_t*)calloc(1,sizeof(kevent_pair_t)); + get_fd_map()->set( fd,ptr ); + } + + int ret = 0; + struct timespec t = { 0 }; + + // printf("ptr->events 0x%X\n",ptr->events); + + if( EPOLL_CTL_MOD == op ) + { + //1.delete if exists + if( ptr->events & EPOLLIN ) + { + struct kevent kev = { 0 }; + EV_SET( &kev,fd,EVFILT_READ,EV_DELETE,0,0,NULL ); + kevent( epfd, &kev,1, NULL,0, &t ); + } + //1.delete if exists + if( ptr->events & EPOLLOUT ) + { + struct kevent kev = { 0 }; + EV_SET( &kev,fd,EVFILT_WRITE,EV_DELETE,0,0,NULL ); + ret = kevent( epfd, &kev,1, NULL,0, &t ); + // printf("delete write ret %d\n",ret ); + } + } + + do + { + if( ev->events & EPOLLIN ) + { + + //2.add + struct kevent kev = { 0 }; + EV_SET( &kev,fd,EVFILT_READ,EV_ADD,0,0,ptr ); + ret = kevent( epfd, &kev,1, NULL,0, &t ); + if( ret ) break; + } + if( ev->events & EPOLLOUT ) + { + //2.add + struct kevent kev = { 0 }; + EV_SET( &kev,fd,EVFILT_WRITE,EV_ADD,0,0,ptr ); + ret = kevent( epfd, &kev,1, NULL,0, &t ); + if( ret ) break; + } + } while( 0 ); + + if( ret ) + { + get_fd_map()->clear( fd ); + free( ptr ); + return ret; + } + + ptr->events = ev->events; + ptr->u64 = ev->data.u64; + + + return ret; +} + +struct co_epoll_res *co_epoll_res_alloc( int n ) +{ + struct co_epoll_res * ptr = + (struct co_epoll_res *)malloc( sizeof( struct co_epoll_res ) ); + + ptr->size = n; + ptr->events = (struct epoll_event*)calloc( 1,n * sizeof( struct epoll_event ) ); + ptr->eventlist = (struct kevent*)calloc( 1,n * sizeof( struct kevent) ); + + return ptr; +} + +void co_epoll_res_free( struct co_epoll_res * ptr ) +{ + if( !ptr ) return; + if( ptr->events ) free( ptr->events ); + if( ptr->eventlist ) free( ptr->eventlist ); + free( ptr ); +} + +#endif + + diff --git a/trunk/3rdparty/libco/co_epoll.h b/trunk/3rdparty/libco/co_epoll.h new file mode 100644 index 0000000000..a1b0e4a7d9 --- /dev/null +++ b/trunk/3rdparty/libco/co_epoll.h @@ -0,0 +1,93 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __CO_EPOLL_H__ +#define __CO_EPOLL_H__ +#include +#include +#include +#include +#include +#include +#include + +#if !defined( __APPLE__ ) && !defined( __FreeBSD__ ) + +#include + +struct co_epoll_res +{ + int size; + struct epoll_event *events; + struct kevent *eventlist; +}; +int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout ); +int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ); +int co_epoll_create( int size ); +struct co_epoll_res *co_epoll_res_alloc( int n ); +void co_epoll_res_free( struct co_epoll_res * ); + +#else + +#include +enum EPOLL_EVENTS +{ + EPOLLIN = 0X001, + EPOLLPRI = 0X002, + EPOLLOUT = 0X004, + + EPOLLERR = 0X008, + EPOLLHUP = 0X010, + + EPOLLRDNORM = 0x40, + EPOLLWRNORM = 0x004, +}; +#define EPOLL_CTL_ADD 1 +#define EPOLL_CTL_DEL 2 +#define EPOLL_CTL_MOD 3 +typedef union epoll_data +{ + void *ptr; + int fd; + uint32_t u32; + uint64_t u64; + +} epoll_data_t; + +struct epoll_event +{ + uint32_t events; + epoll_data_t data; +}; + +struct co_epoll_res +{ + int size; + struct epoll_event *events; + struct kevent *eventlist; +}; +int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout ); +int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ); +int co_epoll_create( int size ); +struct co_epoll_res *co_epoll_res_alloc( int n ); +void co_epoll_res_free( struct co_epoll_res * ); + +#endif +#endif + + diff --git a/trunk/3rdparty/libco/co_hook_sys_call.cpp b/trunk/3rdparty/libco/co_hook_sys_call.cpp new file mode 100644 index 0000000000..7bb0c417b9 --- /dev/null +++ b/trunk/3rdparty/libco/co_hook_sys_call.cpp @@ -0,0 +1,1003 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include "co_routine.h" +#include "co_routine_inner.h" +#include "co_routine_specific.h" + +typedef long long ll64_t; + +struct rpchook_t +{ + int user_flag; + struct sockaddr_in dest; //maybe sockaddr_un; + int domain; //AF_LOCAL , AF_INET + + struct timeval read_timeout; + struct timeval write_timeout; +}; +static inline pid_t GetPid() +{ + char **p = (char**)pthread_self(); + return p ? *(pid_t*)(p + 18) : getpid(); +} +static rpchook_t *g_rpchook_socket_fd[ 102400 ] = { 0 }; + +typedef int (*socket_pfn_t)(int domain, int type, int protocol); +typedef int (*connect_pfn_t)(int socket, const struct sockaddr *address, socklen_t address_len); +typedef int (*close_pfn_t)(int fd); + +typedef ssize_t (*read_pfn_t)(int fildes, void *buf, size_t nbyte); +typedef ssize_t (*write_pfn_t)(int fildes, const void *buf, size_t nbyte); + +typedef ssize_t (*sendto_pfn_t)(int socket, const void *message, size_t length, + int flags, const struct sockaddr *dest_addr, + socklen_t dest_len); + +typedef ssize_t (*recvfrom_pfn_t)(int socket, void *buffer, size_t length, + int flags, struct sockaddr *address, + socklen_t *address_len); + +typedef size_t (*send_pfn_t)(int socket, const void *buffer, size_t length, int flags); +typedef ssize_t (*recv_pfn_t)(int socket, void *buffer, size_t length, int flags); + +typedef int (*poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout); +typedef int (*setsockopt_pfn_t)(int socket, int level, int option_name, + const void *option_value, socklen_t option_len); + +typedef int (*fcntl_pfn_t)(int fildes, int cmd, ...); +typedef struct tm *(*localtime_r_pfn_t)( const time_t *timep, struct tm *result ); + +typedef void *(*pthread_getspecific_pfn_t)(pthread_key_t key); +typedef int (*pthread_setspecific_pfn_t)(pthread_key_t key, const void *value); + +typedef int (*setenv_pfn_t)(const char *name, const char *value, int overwrite); +typedef int (*unsetenv_pfn_t)(const char *name); +typedef char *(*getenv_pfn_t)(const char *name); +typedef hostent* (*gethostbyname_pfn_t)(const char *name); +typedef res_state (*__res_state_pfn_t)(); +typedef int (*__poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout); + +static socket_pfn_t g_sys_socket_func = (socket_pfn_t)dlsym(RTLD_NEXT,"socket"); +static connect_pfn_t g_sys_connect_func = (connect_pfn_t)dlsym(RTLD_NEXT,"connect"); +static close_pfn_t g_sys_close_func = (close_pfn_t)dlsym(RTLD_NEXT,"close"); + +static read_pfn_t g_sys_read_func = (read_pfn_t)dlsym(RTLD_NEXT,"read"); +static write_pfn_t g_sys_write_func = (write_pfn_t)dlsym(RTLD_NEXT,"write"); + +static sendto_pfn_t g_sys_sendto_func = (sendto_pfn_t)dlsym(RTLD_NEXT,"sendto"); +static recvfrom_pfn_t g_sys_recvfrom_func = (recvfrom_pfn_t)dlsym(RTLD_NEXT,"recvfrom"); + +static send_pfn_t g_sys_send_func = (send_pfn_t)dlsym(RTLD_NEXT,"send"); +static recv_pfn_t g_sys_recv_func = (recv_pfn_t)dlsym(RTLD_NEXT,"recv"); + +static poll_pfn_t g_sys_poll_func = (poll_pfn_t)dlsym(RTLD_NEXT,"poll"); + +static setsockopt_pfn_t g_sys_setsockopt_func + = (setsockopt_pfn_t)dlsym(RTLD_NEXT,"setsockopt"); +static fcntl_pfn_t g_sys_fcntl_func = (fcntl_pfn_t)dlsym(RTLD_NEXT,"fcntl"); + +static setenv_pfn_t g_sys_setenv_func = (setenv_pfn_t)dlsym(RTLD_NEXT,"setenv"); +static unsetenv_pfn_t g_sys_unsetenv_func = (unsetenv_pfn_t)dlsym(RTLD_NEXT,"unsetenv"); +static getenv_pfn_t g_sys_getenv_func = (getenv_pfn_t)dlsym(RTLD_NEXT,"getenv"); +static __res_state_pfn_t g_sys___res_state_func = (__res_state_pfn_t)dlsym(RTLD_NEXT,"__res_state"); + +static gethostbyname_pfn_t g_sys_gethostbyname_func = (gethostbyname_pfn_t)dlsym(RTLD_NEXT, "gethostbyname"); + +static __poll_pfn_t g_sys___poll_func = (__poll_pfn_t)dlsym(RTLD_NEXT, "__poll"); + + +/* +static pthread_getspecific_pfn_t g_sys_pthread_getspecific_func + = (pthread_getspecific_pfn_t)dlsym(RTLD_NEXT,"pthread_getspecific"); + +static pthread_setspecific_pfn_t g_sys_pthread_setspecific_func + = (pthread_setspecific_pfn_t)dlsym(RTLD_NEXT,"pthread_setspecific"); + +static pthread_rwlock_rdlock_pfn_t g_sys_pthread_rwlock_rdlock_func + = (pthread_rwlock_rdlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_rdlock"); + +static pthread_rwlock_wrlock_pfn_t g_sys_pthread_rwlock_wrlock_func + = (pthread_rwlock_wrlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_wrlock"); + +static pthread_rwlock_unlock_pfn_t g_sys_pthread_rwlock_unlock_func + = (pthread_rwlock_unlock_pfn_t)dlsym(RTLD_NEXT,"pthread_rwlock_unlock"); +*/ + + + +static inline unsigned long long get_tick_count() +{ + uint32_t lo, hi; + __asm__ __volatile__ ( + "rdtscp" : "=a"(lo), "=d"(hi) + ); + return ((unsigned long long)lo) | (((unsigned long long)hi) << 32); +} + +struct rpchook_connagent_head_t +{ + unsigned char bVersion; + struct in_addr iIP; + unsigned short hPort; + unsigned int iBodyLen; + unsigned int iOssAttrID; + unsigned char bIsRespNotExist; + unsigned char sReserved[6]; +}__attribute__((packed)); + + +#define HOOK_SYS_FUNC(name) if( !g_sys_##name##_func ) { g_sys_##name##_func = (name##_pfn_t)dlsym(RTLD_NEXT,#name); } + +static inline ll64_t diff_ms(struct timeval &begin,struct timeval &end) +{ + ll64_t u = (end.tv_sec - begin.tv_sec) ; + u *= 1000 * 10; + u += ( end.tv_usec - begin.tv_usec ) / ( 100 ); + return u; +} + + + +static inline rpchook_t * get_by_fd( int fd ) +{ + if( fd > -1 && fd < (int)sizeof(g_rpchook_socket_fd) / (int)sizeof(g_rpchook_socket_fd[0]) ) + { + return g_rpchook_socket_fd[ fd ]; + } + return NULL; +} +static inline rpchook_t * alloc_by_fd( int fd ) +{ + if( fd > -1 && fd < (int)sizeof(g_rpchook_socket_fd) / (int)sizeof(g_rpchook_socket_fd[0]) ) + { + rpchook_t *lp = (rpchook_t*)calloc( 1,sizeof(rpchook_t) ); + lp->read_timeout.tv_sec = 1; + lp->write_timeout.tv_sec = 1; + g_rpchook_socket_fd[ fd ] = lp; + return lp; + } + return NULL; +} +static inline void free_by_fd( int fd ) +{ + if( fd > -1 && fd < (int)sizeof(g_rpchook_socket_fd) / (int)sizeof(g_rpchook_socket_fd[0]) ) + { + rpchook_t *lp = g_rpchook_socket_fd[ fd ]; + if( lp ) + { + g_rpchook_socket_fd[ fd ] = NULL; + free(lp); + } + } + return; + +} +int socket(int domain, int type, int protocol) +{ + HOOK_SYS_FUNC( socket ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_socket_func( domain,type,protocol ); + } + int fd = g_sys_socket_func(domain,type,protocol); + if( fd < 0 ) + { + return fd; + } + + rpchook_t *lp = alloc_by_fd( fd ); + lp->domain = domain; + + fcntl( fd, F_SETFL, g_sys_fcntl_func(fd, F_GETFL,0 ) ); + + return fd; +} + +int co_accept( int fd, struct sockaddr *addr, socklen_t *len ) +{ + int cli = accept( fd,addr,len ); + if( cli < 0 ) + { + return cli; + } + alloc_by_fd( cli ); + return cli; +} +int connect(int fd, const struct sockaddr *address, socklen_t address_len) +{ + HOOK_SYS_FUNC( connect ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_connect_func(fd,address,address_len); + } + + //1.sys call + int ret = g_sys_connect_func( fd,address,address_len ); + + rpchook_t *lp = get_by_fd( fd ); + if( !lp ) return ret; + + if( sizeof(lp->dest) >= address_len ) + { + memcpy( &(lp->dest),address,(int)address_len ); + } + if( O_NONBLOCK & lp->user_flag ) + { + return ret; + } + + if (!(ret < 0 && errno == EINPROGRESS)) + { + return ret; + } + + //2.wait + int pollret = 0; + struct pollfd pf = { 0 }; + + for(int i=0;i<3;i++) //25s * 3 = 75s + { + memset( &pf,0,sizeof(pf) ); + pf.fd = fd; + pf.events = ( POLLOUT | POLLERR | POLLHUP ); + + pollret = poll( &pf,1,25000 ); + + if( 1 == pollret ) + { + break; + } + } + + if( pf.revents & POLLOUT ) //connect succ + { + // 3.check getsockopt ret + int err = 0; + socklen_t errlen = sizeof(err); + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen); + if (ret < 0) { + return ret; + } else if (err != 0) { + errno = err; + return -1; + } + errno = 0; + return 0; + } + + errno = ETIMEDOUT; + return ret; +} + + +int close(int fd) +{ + HOOK_SYS_FUNC( close ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_close_func( fd ); + } + + free_by_fd( fd ); + int ret = g_sys_close_func(fd); + + return ret; +} +ssize_t read( int fd, void *buf, size_t nbyte ) +{ + HOOK_SYS_FUNC( read ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_read_func( fd,buf,nbyte ); + } + rpchook_t *lp = get_by_fd( fd ); + + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + ssize_t ret = g_sys_read_func( fd,buf,nbyte ); + return ret; + } + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + + ( lp->read_timeout.tv_usec / 1000 ); + + struct pollfd pf = { 0 }; + pf.fd = fd; + pf.events = ( POLLIN | POLLERR | POLLHUP ); + + int pollret = poll( &pf,1,timeout ); + + ssize_t readret = g_sys_read_func( fd,(char*)buf ,nbyte ); + + if( readret < 0 ) + { + co_log_err("CO_ERR: read fd %d ret %ld errno %d poll ret %d timeout %d", + fd,readret,errno,pollret,timeout); + } + + return readret; + +} +ssize_t write( int fd, const void *buf, size_t nbyte ) +{ + HOOK_SYS_FUNC( write ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_write_func( fd,buf,nbyte ); + } + rpchook_t *lp = get_by_fd( fd ); + + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + ssize_t ret = g_sys_write_func( fd,buf,nbyte ); + return ret; + } + size_t wrotelen = 0; + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + + ( lp->write_timeout.tv_usec / 1000 ); + + ssize_t writeret = g_sys_write_func( fd,(const char*)buf + wrotelen,nbyte - wrotelen ); + + if (writeret == 0) + { + return writeret; + } + + if( writeret > 0 ) + { + wrotelen += writeret; + } + while( wrotelen < nbyte ) + { + + struct pollfd pf = { 0 }; + pf.fd = fd; + pf.events = ( POLLOUT | POLLERR | POLLHUP ); + poll( &pf,1,timeout ); + + writeret = g_sys_write_func( fd,(const char*)buf + wrotelen,nbyte - wrotelen ); + + if( writeret <= 0 ) + { + break; + } + wrotelen += writeret ; + } + if (writeret <= 0 && wrotelen == 0) + { + return writeret; + } + return wrotelen; +} + +ssize_t sendto(int socket, const void *message, size_t length, + int flags, const struct sockaddr *dest_addr, + socklen_t dest_len) +{ + /* + 1.no enable sys call ? sys + 2.( !lp || lp is non block ) ? sys + 3.try + 4.wait + 5.try + */ + HOOK_SYS_FUNC( sendto ); + if( !co_is_enable_sys_hook() ) + { + return g_sys_sendto_func( socket,message,length,flags,dest_addr,dest_len ); + } + + rpchook_t *lp = get_by_fd( socket ); + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + return g_sys_sendto_func( socket,message,length,flags,dest_addr,dest_len ); + } + + ssize_t ret = g_sys_sendto_func( socket,message,length,flags,dest_addr,dest_len ); + if( ret < 0 && EAGAIN == errno ) + { + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + + ( lp->write_timeout.tv_usec / 1000 ); + + + struct pollfd pf = { 0 }; + pf.fd = socket; + pf.events = ( POLLOUT | POLLERR | POLLHUP ); + poll( &pf,1,timeout ); + + ret = g_sys_sendto_func( socket,message,length,flags,dest_addr,dest_len ); + + } + return ret; +} + +ssize_t recvfrom(int socket, void *buffer, size_t length, + int flags, struct sockaddr *address, + socklen_t *address_len) +{ + HOOK_SYS_FUNC( recvfrom ); + if( !co_is_enable_sys_hook() ) + { + return g_sys_recvfrom_func( socket,buffer,length,flags,address,address_len ); + } + + rpchook_t *lp = get_by_fd( socket ); + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + return g_sys_recvfrom_func( socket,buffer,length,flags,address,address_len ); + } + + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + + ( lp->read_timeout.tv_usec / 1000 ); + + + struct pollfd pf = { 0 }; + pf.fd = socket; + pf.events = ( POLLIN | POLLERR | POLLHUP ); + poll( &pf,1,timeout ); + + ssize_t ret = g_sys_recvfrom_func( socket,buffer,length,flags,address,address_len ); + return ret; +} + +ssize_t send(int socket, const void *buffer, size_t length, int flags) +{ + HOOK_SYS_FUNC( send ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_send_func( socket,buffer,length,flags ); + } + rpchook_t *lp = get_by_fd( socket ); + + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + return g_sys_send_func( socket,buffer,length,flags ); + } + size_t wrotelen = 0; + int timeout = ( lp->write_timeout.tv_sec * 1000 ) + + ( lp->write_timeout.tv_usec / 1000 ); + + ssize_t writeret = g_sys_send_func( socket,buffer,length,flags ); + if (writeret == 0) + { + return writeret; + } + + if( writeret > 0 ) + { + wrotelen += writeret; + } + while( wrotelen < length ) + { + + struct pollfd pf = { 0 }; + pf.fd = socket; + pf.events = ( POLLOUT | POLLERR | POLLHUP ); + poll( &pf,1,timeout ); + + writeret = g_sys_send_func( socket,(const char*)buffer + wrotelen,length - wrotelen,flags ); + + if( writeret <= 0 ) + { + break; + } + wrotelen += writeret ; + } + if (writeret <= 0 && wrotelen == 0) + { + return writeret; + } + return wrotelen; +} + +ssize_t recv( int socket, void *buffer, size_t length, int flags ) +{ + HOOK_SYS_FUNC( recv ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_recv_func( socket,buffer,length,flags ); + } + rpchook_t *lp = get_by_fd( socket ); + + if( !lp || ( O_NONBLOCK & lp->user_flag ) ) + { + return g_sys_recv_func( socket,buffer,length,flags ); + } + int timeout = ( lp->read_timeout.tv_sec * 1000 ) + + ( lp->read_timeout.tv_usec / 1000 ); + + struct pollfd pf = { 0 }; + pf.fd = socket; + pf.events = ( POLLIN | POLLERR | POLLHUP ); + + int pollret = poll( &pf,1,timeout ); + + ssize_t readret = g_sys_recv_func( socket,buffer,length,flags ); + + if( readret < 0 ) + { + co_log_err("CO_ERR: read fd %d ret %ld errno %d poll ret %d timeout %d", + socket,readret,errno,pollret,timeout); + } + + return readret; + +} + +extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc); + +int poll(struct pollfd fds[], nfds_t nfds, int timeout) +{ + HOOK_SYS_FUNC( poll ); + + if (!co_is_enable_sys_hook() || timeout == 0) { + return g_sys_poll_func(fds, nfds, timeout); + } + pollfd *fds_merge = NULL; + nfds_t nfds_merge = 0; + std::map m; // fd --> idx + std::map::iterator it; + if (nfds > 1) { + fds_merge = (pollfd *)malloc(sizeof(pollfd) * nfds); + for (size_t i = 0; i < nfds; i++) { + if ((it = m.find(fds[i].fd)) == m.end()) { + fds_merge[nfds_merge] = fds[i]; + m[fds[i].fd] = nfds_merge; + nfds_merge++; + } else { + int j = it->second; + fds_merge[j].events |= fds[i].events; // merge in j slot + } + } + } + + int ret = 0; + if (nfds_merge == nfds || nfds == 1) { + ret = co_poll_inner(co_get_epoll_ct(), fds, nfds, timeout, g_sys_poll_func); + } else { + ret = co_poll_inner(co_get_epoll_ct(), fds_merge, nfds_merge, timeout, + g_sys_poll_func); + if (ret > 0) { + for (size_t i = 0; i < nfds; i++) { + it = m.find(fds[i].fd); + if (it != m.end()) { + int j = it->second; + fds[i].revents = fds_merge[j].revents & fds[i].events; + } + } + } + } + free(fds_merge); + return ret; + + +} +int setsockopt(int fd, int level, int option_name, + const void *option_value, socklen_t option_len) +{ + HOOK_SYS_FUNC( setsockopt ); + + if( !co_is_enable_sys_hook() ) + { + return g_sys_setsockopt_func( fd,level,option_name,option_value,option_len ); + } + rpchook_t *lp = get_by_fd( fd ); + + if( lp && SOL_SOCKET == level ) + { + struct timeval *val = (struct timeval*)option_value; + if( SO_RCVTIMEO == option_name ) + { + memcpy( &lp->read_timeout,val,sizeof(*val) ); + } + else if( SO_SNDTIMEO == option_name ) + { + memcpy( &lp->write_timeout,val,sizeof(*val) ); + } + } + return g_sys_setsockopt_func( fd,level,option_name,option_value,option_len ); +} + + +int fcntl(int fildes, int cmd, ...) +{ + HOOK_SYS_FUNC( fcntl ); + + if( fildes < 0 ) + { + return __LINE__; + } + + va_list arg_list; + va_start( arg_list,cmd ); + + int ret = -1; + rpchook_t *lp = get_by_fd( fildes ); + switch( cmd ) + { + case F_DUPFD: + { + int param = va_arg(arg_list,int); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + case F_GETFD: + { + ret = g_sys_fcntl_func( fildes,cmd ); + if (lp && !(lp->user_flag & O_NONBLOCK)) { + ret = ret & (~O_NONBLOCK); + } + break; + } + case F_SETFD: + { + int param = va_arg(arg_list,int); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + case F_GETFL: + { + ret = g_sys_fcntl_func( fildes,cmd ); + break; + } + case F_SETFL: + { + int param = va_arg(arg_list,int); + int flag = param; + if( co_is_enable_sys_hook() && lp ) + { + flag |= O_NONBLOCK; + } + ret = g_sys_fcntl_func( fildes,cmd,flag ); + if( 0 == ret && lp ) + { + lp->user_flag = param; + } + break; + } + case F_GETOWN: + { + ret = g_sys_fcntl_func( fildes,cmd ); + break; + } + case F_SETOWN: + { + int param = va_arg(arg_list,int); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + case F_GETLK: + { + struct flock *param = va_arg(arg_list,struct flock *); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + case F_SETLK: + { + struct flock *param = va_arg(arg_list,struct flock *); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + case F_SETLKW: + { + struct flock *param = va_arg(arg_list,struct flock *); + ret = g_sys_fcntl_func( fildes,cmd,param ); + break; + } + } + + va_end( arg_list ); + + return ret; +} + +struct stCoSysEnv_t +{ + char *name; + char *value; +}; +struct stCoSysEnvArr_t +{ + stCoSysEnv_t *data; + size_t cnt; +}; +static stCoSysEnvArr_t *dup_co_sysenv_arr( stCoSysEnvArr_t * arr ) +{ + stCoSysEnvArr_t *lp = (stCoSysEnvArr_t*)calloc( sizeof(stCoSysEnvArr_t),1 ); + if( arr->cnt ) + { + lp->data = (stCoSysEnv_t*)calloc( sizeof(stCoSysEnv_t) * arr->cnt,1 ); + lp->cnt = arr->cnt; + memcpy( lp->data,arr->data,sizeof( stCoSysEnv_t ) * arr->cnt ); + } + return lp; +} + +static int co_sysenv_comp(const void *a, const void *b) +{ + return strcmp(((stCoSysEnv_t*)a)->name, ((stCoSysEnv_t*)b)->name); +} +static stCoSysEnvArr_t g_co_sysenv = { 0 }; + + + +void co_set_env_list( const char *name[],size_t cnt) +{ + if( g_co_sysenv.data ) + { + return ; + } + g_co_sysenv.data = (stCoSysEnv_t*)calloc( 1,sizeof(stCoSysEnv_t) * cnt ); + + for(size_t i=0;i 1 ) + { + qsort( g_co_sysenv.data,g_co_sysenv.cnt,sizeof(stCoSysEnv_t),co_sysenv_comp ); + stCoSysEnv_t *lp = g_co_sysenv.data; + stCoSysEnv_t *lq = g_co_sysenv.data + 1; + for(size_t i=1;iname,lq->name ) ) + { + ++lp; + if( lq != lp ) + { + *lp = *lq; + } + } + ++lq; + } + g_co_sysenv.cnt = lp - g_co_sysenv.data + 1; + } + +} + +int setenv(const char *n, const char *value, int overwrite) +{ + HOOK_SYS_FUNC( setenv ) + if( co_is_enable_sys_hook() && g_co_sysenv.data ) + { + stCoRoutine_t *self = co_self(); + if( self ) + { + if( !self->pvEnv ) + { + self->pvEnv = dup_co_sysenv_arr( &g_co_sysenv ); + } + stCoSysEnvArr_t *arr = (stCoSysEnvArr_t*)(self->pvEnv); + + stCoSysEnv_t name = { (char*)n,0 }; + + stCoSysEnv_t *e = (stCoSysEnv_t*)bsearch( &name,arr->data,arr->cnt,sizeof(name),co_sysenv_comp ); + + if( e ) + { + if( overwrite || !e->value ) + { + if( e->value ) free( e->value ); + e->value = ( value ? strdup( value ) : 0 ); + } + return 0; + } + } + + } + return g_sys_setenv_func( n,value,overwrite ); +} +int unsetenv(const char *n) +{ + HOOK_SYS_FUNC( unsetenv ) + if( co_is_enable_sys_hook() && g_co_sysenv.data ) + { + stCoRoutine_t *self = co_self(); + if( self ) + { + if( !self->pvEnv ) + { + self->pvEnv = dup_co_sysenv_arr( &g_co_sysenv ); + } + stCoSysEnvArr_t *arr = (stCoSysEnvArr_t*)(self->pvEnv); + + stCoSysEnv_t name = { (char*)n,0 }; + + stCoSysEnv_t *e = (stCoSysEnv_t*)bsearch( &name,arr->data,arr->cnt,sizeof(name),co_sysenv_comp ); + + if( e ) + { + if( e->value ) + { + free( e->value ); + e->value = 0; + } + return 0; + } + } + + } + return g_sys_unsetenv_func( n ); +} +char *getenv( const char *n ) +{ + HOOK_SYS_FUNC( getenv ) + if( co_is_enable_sys_hook() && g_co_sysenv.data ) + { + stCoRoutine_t *self = co_self(); + + stCoSysEnv_t name = { (char*)n,0 }; + + if( !self->pvEnv ) + { + self->pvEnv = dup_co_sysenv_arr( &g_co_sysenv ); + } + stCoSysEnvArr_t *arr = (stCoSysEnvArr_t*)(self->pvEnv); + + stCoSysEnv_t *e = (stCoSysEnv_t*)bsearch( &name,arr->data,arr->cnt,sizeof(name),co_sysenv_comp ); + + if( e ) + { + return e->value; + } + + } + return g_sys_getenv_func( n ); + +} +struct hostent* co_gethostbyname(const char *name); + +struct hostent *gethostbyname(const char *name) +{ + HOOK_SYS_FUNC( gethostbyname ); + +#if defined( __APPLE__ ) || defined( __FreeBSD__ ) + return g_sys_gethostbyname_func( name ); +#else + if (!co_is_enable_sys_hook()) + { + return g_sys_gethostbyname_func(name); + } + return co_gethostbyname(name); +#endif + +} + + +struct res_state_wrap +{ + struct __res_state state; +}; +CO_ROUTINE_SPECIFIC(res_state_wrap, __co_state_wrap); + +extern "C" +{ + res_state __res_state() + { + HOOK_SYS_FUNC(__res_state); + + if (!co_is_enable_sys_hook()) + { + return g_sys___res_state_func(); + } + + return &(__co_state_wrap->state); + } + int __poll(struct pollfd fds[], nfds_t nfds, int timeout) + { + return poll(fds, nfds, timeout); + } +} + +struct hostbuf_wrap +{ + struct hostent host; + char* buffer; + size_t iBufferSize; + int host_errno; +}; + +CO_ROUTINE_SPECIFIC(hostbuf_wrap, __co_hostbuf_wrap); + +#if !defined( __APPLE__ ) && !defined( __FreeBSD__ ) +struct hostent *co_gethostbyname(const char *name) +{ + if (!name) + { + return NULL; + } + + if (__co_hostbuf_wrap->buffer && __co_hostbuf_wrap->iBufferSize > 1024) + { + free(__co_hostbuf_wrap->buffer); + __co_hostbuf_wrap->buffer = NULL; + } + if (!__co_hostbuf_wrap->buffer) + { + __co_hostbuf_wrap->buffer = (char*)malloc(1024); + __co_hostbuf_wrap->iBufferSize = 1024; + } + + struct hostent *host = &__co_hostbuf_wrap->host; + struct hostent *result = NULL; + int *h_errnop = &(__co_hostbuf_wrap->host_errno); + + int ret = -1; + while (ret = gethostbyname_r(name, host, __co_hostbuf_wrap->buffer, + __co_hostbuf_wrap->iBufferSize, &result, h_errnop) == ERANGE && + *h_errnop == NETDB_INTERNAL ) + { + free(__co_hostbuf_wrap->buffer); + __co_hostbuf_wrap->iBufferSize = __co_hostbuf_wrap->iBufferSize * 2; + __co_hostbuf_wrap->buffer = (char*)malloc(__co_hostbuf_wrap->iBufferSize); + } + + if (ret == 0 && (host == result)) + { + return host; + } + return NULL; +} +#endif + + +void co_enable_hook_sys() //这函数必须在这里,否则本文件会被忽略!!! +{ + stCoRoutine_t *co = GetCurrThreadCo(); + if( co ) + { + co->cEnableSysHook = 1; + } +} + diff --git a/trunk/3rdparty/libco/co_routine.cpp b/trunk/3rdparty/libco/co_routine.cpp new file mode 100644 index 0000000000..352ae7ea0b --- /dev/null +++ b/trunk/3rdparty/libco/co_routine.cpp @@ -0,0 +1,1196 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_routine.h" +#include "co_routine_inner.h" +#include "co_epoll.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +extern "C" +{ + extern void coctx_swap( coctx_t *,coctx_t* ) asm("coctx_swap"); +}; +using namespace std; +stCoRoutine_t *GetCurrCo( stCoRoutineEnv_t *env ); +struct stCoEpoll_t; + +struct stCoRoutineEnv_t +{ + stCoRoutine_t *pCallStack[ 128 ]; + int iCallStackSize; + stCoEpoll_t *pEpoll; + + //for copy stack log lastco and nextco + stCoRoutine_t* pending_co; + stCoRoutine_t* occupy_co; +}; +//int socket(int domain, int type, int protocol); +void co_log_err( const char *fmt,... ) +{ +} + + +#if defined( __LIBCO_RDTSCP__) +static unsigned long long counter(void) +{ + register uint32_t lo, hi; + register unsigned long long o; + __asm__ __volatile__ ( + "rdtscp" : "=a"(lo), "=d"(hi)::"%rcx" + ); + o = hi; + o <<= 32; + return (o | lo); + +} +static unsigned long long getCpuKhz() +{ + FILE *fp = fopen("/proc/cpuinfo","r"); + if(!fp) return 1; + char buf[4096] = {0}; + fread(buf,1,sizeof(buf),fp); + fclose(fp); + + char *lp = strstr(buf,"cpu MHz"); + if(!lp) return 1; + lp += strlen("cpu MHz"); + while(*lp == ' ' || *lp == '\t' || *lp == ':') + { + ++lp; + } + + double mhz = atof(lp); + unsigned long long u = (unsigned long long)(mhz * 1000); + return u; +} +#endif + +static unsigned long long GetTickMS() +{ +#if defined( __LIBCO_RDTSCP__) + static uint32_t khz = getCpuKhz(); + return counter() / khz; +#else + struct timeval now = { 0 }; + gettimeofday( &now,NULL ); + unsigned long long u = now.tv_sec; + u *= 1000; + u += now.tv_usec / 1000; + return u; +#endif +} + +/* no longer use +static pid_t GetPid() +{ + static __thread pid_t pid = 0; + static __thread pid_t tid = 0; + if( !pid || !tid || pid != getpid() ) + { + pid = getpid(); +#if defined( __APPLE__ ) + tid = syscall( SYS_gettid ); + if( -1 == (long)tid ) + { + tid = pid; + } +#elif defined( __FreeBSD__ ) + syscall(SYS_thr_self, &tid); + if( tid < 0 ) + { + tid = pid; + } +#else + tid = syscall( __NR_gettid ); +#endif + + } + return tid; + +} +static pid_t GetPid() +{ + char **p = (char**)pthread_self(); + return p ? *(pid_t*)(p + 18) : getpid(); +} +*/ +template +void RemoveFromLink(T *ap) +{ + TLink *lst = ap->pLink; + if(!lst) return ; + assert( lst->head && lst->tail ); + + if( ap == lst->head ) + { + lst->head = ap->pNext; + if(lst->head) + { + lst->head->pPrev = NULL; + } + } + else + { + if(ap->pPrev) + { + ap->pPrev->pNext = ap->pNext; + } + } + + if( ap == lst->tail ) + { + lst->tail = ap->pPrev; + if(lst->tail) + { + lst->tail->pNext = NULL; + } + } + else + { + ap->pNext->pPrev = ap->pPrev; + } + + ap->pPrev = ap->pNext = NULL; + ap->pLink = NULL; +} + +template +void inline AddTail(TLink*apLink,TNode *ap) +{ + if( ap->pLink ) + { + return ; + } + if(apLink->tail) + { + apLink->tail->pNext = (TNode*)ap; + ap->pNext = NULL; + ap->pPrev = apLink->tail; + apLink->tail = ap; + } + else + { + apLink->head = apLink->tail = ap; + ap->pNext = ap->pPrev = NULL; + } + ap->pLink = apLink; +} +template +void inline PopHead( TLink*apLink ) +{ + if( !apLink->head ) + { + return ; + } + TNode *lp = apLink->head; + if( apLink->head == apLink->tail ) + { + apLink->head = apLink->tail = NULL; + } + else + { + apLink->head = apLink->head->pNext; + } + + lp->pPrev = lp->pNext = NULL; + lp->pLink = NULL; + + if( apLink->head ) + { + apLink->head->pPrev = NULL; + } +} + +template +void inline Join( TLink*apLink,TLink *apOther ) +{ + //printf("apOther %p\n",apOther); + if( !apOther->head ) + { + return ; + } + TNode *lp = apOther->head; + while( lp ) + { + lp->pLink = apLink; + lp = lp->pNext; + } + lp = apOther->head; + if(apLink->tail) + { + apLink->tail->pNext = (TNode*)lp; + lp->pPrev = apLink->tail; + apLink->tail = apOther->tail; + } + else + { + apLink->head = apOther->head; + apLink->tail = apOther->tail; + } + + apOther->head = apOther->tail = NULL; +} + +/////////////////for copy stack ////////////////////////// +stStackMem_t* co_alloc_stackmem(unsigned int stack_size) +{ + stStackMem_t* stack_mem = (stStackMem_t*)malloc(sizeof(stStackMem_t)); + stack_mem->occupy_co= NULL; + stack_mem->stack_size = stack_size; + stack_mem->stack_buffer = (char*)malloc(stack_size); + stack_mem->stack_bp = stack_mem->stack_buffer + stack_size; + return stack_mem; +} + +stShareStack_t* co_alloc_sharestack(int count, int stack_size) +{ + stShareStack_t* share_stack = (stShareStack_t*)malloc(sizeof(stShareStack_t)); + share_stack->alloc_idx = 0; + share_stack->stack_size = stack_size; + + //alloc stack array + share_stack->count = count; + stStackMem_t** stack_array = (stStackMem_t**)calloc(count, sizeof(stStackMem_t*)); + for (int i = 0; i < count; i++) + { + stack_array[i] = co_alloc_stackmem(stack_size); + } + share_stack->stack_array = stack_array; + return share_stack; +} + +static stStackMem_t* co_get_stackmem(stShareStack_t* share_stack) +{ + if (!share_stack) + { + return NULL; + } + int idx = share_stack->alloc_idx % share_stack->count; + share_stack->alloc_idx++; + + return share_stack->stack_array[idx]; +} + + +// ---------------------------------------------------------------------------- +struct stTimeoutItemLink_t; +struct stTimeoutItem_t; +struct stCoEpoll_t +{ + int iEpollFd; + static const int _EPOLL_SIZE = 1024 * 10; + + struct stTimeout_t *pTimeout; + + struct stTimeoutItemLink_t *pstTimeoutList; + + struct stTimeoutItemLink_t *pstActiveList; + + co_epoll_res *result; + +}; +typedef void (*OnPreparePfn_t)( stTimeoutItem_t *,struct epoll_event &ev, stTimeoutItemLink_t *active ); +typedef void (*OnProcessPfn_t)( stTimeoutItem_t *); +struct stTimeoutItem_t +{ + + enum + { + eMaxTimeout = 40 * 1000 //40s + }; + stTimeoutItem_t *pPrev; + stTimeoutItem_t *pNext; + stTimeoutItemLink_t *pLink; + + unsigned long long ullExpireTime; + + OnPreparePfn_t pfnPrepare; + OnProcessPfn_t pfnProcess; + + void *pArg; // routine + bool bTimeout; +}; +struct stTimeoutItemLink_t +{ + stTimeoutItem_t *head; + stTimeoutItem_t *tail; + +}; +struct stTimeout_t +{ + stTimeoutItemLink_t *pItems; + int iItemSize; + + unsigned long long ullStart; + long long llStartIdx; +}; +stTimeout_t *AllocTimeout( int iSize ) +{ + stTimeout_t *lp = (stTimeout_t*)calloc( 1,sizeof(stTimeout_t) ); + + lp->iItemSize = iSize; + lp->pItems = (stTimeoutItemLink_t*)calloc( 1,sizeof(stTimeoutItemLink_t) * lp->iItemSize ); + + lp->ullStart = GetTickMS(); + lp->llStartIdx = 0; + + return lp; +} +void FreeTimeout( stTimeout_t *apTimeout ) +{ + free( apTimeout->pItems ); + free ( apTimeout ); +} +int AddTimeout( stTimeout_t *apTimeout,stTimeoutItem_t *apItem ,unsigned long long allNow ) +{ + if( apTimeout->ullStart == 0 ) + { + apTimeout->ullStart = allNow; + apTimeout->llStartIdx = 0; + } + if( allNow < apTimeout->ullStart ) + { + co_log_err("CO_ERR: AddTimeout line %d allNow %llu apTimeout->ullStart %llu", + __LINE__,allNow,apTimeout->ullStart); + + return __LINE__; + } + if( apItem->ullExpireTime < allNow ) + { + co_log_err("CO_ERR: AddTimeout line %d apItem->ullExpireTime %llu allNow %llu apTimeout->ullStart %llu", + __LINE__,apItem->ullExpireTime,allNow,apTimeout->ullStart); + + return __LINE__; + } + unsigned long long diff = apItem->ullExpireTime - apTimeout->ullStart; + + if( diff >= (unsigned long long)apTimeout->iItemSize ) + { + diff = apTimeout->iItemSize - 1; + co_log_err("CO_ERR: AddTimeout line %d diff %d", + __LINE__,diff); + + //return __LINE__; + } + AddTail( apTimeout->pItems + ( apTimeout->llStartIdx + diff ) % apTimeout->iItemSize , apItem ); + + return 0; +} +inline void TakeAllTimeout( stTimeout_t *apTimeout,unsigned long long allNow,stTimeoutItemLink_t *apResult ) +{ + if( apTimeout->ullStart == 0 ) + { + apTimeout->ullStart = allNow; + apTimeout->llStartIdx = 0; + } + + if( allNow < apTimeout->ullStart ) + { + return ; + } + int cnt = allNow - apTimeout->ullStart + 1; + if( cnt > apTimeout->iItemSize ) + { + cnt = apTimeout->iItemSize; + } + if( cnt < 0 ) + { + return; + } + for( int i = 0;illStartIdx + i) % apTimeout->iItemSize; + Join( apResult,apTimeout->pItems + idx ); + } + apTimeout->ullStart = allNow; + apTimeout->llStartIdx += cnt - 1; + + +} +static int CoRoutineFunc( stCoRoutine_t *co,void * ) +{ + if( co->pfn ) + { + co->pfn( co->arg ); + } + co->cEnd = 1; + + stCoRoutineEnv_t *env = co->env; + + co_yield_env( env ); + + return 0; +} + + + +struct stCoRoutine_t *co_create_env( stCoRoutineEnv_t * env, const stCoRoutineAttr_t* attr, + pfn_co_routine_t pfn,void *arg ) +{ + + stCoRoutineAttr_t at; + if( attr ) + { + memcpy( &at,attr,sizeof(at) ); + } + if( at.stack_size <= 0 ) + { + at.stack_size = 128 * 1024; + } + else if( at.stack_size > 1024 * 1024 * 8 ) + { + at.stack_size = 1024 * 1024 * 8; + } + + if( at.stack_size & 0xFFF ) + { + at.stack_size &= ~0xFFF; + at.stack_size += 0x1000; + } + + stCoRoutine_t *lp = (stCoRoutine_t*)malloc( sizeof(stCoRoutine_t) ); + + memset( lp,0,(long)(sizeof(stCoRoutine_t))); + + + lp->env = env; + lp->pfn = pfn; + lp->arg = arg; + + stStackMem_t* stack_mem = NULL; + if( at.share_stack ) + { + stack_mem = co_get_stackmem( at.share_stack); + at.stack_size = at.share_stack->stack_size; + } + else + { + stack_mem = co_alloc_stackmem(at.stack_size); + } + lp->stack_mem = stack_mem; + + lp->ctx.ss_sp = stack_mem->stack_buffer; + lp->ctx.ss_size = at.stack_size; + + lp->cStart = 0; + lp->cEnd = 0; + lp->cIsMain = 0; + lp->cEnableSysHook = 0; + lp->cIsShareStack = at.share_stack != NULL; + + lp->save_size = 0; + lp->save_buffer = NULL; + + return lp; +} + +int co_create( stCoRoutine_t **ppco,const stCoRoutineAttr_t *attr,pfn_co_routine_t pfn,void *arg ) +{ + if( !co_get_curr_thread_env() ) + { + co_init_curr_thread_env(); + } + stCoRoutine_t *co = co_create_env( co_get_curr_thread_env(), attr, pfn,arg ); + *ppco = co; + return 0; +} +void co_free( stCoRoutine_t *co ) +{ + if (!co->cIsShareStack) + { + free(co->stack_mem->stack_buffer); + free(co->stack_mem); + } + //walkerdu fix at 2018-01-20 + //瀛樺湪鍐呭瓨娉勬紡 + else + { + if(co->save_buffer) + free(co->save_buffer); + + if(co->stack_mem->occupy_co == co) + co->stack_mem->occupy_co = NULL; + } + + free( co ); +} +void co_release( stCoRoutine_t *co ) +{ + co_free( co ); +} + +void co_swap(stCoRoutine_t* curr, stCoRoutine_t* pending_co); + +void co_resume( stCoRoutine_t *co ) +{ + stCoRoutineEnv_t *env = co->env; + stCoRoutine_t *lpCurrRoutine = env->pCallStack[ env->iCallStackSize - 1 ]; + if( !co->cStart ) + { + coctx_make( &co->ctx,(coctx_pfn_t)CoRoutineFunc,co,0 ); + co->cStart = 1; + } + env->pCallStack[ env->iCallStackSize++ ] = co; + co_swap( lpCurrRoutine, co ); + + +} + + +// walkerdu 2018-01-14 +// 鐢ㄤ簬reset瓒呮椂鏃犳硶閲嶅浣跨敤鐨勫崗绋 +void co_reset(stCoRoutine_t * co) +{ + if(!co->cStart || co->cIsMain) + return; + + co->cStart = 0; + co->cEnd = 0; + + // 濡傛灉褰撳墠鍗忕▼鏈夊叡浜爤琚垏鍑虹殑buff锛岃杩涜閲婃斁 + if(co->save_buffer) + { + free(co->save_buffer); + co->save_buffer = NULL; + co->save_size = 0; + } + + // 濡傛灉鍏变韩鏍堣褰撳墠鍗忕▼鍗犵敤锛岃閲婃斁鍗犵敤鏍囧織锛屽惁鍒欒鍒囨崲锛屼細鎵цsave_stack_buffer() + if(co->stack_mem->occupy_co == co) + co->stack_mem->occupy_co = NULL; +} + +void co_yield_env( stCoRoutineEnv_t *env ) +{ + + stCoRoutine_t *last = env->pCallStack[ env->iCallStackSize - 2 ]; + stCoRoutine_t *curr = env->pCallStack[ env->iCallStackSize - 1 ]; + + env->iCallStackSize--; + + co_swap( curr, last); +} + +void co_yield_ct() +{ + + co_yield_env( co_get_curr_thread_env() ); +} +void co_yield( stCoRoutine_t *co ) +{ + co_yield_env( co->env ); +} + +void save_stack_buffer(stCoRoutine_t* occupy_co) +{ + ///copy out + stStackMem_t* stack_mem = occupy_co->stack_mem; + int len = stack_mem->stack_bp - occupy_co->stack_sp; + + if (occupy_co->save_buffer) + { + free(occupy_co->save_buffer), occupy_co->save_buffer = NULL; + } + + occupy_co->save_buffer = (char*)malloc(len); //malloc buf; + occupy_co->save_size = len; + + memcpy(occupy_co->save_buffer, occupy_co->stack_sp, len); +} + +void co_swap(stCoRoutine_t* curr, stCoRoutine_t* pending_co) +{ + stCoRoutineEnv_t* env = co_get_curr_thread_env(); + + //get curr stack sp + char c; + curr->stack_sp= &c; + + if (!pending_co->cIsShareStack) + { + env->pending_co = NULL; + env->occupy_co = NULL; + } + else + { + env->pending_co = pending_co; + //get last occupy co on the same stack mem + stCoRoutine_t* occupy_co = pending_co->stack_mem->occupy_co; + //set pending co to occupy thest stack mem; + pending_co->stack_mem->occupy_co = pending_co; + + env->occupy_co = occupy_co; + if (occupy_co && occupy_co != pending_co) + { + save_stack_buffer(occupy_co); + } + } + + //swap context + coctx_swap(&(curr->ctx),&(pending_co->ctx) ); + + //stack buffer may be overwrite, so get again; + stCoRoutineEnv_t* curr_env = co_get_curr_thread_env(); + stCoRoutine_t* update_occupy_co = curr_env->occupy_co; + stCoRoutine_t* update_pending_co = curr_env->pending_co; + + if (update_occupy_co && update_pending_co && update_occupy_co != update_pending_co) + { + //resume stack buffer + if (update_pending_co->save_buffer && update_pending_co->save_size > 0) + { + memcpy(update_pending_co->stack_sp, update_pending_co->save_buffer, update_pending_co->save_size); + } + } +} + + + +//int poll(struct pollfd fds[], nfds_t nfds, int timeout); +// { fd,events,revents } +struct stPollItem_t ; +struct stPoll_t : public stTimeoutItem_t +{ + struct pollfd *fds; + nfds_t nfds; // typedef unsigned long int nfds_t; + + stPollItem_t *pPollItems; + + int iAllEventDetach; + + int iEpollFd; + + int iRaiseCnt; + + +}; +struct stPollItem_t : public stTimeoutItem_t +{ + struct pollfd *pSelf; + stPoll_t *pPoll; + + struct epoll_event stEvent; +}; +/* + * EPOLLPRI POLLPRI // There is urgent data to read. + * EPOLLMSG POLLMSG + * + * POLLREMOVE + * POLLRDHUP + * POLLNVAL + * + * */ +static uint32_t PollEvent2Epoll( short events ) +{ + uint32_t e = 0; + if( events & POLLIN ) e |= EPOLLIN; + if( events & POLLOUT ) e |= EPOLLOUT; + if( events & POLLHUP ) e |= EPOLLHUP; + if( events & POLLERR ) e |= EPOLLERR; + if( events & POLLRDNORM ) e |= EPOLLRDNORM; + if( events & POLLWRNORM ) e |= EPOLLWRNORM; + return e; +} +static short EpollEvent2Poll( uint32_t events ) +{ + short e = 0; + if( events & EPOLLIN ) e |= POLLIN; + if( events & EPOLLOUT ) e |= POLLOUT; + if( events & EPOLLHUP ) e |= POLLHUP; + if( events & EPOLLERR ) e |= POLLERR; + if( events & EPOLLRDNORM ) e |= POLLRDNORM; + if( events & EPOLLWRNORM ) e |= POLLWRNORM; + return e; +} + +static __thread stCoRoutineEnv_t* gCoEnvPerThread = NULL; + +void co_init_curr_thread_env() +{ + gCoEnvPerThread = (stCoRoutineEnv_t*)calloc( 1, sizeof(stCoRoutineEnv_t) ); + stCoRoutineEnv_t *env = gCoEnvPerThread; + + env->iCallStackSize = 0; + struct stCoRoutine_t *self = co_create_env( env, NULL, NULL,NULL ); + self->cIsMain = 1; + + env->pending_co = NULL; + env->occupy_co = NULL; + + coctx_init( &self->ctx ); + + env->pCallStack[ env->iCallStackSize++ ] = self; + + stCoEpoll_t *ev = AllocEpoll(); + SetEpoll( env,ev ); +} +stCoRoutineEnv_t *co_get_curr_thread_env() +{ + return gCoEnvPerThread; +} + +void OnPollProcessEvent( stTimeoutItem_t * ap ) +{ + stCoRoutine_t *co = (stCoRoutine_t*)ap->pArg; + co_resume( co ); +} + +void OnPollPreparePfn( stTimeoutItem_t * ap,struct epoll_event &e,stTimeoutItemLink_t *active ) +{ + stPollItem_t *lp = (stPollItem_t *)ap; + lp->pSelf->revents = EpollEvent2Poll( e.events ); + + + stPoll_t *pPoll = lp->pPoll; + pPoll->iRaiseCnt++; + + if( !pPoll->iAllEventDetach ) + { + pPoll->iAllEventDetach = 1; + + RemoveFromLink( pPoll ); + + AddTail( active,pPoll ); + + } +} + + +void co_eventloop( stCoEpoll_t *ctx,pfn_co_eventloop_t pfn,void *arg ) +{ + if( !ctx->result ) + { + ctx->result = co_epoll_res_alloc( stCoEpoll_t::_EPOLL_SIZE ); + } + co_epoll_res *result = ctx->result; + + + for(;;) + { + int ret = co_epoll_wait( ctx->iEpollFd,result,stCoEpoll_t::_EPOLL_SIZE, 1 ); + + stTimeoutItemLink_t *active = (ctx->pstActiveList); + stTimeoutItemLink_t *timeout = (ctx->pstTimeoutList); + + memset( timeout,0,sizeof(stTimeoutItemLink_t) ); + + for(int i=0;ievents[i].data.ptr; + if( item->pfnPrepare ) + { + item->pfnPrepare( item,result->events[i],active ); + } + else + { + AddTail( active,item ); + } + } + + + unsigned long long now = GetTickMS(); + TakeAllTimeout( ctx->pTimeout,now,timeout ); + + stTimeoutItem_t *lp = timeout->head; + while( lp ) + { + //printf("raise timeout %p\n",lp); + lp->bTimeout = true; + lp = lp->pNext; + } + + Join( active,timeout ); + + lp = active->head; + while( lp ) + { + + PopHead( active ); + if (lp->bTimeout && now < lp->ullExpireTime) + { + int ret = AddTimeout(ctx->pTimeout, lp, now); + if (!ret) + { + lp->bTimeout = false; + lp = active->head; + continue; + } + } + if( lp->pfnProcess ) + { + lp->pfnProcess( lp ); + } + + lp = active->head; + } + if( pfn ) + { + if( -1 == pfn( arg ) ) + { + break; + } + } + + } +} +void OnCoroutineEvent( stTimeoutItem_t * ap ) +{ + stCoRoutine_t *co = (stCoRoutine_t*)ap->pArg; + co_resume( co ); +} + + +stCoEpoll_t *AllocEpoll() +{ + stCoEpoll_t *ctx = (stCoEpoll_t*)calloc( 1,sizeof(stCoEpoll_t) ); + + ctx->iEpollFd = co_epoll_create( stCoEpoll_t::_EPOLL_SIZE ); + ctx->pTimeout = AllocTimeout( 60 * 1000 ); + + ctx->pstActiveList = (stTimeoutItemLink_t*)calloc( 1,sizeof(stTimeoutItemLink_t) ); + ctx->pstTimeoutList = (stTimeoutItemLink_t*)calloc( 1,sizeof(stTimeoutItemLink_t) ); + + + return ctx; +} + +void FreeEpoll( stCoEpoll_t *ctx ) +{ + if( ctx ) + { + free( ctx->pstActiveList ); + free( ctx->pstTimeoutList ); + FreeTimeout( ctx->pTimeout ); + co_epoll_res_free( ctx->result ); + } + free( ctx ); +} + +stCoRoutine_t *GetCurrCo( stCoRoutineEnv_t *env ) +{ + return env->pCallStack[ env->iCallStackSize - 1 ]; +} +stCoRoutine_t *GetCurrThreadCo( ) +{ + stCoRoutineEnv_t *env = co_get_curr_thread_env(); + if( !env ) return 0; + return GetCurrCo(env); +} + + + +typedef int (*poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout); +int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc) +{ + if (timeout == 0) + { + return pollfunc(fds, nfds, timeout); + } + if (timeout < 0) + { + timeout = INT_MAX; + } + int epfd = ctx->iEpollFd; + stCoRoutine_t* self = co_self(); + + //1.struct change + stPoll_t& arg = *((stPoll_t*)malloc(sizeof(stPoll_t))); + memset( &arg,0,sizeof(arg) ); + + arg.iEpollFd = epfd; + arg.fds = (pollfd*)calloc(nfds, sizeof(pollfd)); + arg.nfds = nfds; + + stPollItem_t arr[2]; + if( nfds < sizeof(arr) / sizeof(arr[0]) && !self->cIsShareStack) + { + arg.pPollItems = arr; + } + else + { + arg.pPollItems = (stPollItem_t*)malloc( nfds * sizeof( stPollItem_t ) ); + } + memset( arg.pPollItems,0,nfds * sizeof(stPollItem_t) ); + + arg.pfnProcess = OnPollProcessEvent; + arg.pArg = GetCurrCo( co_get_curr_thread_env() ); + + + //2. add epoll + for(nfds_t i=0;i -1 ) + { + ev.data.ptr = arg.pPollItems + i; + ev.events = PollEvent2Epoll( fds[i].events ); + + int ret = co_epoll_ctl( epfd,EPOLL_CTL_ADD, fds[i].fd, &ev ); + if (ret < 0 && errno == EPERM && nfds == 1 && pollfunc != NULL) + { + if( arg.pPollItems != arr ) + { + free( arg.pPollItems ); + arg.pPollItems = NULL; + } + free(arg.fds); + free(&arg); + return pollfunc(fds, nfds, timeout); + } + } + //if fail,the timeout would work + } + + //3.add timeout + + unsigned long long now = GetTickMS(); + arg.ullExpireTime = now + timeout; + int ret = AddTimeout( ctx->pTimeout,&arg,now ); + int iRaiseCnt = 0; + if( ret != 0 ) + { + co_log_err("CO_ERR: AddTimeout ret %d now %lld timeout %d arg.ullExpireTime %lld", + ret,now,timeout,arg.ullExpireTime); + errno = EINVAL; + iRaiseCnt = -1; + + } + else + { + co_yield_env( co_get_curr_thread_env() ); + iRaiseCnt = arg.iRaiseCnt; + } + + { + //clear epoll status and memory + RemoveFromLink( &arg ); + for(nfds_t i = 0;i < nfds;i++) + { + int fd = fds[i].fd; + if( fd > -1 ) + { + co_epoll_ctl( epfd,EPOLL_CTL_DEL,fd,&arg.pPollItems[i].stEvent ); + } + fds[i].revents = arg.fds[i].revents; + } + + + if( arg.pPollItems != arr ) + { + free( arg.pPollItems ); + arg.pPollItems = NULL; + } + + free(arg.fds); + free(&arg); + } + + return iRaiseCnt; +} + +int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout_ms ) +{ + return co_poll_inner(ctx, fds, nfds, timeout_ms, NULL); +} + +void SetEpoll( stCoRoutineEnv_t *env,stCoEpoll_t *ev ) +{ + env->pEpoll = ev; +} +stCoEpoll_t *co_get_epoll_ct() +{ + if( !co_get_curr_thread_env() ) + { + co_init_curr_thread_env(); + } + return co_get_curr_thread_env()->pEpoll; +} +struct stHookPThreadSpec_t +{ + stCoRoutine_t *co; + void *value; + + enum + { + size = 1024 + }; +}; +void *co_getspecific(pthread_key_t key) +{ + stCoRoutine_t *co = GetCurrThreadCo(); + if( !co || co->cIsMain ) + { + return pthread_getspecific( key ); + } + return co->aSpec[ key ].value; +} +int co_setspecific(pthread_key_t key, const void *value) +{ + stCoRoutine_t *co = GetCurrThreadCo(); + if( !co || co->cIsMain ) + { + return pthread_setspecific( key,value ); + } + co->aSpec[ key ].value = (void*)value; + return 0; +} + + + +void co_disable_hook_sys() +{ + stCoRoutine_t *co = GetCurrThreadCo(); + if( co ) + { + co->cEnableSysHook = 0; + } +} +bool co_is_enable_sys_hook() +{ + stCoRoutine_t *co = GetCurrThreadCo(); + return ( co && co->cEnableSysHook ); +} + +stCoRoutine_t *co_self() +{ + return GetCurrThreadCo(); +} + +//co cond +struct stCoCond_t; +struct stCoCondItem_t +{ + stCoCondItem_t *pPrev; + stCoCondItem_t *pNext; + stCoCond_t *pLink; + + stTimeoutItem_t timeout; +}; +struct stCoCond_t +{ + stCoCondItem_t *head; + stCoCondItem_t *tail; +}; +static void OnSignalProcessEvent( stTimeoutItem_t * ap ) +{ + stCoRoutine_t *co = (stCoRoutine_t*)ap->pArg; + co_resume( co ); +} + +stCoCondItem_t *co_cond_pop( stCoCond_t *link ); +int co_cond_signal( stCoCond_t *si ) +{ + stCoCondItem_t * sp = co_cond_pop( si ); + if( !sp ) + { + return 0; + } + RemoveFromLink( &sp->timeout ); + + AddTail( co_get_curr_thread_env()->pEpoll->pstActiveList,&sp->timeout ); + + return 0; +} +int co_cond_broadcast( stCoCond_t *si ) +{ + for(;;) + { + stCoCondItem_t * sp = co_cond_pop( si ); + if( !sp ) return 0; + + RemoveFromLink( &sp->timeout ); + + AddTail( co_get_curr_thread_env()->pEpoll->pstActiveList,&sp->timeout ); + } + + return 0; +} + + +int co_cond_timedwait( stCoCond_t *link,int ms ) +{ + stCoCondItem_t* psi = (stCoCondItem_t*)calloc(1, sizeof(stCoCondItem_t)); + psi->timeout.pArg = GetCurrThreadCo(); + psi->timeout.pfnProcess = OnSignalProcessEvent; + + if( ms > 0 ) + { + unsigned long long now = GetTickMS(); + psi->timeout.ullExpireTime = now + ms; + + int ret = AddTimeout( co_get_curr_thread_env()->pEpoll->pTimeout,&psi->timeout,now ); + if( ret != 0 ) + { + free(psi); + return ret; + } + } + AddTail( link, psi); + + co_yield_ct(); + + + RemoveFromLink( psi ); + free(psi); + + return 0; +} +stCoCond_t *co_cond_alloc() +{ + return (stCoCond_t*)calloc( 1,sizeof(stCoCond_t) ); +} +int co_cond_free( stCoCond_t * cc ) +{ + free( cc ); + return 0; +} + + +stCoCondItem_t *co_cond_pop( stCoCond_t *link ) +{ + stCoCondItem_t *p = link->head; + if( p ) + { + PopHead( link ); + } + return p; +} diff --git a/trunk/3rdparty/libco/co_routine.h b/trunk/3rdparty/libco/co_routine.h new file mode 100644 index 0000000000..d6f478928a --- /dev/null +++ b/trunk/3rdparty/libco/co_routine.h @@ -0,0 +1,93 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __CO_ROUTINE_H__ +#define __CO_ROUTINE_H__ + +#include +#include +#include + +//1.struct + +struct stCoRoutine_t; +struct stShareStack_t; + +struct stCoRoutineAttr_t +{ + int stack_size; + stShareStack_t* share_stack; + stCoRoutineAttr_t() + { + stack_size = 128 * 1024; + share_stack = NULL; + } +}__attribute__ ((packed)); + +struct stCoEpoll_t; +typedef int (*pfn_co_eventloop_t)(void *); +typedef void *(*pfn_co_routine_t)( void * ); + +//2.co_routine + +int co_create( stCoRoutine_t **co,const stCoRoutineAttr_t *attr,void *(*routine)(void*),void *arg ); +void co_resume( stCoRoutine_t *co ); +void co_yield( stCoRoutine_t *co ); +void co_yield_ct(); //ct = current thread +void co_release( stCoRoutine_t *co ); +void co_reset(stCoRoutine_t * co); + +stCoRoutine_t *co_self(); + +int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout_ms ); +void co_eventloop( stCoEpoll_t *ctx,pfn_co_eventloop_t pfn,void *arg ); + +//3.specific + +int co_setspecific( pthread_key_t key, const void *value ); +void * co_getspecific( pthread_key_t key ); + +//4.event + +stCoEpoll_t * co_get_epoll_ct(); //ct = current thread + +//5.hook syscall ( poll/read/write/recv/send/recvfrom/sendto ) + +void co_enable_hook_sys(); +void co_disable_hook_sys(); +bool co_is_enable_sys_hook(); + +//6.sync +struct stCoCond_t; + +stCoCond_t *co_cond_alloc(); +int co_cond_free( stCoCond_t * cc ); + +int co_cond_signal( stCoCond_t * ); +int co_cond_broadcast( stCoCond_t * ); +int co_cond_timedwait( stCoCond_t *,int timeout_ms ); + +//7.share stack +stShareStack_t* co_alloc_sharestack(int iCount, int iStackSize); + +//8.init envlist for hook get/set env +void co_set_env_list( const char *name[],size_t cnt); + +void co_log_err( const char *fmt,... ); +#endif + diff --git a/trunk/3rdparty/libco/co_routine_inner.h b/trunk/3rdparty/libco/co_routine_inner.h new file mode 100644 index 0000000000..9d0e092de2 --- /dev/null +++ b/trunk/3rdparty/libco/co_routine_inner.h @@ -0,0 +1,111 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + +#ifndef __CO_ROUTINE_INNER_H__ + +#include "co_routine.h" +#include "coctx.h" +struct stCoRoutineEnv_t; +struct stCoSpec_t +{ + void *value; +}; + +struct stStackMem_t +{ + stCoRoutine_t* occupy_co; + int stack_size; + char* stack_bp; //stack_buffer + stack_size + char* stack_buffer; + +}; + +struct stShareStack_t +{ + unsigned int alloc_idx; + int stack_size; + int count; + stStackMem_t** stack_array; +}; + + + +struct stCoRoutine_t +{ + stCoRoutineEnv_t *env; + pfn_co_routine_t pfn; + void *arg; + coctx_t ctx; + + char cStart; + char cEnd; + char cIsMain; + char cEnableSysHook; + char cIsShareStack; + + void *pvEnv; + + //char sRunStack[ 1024 * 128 ]; + stStackMem_t* stack_mem; + + + //save satck buffer while confilct on same stack_buffer; + char* stack_sp; + unsigned int save_size; + char* save_buffer; + + stCoSpec_t aSpec[1024]; + +}; + + + +//1.env +void co_init_curr_thread_env(); +stCoRoutineEnv_t * co_get_curr_thread_env(); + +//2.coroutine +void co_free( stCoRoutine_t * co ); +void co_yield_env( stCoRoutineEnv_t *env ); + +//3.func + + + +//----------------------------------------------------------------------------------------------- + +struct stTimeout_t; +struct stTimeoutItem_t ; + +stTimeout_t *AllocTimeout( int iSize ); +void FreeTimeout( stTimeout_t *apTimeout ); +int AddTimeout( stTimeout_t *apTimeout,stTimeoutItem_t *apItem ,uint64_t allNow ); + +struct stCoEpoll_t; +stCoEpoll_t * AllocEpoll(); +void FreeEpoll( stCoEpoll_t *ctx ); + +stCoRoutine_t * GetCurrThreadCo(); +void SetEpoll( stCoRoutineEnv_t *env,stCoEpoll_t *ev ); + +typedef void (*pfnCoRoutineFunc_t)(); + +#endif + +#define __CO_ROUTINE_INNER_H__ diff --git a/trunk/3rdparty/libco/co_routine_specific.h b/trunk/3rdparty/libco/co_routine_specific.h new file mode 100644 index 0000000000..1b451caa52 --- /dev/null +++ b/trunk/3rdparty/libco/co_routine_specific.h @@ -0,0 +1,86 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#pragma once +#include +#include + +/* +invoke only once in the whole program +CoRoutineSetSpecificCallBack(CoRoutineGetSpecificFunc_t pfnGet,CoRoutineSetSpecificFunc_t pfnSet) + +struct MyData_t +{ + int iValue; + char szValue[100]; +}; +CO_ROUTINE_SPECIFIC( MyData_t,__routine ); + +int main() +{ + CoRoutineSetSpecificCallBack( co_getspecific,co_setspecific ); + + __routine->iValue = 10; + strcpy( __routine->szValue,"hello world" ); + + return 0; +} +*/ +extern int co_setspecific( pthread_key_t key, const void *value ); +extern void * co_getspecific( pthread_key_t key ); + +#define CO_ROUTINE_SPECIFIC( name,y ) \ +\ +static pthread_once_t _routine_once_##name = PTHREAD_ONCE_INIT; \ +static pthread_key_t _routine_key_##name;\ +static int _routine_init_##name = 0;\ +static void _routine_make_key_##name() \ +{\ + (void) pthread_key_create(&_routine_key_##name, NULL); \ +}\ +template \ +class clsRoutineData_routine_##name\ +{\ +public:\ + inline T *operator->()\ + {\ + if( !_routine_init_##name ) \ + {\ + pthread_once( &_routine_once_##name,_routine_make_key_##name );\ + _routine_init_##name = 1;\ + }\ + T* p = (T*)co_getspecific( _routine_key_##name );\ + if( !p )\ + {\ + p = (T*)calloc(1,sizeof( T ));\ + int ret = co_setspecific( _routine_key_##name,p) ;\ + if ( ret )\ + {\ + if ( p )\ + {\ + free(p);\ + p = NULL;\ + }\ + }\ + }\ + return p;\ + }\ +};\ +\ +static clsRoutineData_routine_##name y; + diff --git a/trunk/3rdparty/libco/coctx.cpp b/trunk/3rdparty/libco/coctx.cpp new file mode 100644 index 0000000000..d5eeed1486 --- /dev/null +++ b/trunk/3rdparty/libco/coctx.cpp @@ -0,0 +1,132 @@ +/* +* Tencent is pleased to support the open source community by making Libco +available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "coctx.h" +#include +#include + +#define ESP 0 +#define EIP 1 +#define EAX 2 +#define ECX 3 +// ----------- +#define RSP 0 +#define RIP 1 +#define RBX 2 +#define RDI 3 +#define RSI 4 + +#define RBP 5 +#define R12 6 +#define R13 7 +#define R14 8 +#define R15 9 +#define RDX 10 +#define RCX 11 +#define R8 12 +#define R9 13 + +//----- -------- +// 32 bit +// | regs[0]: ret | +// | regs[1]: ebx | +// | regs[2]: ecx | +// | regs[3]: edx | +// | regs[4]: edi | +// | regs[5]: esi | +// | regs[6]: ebp | +// | regs[7]: eax | = esp +enum { + kEIP = 0, + kEBP = 6, + kESP = 7, +}; + +//------------- +// 64 bit +// low | regs[0]: r15 | +// | regs[1]: r14 | +// | regs[2]: r13 | +// | regs[3]: r12 | +// | regs[4]: r9 | +// | regs[5]: r8 | +// | regs[6]: rbp | +// | regs[7]: rdi | +// | regs[8]: rsi | +// | regs[9]: ret | //ret func addr +// | regs[10]: rdx | +// | regs[11]: rcx | +// | regs[12]: rbx | +// hig | regs[13]: rsp | +enum { + kRDI = 7, + kRSI = 8, + kRETAddr = 9, + kRSP = 13, +}; + +// 64 bit +extern "C" { +extern void coctx_swap(coctx_t*, coctx_t*) asm("coctx_swap"); +}; +#if defined(__i386__) +int coctx_init(coctx_t* ctx) { + memset(ctx, 0, sizeof(*ctx)); + return 0; +} +int coctx_make(coctx_t* ctx, coctx_pfn_t pfn, const void* s, const void* s1) { + // make room for coctx_param + char* sp = ctx->ss_sp + ctx->ss_size - sizeof(coctx_param_t); + sp = (char*)((unsigned long)sp & -16L); + + coctx_param_t* param = (coctx_param_t*)sp; + void** ret_addr = (void**)(sp - sizeof(void*) * 2); + *ret_addr = (void*)pfn; + param->s1 = s; + param->s2 = s1; + + memset(ctx->regs, 0, sizeof(ctx->regs)); + + ctx->regs[kESP] = (char*)(sp) - sizeof(void*) * 2; + return 0; +} +#elif defined(__x86_64__) +int coctx_make(coctx_t* ctx, coctx_pfn_t pfn, const void* s, const void* s1) { + char* sp = ctx->ss_sp + ctx->ss_size - sizeof(void*); + sp = (char*)((unsigned long)sp & -16LL); + + memset(ctx->regs, 0, sizeof(ctx->regs)); + void** ret_addr = (void**)(sp); + *ret_addr = (void*)pfn; + + ctx->regs[kRSP] = sp; + + ctx->regs[kRETAddr] = (char*)pfn; + + ctx->regs[kRDI] = (char*)s; + ctx->regs[kRSI] = (char*)s1; + return 0; +} + +int coctx_init(coctx_t* ctx) { + memset(ctx, 0, sizeof(*ctx)); + return 0; +} + +#endif diff --git a/trunk/3rdparty/libco/coctx.h b/trunk/3rdparty/libco/coctx.h new file mode 100644 index 0000000000..c1fdfa9da9 --- /dev/null +++ b/trunk/3rdparty/libco/coctx.h @@ -0,0 +1,42 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __CO_CTX_H__ +#define __CO_CTX_H__ +#include +typedef void* (*coctx_pfn_t)( void* s, void* s2 ); +struct coctx_param_t +{ + const void *s1; + const void *s2; +}; +struct coctx_t +{ +#if defined(__i386__) + void *regs[ 8 ]; +#else + void *regs[ 14 ]; +#endif + size_t ss_size; + char *ss_sp; + +}; + +int coctx_init( coctx_t *ctx ); +int coctx_make( coctx_t *ctx,coctx_pfn_t pfn,const void *s,const void *s1 ); +#endif diff --git a/trunk/3rdparty/libco/coctx_swap.S b/trunk/3rdparty/libco/coctx_swap.S new file mode 100644 index 0000000000..0e4ce1c92b --- /dev/null +++ b/trunk/3rdparty/libco/coctx_swap.S @@ -0,0 +1,83 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +.globl coctx_swap +#if !defined( __APPLE__ ) +.type coctx_swap, @function +#endif +coctx_swap: + +#if defined(__i386__) + movl 4(%esp), %eax + movl %esp, 28(%eax) + movl %ebp, 24(%eax) + movl %esi, 20(%eax) + movl %edi, 16(%eax) + movl %edx, 12(%eax) + movl %ecx, 8(%eax) + movl %ebx, 4(%eax) + + + movl 8(%esp), %eax + movl 4(%eax), %ebx + movl 8(%eax), %ecx + movl 12(%eax), %edx + movl 16(%eax), %edi + movl 20(%eax), %esi + movl 24(%eax), %ebp + movl 28(%eax), %esp + + ret + +#elif defined(__x86_64__) + leaq (%rsp),%rax + movq %rax, 104(%rdi) + movq %rbx, 96(%rdi) + movq %rcx, 88(%rdi) + movq %rdx, 80(%rdi) + movq 0(%rax), %rax + movq %rax, 72(%rdi) + movq %rsi, 64(%rdi) + movq %rdi, 56(%rdi) + movq %rbp, 48(%rdi) + movq %r8, 40(%rdi) + movq %r9, 32(%rdi) + movq %r12, 24(%rdi) + movq %r13, 16(%rdi) + movq %r14, 8(%rdi) + movq %r15, (%rdi) + xorq %rax, %rax + + movq 48(%rsi), %rbp + movq 104(%rsi), %rsp + movq (%rsi), %r15 + movq 8(%rsi), %r14 + movq 16(%rsi), %r13 + movq 24(%rsi), %r12 + movq 32(%rsi), %r9 + movq 40(%rsi), %r8 + movq 56(%rsi), %rdi + movq 80(%rsi), %rdx + movq 88(%rsi), %rcx + movq 96(%rsi), %rbx + leaq 8(%rsp), %rsp + pushq 72(%rsi) + + movq 64(%rsi), %rsi + ret +#endif diff --git a/trunk/3rdparty/libco/example_closure.cpp b/trunk/3rdparty/libco/example_closure.cpp new file mode 100644 index 0000000000..c5bae1cc4b --- /dev/null +++ b/trunk/3rdparty/libco/example_closure.cpp @@ -0,0 +1,91 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_closure.h" +#include +#include +#include +#include +#include +using namespace std; + +static void *thread_func( void * arg ) +{ + stCoClosure_t *p = (stCoClosure_t*) arg; + p->exec(); + return 0; +} +static void batch_exec( vector &v ) +{ + vector ths; + for( size_t i=0;i v; + + pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; + + int total = 100; + vector v2; + co_ref( ref,total,v2,m); + for(int i=0;i<10;i++) + { + co_func( f,ref,i ) + { + printf("ref.total %d i %d\n",ref.total,i ); + //lock + pthread_mutex_lock(&ref.m); + ref.v2.push_back( i ); + pthread_mutex_unlock(&ref.m); + //unlock + } + co_func_end; + v.push_back( new f( ref,i ) ); + } + for(int i=0;i<2;i++) + { + co_func( f2,i ) + { + printf("i: %d\n",i); + for(int j=0;j<2;j++) + { + usleep( 1000 ); + printf("i %d j %d\n",i,j); + } + } + co_func_end; + v.push_back( new f2( i ) ); + } + + batch_exec( v ); + printf("done\n"); + + return 0; +} + + diff --git a/trunk/3rdparty/libco/example_cond.cpp b/trunk/3rdparty/libco/example_cond.cpp new file mode 100644 index 0000000000..5262568074 --- /dev/null +++ b/trunk/3rdparty/libco/example_cond.cpp @@ -0,0 +1,83 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include +#include "co_routine.h" +using namespace std; +struct stTask_t +{ + int id; +}; +struct stEnv_t +{ + stCoCond_t* cond; + queue task_queue; +}; +void* Producer(void* args) +{ + co_enable_hook_sys(); + stEnv_t* env= (stEnv_t*)args; + int id = 0; + while (true) + { + stTask_t* task = (stTask_t*)calloc(1, sizeof(stTask_t)); + task->id = id++; + env->task_queue.push(task); + printf("%s:%d produce task %d\n", __func__, __LINE__, task->id); + co_cond_signal(env->cond); + poll(NULL, 0, 1000); + } + return NULL; +} +void* Consumer(void* args) +{ + co_enable_hook_sys(); + stEnv_t* env = (stEnv_t*)args; + while (true) + { + if (env->task_queue.empty()) + { + co_cond_timedwait(env->cond, -1); + continue; + } + stTask_t* task = env->task_queue.front(); + env->task_queue.pop(); + printf("%s:%d consume task %d\n", __func__, __LINE__, task->id); + free(task); + } + return NULL; +} +int main() +{ + stEnv_t* env = new stEnv_t; + env->cond = co_cond_alloc(); + + stCoRoutine_t* consumer_routine; + co_create(&consumer_routine, NULL, Consumer, env); + co_resume(consumer_routine); + + stCoRoutine_t* producer_routine; + co_create(&producer_routine, NULL, Producer, env); + co_resume(producer_routine); + + co_eventloop(co_get_epoll_ct(), NULL, NULL); + return 0; +} diff --git a/trunk/3rdparty/libco/example_copystack.cpp b/trunk/3rdparty/libco/example_copystack.cpp new file mode 100644 index 0000000000..92062a6ea4 --- /dev/null +++ b/trunk/3rdparty/libco/example_copystack.cpp @@ -0,0 +1,61 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include +#include +#include +#include "coctx.h" +#include "co_routine.h" +#include "co_routine_inner.h" + +void* RoutineFunc(void* args) +{ + co_enable_hook_sys(); + int* routineid = (int*)args; + while (true) + { + char sBuff[128]; + sprintf(sBuff, "from routineid %d stack addr %p\n", *routineid, sBuff); + + printf("%s", sBuff); + poll(NULL, 0, 1000); //sleep 1s + } + return NULL; +} + +int main() +{ + stShareStack_t* share_stack= co_alloc_sharestack(1, 1024 * 128); + stCoRoutineAttr_t attr; + attr.stack_size = 0; + attr.share_stack = share_stack; + + stCoRoutine_t* co[2]; + int routineid[2]; + for (int i = 0; i < 2; i++) + { + routineid[i] = i; + co_create(&co[i], &attr, RoutineFunc, routineid + i); + co_resume(co[i]); + } + co_eventloop(co_get_epoll_ct(), NULL, NULL); + return 0; +} diff --git a/trunk/3rdparty/libco/example_echocli.cpp b/trunk/3rdparty/libco/example_echocli.cpp new file mode 100644 index 0000000000..083c1e7009 --- /dev/null +++ b/trunk/3rdparty/libco/example_echocli.cpp @@ -0,0 +1,218 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_routine.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +struct stEndPoint +{ + char *ip; + unsigned short int port; +}; + +static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr) +{ + bzero(&addr,sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(shPort); + int nIP = 0; + if( !pszIP || '\0' == *pszIP + || 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0") + || 0 == strcmp(pszIP,"*") + ) + { + nIP = htonl(INADDR_ANY); + } + else + { + nIP = inet_addr(pszIP); + } + addr.sin_addr.s_addr = nIP; + +} + +static int iSuccCnt = 0; +static int iFailCnt = 0; +static int iTime = 0; + +void AddSuccCnt() +{ + int now = time(NULL); + if (now >iTime) + { + printf("time %d Succ Cnt %d Fail Cnt %d\n", iTime, iSuccCnt, iFailCnt); + iTime = now; + iSuccCnt = 0; + iFailCnt = 0; + } + else + { + iSuccCnt++; + } +} +void AddFailCnt() +{ + int now = time(NULL); + if (now >iTime) + { + printf("time %d Succ Cnt %d Fail Cnt %d\n", iTime, iSuccCnt, iFailCnt); + iTime = now; + iSuccCnt = 0; + iFailCnt = 0; + } + else + { + iFailCnt++; + } +} + +static void *readwrite_routine( void *arg ) +{ + + co_enable_hook_sys(); + + stEndPoint *endpoint = (stEndPoint *)arg; + char str[8]="sarlmol"; + char buf[ 1024 * 16 ]; + int fd = -1; + int ret = 0; + for(;;) + { + if ( fd < 0 ) + { + fd = socket(PF_INET, SOCK_STREAM, 0); + struct sockaddr_in addr; + SetAddr(endpoint->ip, endpoint->port, addr); + ret = connect(fd,(struct sockaddr*)&addr,sizeof(addr)); + + if ( errno == EALREADY || errno == EINPROGRESS ) + { + struct pollfd pf = { 0 }; + pf.fd = fd; + pf.events = (POLLOUT|POLLERR|POLLHUP); + co_poll( co_get_epoll_ct(),&pf,1,200); + //check connect + int error = 0; + uint32_t socklen = sizeof(error); + errno = 0; + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,(void *)&error, &socklen); + if ( ret == -1 ) + { + //printf("getsockopt ERROR ret %d %d:%s\n", ret, errno, strerror(errno)); + close(fd); + fd = -1; + AddFailCnt(); + continue; + } + if ( error ) + { + errno = error; + //printf("connect ERROR ret %d %d:%s\n", error, errno, strerror(errno)); + close(fd); + fd = -1; + AddFailCnt(); + continue; + } + } + + } + + ret = write( fd,str, 8); + if ( ret > 0 ) + { + ret = read( fd,buf, sizeof(buf) ); + if ( ret <= 0 ) + { + //printf("co %p read ret %d errno %d (%s)\n", + // co_self(), ret,errno,strerror(errno)); + close(fd); + fd = -1; + AddFailCnt(); + } + else + { + //printf("echo %s fd %d\n", buf,fd); + AddSuccCnt(); + } + } + else + { + //printf("co %p write ret %d errno %d (%s)\n", + // co_self(), ret,errno,strerror(errno)); + close(fd); + fd = -1; + AddFailCnt(); + } + } + return 0; +} + +int main(int argc,char *argv[]) +{ + stEndPoint endpoint; + endpoint.ip = argv[1]; + endpoint.port = atoi(argv[2]); + int cnt = atoi( argv[3] ); + int proccnt = atoi( argv[4] ); + + struct sigaction sa; + sa.sa_handler = SIG_IGN; + sigaction( SIGPIPE, &sa, NULL ); + + for(int k=0;k 0 ) + { + continue; + } + else if( pid < 0 ) + { + break; + } + for(int i=0;i +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __FreeBSD__ +#include +#include +#include +#endif + +using namespace std; +struct task_t +{ + stCoRoutine_t *co; + int fd; +}; + +static stack g_readwrite; +static int g_listen_fd = -1; +static int SetNonBlock(int iSock) +{ + int iFlags; + + iFlags = fcntl(iSock, F_GETFL, 0); + iFlags |= O_NONBLOCK; + iFlags |= O_NDELAY; + int ret = fcntl(iSock, F_SETFL, iFlags); + return ret; +} + +static void *readwrite_routine( void *arg ) +{ + + co_enable_hook_sys(); + + task_t *co = (task_t*)arg; + char buf[ 1024 * 16 ]; + for(;;) + { + if( -1 == co->fd ) + { + g_readwrite.push( co ); + co_yield_ct(); + continue; + } + + int fd = co->fd; + co->fd = -1; + + for(;;) + { + struct pollfd pf = { 0 }; + pf.fd = fd; + pf.events = (POLLIN|POLLERR|POLLHUP); + co_poll( co_get_epoll_ct(),&pf,1,1000); + + int ret = read( fd,buf,sizeof(buf) ); + if( ret > 0 ) + { + ret = write( fd,buf,ret ); + } + if( ret > 0 || ( -1 == ret && EAGAIN == errno ) ) + { + continue; + } + close( fd ); + break; + } + + } + return 0; +} +int co_accept(int fd, struct sockaddr *addr, socklen_t *len ); +static void *accept_routine( void * ) +{ + co_enable_hook_sys(); + printf("accept_routine\n"); + fflush(stdout); + for(;;) + { + //printf("pid %ld g_readwrite.size %ld\n",getpid(),g_readwrite.size()); + if( g_readwrite.empty() ) + { + printf("empty\n"); //sleep + struct pollfd pf = { 0 }; + pf.fd = -1; + poll( &pf,1,1000); + + continue; + + } + struct sockaddr_in addr; //maybe sockaddr_un; + memset( &addr,0,sizeof(addr) ); + socklen_t len = sizeof(addr); + + int fd = co_accept(g_listen_fd, (struct sockaddr *)&addr, &len); + if( fd < 0 ) + { + struct pollfd pf = { 0 }; + pf.fd = g_listen_fd; + pf.events = (POLLIN|POLLERR|POLLHUP); + co_poll( co_get_epoll_ct(),&pf,1,1000 ); + continue; + } + if( g_readwrite.empty() ) + { + close( fd ); + continue; + } + SetNonBlock( fd ); + task_t *co = g_readwrite.top(); + co->fd = fd; + g_readwrite.pop(); + co_resume( co->co ); + } + return 0; +} + +static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr) +{ + bzero(&addr,sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(shPort); + int nIP = 0; + if( !pszIP || '\0' == *pszIP + || 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0") + || 0 == strcmp(pszIP,"*") + ) + { + nIP = htonl(INADDR_ANY); + } + else + { + nIP = inet_addr(pszIP); + } + addr.sin_addr.s_addr = nIP; + +} + +static int CreateTcpSocket(const unsigned short shPort /* = 0 */,const char *pszIP /* = "*" */,bool bReuse /* = false */) +{ + int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); + if( fd >= 0 ) + { + if(shPort != 0) + { + if(bReuse) + { + int nReuseAddr = 1; + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr)); + } + struct sockaddr_in addr ; + SetAddr(pszIP,shPort,addr); + int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr)); + if( ret != 0) + { + close(fd); + return -1; + } + } + } + return fd; +} + + +int main(int argc,char *argv[]) +{ + if(argc<5){ + printf("Usage:\n" + "example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT]\n" + "example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT] -d # daemonize mode\n"); + return -1; + } + const char *ip = argv[1]; + int port = atoi( argv[2] ); + int cnt = atoi( argv[3] ); + int proccnt = atoi( argv[4] ); + bool deamonize = argc >= 6 && strcmp(argv[5], "-d") == 0; + + g_listen_fd = CreateTcpSocket( port,ip,true ); + listen( g_listen_fd,1024 ); + if(g_listen_fd==-1){ + printf("Port %d is in use\n", port); + return -1; + } + printf("listen %d %s:%d\n",g_listen_fd,ip,port); + + SetNonBlock( g_listen_fd ); + + for(int k=0;k 0 ) + { + continue; + } + else if( pid < 0 ) + { + break; + } + for(int i=0;ifd = -1; + + co_create( &(task->co),NULL,readwrite_routine,task ); + co_resume( task->co ); + + } + stCoRoutine_t *accept_co = NULL; + co_create( &accept_co,NULL,accept_routine,0 ); + co_resume( accept_co ); + + co_eventloop( co_get_epoll_ct(),0,0 ); + + exit(0); + } + if(!deamonize) wait(NULL); + return 0; +} + diff --git a/trunk/3rdparty/libco/example_poll.cpp b/trunk/3rdparty/libco/example_poll.cpp new file mode 100644 index 0000000000..eb92f219e7 --- /dev/null +++ b/trunk/3rdparty/libco/example_poll.cpp @@ -0,0 +1,211 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_routine.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __FreeBSD__ +#include +#endif + +using namespace std; + +struct task_t +{ + stCoRoutine_t *co; + int fd; + struct sockaddr_in addr; +}; + +static int SetNonBlock(int iSock) +{ + int iFlags; + + iFlags = fcntl(iSock, F_GETFL, 0); + iFlags |= O_NONBLOCK; + iFlags |= O_NDELAY; + int ret = fcntl(iSock, F_SETFL, iFlags); + return ret; +} + + + +static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr) +{ + bzero(&addr,sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(shPort); + int nIP = 0; + if( !pszIP || '\0' == *pszIP + || 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0") + || 0 == strcmp(pszIP,"*") + ) + { + nIP = htonl(INADDR_ANY); + } + else + { + nIP = inet_addr(pszIP); + } + addr.sin_addr.s_addr = nIP; + +} + +static int CreateTcpSocket(const unsigned short shPort = 0 ,const char *pszIP = "*" ,bool bReuse = false ) +{ + int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); + if( fd >= 0 ) + { + if(shPort != 0) + { + if(bReuse) + { + int nReuseAddr = 1; + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr)); + } + struct sockaddr_in addr ; + SetAddr(pszIP,shPort,addr); + int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr)); + if( ret != 0) + { + close(fd); + return -1; + } + } + } + return fd; +} + +static void *poll_routine( void *arg ) +{ + co_enable_hook_sys(); + + vector &v = *(vector*)arg; + for(size_t i=0;i setRaiseFds; + size_t iWaitCnt = v.size(); + for(;;) + { + int ret = poll( pf,iWaitCnt,1000 ); + printf("co %p poll wait %ld ret %d\n", + co_self(),iWaitCnt,ret); + for(int i=0;i<(int)iWaitCnt;i++) + { + printf("co %p fire fd %d revents 0x%X POLLOUT 0x%X POLLERR 0x%X POLLHUP 0x%X\n", + co_self(), + pf[i].fd, + pf[i].revents, + POLLOUT, + POLLERR, + POLLHUP + ); + setRaiseFds.insert( pf[i].fd ); + } + if( setRaiseFds.size() == v.size()) + { + break; + } + if( ret <= 0 ) + { + break; + } + + iWaitCnt = 0; + for(size_t i=0;i v; + for(int i=1;i v2 = v; + poll_routine( &v2 ); + printf("--------------------- routine -------------------\n"); + + for(int i=0;i<10;i++) + { + stCoRoutine_t *co = 0; + vector *v2 = new vector(); + *v2 = v; + co_create( &co,NULL,poll_routine,v2 ); + printf("routine i %d\n",i); + co_resume( co ); + } + + co_eventloop( co_get_epoll_ct(),0,0 ); + + return 0; +} +//./example_poll 127.0.0.1 12365 127.0.0.1 12222 192.168.1.1 1000 192.168.1.2 1111 + diff --git a/trunk/3rdparty/libco/example_setenv.cpp b/trunk/3rdparty/libco/example_setenv.cpp new file mode 100644 index 0000000000..520cfb6333 --- /dev/null +++ b/trunk/3rdparty/libco/example_setenv.cpp @@ -0,0 +1,89 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include +#include +#include +#include "co_routine.h" + +const char* CGI_ENV_HOOK_LIST [] = +{ + "CGINAME", +}; +struct stRoutineArgs_t +{ + int iRoutineID; +}; +void SetAndGetEnv(int iRoutineID) +{ + printf("routineid %d begin\n", iRoutineID); + + //use poll as sleep + poll(NULL, 0, 500); + + char sBuf[128]; + sprintf(sBuf, "cgi_routine_%d", iRoutineID); + int ret = setenv("CGINAME", sBuf, 1); + if (ret) + { + printf("%s:%d set env err ret %d errno %d %s\n", __func__, __LINE__, + ret, errno, strerror(errno)); + return; + } + printf("routineid %d set env CGINAME %s\n", iRoutineID, sBuf); + + poll(NULL, 0, 500); + + char* env = getenv("CGINAME"); + if (!env) + { + printf("%s:%d get env err errno %d %s\n", __func__, __LINE__, + errno, strerror(errno)); + return; + } + printf("routineid %d get env CGINAME %s\n", iRoutineID, env); +} + +void* RoutineFunc(void* args) +{ + co_enable_hook_sys(); + + stRoutineArgs_t* g = (stRoutineArgs_t*)args; + + SetAndGetEnv(g->iRoutineID); + return NULL; +} + +int main(int argc, char* argv[]) +{ + co_set_env_list(CGI_ENV_HOOK_LIST, sizeof(CGI_ENV_HOOK_LIST) / sizeof(char*)); + stRoutineArgs_t args[3]; + for (int i = 0; i < 3; i++) + { + stCoRoutine_t* co = NULL; + args[i].iRoutineID = i; + co_create(&co, NULL, RoutineFunc, &args[i]); + co_resume(co); + } + co_eventloop(co_get_epoll_ct(), NULL, NULL); + return 0; +} + diff --git a/trunk/3rdparty/libco/example_specific.cpp b/trunk/3rdparty/libco/example_specific.cpp new file mode 100644 index 0000000000..5d20a8b1a8 --- /dev/null +++ b/trunk/3rdparty/libco/example_specific.cpp @@ -0,0 +1,61 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "co_routine_specific.h" +#include "co_routine.h" +#include +#include +#include +#include +using namespace std; +struct stRoutineArgs_t +{ + stCoRoutine_t* co; + int routine_id; +}; +struct stRoutineSpecificData_t +{ + int idx; +}; + +CO_ROUTINE_SPECIFIC(stRoutineSpecificData_t, __routine); + +void* RoutineFunc(void* args) +{ + co_enable_hook_sys(); + stRoutineArgs_t* routine_args = (stRoutineArgs_t*)args; + __routine->idx = routine_args->routine_id; + while (true) + { + printf("%s:%d routine specific data idx %d\n", __func__, __LINE__, __routine->idx); + poll(NULL, 0, 1000); + } + return NULL; +} +int main() +{ + stRoutineArgs_t args[10]; + for (int i = 0; i < 10; i++) + { + args[i].routine_id = i; + co_create(&args[i].co, NULL, RoutineFunc, (void*)&args[i]); + co_resume(args[i].co); + } + co_eventloop(co_get_epoll_ct(), NULL, NULL); + return 0; +} diff --git a/trunk/3rdparty/libco/example_thread.cpp b/trunk/3rdparty/libco/example_thread.cpp new file mode 100644 index 0000000000..401771071a --- /dev/null +++ b/trunk/3rdparty/libco/example_thread.cpp @@ -0,0 +1,56 @@ +/* +* Tencent is pleased to support the open source community by making Libco available. + +* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + + +#include "co_routine.h" +#include "co_routine_inner.h" + +#include +#include +#include +#include +#include + +int loop(void *) +{ + return 0; +} +static void *routine_func( void * ) +{ + stCoEpoll_t * ev = co_get_epoll_ct(); //ct = current thread + co_eventloop( ev,loop,0 ); + return 0; +} +int main(int argc,char *argv[]) +{ + int cnt = atoi( argv[1] ); + + pthread_t tid[ cnt ]; + for(int i=0;i -# -# To start with more than the default 64 initial pollfd slots -# (but the table grows dynamically anyway): -# DEFINES += -DST_MIN_POLLFDS_SIZE= -# -# Note that you can also add these defines by specifying them as -# make/gmake arguments (without editing this Makefile). For example: -# -# make EXTRA_CFLAGS=-DUSE_POLL -# -# (replace make with gmake if needed). -# -# You can also modify the default selection of an alternative event -# notification mechanism. E.g., to enable kqueue(2) support (if it's not -# enabled by default): -# -# gmake EXTRA_CFLAGS=-DMD_HAVE_KQUEUE -# -# or to disable default epoll(4) support: -# -# make EXTRA_CFLAGS=-UMD_HAVE_EPOLL -# -########################## - -CFLAGS += $(DEFINES) $(OTHER_FLAGS) $(EXTRA_CFLAGS) - -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) -EXAMPLES = examples - -LINKNAME = libst.$(DSO_SUFFIX) -SONAME = libst.$(DSO_SUFFIX).$(MAJOR) -FULLNAME = libst.$(DSO_SUFFIX).$(VERSION) - -ifeq ($(OS), CYGWIN) -SONAME = cygst.$(DSO_SUFFIX) -SLIBRARY = $(TARGETDIR)/libst.dll.a -DLIBRARY = $(TARGETDIR)/$(SONAME) -LINKNAME = -# examples directory does not compile under cygwin -EXAMPLES = -endif - -# for SRS -# disable examples for ubuntu crossbuild failed. -# @see https://github.com/winlinvip/simple-rtmp-server/issues/308 -EXAMPLES = - -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) $(EXAMPLES) $(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 - -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.o: md.S - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGETDIR)/%.o: %.c common.h md.h - $(CC) $(CFLAGS) -c $< -o $@ - -examples:: - @echo Making $@ - @cd $@; $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)" OS="$(OS)" TARGETDIR="$(TARGETDIR)" - -clean: - rm -rf *_OPT *_DBG obj st.pc - -########################## -# Pattern rules: - -ifneq ($(SFLAGS),) -# Compile with shared library options if it's a C file -$(TARGETDIR)/%-pic.o: %.c common.h md.h - $(CC) $(CFLAGS) $(SFLAGS) -c $< -o $@ -endif - -# Compile assembly as normal or C as normal if no SFLAGS -%-pic.o: %.o - rm -f $@; $(LN) $(LNFLAGS) $(. Install them with: - # rpm -i libst*.rpm -Requires GNU automake and rpm 3.0.3 or later. - -Debian users: - If you run potato, please upgrade to woody. - If you run woody, "apt-get install libst-dev" will get you v1.3. - If you run testing/unstable, you will get the newest available version. - If you *must* have the newest libst in woody, you may follow these - not-recommended instructions: - 1. Add "deb-src unstable main" to your - /etc/apt/sources.list - 2. apt-get update - 3. apt-get source st - 4. cd st-1.4 (or whatever version you got) - 5. debuild - 6. dpkg -i ../*.deb - -If your application uses autoconf to search for dependencies and you -want to search for a given version of libst, you can simply add - PKG_CHECK_MODULES(MYAPP, st >= 1.3 mumble >= 0.2.23) -to your configure.ac/in. This will define @MYAPP_LIBS@ and -@MYAPP_CFLAGS@ which you may then use in your Makefile.am/in files to -link against mumble and st. - - -LICENSE - -The State Threads library is a derivative of the Netscape Portable -Runtime library (NSPR). All source code in this directory is -distributed under the terms of the Mozilla Public License (MPL) version -1.1 or the GNU General Public License (GPL) version 2 or later. For -more information about these licenses please see -http://www.mozilla.org/MPL/ and http://www.gnu.org/copyleft/. - -All source code in the "examples" directory is distributed under the BSD -style license. - - -PLATFORMS - -Please see the "docs/notes.html" file for the list of currently -supported platforms. - - -DEBUGGER SUPPORT - -It's almost impossible to print SP and PC in a portable way. The only -way to see thread's stack platform-independently is to actually jump to -the saved context. That's what the _st_iterate_threads() function does. -Do the following to iterate over all threads: - -- set the _st_iterate_threads_flag to 1 in debugger -- set breakpoint at the _st_show_thread_stack() function - (which does nothing) -- call the _st_iterate_threads() function which jumps to the - next thread -- at each break you can explore thread's stack -- continue -- when iteration is complete, you return to the original - point (you can see thread id and a message as arguments of - the _st_show_thread_stack() function). - -You can call _st_iterate_threads() in three ways: - -- Insert it into your source code at the point you want to - go over threads. -- Just run application and this function will be called at - the first context switch. -- Call it directly from the debugger at any point. - -This works with gdb and dbx. - -Example using gdb: - -(gdb) set _st_iterate_threads_flag = 1 -(gdb) b _st_show_thread_stack -... -(gdb) call _st_iterate_threads() -... -(gdb) bt -... -(gdb) c -... -(gdb) bt -... -(gdb) c -... -and so on... - -_st_iterate_threads_flag will be set to 0 automatically -after iteration is over or you can set it to 0 at any time -to stop iteration. - -Sometimes gdb complains about SIGSEGV when you call a function -directly at gdb command-line. It can be ignored -- just call the -same function right away again, it works just fine. For example: - -(gdb) set _st_iterate_threads_flag = 1 -(gdb) b _st_show_thread_stack -Breakpoint 1 at 0x809bbbb: file sched.c, line 856. -(gdb) call _st_iterate_threads() -Program received signal SIGSEGV, Segmentation fault. -.... -(gdb) # just call the function again: -(gdb) call _st_iterate_threads() -Breakpoint 1, _st_show_thread_stack (thread=0x4017aee4, messg=0x80ae7a2 -"Iteration started") at sched.c:856 -856 } -.... - -You can use simple gdb command-line scripting to display -all threads and their stack traces at once: - -(gdb) while _st_iterate_threads_flag - >bt - >c - >end -.... - -Another script to stop at the thread with the specific thread id -(e.g., 0x40252ee4): - -(gdb) # set the flag again: -(gdb) set _st_iterate_threads_flag = 1 -(gdb) call _st_iterate_threads() -Breakpoint 1, _st_show_thread_stack (thread=0x4017aee4, messg=0x80ae7a2 -"Iteration started") at sched.c:856 -856 } -.... -(gdb) while thread != 0x40252ee4 - >c - >end -.... -.... -Breakpoint 1, _st_show_thread_stack (thread=0x40252ee4, messg=0x0) at -sched.c:856 -856 } -(gdb) bt -.... -(gdb) # don't want to continue iteration, unset the flag: -(gdb) set _st_iterate_threads_flag = 0 -(gdb) c -Continuing. -Breakpoint 1, _st_show_thread_stack (thread=0x0, messg=0x80ae78e "Iteration -completed") - at sched.c:856 -856 } -(gdb) c -Continuing. -(gdb) return -Make selected stack frame return now? (y or n) y -#0 0x4011254e in __select () - from /lib/libc.so.6 -(gdb) detach - - -CHANGE LOG - -Changes from 1.8 to 1.9. ------------------------- -o Support 32-bit and 64-bit Intel Macs. - -o Added ST_VERSION string, and ST_VERSION_MAJOR and ST_VERSION_MINOR - [bug 1796801]. - -o Fixed some compiler warnings, based on a patch from Brian Wellington - [bug 1932741]. - - -Changes from 1.7 to 1.8. --------------------------- -o Added support for kqueue and epoll on platforms that support them. - Added ability to choose the event notification system at program - startup. - -o Long-overdue public definitions of ST_UTIME_NO_TIMEOUT (-1ULL) and - ST_UTIME_NO_WAIT (0) [bug 1514436]. - -o Documentation patch for st_utime() [bug 1514484]. - -o Documentation patch for st_timecache_set() [bug 1514486]. - -o Documentation patch for st_netfd_serialize_accept() [bug 1514494]. - -o Added st_writev_resid() [rfe 1538344]. - -o Added st_readv_resid() [rfe 1538768] and, for symmetry, st_readv(). - - -Changes from 1.6 to 1.7. ------------------------- -o Support glibc 2.4, which breaks programs that manipulate jump buffers. - Replaced Linux IA64 special cases with new md.S that covers all - Linux. - - -Changes from 1.5.2 to 1.6. --------------------------- -none - - -Changes from 1.5.1 to 1.5.2. ----------------------------- -o Alfred Perlstein's context switch callback feature. - -o Claus Assmann's st_recvmsg/st_sendmsg wrappers. - -o Extra stack padding for platforms that need it. - -o Ron Arts's timeout clarifications in the reference manual. - -o Raymond Bero and Anton Berezin's AMD64 FreeBSD port. - -o Claus Assmann's AMD64 SunOS 5.10 port. - -o Claus Assmann's AMD64 OpenBSD port. - -o Michael Abd-El-Malek's Mac OS X port. - -o Michael Abd-El-Malek's stack printing patch. - - -Changes from 1.5.0 to 1.5.1. ----------------------------- -o Andreas Gustafsson's USE_POLL fix. - -o Gene's st_set_utime_function() enhancement. - - -Changes from 1.4 to 1.5.0. --------------------------- -o Andreas Gustafsson's performance patch. - -o New extensions: Improved DNS resolver, generic LRU cache, in-process - DNS cache, and a program to test the resolver and cache. - -o Support for AMD Opteron 64-bit CPUs under Linux. - -o Support for SPARC-64 under Solaris. - -o Andreas Gustafsson's support for VAX under NetBSD. - -o Changed unportable #warning directives in md.h to #error. - - -Changes from 1.3 to 1.4. ------------------------- -o Andreas Gustafsson's NetBSD port. - -o Wesley W. Terpstra's Darwin (MacOS X) port. - -o Support for many CPU architectures under Linux and *BSD. - -o Renamed private typedefs so they don't conflict with public ones any - more. - -o common.h now includes public.h for strict prototyping. - -o Joshua Levy's recommendation to make st_connect() and st_sendto() - accept const struct sockaddr pointers, as the originals do. - -o Clarified the documentation regarding blocking vs. non-blocking I/O. - -o Cygwin support. - -o Created the extensions directory. - -o Fixed warnings from ia64asm.S. - - -Changes from 1.2 to 1.3. ------------------------- -o Added st_read_resid() and st_write_resid() to allow the caller to know - how much data was transferred before an error occurred. Updated - documentation. - -o Updated project link, copyrights, and documentation regarding - timeouts. Added comment to st_connect(). - -o Optimized the _st_add_sleep_q() function in sched.c. Now we walk the - sleep queue *backward* when inserting a thread into it. When you - have lots (hundreds) of threads and several timeout values, it takes - a while to insert a thread at the appropriate point in the sleep - queue. The idea is that often this appropriate point is closer to - the end of the queue rather than the beginning. Measurements show - performance improves with this change. In any case this change - should do no harm. - -o Added a hint of when to define USE_POLL and when not to, to the - Makefile. - -o Added debugging support (files common.h and sched.c). See above. - -o Decreased the number of reallocations of _ST_POLLFDS in sched.c. - Inspired by Lev Walkin. - -o Fixed st_usleep(-1) and st_sleep(-1), and added a warning to the - documentation about too-large timeouts. - -o Linux/*BSD Alpha port. - -o Wesley W. Terpstra modernized the build process: - - properly build relocatable libraries under bsd and linux - - use library versioning - - added rpm spec file - - added debian/ files - See above for build instructions. - - -Changes from 1.1 to 1.2. ------------------------- -o Added st_randomize_stacks(). - -o Added a patch contributed by Sascha Schumann. - - -Changes from 1.0 to 1.1. ------------------------- -o Relicensed under dual MPL-GPL. - -o OpenBSD port. - -o Compile-time option to use poll() instead of select() for - event polling (see Makefile). - This is useful if you want to support a large number of open - file descriptors (larger than FD_SETSIZE) within a single - process. - -o Linux IA-64 port. - Two issues make IA-64 different from other platforms: - - - Besides the traditional call stack in memory, IA-64 uses the - general register stack. Thus each thread needs a backing store - for the register stack in addition to the memory stack. - - - Current implementation of setjmp()/longjmp() can not be used - for thread context-switching since it assumes that only one - register stack exists. Using special assembly functions for - context-switching is unavoidable. - -o Thread stack capping on IRIX. - This allows some profiling tools (such as SpeedShop) to know when - to stop unwinding the stack. Without this libexc, used by SpeedShop, - traces right off the stack and crashes. - -o Miscellaneous documentation additions. - - -COPYRIGHTS - -Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. -All Rights Reserved. diff --git a/trunk/3rdparty/st-srs/README.md b/trunk/3rdparty/st-srs/README.md deleted file mode 100644 index 60fc1a6518..0000000000 --- a/trunk/3rdparty/st-srs/README.md +++ /dev/null @@ -1,88 +0,0 @@ -# state-threads - -![](http://ossrs.net:8000/gif/v1/sls.gif?site=github.com&path=/srs/srsst) -[![](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). - -## Branch SRS - -The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patched the following patches: - -- [x] Patch [st.arm.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/1.st.arm.patch), for ARM. -- [x] Patch [st.osx.kqueue.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/3.st.osx.kqueue.patch), for osx. -- [x] Patch [st.disable.examples.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/4.st.disable.examples.patch), for ubuntu. -- [x] [Refine TAB of code](https://github.com/ossrs/state-threads/compare/c2001d30ca58f55d72a6cc6b9b6c70391eaf14db...d2101b26988b0e0db0aabc53ddf452068c1e2cbc). -- [x] 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] Merge from [toffaletti](https://github.com/toffaletti/state-threads), support [valgrind](https://github.com/ossrs/state-threads/issues/2) for ST. -- [x] 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] Support macro `MD_ST_NO_ASM` to disable ASM, [#8](https://github.com/ossrs/state-threads/issues/8). -- [x] 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). - -## Docs - -* 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 - -## Usage - -Get code: - -``` -git clone https://github.com/ossrs/state-threads.git st-1.9 && -git checkout -b srs origin/srs -``` - -For Linux: - -``` -make linux-debug EXTRA_CFLAGS="-DMD_HAVE_EPOLL" -``` - -For OSX: - -``` -make darwin-debug EXTRA_CFLAGS="-DMD_HAVE_KQUEUE" -``` - -Linux with valgrind: - -``` -make linux-debug EXTRA_CFLAGS="-DMD_VALGRIND" -``` - -> Remark: User must install valgrind, for instance, in centos6 `sudo yum install -y valgrind valgrind-devel`. - -Linux with valgrind and epoll: - -``` -make linux-debug EXTRA_CFLAGS="-DMD_HAVE_EPOLL -DMD_VALGRIND" -``` - -For OSX, user must specifies the valgrind header files: - -``` -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. - -## Valgrind - -How to debug with gdb under valgrind, read [valgrind manual](http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-simple). - -About startup parameters, read [valgrind cli](http://valgrind.org/docs/manual/mc-manual.html#mc-manual.options). - -Important cli options: - -1. `--undef-value-errors= [default: yes]`, Controls whether Memcheck reports uses of undefined value errors. Set this to no if you don't want to see undefined value errors. It also has the side effect of speeding up Memcheck somewhat. -1. `--leak-check= [default: summary]`, When enabled, search for memory leaks when the client program finishes. If set to summary, it says how many leaks occurred. If set to full or yes, each individual leak will be shown in detail and/or counted as an error, as specified by the options `--show-leak-kinds` and `--errors-for-leak-kinds`. -1. `--track-origins= [default: no]`, Controls whether Memcheck tracks the origin of uninitialised values. By default, it does not, which means that although it can tell you that an uninitialised value is being used in a dangerous way, it cannot tell you where the uninitialised value came from. This often makes it difficult to track down the root problem. -1. `--show-reachable= , --show-possibly-lost=`, to show the using memory. - -Winlin 2016 diff --git a/trunk/3rdparty/st-srs/common.h b/trunk/3rdparty/st-srs/common.h deleted file mode 100644 index 0c0685b9ae..0000000000 --- a/trunk/3rdparty/st-srs/common.h +++ /dev/null @@ -1,479 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#ifndef __ST_COMMON_H__ -#define __ST_COMMON_H__ - -#include -#include -#include -#include -#include - -/* Enable assertions only if DEBUG is defined */ -#ifndef DEBUG - #define NDEBUG -#endif -#include -#define ST_ASSERT(expr) assert(expr) - -#define ST_BEGIN_MACRO { -#define ST_END_MACRO } - -#ifdef DEBUG - #define ST_HIDDEN /*nothing*/ -#else - #define ST_HIDDEN static -#endif - -#include "public.h" -#include "md.h" - -/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef MD_VALGRIND - #ifndef NVALGRIND - #define NVALGRIND - #endif -#else - #undef NVALGRIND -#endif - - -/***************************************** - * Circular linked list definitions - */ - -typedef struct _st_clist { - struct _st_clist *next; - struct _st_clist *prev; -} _st_clist_t; - -/* Insert element "_e" into the list, before "_l" */ -#define ST_INSERT_BEFORE(_e,_l) \ - ST_BEGIN_MACRO \ - (_e)->next = (_l); \ - (_e)->prev = (_l)->prev; \ - (_l)->prev->next = (_e); \ - (_l)->prev = (_e); \ - ST_END_MACRO - -/* Insert element "_e" into the list, after "_l" */ -#define ST_INSERT_AFTER(_e,_l) \ - ST_BEGIN_MACRO \ - (_e)->next = (_l)->next; \ - (_e)->prev = (_l); \ - (_l)->next->prev = (_e); \ - (_l)->next = (_e); \ - ST_END_MACRO - -/* Return the element following element "_e" */ -#define ST_NEXT_LINK(_e) ((_e)->next) - -/* Append an element "_e" to the end of the list "_l" */ -#define ST_APPEND_LINK(_e,_l) ST_INSERT_BEFORE(_e,_l) - -/* Insert an element "_e" at the head of the list "_l" */ -#define ST_INSERT_LINK(_e,_l) ST_INSERT_AFTER(_e,_l) - -/* Return the head/tail of the list */ -#define ST_LIST_HEAD(_l) (_l)->next -#define ST_LIST_TAIL(_l) (_l)->prev - -/* Remove the element "_e" from it's circular list */ -#define ST_REMOVE_LINK(_e) \ - ST_BEGIN_MACRO \ - (_e)->prev->next = (_e)->next; \ - (_e)->next->prev = (_e)->prev; \ - ST_END_MACRO - -/* Return non-zero if the given circular list "_l" is empty, */ -/* zero if the circular list is not empty */ -#define ST_CLIST_IS_EMPTY(_l) \ - ((_l)->next == (_l)) - -/* Initialize a circular list */ -#define ST_INIT_CLIST(_l) \ - ST_BEGIN_MACRO \ - (_l)->next = (_l); \ - (_l)->prev = (_l); \ - ST_END_MACRO - -#define ST_INIT_STATIC_CLIST(_l) \ - {(_l), (_l)} - - -/***************************************** - * Basic types definitions - */ - -typedef void (*_st_destructor_t)(void *); - - -typedef struct _st_stack { - _st_clist_t links; - char *vaddr; /* Base of stack's allocated memory */ - int vaddr_size; /* Size of stack's allocated memory */ - int stk_size; /* Size of usable portion of the stack */ - char *stk_bottom; /* Lowest address of stack's usable portion */ - char *stk_top; /* Highest address of stack's usable portion */ - void *sp; /* Stack pointer from C's point of view */ -#ifdef __ia64__ - void *bsp; /* Register stack backing store pointer */ -#endif - /* 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_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 - -#ifndef __ia64__ - #define ST_DEFAULT_STACK_SIZE (64*1024) -#else - #define ST_DEFAULT_STACK_SIZE (128*1024) /* Includes register stack size */ -#endif - -#ifndef ST_KEYS_MAX - #define ST_KEYS_MAX 16 -#endif - -#ifndef ST_MIN_POLLFDS_SIZE - #define ST_MIN_POLLFDS_SIZE 64 -#endif - - -/***************************************** - * Threads context switching - */ - -#ifdef DEBUG - void _st_iterate_threads(void); - #define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads() -#else - #define ST_DEBUG_ITERATE_THREADS() -#endif - -#ifdef ST_SWITCH_CB - #define ST_SWITCH_OUT_CB(_thread) \ - if (_st_this_vp.switch_out_cb != NULL && \ - _thread != _st_this_vp.idle_thread && \ - _thread->state != _ST_ST_ZOMBIE) { \ - _st_this_vp.switch_out_cb(); \ - } - #define ST_SWITCH_IN_CB(_thread) \ - if (_st_this_vp.switch_in_cb != NULL && \ - _thread != _st_this_vp.idle_thread && \ - _thread->state != _ST_ST_ZOMBIE) { \ - _st_this_vp.switch_in_cb(); \ - } -#else - #define ST_SWITCH_OUT_CB(_thread) - #define ST_SWITCH_IN_CB(_thread) -#endif - -/* - * Switch away from the current thread context by saving its state and - * calling the thread scheduler - */ -#define _ST_SWITCH_CONTEXT(_thread) \ - ST_BEGIN_MACRO \ - ST_SWITCH_OUT_CB(_thread); \ - if (!MD_SETJMP((_thread)->context)) { \ - _st_vp_schedule(); \ - } \ - ST_DEBUG_ITERATE_THREADS(); \ - ST_SWITCH_IN_CB(_thread); \ - ST_END_MACRO - -/* - * Restore a thread context that was saved by _ST_SWITCH_CONTEXT or - * initialized by _ST_INIT_CONTEXT - */ -#define _ST_RESTORE_CONTEXT(_thread) \ - ST_BEGIN_MACRO \ - _ST_SET_CURRENT_THREAD(_thread); \ - MD_LONGJMP((_thread)->context, 1); \ - ST_END_MACRO - -/* - * Initialize the thread context preparing it to execute _main - */ -#ifdef MD_INIT_CONTEXT - #define _ST_INIT_CONTEXT MD_INIT_CONTEXT -#else - #error Unknown OS -#endif - -/* - * Number of bytes reserved under the stack "bottom" - */ -#define _ST_STACK_PAD_SIZE MD_STACK_PAD_SIZE - - -/***************************************** - * Forward declarations - */ - -void _st_vp_schedule(void); -void _st_vp_check_clock(void); -void *_st_idle_thread_start(void *arg); -void _st_thread_main(void); -void _st_thread_cleanup(_st_thread_t *thread); -void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout); -void _st_del_sleep_q(_st_thread_t *thread); -_st_stack_t *_st_stack_new(int stack_size); -void _st_stack_free(_st_stack_t *ts); -int _st_io_init(void); - -st_utime_t st_utime(void); -_st_cond_t *st_cond_new(void); -int st_cond_destroy(_st_cond_t *cvar); -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout); -int st_cond_signal(_st_cond_t *cvar); -ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout); -ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout); -int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size); - -#endif /* !__ST_COMMON_H__ */ - diff --git a/trunk/3rdparty/st-srs/docs/fig.gif b/trunk/3rdparty/st-srs/docs/fig.gif deleted file mode 100644 index 7265a05db4f516b44fcf37c3949922ff4f62999d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5374 zcmd6m^;;9(+s6+fj8H~5N)H$!A+63y4h$3-t#pcjh%^XDj2hiF5Jtn0Zs|rqY9OIV z>BppTd;EOA&+|V#=a+L`=en=^ykD<#A8lPNWhE;pr5(i~`0r`}fXp1lEEFXqlqobe zHby3s0e}L)0D#z2zy`p10M-Ct0ssvFjv7qS0uTZ;0eIs9^qBw@4H$4hY)S;m#00#& zKt2}K;{UH2fGKK#0uVB2aR&(Sw*rj@I2`5F)W0{-0busu-cW#M1i1I$?=cfC#4DsF zA0TQYrXb+C7l2`bE&g)IGN9}RN(AV?t)Bx57!bCF2LKjYCJ^GaL5Y2@gdczg11fTZ zC@gcKHU>Pvnm}_6^eBI^rG+>D8f^q zOaOug2{-_m`YVln->BBEhvC!rba>_*{FnqGX*9lCNvt2!{Mf;rjSUl ztT61E;a}as08Ius7?|P$&&XgCCJHV=7(q)fKwv2}y{J~PY-9L7&_&QwFNz45LQX^q1wXUNT@I>N2d0~}4jGiC$2LG-|Ct1>BAfOU zE~=sYeT8wo^U6p=#eB`f z)G{@3D683ijc%eXi68fO|GF&j@)lcXTa!mu7i-tV^2z?o&-uDi`PVaj_xC0ZF2aM_ z{Xcw~ttZLk-{JN5{joimJXvkF?+{w{%0FM`_?p+j*I%oHlR@M_^0$UT{y^%^)SRGq zRGOx3O*l${BQrG)P9J5H#B@1hJ?@N-D?iatQY=4~@qDyh zs`<$KkEt4}#daAMY25Q6#Kg?kY4(jfD;aJB!^8};5vMIDuMcHg_Fm;JTlt~;blU}K z?{&8eJCCEcaUt9z+eLmV^gG4Rbx}L7-Ayn%B`!`QJEiu%^t*VgXw+_*c_wDJ99=fD zTS28oTnAffer2165M5>!UHNbRU4K@zo7*YBzlAFlN63D9CyG zh;G^pedvf-uX+gLa#N+}n8b{F{kYs{W&OmpEm!#|iBoljNlgX|`3e1R?Z3zG(OT3S z2q(iI_6a;YLl3bFGa9}(y0x$1NIe7hy7PyH;Xse6#+7e=&pTUDSC=#D*=-kL)fXfk z_Xj#29lm?~KDtzoU2PZlJKJ7pQ#S1=TK~SB$K0EctI6sC5h%|!IQ^7*)&OI0h1 zsOXoX{+zV6%4aw6?K5xLb=Vkx&z-V8mehF1kKBtvZQnR8Ivq#yA5fnaH{U2$D~`}h zIV+r$7g2w$rtNk1`kjo3f9<@@>NSm5`KAHLMTY8`gB%>w>W<52FS&~MPX~ief7KY0 ziVoGx&JGJc-@u*}`*D#^l)A&gcSCWhOZ9AymjPdG5YoC}nVRJ~WI92N$$V`z&7lXf zL~U1}g&k3i!k&42H`EweJNsFR{32Z(D$>VHGpH>I&#etR%(f(38z-JNK!Zu@Uka(2 zK(B!lCV~ULdfq}O@uK9N&`Xu;nv#*df^$=trk@u}4>mPgWZk0?wtOtwoE?WxP^Jdq z1ssXuELX0}Mtu^F=H-pjR7|stw+xTIP`ITgPmuBTnVs2h9qLfsjT6-zo9Cf;rG}iA z^o@_@;JZ6LBpd3Il)|?rkSv<4gL4VVD>mT26_Y64BWsmh`UHdV7 zSF+@R3xDr|mdeSsR~%(U40I0g8;1%_nLh%z)FsrY=I+!KDkvnG?WiYDw?~M$7LZ*8 zuHRj{x@hvCNc-G*eUD+bbk*>J&9`%gfC=X2t8At;$u(VT;c-*V zr?8c$WP({akA_(}UHlIoIhnob&9)WU#9xj@mdmy1_JKIYfR8R04~x-CPWR)(@_q?} z8O~eOZWAFpdpX}(E_XFn<%e+VuThcFLDGJSFDq=;`J9^HvWrb)ksBZMzrM)hYrGqV z@>zrOF7$KIT#39Hu+EULq$TY+8RM1nk^i)LaFIC#8>#*4r=)LBKX^2D<-&JTilgVIV%zb!dwhM)mj0F! z?b-wd43XU8`kAiRxX=26Le`-AM`XIM;6HxViIIC-ub`Qi5(I{@aYxoSlz233N<1?C z!`Ck4XU=d^(@6=(<#65&pQS}yeIjMC#`x0sMx+)~C9~_c_$%Yviw_z+wQ(-r?W6m7 zd@XB)H`OQ*OADRXYs%(1zw}VJFC@ObqFazVLKE4tDCFCS4W#GYGE$y=R(QcD zQcl%k0e`6didAoOOzW6%b!D9`zx|zZ@KC$0W!Y;;I|_zs8GS)|mKUblZ-7i|iz0m} zS!l0UvSjhnP!N=1IN7?saXflsL$2IE+N?!HMXqh-xsyC`)ZC$U;q`?^LgpWRE)|bg zcP<~~oLUUfT~J*$yY3@#hf9 zy7cz#p*L$pMj}sA)4v(+v2^+tUwmykwdAm?XQ(#UL@KCJvV5$VRxU8m-=zx)HYNMCyEOb+g{n}6u~ZoS#1<lz_AHv>M$_g`n zBB>K<@$%)BAE7SnVP80vShuCQ>OvblSrs5pt2e?>??Tm@S#QC@nmxm#r^8T=?2!II zMXp=r$Z$h>qjjZM551U;bl7eVv6|wwR;OPrzU8)RM%o;O!+ne&LEK7^5jP4=pJcc` z#YYtNgg5p}yGMmTi;9#th?Lj4YUB}7C@kq8rATT)vxnn#mOP^n+x#&{T5Ri43RdAJ z%+b5nQQoj9&F#q9`_d_np?dEkc_El=Wwx~KXmxzRJsr_f{KJw3Dfke3#x^{Q1+&f( zR19-!)-fBJj*+N~34_FT^^t_xg!i> zJ9cp!-R&87u`cGrP|Wg?&bm*0Dn8gMF}7AnVx!qFY9l^9HNK)5_9Nrrg~SA3bf|4% z{62*F%#nGM%WDhsUFd3K6Fl2zOGI?j_M}<@0x-Sl$>mI)9g&r?>c2* zOrVcd(u1Ref(U8x;+N|OQ$2?>9+{+Pv}Ejir}@%Z$Q)<*HYJ5yMW)fEV|3H)qSGtr zoXQ$Ajmpw+y6&1UxD-uNJ&)ZUEoN0oIMocN*X^Vy7-xHX#Me9V8Q)Ko(-mA4&xDKS z>QSB!iUhbGcXQa-!>V-f*r@%aAkV%`NK z=ir`Pox{A#9(lYk^Sib4Y3=gQ8wZ~4Y#p%`;ZfBa*wu zxtxaECr&l9#kA6brp#TtvUj9Ht0kkFr!t2}tfAytv9qVYv(qPovb@ig3Ycu9cvTHg zMQ~KWhpd>jR>xJ#(2Q<8P2Nc+kHN7T4_RCer3a2b+k&xUqqJIQh~QllIBf= zY6Z%_vRjKk8dF8utlw09^Wz}%yT=;^UVd3I-=U?328@%mL*@NZ9~<60BeftVrFv6h z&C@MBLXBXtS8t$KuFh-!NUef|ipyAk+P&0xF-Ayz&)HY%4ckFIFN=0K=NqT&yyJn! zkiACMk+j3c@HiJD#**T|0$Y8*1hsQD-U*Wml2>)lw>Q z$1=QHo1V7(6#YeP7K*^@-G3`6xvN?oD;sYU^J*u$-W@9!gCHzJ?gQRhX z9zo9mgks;(NJEx)xqN_=?djF)rGrn-1WmYy?h#5u19*|IXq`D64)+8RDJvE%T0I2{(zi;vYgg~pB9`f-ffIS zxwQ6Tv18{rhEEFz%`253FLh6=kI$Nq7iEY@orTB*l!Y(%JV%wxxrPLM8UKezW|D70 z)=6MA)8;)FPTa5iuycU0@VeQ~>W7u;ne zHm|aM@>G9XFWdfPY$A?PStGFj!uer*?6{@$)Z^UQ2RyxE<7t+>FO;`tOjauNEoS$> z%*wAgE9MG3m!3&coe1`u(C?dZxb3^_ToyJsS3NuC*)eZ(Ui9~wV8qJQFr$Fl*P;OJ z_inVlF#g%wO0yZlkw?qC?DIa;dXoWJ(^&xvnyNv2e37|d7jt};GI$5!9VLAYOWUr! nQO3mPTw-env3-Tu`G?rezx?*va-YTWK;ZIF?s5+c6sY|V5v;Zh diff --git a/trunk/3rdparty/st-srs/docs/notes.html b/trunk/3rdparty/st-srs/docs/notes.html deleted file mode 100644 index 5a24369e22..0000000000 --- a/trunk/3rdparty/st-srs/docs/notes.html +++ /dev/null @@ -1,434 +0,0 @@ - - -State Threads Library Programming Notes - - -

Programming Notes

-

- -

- -

-


-

- -

Porting

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

-

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

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

  • -

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

    -

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

    -

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

    -

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

    -

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

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

  • -
-

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

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

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

- - -

Signals

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

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

- - -

Intra-Process Synchronization

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

- - -

Inter-Process Synchronization

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

-There are several reasons for creating multiple processes: -

-

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

    -

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

    -

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

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

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

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

-

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

    -

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

    -

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

    -

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

- - -

Non-Network I/O

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

-

-

- - -

Timeouts

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

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

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

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

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

-


-

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

State Threads Library Reference

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

-

Error Handling
-

-

Library Initialization
-

-

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

-

st_switch_cb_t type
-
st_set_switch_in_cb()
-
st_set_switch_out_cb()
-

-

Thread Control and Identification
-

-

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

-

Per-Thread Private Data
-

-

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

-

Synchronization
-

-

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

-

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

-

Timing
-

-

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

-

I/O Functions
-

-

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

-

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

-

st_open()
-
st_poll()
-

-

Program Structure
-

-

List of Blocking Functions
-

-

-

-


-

- - - -

Types

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

-

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

-


-

- - -

st_thread_t

- -Thread type. -

-

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

-

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

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

-


-

- - -

st_cond_t

- -Condition variable type. -

-

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

-

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

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

-


-

- - -

st_mutex_t

- -Mutex type. -

-

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

-

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

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

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

-


-

- - -

st_utime_t

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

-

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

-

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

-


-

- - -

st_netfd_t

- -File descriptor type. -

-

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

-

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

-


-

- - -

st_switch_cb_t

- -Context switch callback function type. -

-

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

-

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

-


-

- - -

Error Handling

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

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

-


-

- - -

Library Initialization

- -

-

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

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

st_set_switch_in_cb()
-
st_set_switch_out_cb()
-
-

-


-

- - -

st_init()

- -Initializes the runtime. -

-

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

-

Parameters
-None. -

-

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

-

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

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

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

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

-


-

- -

st_getfdlimit()

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

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- - -

st_set_eventsys()

- -Sets event notification mechanism. -

-

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

-

Parameters
-st_set_eventsys() has the following parameter:

-eventsys

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

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

-

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

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

-

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

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

-


-

- -

st_get_eventsys()

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

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- -

st_get_eventsys_name()

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

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- - -

st_set_switch_in_cb()

- - -

st_set_switch_out_cb()

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

-

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

-

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

-cb

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

-

Returns
-The previous callback function pointer. -

-

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

-These functions can be called at any time.

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

-


-

- - -

Thread Control and Identification

- -

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

-

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

-


-

- -

st_thread_create()

- -Creates a new thread. -

-

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

-

Parameters
-st_thread_create() has the following parameters:

-start

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

-arg

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

-joinable

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

-stack_size

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

-

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

-

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

-


-

- -

st_thread_exit()

- -Terminates the calling thread. -

-

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

-

Parameters
-st_thread_exit() has the following parameters:

-retval

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

-

Returns
-Nothing. -

-

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

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

-


-

- -

st_thread_join()

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

-

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

-

Parameters
-st_thread_join() has the following parameters:

-thread

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

-retvalp

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

-

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

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

-

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

-


-

- -

st_thread_self()

- -Identifies the calling thread. -

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- -

st_thread_interrupt()

- -Interrupts a target thread. -

-

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

-

Parameters
-st_thread_interrupt() has the following parameters:

-thread

-A valid identifier for the thread being interrupted. -

-

Returns
-Nothing. -

-

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

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

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

-


-

- -

st_sleep(), st_usleep()

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

-

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

-

Parameters
-st_sleep() has the following parameters:

-secs

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

-st_usleep() has the following parameters:

-usecs

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

-

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

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

-

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

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

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

-


-

- -

st_randomize_stacks()

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

-

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

-

Parameters
-st_randomize_stacks() has the following parameters:

-on

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

-

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

-

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

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

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

-


-

- - -

Per-Thread Private Data

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

-

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

-


-

- -

st_key_create()

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

-

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

-

Parameters
-st_key_create() has the following parameters:

-keyp

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

-destructor

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

-

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

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

-

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

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

-


-

- -

st_key_getlimit()

- -Returns the key limit. -

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- -

st_thread_setspecific()

- -Sets per-thread private data. -

-

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

-

Parameters
-st_thread_setspecific() has the following parameters:

-key

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

-value

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

-

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

- - - -
EINVALThe specified key is invalid.
-

-

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

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

-


-

- -

st_thread_getspecific()

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

-

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

-

Parameters
-st_thread_getspecific() has the following parameters:

-key

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

-

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

-

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

-


-

- - -

Synchronization

- -

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

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

-

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

-

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

-


-

- -

st_cond_new()

- -Creates a new condition variable. -

-

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

-

Parameters
-None. -

-

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

-

Description
-This function creates a new condition variable. -

-


-

- -

st_cond_destroy()

- -Destroys a condition variable. -

-

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

-

Parameters
-st_cond_destroy() has the following parameters:

-cvar

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

-

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

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

-

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

-


-

- -

st_cond_wait()

- -Waits on a condition. -

-

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

-

Parameters
-st_cond_wait() has the following parameters:

-cvar

-The condition variable on which to wait. -

-

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

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

-

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

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

-


-

- -

st_cond_timedwait()

- -Waits on a condition. -

-

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

-

Parameters
-st_cond_timedwait() has the following parameters:

-cvar

-The condition variable on which to wait.

-timeout

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

-

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

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

-

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

-


-

- -

st_cond_signal()

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

-

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

-

Parameters
-st_cond_signal() has the following parameters:

-cvar

-The condition variable to signal. -

-

Returns
-Always zero. -

-

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

-


-

- -

st_cond_broadcast()

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

-

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

-

Parameters
-st_cond_broadcast() has the following parameters:

-cvar

-The condition variable to broadcast. -

-

Returns
-Always zero. -

-

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

-


-

- - -

st_mutex_new()

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

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- -

st_mutex_destroy()

- -Destroys a specified mutex object. -

-

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

-

Parameters
-st_mutex_destroy() has the following parameters:

-lock

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

-

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

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

-

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

-


-

- -

st_mutex_lock()

- -Locks a specified mutex object. -

-

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

-

Parameters
-st_mutex_lock() has the following parameters:

-lock

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

-

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

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

-

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

-


-

- -

st_mutex_trylock()

- -Attempts to acquire a mutex. -

-

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

-

Parameters
-st_mutex_trylock() has the following parameters:

-lock

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

-

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

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

-

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

-


-

- -

st_mutex_unlock()

- -Releases a specified mutex object. -

-

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

-

Parameters
-st_mutex_unlock() has the following parameters:

-lock

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

-

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

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

-

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

-


-

- - -

Timing

- -

-

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

-


-

- -

st_utime()

- -Returns current high-resolution time. -

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- -

st_set_utime_function()

- -Set high-resolution time function. -

-

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

-

Parameters
-st_set_utime_function() has the following parameters:

-func

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

-

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

-

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

-


-

- -

st_timecache_set()

- -Turns the time caching on or off. -

-

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

-

Parameters
-st_timecache_set() has the following parameters:

-on

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

-

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

-

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

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

-


-

- -

st_time()

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

-

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

-

Parameters
-None. -

-

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

-

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

-


-

- - -

I/O Functions

- -

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

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

-

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

-

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

-


-

- -

st_netfd_open()

- -Creates a new file descriptor object. -

-

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

-

Parameters
-st_netfd_open() has the following parameters:

-osfd

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

-

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

-

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

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

-


-

- -

st_netfd_open_socket()

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

-

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

-

Parameters
-st_netfd_open_socket() has the following parameters:

-osfd

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

-

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

-

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

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

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

-


-

- -

st_netfd_free()

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

-

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

-

Parameters
-st_netfd_free() has the following parameters:

-fd

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

-

Returns
-Nothing. -

-

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

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

-


-

- -

st_netfd_close()

- -Closes a file descriptor. -

-

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

-

Parameters
-st_netfd_close() has the following parameters:

-fd

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

-

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

-

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

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

-


-

- -

st_netfd_fileno()

- -Returns an underlying OS file descriptor. -

-

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

-

Parameters
-st_netfd_fileno() has the following parameters:

-fd

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

-

Returns
-An underlying OS file descriptor. -

-

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

-


-

- -

st_netfd_setspecific()

- -Sets per-descriptor private data. -

-

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

-

Parameters
-st_netfd_setspecific() has the following parameters:

-fd

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

-value

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

-destructor

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

-

Returns
-Nothing. -

-

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

-


-

- -

st_netfd_getspecific()

- -Retrieves the per-descriptor private data. -

-

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

-

Parameters
-st_netfd_getspecific() has the following parameters:

-fd

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

-

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

-

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

-


-

- -

st_netfd_serialize_accept()

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

-

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

-

Parameters
-st_netfd_serialize_accept() has the following parameters:

-fd

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

-

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

-

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

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

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

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

-


-

- -

st_netfd_poll()

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

-

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

-

Parameters
-st_netfd_poll() has the following parameters:

-fd

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

-how

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

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

-timeout

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

-

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

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

-

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

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

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

-


-

- -

st_accept()

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

-

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

-

Parameters
-st_accept() has the following parameters:

-fd

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

-addr

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

-addrlen

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

-timeout

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

-

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

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

-

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

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

-


-

- -

st_connect()

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

-

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

-

Parameters
-st_connect() has the following parameters:

-fd

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

-addr

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

-addrlen

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

-timeout

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

-

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

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

-

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

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

-


-

- -

st_read()

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

-

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

-

Parameters
-st_read() has the following parameters:

-fd

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

-buf

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

-nbyte

-The size of buf in bytes.

-timeout

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

-

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

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

-

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

-


-

- -

st_read_fully()

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

-

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

-

Parameters
-st_read_fully() has the following parameters:

-fd

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

-buf

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

-nbyte

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

-timeout

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

-

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

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

-

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

-


-

- -

st_read_resid()

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

-

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

-

Parameters
-st_read_resid() has the following parameters:

-fd

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

-buf

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

-resid

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

-timeout

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

-

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

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

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

-

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

-


-

- -

st_readv()

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

-

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

-

Parameters
-st_readv() has the following parameters:

-fd

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

-iov

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

-iov_size

-The number of iovec structures in the iov array.

-timeout

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

-

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

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

-

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

-


-

- -

st_readv_resid()

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

-

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

-

Parameters
-st_readv_resid() has the following parameters:

-fd

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

-iov

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

-iov_size

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

-timeout

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

-

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

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

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

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

-

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

-


-

- -

st_write()

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

-

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

-

Parameters
-st_write() has the following parameters:

-fd

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

-buf

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

-nbyte

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

-timeout

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

-

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

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

-

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

-


-

- -

st_write_resid()

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

-

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

-

Parameters
-st_write_resid() has the following parameters:

-fd

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

-buf

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

-resid

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

-timeout

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

-

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

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

-

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

-


-

- -

st_writev()

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

-

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

-

Parameters
-st_writev() has the following parameters:

-fd

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

-iov

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

-iov_size

-Number of iovec structures in the iov array.

-timeout

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

-

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

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

-

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

-


-

- -

st_writev_resid()

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

-

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

-

Parameters
-st_writev_resid() has the following parameters:

-fd

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

-iov

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

-iov_size

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

-timeout

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

-

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

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

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

-

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

-


-

- -

st_recvfrom()

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

-

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

-

Parameters
-st_recvfrom() has the following parameters:

-fd

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

-buf

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

-len

-The size of buf in bytes.

-from

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

-fromlen

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

-timeout

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

-

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

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

-

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

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

-


-

- -

st_sendto()

- -Sends bytes to a specified destination. -

-

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

-

Parameters
-st_sendto() has the following parameters:

-fd

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

-msg

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

-len

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

-to

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

-tolen

-This parameter specifies the size of the destination address.

-timeout

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

-

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

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

-

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

-


-

- -

st_recvmsg()

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

-

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

-

Parameters
-st_recvmsg() has the following parameters:

-fd

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

-msg

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

-flags

-Control flags for recvmsg(3).

-timeout

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

-

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

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

-

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

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

-


-

- -

st_sendmsg()

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

-

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

-

Parameters
-st_sendmsg() has the following parameters:

-fd

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

-msg

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

-flags

-Control flags for sendmsg(3).

-timeout

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

-

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

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

-

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

-


-

- -

st_open()

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

-

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

-

Parameters
-st_open() has the following parameters:

-path

-The pathname of the file to be opened.

-oflags

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

-mode

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

-

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

-

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

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

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

-


-

- -

st_poll()

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

-

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

-

Parameters
-st_poll() has the following parameters:

-pds

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

-npds

-The number of elements in the pds array.

-timeout

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

-

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

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

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

-

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

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

-

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

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

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

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

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

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

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

-


-

- - -

Program Structure

- -

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

-

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

    -

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

    -

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

    -

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

    -

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

    -

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

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

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

-


-

- - -

List of Blocking Functions

- -

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

-

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

-


-

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

State Threads for Internet Applications

-

Introduction

-

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

- -

1. Definitions

-

- -

1.1 Internet Applications

- -

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

-

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

- - -

1.2 Performance and Scalability

- -

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

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

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

-

Figure: Throughput vs. Number of clients -
-

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

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

- - -

1.3 Concurrency

- -

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

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

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

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

- -

2. Existing Architectures

-

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

- -

2.1 Multi-Process Architecture

- -

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

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

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

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

- -

2.2 Multi-Threaded Architecture

- -

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

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

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

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

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

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

- - -

2.3 Event-Driven State Machine Architecture

- -

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

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

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

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

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

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

- - -

3. State Threads Library

- -

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

- -

3.1 State Changes and Scheduling

-

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

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

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

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

- -

3.2 Scalability

-

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

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

- -

3.3 Performance

-

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

- -

3.4 Portability

-

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

- -

3.5 State Threads and NSPR

-

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

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

- -

Conclusion

-

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

-

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

    -

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

-The library's main limitation: -

-

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

- -

References

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

Other resources covering various architectural issues in IAs

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

-


-

- -

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

- - - - diff --git a/trunk/3rdparty/st-srs/docs/timeout_heap.txt b/trunk/3rdparty/st-srs/docs/timeout_heap.txt deleted file mode 100644 index 1582dc1293..0000000000 --- a/trunk/3rdparty/st-srs/docs/timeout_heap.txt +++ /dev/null @@ -1,60 +0,0 @@ -How the timeout heap works - -As of version 1.5, the State Threads Library represents the queue of -sleeping threads using a heap data structure rather than a sorted -linked list. This improves performance when there is a large number -of sleeping threads, since insertion into a heap takes O(log N) time -while insertion into a sorted list takes O(N) time. For example, in -one test 1000 threads were created, each thread called st_usleep() -with a random time interval, and then all the threads where -immediately interrupted and joined before the sleeps had a chance to -finish. The whole process was repeated 1000 times, for a total of a -million sleep queue insertions and removals. With the old list-based -sleep queue, this test took 100 seconds; now it takes only 12 seconds. - -Heap data structures are typically based on dynamically resized -arrays. However, since the existing ST code base was very nicely -structured around linking the thread objects into pointer-based lists -without the need for any auxiliary data structures, implementing the -heap using a similar nodes-and-pointers based approach seemed more -appropriate for ST than introducing a separate array. - -Thus, the new ST timeout heap works by organizing the existing -_st_thread_t objects in a balanced binary tree, just as they were -previously organized into a doubly-linked, sorted list. The global -_ST_SLEEPQ variable, formerly a linked list head, is now simply a -pointer to the root of this tree, and the root node of the tree is the -thread with the earliest timeout. Each thread object has two child -pointers, "left" and "right", pointing to threads with later timeouts. - -Each node in the tree is numbered with an integer index, corresponding -to the array index in an array-based heap, and the tree is kept fully -balanced and left-adjusted at all times. In other words, the tree -consists of any number of fully populated top levels, followed by a -single bottom level which may be partially populated, such that any -existing nodes form a contiguous block to the left and the spaces for -missing nodes form a contiguous block to the right. For example, if -there are nine threads waiting for a timeout, they are numbered and -arranged in a tree exactly as follows: - - 1 - / \ - 2 3 - / \ / \ - 4 5 6 7 - / \ - 8 9 - -Each node has either no children, only a left child, or both a left -and a right child. Children always time out later than their parents -(this is called the "heap invariant"), but when a node has two -children, their mutual order is unspecified - the left child may time -out before or after the right child. If a node is numbered N, its -left child is numbered 2N, and its right child is numbered 2N+1. - -There is no pointer from a child to its parent; all pointers point -downward. Additions and deletions both work by starting at the root -and traversing the tree towards the leaves, going left or right -according to the binary digits forming the index of the destination -node. As nodes are added or deleted, existing nodes are rearranged to -maintain the heap invariant. diff --git a/trunk/3rdparty/st-srs/event.c b/trunk/3rdparty/st-srs/event.c deleted file mode 100644 index 142882a51e..0000000000 --- a/trunk/3rdparty/st-srs/event.c +++ /dev/null @@ -1,1413 +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. - * Yahoo! Inc. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -#ifdef MD_HAVE_KQUEUE -#include -#endif -#ifdef MD_HAVE_EPOLL -#include -#endif - -#if defined(USE_POLL) && !defined(MD_HAVE_POLL) - /* Force poll usage if explicitly asked for it */ - #define MD_HAVE_POLL -#endif - - -static struct _st_seldata { - fd_set fd_read_set, fd_write_set, fd_exception_set; - int fd_ref_cnts[FD_SETSIZE][3]; - int maxfd; -} *_st_select_data; - -#define _ST_SELECT_MAX_OSFD (_st_select_data->maxfd) -#define _ST_SELECT_READ_SET (_st_select_data->fd_read_set) -#define _ST_SELECT_WRITE_SET (_st_select_data->fd_write_set) -#define _ST_SELECT_EXCEP_SET (_st_select_data->fd_exception_set) -#define _ST_SELECT_READ_CNT(fd) (_st_select_data->fd_ref_cnts[fd][0]) -#define _ST_SELECT_WRITE_CNT(fd) (_st_select_data->fd_ref_cnts[fd][1]) -#define _ST_SELECT_EXCEP_CNT(fd) (_st_select_data->fd_ref_cnts[fd][2]) - - -#ifdef MD_HAVE_POLL -static struct _st_polldata { - struct pollfd *pollfds; - int pollfds_size; - int fdcnt; -} *_st_poll_data; - -#define _ST_POLL_OSFD_CNT (_st_poll_data->fdcnt) -#define _ST_POLLFDS (_st_poll_data->pollfds) -#define _ST_POLLFDS_SIZE (_st_poll_data->pollfds_size) -#endif /* MD_HAVE_POLL */ - - -#ifdef MD_HAVE_KQUEUE -typedef struct _kq_fd_data { - int rd_ref_cnt; - int wr_ref_cnt; - int revents; -} _kq_fd_data_t; - -static struct _st_kqdata { - _kq_fd_data_t *fd_data; - struct kevent *evtlist; - struct kevent *addlist; - struct kevent *dellist; - int fd_data_size; - int evtlist_size; - int addlist_size; - int addlist_cnt; - int dellist_size; - int dellist_cnt; - int kq; - pid_t pid; -} *_st_kq_data; - -#ifndef ST_KQ_MIN_EVTLIST_SIZE -#define ST_KQ_MIN_EVTLIST_SIZE 64 -#endif - -#define _ST_KQ_READ_CNT(fd) (_st_kq_data->fd_data[fd].rd_ref_cnt) -#define _ST_KQ_WRITE_CNT(fd) (_st_kq_data->fd_data[fd].wr_ref_cnt) -#define _ST_KQ_REVENTS(fd) (_st_kq_data->fd_data[fd].revents) -#endif /* MD_HAVE_KQUEUE */ - - -#ifdef MD_HAVE_EPOLL -typedef struct _epoll_fd_data { - int rd_ref_cnt; - int wr_ref_cnt; - int ex_ref_cnt; - int revents; -} _epoll_fd_data_t; - -static struct _st_epolldata { - _epoll_fd_data_t *fd_data; - struct epoll_event *evtlist; - int fd_data_size; - int evtlist_size; - int evtlist_cnt; - int fd_hint; - int epfd; - pid_t pid; -} *_st_epoll_data; - -#ifndef ST_EPOLL_EVTLIST_SIZE - /* Not a limit, just a hint */ - #define ST_EPOLL_EVTLIST_SIZE 4096 -#endif - -#define _ST_EPOLL_READ_CNT(fd) (_st_epoll_data->fd_data[fd].rd_ref_cnt) -#define _ST_EPOLL_WRITE_CNT(fd) (_st_epoll_data->fd_data[fd].wr_ref_cnt) -#define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt) -#define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents) - -#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0) -#define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0) -#define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0) -#define _ST_EPOLL_EVENTS(fd) \ - (_ST_EPOLL_READ_BIT(fd)|_ST_EPOLL_WRITE_BIT(fd)|_ST_EPOLL_EXCEP_BIT(fd)) - -#endif /* MD_HAVE_EPOLL */ - -_st_eventsys_t *_st_eventsys = NULL; - - -/***************************************** - * select event system - */ - -ST_HIDDEN int _st_select_init(void) -{ - _st_select_data = (struct _st_seldata *) malloc(sizeof(*_st_select_data)); - if (!_st_select_data) - return -1; - - memset(_st_select_data, 0, sizeof(*_st_select_data)); - _st_select_data->maxfd = -1; - - return 0; -} - -ST_HIDDEN int _st_select_pollset_add(struct pollfd *pds, int npds) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* Do checks up front */ - for (pd = pds; pd < epd; pd++) { - if (pd->fd < 0 || pd->fd >= FD_SETSIZE || !pd->events || - (pd->events & ~(POLLIN | POLLOUT | POLLPRI))) { - errno = EINVAL; - return -1; - } - } - - for (pd = pds; pd < epd; pd++) { - if (pd->events & POLLIN) { - FD_SET(pd->fd, &_ST_SELECT_READ_SET); - _ST_SELECT_READ_CNT(pd->fd)++; - } - if (pd->events & POLLOUT) { - FD_SET(pd->fd, &_ST_SELECT_WRITE_SET); - _ST_SELECT_WRITE_CNT(pd->fd)++; - } - if (pd->events & POLLPRI) { - FD_SET(pd->fd, &_ST_SELECT_EXCEP_SET); - _ST_SELECT_EXCEP_CNT(pd->fd)++; - } - if (_ST_SELECT_MAX_OSFD < pd->fd) - _ST_SELECT_MAX_OSFD = pd->fd; - } - - return 0; -} - -ST_HIDDEN void _st_select_pollset_del(struct pollfd *pds, int npds) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - for (pd = pds; pd < epd; pd++) { - if (pd->events & POLLIN) { - if (--_ST_SELECT_READ_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_READ_SET); - } - if (pd->events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_WRITE_SET); - } - if (pd->events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(pd->fd) == 0) - FD_CLR(pd->fd, &_ST_SELECT_EXCEP_SET); - } - } -} - -ST_HIDDEN void _st_select_find_bad_fd(void) -{ - _st_clist_t *q; - _st_pollq_t *pq; - int notify; - struct pollfd *pds, *epds; - int pq_max_osfd, osfd; - short events; - - _ST_SELECT_MAX_OSFD = -1; - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - pq_max_osfd = -1; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - pds->revents = 0; - if (pds->events == 0) - continue; - if (fcntl(osfd, F_GETFL, 0) < 0) { - pds->revents = POLLNVAL; - notify = 1; - } - if (osfd > pq_max_osfd) { - pq_max_osfd = osfd; - } - } - - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Decrement the count of descriptors for each descriptor/event - * because this I/O request is being removed from the ioq - */ - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - if (events & POLLIN) { - if (--_ST_SELECT_READ_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_READ_SET); - } - } - if (events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_WRITE_SET); - } - } - if (events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); - } - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } else { - if (_ST_SELECT_MAX_OSFD < pq_max_osfd) - _ST_SELECT_MAX_OSFD = pq_max_osfd; - } - } -} - -ST_HIDDEN void _st_select_dispatch(void) -{ - struct timeval timeout, *tvp; - fd_set r, w, e; - fd_set *rp, *wp, *ep; - int nfd, pq_max_osfd, osfd; - _st_clist_t *q; - st_utime_t min_timeout; - _st_pollq_t *pq; - int notify; - struct pollfd *pds, *epds; - short events, revents; - - /* - * Assignment of fd_sets - */ - r = _ST_SELECT_READ_SET; - w = _ST_SELECT_WRITE_SET; - e = _ST_SELECT_EXCEP_SET; - - rp = &r; - wp = &w; - ep = &e; - - if (_ST_SLEEPQ == NULL) { - tvp = NULL; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : - (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout.tv_sec = (int) (min_timeout / 1000000); - timeout.tv_usec = (int) (min_timeout % 1000000); - tvp = &timeout; - } - - /* Check for I/O operations */ - nfd = select(_ST_SELECT_MAX_OSFD + 1, rp, wp, ep, tvp); - - /* Notify threads that are associated with the selected descriptors */ - if (nfd > 0) { - _ST_SELECT_MAX_OSFD = -1; - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - pq_max_osfd = -1; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - revents = 0; - if ((events & POLLIN) && FD_ISSET(osfd, rp)) { - revents |= POLLIN; - } - if ((events & POLLOUT) && FD_ISSET(osfd, wp)) { - revents |= POLLOUT; - } - if ((events & POLLPRI) && FD_ISSET(osfd, ep)) { - revents |= POLLPRI; - } - pds->revents = revents; - if (revents) { - notify = 1; - } - if (osfd > pq_max_osfd) { - pq_max_osfd = osfd; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Decrement the count of descriptors for each descriptor/event - * because this I/O request is being removed from the ioq - */ - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - if (events & POLLIN) { - if (--_ST_SELECT_READ_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_READ_SET); - } - } - if (events & POLLOUT) { - if (--_ST_SELECT_WRITE_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_WRITE_SET); - } - } - if (events & POLLPRI) { - if (--_ST_SELECT_EXCEP_CNT(osfd) == 0) { - FD_CLR(osfd, &_ST_SELECT_EXCEP_SET); - } - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } else { - if (_ST_SELECT_MAX_OSFD < pq_max_osfd) - _ST_SELECT_MAX_OSFD = pq_max_osfd; - } - } - } else if (nfd < 0) { - /* - * It can happen when a thread closes file descriptor - * that is being used by some other thread -- BAD! - */ - if (errno == EBADF) - _st_select_find_bad_fd(); - } -} - -ST_HIDDEN int _st_select_fd_new(int osfd) -{ - if (osfd >= FD_SETSIZE) { - errno = EMFILE; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_select_fd_close(int osfd) -{ - if (_ST_SELECT_READ_CNT(osfd) || _ST_SELECT_WRITE_CNT(osfd) || - _ST_SELECT_EXCEP_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_select_fd_getlimit(void) -{ - return FD_SETSIZE; -} - -static _st_eventsys_t _st_select_eventsys = { - "select", - ST_EVENTSYS_SELECT, - _st_select_init, - _st_select_dispatch, - _st_select_pollset_add, - _st_select_pollset_del, - _st_select_fd_new, - _st_select_fd_close, - _st_select_fd_getlimit -}; - - -#ifdef MD_HAVE_POLL -/***************************************** - * poll event system - */ - -ST_HIDDEN int _st_poll_init(void) -{ - _st_poll_data = (struct _st_polldata *) malloc(sizeof(*_st_poll_data)); - if (!_st_poll_data) - return -1; - - _ST_POLLFDS = (struct pollfd *) malloc(ST_MIN_POLLFDS_SIZE * - sizeof(struct pollfd)); - if (!_ST_POLLFDS) { - free(_st_poll_data); - _st_poll_data = NULL; - return -1; - } - _ST_POLLFDS_SIZE = ST_MIN_POLLFDS_SIZE; - _ST_POLL_OSFD_CNT = 0; - - return 0; -} - -ST_HIDDEN int _st_poll_pollset_add(struct pollfd *pds, int npds) -{ - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - for (pd = pds; pd < epd; pd++) { - if (pd->fd < 0 || !pd->events) { - errno = EINVAL; - return -1; - } - } - - _ST_POLL_OSFD_CNT += npds; - - return 0; -} - -/* ARGSUSED */ -ST_HIDDEN void _st_poll_pollset_del(struct pollfd *pds, int npds) -{ - _ST_POLL_OSFD_CNT -= npds; - ST_ASSERT(_ST_POLL_OSFD_CNT >= 0); -} - -ST_HIDDEN void _st_poll_dispatch(void) -{ - int timeout, nfd; - _st_clist_t *q; - st_utime_t min_timeout; - _st_pollq_t *pq; - struct pollfd *pds, *epds, *pollfds; - - /* - * Build up the array of struct pollfd to wait on. - * If existing array is not big enough, release it and allocate a new one. - */ - ST_ASSERT(_ST_POLL_OSFD_CNT >= 0); - if (_ST_POLL_OSFD_CNT > _ST_POLLFDS_SIZE) { - free(_ST_POLLFDS); - _ST_POLLFDS = (struct pollfd *) malloc((_ST_POLL_OSFD_CNT + 10) * - sizeof(struct pollfd)); - ST_ASSERT(_ST_POLLFDS != NULL); - _ST_POLLFDS_SIZE = _ST_POLL_OSFD_CNT + 10; - } - pollfds = _ST_POLLFDS; - - /* Gather all descriptors into one array */ - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - memcpy(pollfds, pq->pds, sizeof(struct pollfd) * pq->npds); - pollfds += pq->npds; - } - ST_ASSERT(pollfds <= _ST_POLLFDS + _ST_POLLFDS_SIZE); - - if (_ST_SLEEPQ == NULL) { - timeout = -1; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : - (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout = (int) (min_timeout / 1000); - } - - /* Check for I/O operations */ - nfd = poll(_ST_POLLFDS, _ST_POLL_OSFD_CNT, timeout); - - /* Notify threads that are associated with the selected descriptors */ - if (nfd > 0) { - pollfds = _ST_POLLFDS; - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - epds = pollfds + pq->npds; - for (pds = pollfds; pds < epds; pds++) { - if (pds->revents) - break; - } - if (pds < epds) { - memcpy(pq->pds, pollfds, sizeof(struct pollfd) * pq->npds); - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - - _ST_POLL_OSFD_CNT -= pq->npds; - ST_ASSERT(_ST_POLL_OSFD_CNT >= 0); - } - pollfds = epds; - } - } -} - -/* ARGSUSED */ -ST_HIDDEN int _st_poll_fd_new(int osfd) -{ - return 0; -} - -/* ARGSUSED */ -ST_HIDDEN int _st_poll_fd_close(int osfd) -{ - /* - * We don't maintain I/O counts for poll event system - * so nothing to check here. - */ - return 0; -} - -ST_HIDDEN int _st_poll_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -static _st_eventsys_t _st_poll_eventsys = { - "poll", - ST_EVENTSYS_POLL, - _st_poll_init, - _st_poll_dispatch, - _st_poll_pollset_add, - _st_poll_pollset_del, - _st_poll_fd_new, - _st_poll_fd_close, - _st_poll_fd_getlimit -}; -#endif /* MD_HAVE_POLL */ - - -#ifdef MD_HAVE_KQUEUE -/***************************************** - * kqueue event system - */ - -ST_HIDDEN int _st_kq_init(void) -{ - int err = 0; - int rv = 0; - - _st_kq_data = (struct _st_kqdata *) calloc(1, sizeof(*_st_kq_data)); - if (!_st_kq_data) - return -1; - - if ((_st_kq_data->kq = kqueue()) < 0) { - err = errno; - rv = -1; - goto cleanup_kq; - } - fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC); - _st_kq_data->pid = getpid(); - - /* - * Allocate file descriptor data array. - * FD_SETSIZE looks like good initial size. - */ - _st_kq_data->fd_data_size = FD_SETSIZE; - _st_kq_data->fd_data = (_kq_fd_data_t *)calloc(_st_kq_data->fd_data_size, sizeof(_kq_fd_data_t)); - if (!_st_kq_data->fd_data) { - err = errno; - rv = -1; - goto cleanup_kq; - } - - /* Allocate event lists */ - _st_kq_data->evtlist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->evtlist = (struct kevent *)malloc(_st_kq_data->evtlist_size * sizeof(struct kevent)); - _st_kq_data->addlist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->addlist = (struct kevent *)malloc(_st_kq_data->addlist_size * sizeof(struct kevent)); - _st_kq_data->dellist_size = ST_KQ_MIN_EVTLIST_SIZE; - _st_kq_data->dellist = (struct kevent *)malloc(_st_kq_data->dellist_size * sizeof(struct kevent)); - if (!_st_kq_data->evtlist || !_st_kq_data->addlist || - !_st_kq_data->dellist) { - err = ENOMEM; - rv = -1; - } - - cleanup_kq: - if (rv < 0) { - if (_st_kq_data->kq >= 0) - close(_st_kq_data->kq); - free(_st_kq_data->fd_data); - free(_st_kq_data->evtlist); - free(_st_kq_data->addlist); - free(_st_kq_data->dellist); - free(_st_kq_data); - _st_kq_data = NULL; - errno = err; - } - - return rv; -} - -ST_HIDDEN int _st_kq_fd_data_expand(int maxfd) -{ - _kq_fd_data_t *ptr; - int n = _st_kq_data->fd_data_size; - - while (maxfd >= n) - n <<= 1; - - ptr = (_kq_fd_data_t *)realloc(_st_kq_data->fd_data, n * sizeof(_kq_fd_data_t)); - if (!ptr) - return -1; - - memset(ptr + _st_kq_data->fd_data_size, 0, (n - _st_kq_data->fd_data_size) * sizeof(_kq_fd_data_t)); - - _st_kq_data->fd_data = ptr; - _st_kq_data->fd_data_size = n; - - return 0; -} - -ST_HIDDEN int _st_kq_addlist_expand(int avail) -{ - struct kevent *ptr; - int n = _st_kq_data->addlist_size; - - while (avail > n - _st_kq_data->addlist_cnt) - n <<= 1; - - ptr = (struct kevent *)realloc(_st_kq_data->addlist, n * sizeof(struct kevent)); - if (!ptr) - return -1; - - _st_kq_data->addlist = ptr; - _st_kq_data->addlist_size = n; - - /* - * Try to expand the result event list too - * (although we don't have to do it). - */ - ptr = (struct kevent *)realloc(_st_kq_data->evtlist, n * sizeof(struct kevent)); - if (ptr) { - _st_kq_data->evtlist = ptr; - _st_kq_data->evtlist_size = n; - } - - return 0; -} - -ST_HIDDEN void _st_kq_addlist_add(const struct kevent *kev) -{ - ST_ASSERT(_st_kq_data->addlist_cnt < _st_kq_data->addlist_size); - memcpy(_st_kq_data->addlist + _st_kq_data->addlist_cnt, kev, sizeof(struct kevent)); - _st_kq_data->addlist_cnt++; -} - -ST_HIDDEN void _st_kq_dellist_add(const struct kevent *kev) -{ - int n = _st_kq_data->dellist_size; - - if (_st_kq_data->dellist_cnt >= n) { - struct kevent *ptr; - - n <<= 1; - ptr = (struct kevent *)realloc(_st_kq_data->dellist, n * sizeof(struct kevent)); - if (!ptr) { - /* See comment in _st_kq_pollset_del() */ - return; - } - - _st_kq_data->dellist = ptr; - _st_kq_data->dellist_size = n; - } - - memcpy(_st_kq_data->dellist + _st_kq_data->dellist_cnt, kev, sizeof(struct kevent)); - _st_kq_data->dellist_cnt++; -} - -ST_HIDDEN int _st_kq_pollset_add(struct pollfd *pds, int npds) -{ - struct kevent kev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* - * Pollset adding is "atomic". That is, either it succeeded for - * all descriptors in the set or it failed. It means that we - * need to do all the checks up front so we don't have to - * "unwind" if adding of one of the descriptors failed. - */ - for (pd = pds; pd < epd; pd++) { - /* POLLIN and/or POLLOUT must be set, but nothing else */ - if (pd->fd < 0 || !pd->events || (pd->events & ~(POLLIN | POLLOUT))) { - errno = EINVAL; - return -1; - } - if (pd->fd >= _st_kq_data->fd_data_size && - _st_kq_fd_data_expand(pd->fd) < 0) - return -1; - } - - /* - * Make sure we have enough room in the addlist for twice as many - * descriptors as in the pollset (for both READ and WRITE filters). - */ - npds <<= 1; - if (npds > _st_kq_data->addlist_size - _st_kq_data->addlist_cnt && _st_kq_addlist_expand(npds) < 0) - return -1; - - for (pd = pds; pd < epd; pd++) { - if ((pd->events & POLLIN) && (_ST_KQ_READ_CNT(pd->fd)++ == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_READ; -#ifdef NOTE_EOF - /* Make it behave like select() and poll() */ - kev.fflags = NOTE_EOF; -#endif - kev.flags = (EV_ADD | EV_ONESHOT); - _st_kq_addlist_add(&kev); - } - if ((pd->events & POLLOUT) && (_ST_KQ_WRITE_CNT(pd->fd)++ == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_WRITE; - kev.flags = (EV_ADD | EV_ONESHOT); - _st_kq_addlist_add(&kev); - } - } - - return 0; -} - -ST_HIDDEN void _st_kq_pollset_del(struct pollfd *pds, int npds) -{ - struct kevent kev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - - /* - * It's OK if deleting fails because a descriptor will either be - * closed or fire only once (we set EV_ONESHOT flag). - */ - _st_kq_data->dellist_cnt = 0; - for (pd = pds; pd < epd; pd++) { - if ((pd->events & POLLIN) && (--_ST_KQ_READ_CNT(pd->fd) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_READ; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - if ((pd->events & POLLOUT) && (--_ST_KQ_WRITE_CNT(pd->fd) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = pd->fd; - kev.filter = EVFILT_WRITE; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - } - - if (_st_kq_data->dellist_cnt > 0) { - /* - * We do "synchronous" kqueue deletes to avoid deleting - * closed descriptors and other possible problems. - */ - int rv; - do { - /* This kevent() won't block since result list size is 0 */ - rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL); - } while (rv < 0 && errno == EINTR); - } -} - -ST_HIDDEN void _st_kq_dispatch(void) -{ - struct timespec timeout, *tsp; - struct kevent kev; - st_utime_t min_timeout; - _st_clist_t *q; - _st_pollq_t *pq; - struct pollfd *pds, *epds; - int nfd, i, osfd, notify, filter; - short events, revents; - - if (_ST_SLEEPQ == NULL) { - tsp = NULL; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout.tv_sec = (time_t) (min_timeout / 1000000); - timeout.tv_nsec = (long) ((min_timeout % 1000000) * 1000); - tsp = &timeout; - } - - retry_kevent: - /* Check for I/O operations */ - nfd = kevent(_st_kq_data->kq, - _st_kq_data->addlist, _st_kq_data->addlist_cnt, - _st_kq_data->evtlist, _st_kq_data->evtlist_size, tsp); - - _st_kq_data->addlist_cnt = 0; - - if (nfd > 0) { - for (i = 0; i < nfd; i++) { - osfd = _st_kq_data->evtlist[i].ident; - filter = _st_kq_data->evtlist[i].filter; - - if (filter == EVFILT_READ) { - _ST_KQ_REVENTS(osfd) |= POLLIN; - } else if (filter == EVFILT_WRITE) { - _ST_KQ_REVENTS(osfd) |= POLLOUT; - } - if (_st_kq_data->evtlist[i].flags & EV_ERROR) { - if (_st_kq_data->evtlist[i].data == EBADF) { - _ST_KQ_REVENTS(osfd) |= POLLNVAL; - } else { - _ST_KQ_REVENTS(osfd) |= POLLERR; - } - } - } - - _st_kq_data->dellist_cnt = 0; - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - revents = (short)(_ST_KQ_REVENTS(osfd) & ~(POLLIN | POLLOUT)); - if ((events & POLLIN) && (_ST_KQ_REVENTS(osfd) & POLLIN)) { - revents |= POLLIN; - } - if ((events & POLLOUT) && (_ST_KQ_REVENTS(osfd) & POLLOUT)) { - revents |= POLLOUT; - } - pds->revents = revents; - if (revents) { - notify = 1; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - for (pds = pq->pds; pds < epds; pds++) { - osfd = pds->fd; - events = pds->events; - /* - * We set EV_ONESHOT flag so we only need to delete - * descriptor if it didn't fire. - */ - if ((events & POLLIN) && (--_ST_KQ_READ_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLIN) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = osfd; - kev.filter = EVFILT_READ; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - if ((events & POLLOUT) && (--_ST_KQ_WRITE_CNT(osfd) == 0) && ((_ST_KQ_REVENTS(osfd) & POLLOUT) == 0)) { - memset(&kev, 0, sizeof(kev)); - kev.ident = osfd; - kev.filter = EVFILT_WRITE; - kev.flags = EV_DELETE; - _st_kq_dellist_add(&kev); - } - } - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } - } - - if (_st_kq_data->dellist_cnt > 0) { - int rv; - do { - /* This kevent() won't block since result list size is 0 */ - rv = kevent(_st_kq_data->kq, _st_kq_data->dellist, _st_kq_data->dellist_cnt, NULL, 0, NULL); - } while (rv < 0 && errno == EINTR); - } - - for (i = 0; i < nfd; i++) { - osfd = _st_kq_data->evtlist[i].ident; - _ST_KQ_REVENTS(osfd) = 0; - } - - } else if (nfd < 0) { - if (errno == EBADF && _st_kq_data->pid != getpid()) { - /* We probably forked, reinitialize kqueue */ - if ((_st_kq_data->kq = kqueue()) < 0) { - /* There is nothing we can do here, will retry later */ - return; - } - fcntl(_st_kq_data->kq, F_SETFD, FD_CLOEXEC); - _st_kq_data->pid = getpid(); - /* Re-register all descriptors on ioq with new kqueue */ - memset(_st_kq_data->fd_data, 0, _st_kq_data->fd_data_size * sizeof(_kq_fd_data_t)); - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - _st_kq_pollset_add(pq->pds, pq->npds); - } - goto retry_kevent; - } - } -} - -ST_HIDDEN int _st_kq_fd_new(int osfd) -{ - if (osfd >= _st_kq_data->fd_data_size && _st_kq_fd_data_expand(osfd) < 0) - return -1; - - return 0; -} - -ST_HIDDEN int _st_kq_fd_close(int osfd) -{ - if (_ST_KQ_READ_CNT(osfd) || _ST_KQ_WRITE_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_kq_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -static _st_eventsys_t _st_kq_eventsys = { - "kqueue", - ST_EVENTSYS_ALT, - _st_kq_init, - _st_kq_dispatch, - _st_kq_pollset_add, - _st_kq_pollset_del, - _st_kq_fd_new, - _st_kq_fd_close, - _st_kq_fd_getlimit -}; -#endif /* MD_HAVE_KQUEUE */ - - -#ifdef MD_HAVE_EPOLL -/***************************************** - * epoll event system - */ - -ST_HIDDEN int _st_epoll_init(void) -{ - int fdlim; - int err = 0; - int rv = 0; - - _st_epoll_data = (struct _st_epolldata *) calloc(1, sizeof(*_st_epoll_data)); - if (!_st_epoll_data) - return -1; - - fdlim = st_getfdlimit(); - _st_epoll_data->fd_hint = (fdlim > 0 && fdlim < ST_EPOLL_EVTLIST_SIZE) ? fdlim : ST_EPOLL_EVTLIST_SIZE; - - if ((_st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint)) < 0) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC); - _st_epoll_data->pid = getpid(); - - /* Allocate file descriptor data array */ - _st_epoll_data->fd_data_size = _st_epoll_data->fd_hint; - _st_epoll_data->fd_data = (_epoll_fd_data_t *)calloc(_st_epoll_data->fd_data_size, sizeof(_epoll_fd_data_t)); - if (!_st_epoll_data->fd_data) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - - /* Allocate event lists */ - _st_epoll_data->evtlist_size = _st_epoll_data->fd_hint; - _st_epoll_data->evtlist = (struct epoll_event *)malloc(_st_epoll_data->evtlist_size * sizeof(struct epoll_event)); - if (!_st_epoll_data->evtlist) { - err = errno; - rv = -1; - } - - cleanup_epoll: - if (rv < 0) { - if (_st_epoll_data->epfd >= 0) - close(_st_epoll_data->epfd); - free(_st_epoll_data->fd_data); - free(_st_epoll_data->evtlist); - free(_st_epoll_data); - _st_epoll_data = NULL; - errno = err; - } - - return rv; -} - -ST_HIDDEN int _st_epoll_fd_data_expand(int maxfd) -{ - _epoll_fd_data_t *ptr; - int n = _st_epoll_data->fd_data_size; - - while (maxfd >= n) - n <<= 1; - - ptr = (_epoll_fd_data_t *)realloc(_st_epoll_data->fd_data, n * sizeof(_epoll_fd_data_t)); - if (!ptr) - return -1; - - memset(ptr + _st_epoll_data->fd_data_size, 0, (n - _st_epoll_data->fd_data_size) * sizeof(_epoll_fd_data_t)); - - _st_epoll_data->fd_data = ptr; - _st_epoll_data->fd_data_size = n; - - return 0; -} - -ST_HIDDEN void _st_epoll_evtlist_expand(void) -{ - struct epoll_event *ptr; - int n = _st_epoll_data->evtlist_size; - - while (_st_epoll_data->evtlist_cnt > n) - n <<= 1; - - ptr = (struct epoll_event *)realloc(_st_epoll_data->evtlist, n * sizeof(struct epoll_event)); - if (ptr) { - _st_epoll_data->evtlist = ptr; - _st_epoll_data->evtlist_size = n; - } -} - -ST_HIDDEN void _st_epoll_pollset_del(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - int old_events, events, op; - - /* - * It's more or less OK if deleting fails because a descriptor - * will either be closed or deleted in dispatch function after - * it fires. - */ - for (pd = pds; pd < epd; pd++) { - old_events = _ST_EPOLL_EVENTS(pd->fd); - - if (pd->events & POLLIN) - _ST_EPOLL_READ_CNT(pd->fd)--; - if (pd->events & POLLOUT) - _ST_EPOLL_WRITE_CNT(pd->fd)--; - if (pd->events & POLLPRI) - _ST_EPOLL_EXCEP_CNT(pd->fd)--; - - events = _ST_EPOLL_EVENTS(pd->fd); - /* - * The _ST_EPOLL_REVENTS check below is needed so we can use - * this function inside dispatch(). Outside of dispatch() - * _ST_EPOLL_REVENTS is always zero for all descriptors. - */ - if (events != old_events && _ST_EPOLL_REVENTS(pd->fd) == 0) { - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = pd->fd; - if (epoll_ctl(_st_epoll_data->epfd, op, pd->fd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - int i, fd; - int old_events, events, op; - - /* Do as many checks as possible up front */ - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - if (fd < 0 || !pds[i].events || - (pds[i].events & ~(POLLIN | POLLOUT | POLLPRI))) { - errno = EINVAL; - return -1; - } - if (fd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(fd) < 0) - return -1; - } - - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - old_events = _ST_EPOLL_EVENTS(fd); - - if (pds[i].events & POLLIN) - _ST_EPOLL_READ_CNT(fd)++; - if (pds[i].events & POLLOUT) - _ST_EPOLL_WRITE_CNT(fd)++; - if (pds[i].events & POLLPRI) - _ST_EPOLL_EXCEP_CNT(fd)++; - - events = _ST_EPOLL_EVENTS(fd); - if (events != old_events) { - op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; - ev.events = events; - ev.data.fd = fd; - if (epoll_ctl(_st_epoll_data->epfd, op, fd, &ev) < 0 && (op != EPOLL_CTL_ADD || errno != EEXIST)) - break; - if (op == EPOLL_CTL_ADD) { - _st_epoll_data->evtlist_cnt++; - if (_st_epoll_data->evtlist_cnt > _st_epoll_data->evtlist_size) - _st_epoll_evtlist_expand(); - } - } - } - - if (i < npds) { - /* Error */ - int err = errno; - /* Unroll the state */ - _st_epoll_pollset_del(pds, i + 1); - errno = err; - return -1; - } - - return 0; -} - -ST_HIDDEN void _st_epoll_dispatch(void) -{ - st_utime_t min_timeout; - _st_clist_t *q; - _st_pollq_t *pq; - struct pollfd *pds, *epds; - struct epoll_event ev; - int timeout, nfd, i, osfd, notify; - int events, op; - short revents; - - if (_ST_SLEEPQ == NULL) { - timeout = -1; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout = (int) (min_timeout / 1000); - } - - if (_st_epoll_data->pid != getpid()) { - /* We probably forked, reinitialize epoll set */ - close(_st_epoll_data->epfd); - _st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint); - if (_st_epoll_data->epfd < 0) { - /* There is nothing we can do here, will retry later */ - return; - } - fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC); - _st_epoll_data->pid = getpid(); - - /* Put all descriptors on ioq into new epoll set */ - memset(_st_epoll_data->fd_data, 0, _st_epoll_data->fd_data_size * sizeof(_epoll_fd_data_t)); - _st_epoll_data->evtlist_cnt = 0; - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - _st_epoll_pollset_add(pq->pds, pq->npds); - } - } - - /* Check for I/O operations */ - nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout); - - if (nfd > 0) { - for (i = 0; i < nfd; i++) { - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = _st_epoll_data->evtlist[i].events; - if (_ST_EPOLL_REVENTS(osfd) & (EPOLLERR | EPOLLHUP)) { - /* Also set I/O bits on error */ - _ST_EPOLL_REVENTS(osfd) |= _ST_EPOLL_EVENTS(osfd); - } - } - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - - for (pds = pq->pds; pds < epds; pds++) { - if (_ST_EPOLL_REVENTS(pds->fd) == 0) { - pds->revents = 0; - continue; - } - osfd = pds->fd; - events = pds->events; - revents = 0; - if ((events & POLLIN) && (_ST_EPOLL_REVENTS(osfd) & EPOLLIN)) - revents |= POLLIN; - if ((events & POLLOUT) && (_ST_EPOLL_REVENTS(osfd) & EPOLLOUT)) - revents |= POLLOUT; - if ((events & POLLPRI) && (_ST_EPOLL_REVENTS(osfd) & EPOLLPRI)) - revents |= POLLPRI; - if (_ST_EPOLL_REVENTS(osfd) & EPOLLERR) - revents |= POLLERR; - if (_ST_EPOLL_REVENTS(osfd) & EPOLLHUP) - revents |= POLLHUP; - - pds->revents = revents; - if (revents) { - notify = 1; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Here we will only delete/modify descriptors that - * didn't fire (see comments in _st_epoll_pollset_del()). - */ - _st_epoll_pollset_del(pq->pds, pq->npds); - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(pq->thread); - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } - } - - for (i = 0; i < nfd; i++) { - /* Delete/modify descriptors that fired */ - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = 0; - events = _ST_EPOLL_EVENTS(osfd); - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = osfd; - if (epoll_ctl(_st_epoll_data->epfd, op, osfd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_fd_new(int osfd) -{ - if (osfd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(osfd) < 0) - return -1; - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_close(int osfd) -{ - if (_ST_EPOLL_READ_CNT(osfd) || _ST_EPOLL_WRITE_CNT(osfd) || _ST_EPOLL_EXCEP_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -/* - * Check if epoll functions are just stubs. - */ -ST_HIDDEN int _st_epoll_is_supported(void) -{ - struct epoll_event ev; - - ev.events = EPOLLIN; - ev.data.ptr = NULL; - /* Guaranteed to fail */ - epoll_ctl(-1, EPOLL_CTL_ADD, -1, &ev); - - return (errno != ENOSYS); -} - -static _st_eventsys_t _st_epoll_eventsys = { - "epoll", - ST_EVENTSYS_ALT, - _st_epoll_init, - _st_epoll_dispatch, - _st_epoll_pollset_add, - _st_epoll_pollset_del, - _st_epoll_fd_new, - _st_epoll_fd_close, - _st_epoll_fd_getlimit -}; -#endif /* MD_HAVE_EPOLL */ - - -/***************************************** - * Public functions - */ - -int st_set_eventsys(int eventsys) -{ - if (_st_eventsys) { - errno = EBUSY; - return -1; - } - - switch (eventsys) { - case ST_EVENTSYS_DEFAULT: -#ifdef USE_POLL - _st_eventsys = &_st_poll_eventsys; -#else - _st_eventsys = &_st_select_eventsys; -#endif - break; - case ST_EVENTSYS_SELECT: - _st_eventsys = &_st_select_eventsys; - break; -#ifdef MD_HAVE_POLL - case ST_EVENTSYS_POLL: - _st_eventsys = &_st_poll_eventsys; - break; -#endif - case ST_EVENTSYS_ALT: -#if defined (MD_HAVE_KQUEUE) - _st_eventsys = &_st_kq_eventsys; -#elif defined (MD_HAVE_EPOLL) - if (_st_epoll_is_supported()) - _st_eventsys = &_st_epoll_eventsys; -#endif - break; - default: - errno = EINVAL; - return -1; - } - - return 0; -} - -int st_get_eventsys(void) -{ - return _st_eventsys ? _st_eventsys->val : -1; -} - -const char *st_get_eventsys_name(void) -{ - return _st_eventsys ? _st_eventsys->name : ""; -} - diff --git a/trunk/3rdparty/st-srs/examples/Makefile b/trunk/3rdparty/st-srs/examples/Makefile deleted file mode 100644 index 31c0a6e240..0000000000 --- a/trunk/3rdparty/st-srs/examples/Makefile +++ /dev/null @@ -1,115 +0,0 @@ -# -# Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. -# All Rights Reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# 3. Neither the name of Silicon Graphics, Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -########################## -# Supported OSes: -# -# AIX -# FREEBSD -# HPUX -# HPUX_64 -# IRIX -# IRIX_64 -# LINUX -# LINUX_IA64 -# NETBSD -# OPENBSD -# OSF1 -# SOLARIS -# SOLARIS_64 - -########################## - -CC = cc - -SHELL = /bin/sh -ECHO = /bin/echo - -DEPTH = .. -BUILD = -TARGETDIR = - -DEFINES = -CFLAGS = -OTHER_FLAGS = - -OBJDIR = $(DEPTH)/$(TARGETDIR) -INCDIR = $(DEPTH)/$(TARGETDIR) -LIBST = $(OBJDIR)/libst.a -HEADER = $(INCDIR)/st.h - -LIBRESOLV = -EXTRALIBS = - -ifeq ($(OS),) -EXAMPLES = unknown -else -EXAMPLES = $(OBJDIR)/lookupdns $(OBJDIR)/proxy $(OBJDIR)/server -endif - - -########################## -# Platform section. -# - -ifeq (DARWIN, $(findstring DARWIN, $(OS))) -LIBRESOLV = -lresolv -endif - -ifeq (LINUX, $(findstring LINUX, $(OS))) -LIBRESOLV = -lresolv -endif - -ifeq (SOLARIS, $(findstring SOLARIS, $(OS))) -LIBRESOLV = -lresolv -EXTRALIBS = -lsocket -lnsl -endif - -# -# End of platform section. -########################## - - -all: $(EXAMPLES) - -$(OBJDIR)/lookupdns: lookupdns.c $(OBJDIR)/res.o $(LIBST) $(HEADER) - $(CC) $(CFLAGS) -I$(INCDIR) lookupdns.c $(OBJDIR)/res.o $(LIBST) $(LIBRESOLV) $(EXTRALIBS) -o $@ - -$(OBJDIR)/proxy: proxy.c $(LIBST) $(HEADER) - $(CC) $(CFLAGS) -I$(INCDIR) proxy.c $(LIBST) $(EXTRALIBS) -o $@ - -$(OBJDIR)/server: server.c $(OBJDIR)/error.o $(LIBST) $(HEADER) - $(CC) $(CFLAGS) -I$(INCDIR) server.c $(OBJDIR)/error.o $(LIBST) $(EXTRALIBS) -o $@ - -$(OBJDIR)/%.o: %.c - $(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@ - -.DEFAULT: - @cd $(DEPTH); $(MAKE) $@ - diff --git a/trunk/3rdparty/st-srs/examples/README b/trunk/3rdparty/st-srs/examples/README deleted file mode 100644 index 646d4f6236..0000000000 --- a/trunk/3rdparty/st-srs/examples/README +++ /dev/null @@ -1,98 +0,0 @@ -Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. -All Rights Reserved. - - -This directory contains three example programs. - - ---------------------------------------------------------------------------- - -PROGRAM - - lookupdns - -FILES - - lookupdns.c - res.c - -USAGE - - lookupdns [] ... - -DESCRIPTION - - This program performs asynchronous DNS host name resolution and reports - IP address for each specified as a command line argument. - One ST thread is created for each host name. All threads do host name - resolution concurrently. - - ---------------------------------------------------------------------------- - -PROGRAM - - proxy - -FILES - - proxy.c - -USAGE - - proxy -l -r [-p ] [-S] - - -l bind to local address specified as []: - -r connect to remote address specified as : - -p create specified number of processes - -S serialize accept() calls from different processes - on the same listening socket (if needed). - -DESCRIPTION - - This program acts as a generic gateway. It listens for connections to a - local address. Upon accepting a client connection, it connects to the - specified remote address and then just pumps the data through without any - modification. - - ---------------------------------------------------------------------------- - -PROGRAM - - server - -FILES - - server.c - error.c - -USAGE - - server -l [] - - -l open all log files in specified directory. - - Possible options: - - -b : bind to specified address (multiple addresses - are permitted) - -p create specified number of processes - -t : specify thread limits per listening socket - across all processes - -u change server's user id to specified value - -q set max length of pending connections queue - -a enable access logging - -i run in interactive mode (useful for debugging) - -S serialize accept() calls from different processes - on the same listening socket (if needed). - -DESCRIPTION - - This program is a general server example. It accepts a client connection - and outputs a short HTML page. It can be easily adapted to provide - other services. - - ---------------------------------------------------------------------------- - diff --git a/trunk/3rdparty/st-srs/examples/error.c b/trunk/3rdparty/st-srs/examples/error.c deleted file mode 100644 index 0b2e772874..0000000000 --- a/trunk/3rdparty/st-srs/examples/error.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include "st.h" - -/* - * Simple error reporting functions. - * Suggested in W. Richard Stevens' "Advanced Programming in UNIX - * Environment". - */ - -#define MAXLINE 4096 /* max line length */ - -static void err_doit(int, int, const char *, va_list); - - -/* - * Nonfatal error related to a system call. - * Print a message and return. - */ -void err_sys_report(int fd, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - err_doit(fd, 1, fmt, ap); - va_end(ap); -} - - -/* - * Fatal error related to a system call. - * Print a message and terminate. - */ -void err_sys_quit(int fd, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - err_doit(fd, 1, fmt, ap); - va_end(ap); - exit(1); -} - - -/* - * Fatal error related to a system call. - * Print a message, dump core, and terminate. - */ -void err_sys_dump(int fd, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - err_doit(fd, 1, fmt, ap); - va_end(ap); - abort(); /* dump core and terminate */ - exit(1); /* shouldn't get here */ -} - - -/* - * Nonfatal error unrelated to a system call. - * Print a message and return. - */ -void err_report(int fd, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - err_doit(fd, 0, fmt, ap); - va_end(ap); -} - - -/* - * Fatal error unrelated to a system call. - * Print a message and terminate. - */ -void err_quit(int fd, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - err_doit(fd, 0, fmt, ap); - va_end(ap); - exit(1); -} - - -/* - * Return a pointer to a string containing current time. - */ -char *err_tstamp(void) -{ - static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - static char str[32]; - static time_t lastt = 0; - struct tm *tmp; - time_t currt = st_time(); - - if (currt == lastt) - return str; - - tmp = localtime(&currt); - sprintf(str, "[%02d/%s/%d:%02d:%02d:%02d] ", tmp->tm_mday, - months[tmp->tm_mon], 1900 + tmp->tm_year, tmp->tm_hour, - tmp->tm_min, tmp->tm_sec); - lastt = currt; - - return str; -} - - -/* - * Print a message and return to caller. - * Caller specifies "errnoflag". - */ -static void err_doit(int fd, int errnoflag, const char *fmt, va_list ap) -{ - int errno_save; - char buf[MAXLINE]; - - errno_save = errno; /* value caller might want printed */ - strcpy(buf, err_tstamp()); /* prepend a message with time stamp */ - vsprintf(buf + strlen(buf), fmt, ap); - if (errnoflag) - sprintf(buf + strlen(buf), ": %s\n", strerror(errno_save)); - else - strcat(buf, "\n"); - write(fd, buf, strlen(buf)); - errno = errno_save; -} - diff --git a/trunk/3rdparty/st-srs/examples/lookupdns.c b/trunk/3rdparty/st-srs/examples/lookupdns.c deleted file mode 100644 index 98f6ec5d82..0000000000 --- a/trunk/3rdparty/st-srs/examples/lookupdns.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "st.h" - -#if !defined(NETDB_INTERNAL) && defined(h_NETDB_INTERNAL) -#define NETDB_INTERNAL h_NETDB_INTERNAL -#endif - -/* Resolution timeout (in microseconds) */ -#define TIMEOUT (2*1000000LL) - -/* External function defined in the res.c file */ -int dns_getaddr(const char *host, struct in_addr *addr, st_utime_t timeout); - - -void *do_resolve(void *host) -{ - struct in_addr addr; - - /* Use dns_getaddr() instead of gethostbyname(3) to get IP address */ - if (dns_getaddr(host, &addr, TIMEOUT) < 0) { - fprintf(stderr, "dns_getaddr: can't resolve %s: ", (char *)host); - if (h_errno == NETDB_INTERNAL) - perror(""); - else - herror(""); - } else - printf("%-40s %s\n", (char *)host, inet_ntoa(addr)); - - return NULL; -} - - -/* - * Asynchronous DNS host name resolution. This program creates one - * ST thread for each host name (specified as command line arguments). - * All threads do host name resolution concurrently. - */ -int main(int argc, char *argv[]) -{ - int i; - - if (argc < 2) { - fprintf(stderr, "Usage: %s [] ...\n", argv[0]); - exit(1); - } - - if (st_init() < 0) { - perror("st_init"); - exit(1); - } - - for (i = 1; i < argc; i++) { - /* Create a separate thread for each host name */ - if (st_thread_create(do_resolve, argv[i], 0, 0) == NULL) { - perror("st_thread_create"); - exit(1); - } - } - - st_thread_exit(NULL); - - /* NOTREACHED */ - return 1; -} - diff --git a/trunk/3rdparty/st-srs/examples/proxy.c b/trunk/3rdparty/st-srs/examples/proxy.c deleted file mode 100644 index 2f4636d6b8..0000000000 --- a/trunk/3rdparty/st-srs/examples/proxy.c +++ /dev/null @@ -1,541 +0,0 @@ -/* - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "st.h" - -#define IOBUFSIZE (16*1024) - -#define IOV_LEN 256 -#define IOV_COUNT (IOBUFSIZE / IOV_LEN) - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -static char *prog; /* Program name */ -static struct sockaddr_in rmt_addr; /* Remote address */ - -static unsigned long testing; -#define TESTING_VERBOSE 0x1 -#define TESTING_READV 0x2 -#define TESTING_READ_RESID 0x4 -#define TESTING_WRITEV 0x8 -#define TESTING_WRITE_RESID 0x10 - -static void read_address(const char *str, struct sockaddr_in *sin); -static void start_daemon(void); -static int cpu_count(void); -static void set_concurrency(int nproc); -static void *handle_request(void *arg); -static void print_sys_error(const char *msg); - - -/* - * This program acts as a generic gateway. It listens for connections - * to a local address ('-l' option). Upon accepting a client connection, - * it connects to the specified remote address ('-r' option) and then - * just pumps the data through without any modification. - */ -int main(int argc, char *argv[]) -{ - extern char *optarg; - int opt, sock, n; - int laddr, raddr, num_procs, alt_ev, one_process; - int serialize_accept = 0; - struct sockaddr_in lcl_addr, cli_addr; - st_netfd_t cli_nfd, srv_nfd; - - prog = argv[0]; - num_procs = laddr = raddr = alt_ev = one_process = 0; - - /* Parse arguments */ - while((opt = getopt(argc, argv, "l:r:p:Saht:X")) != EOF) { - switch (opt) { - case 'a': - alt_ev = 1; - break; - case 'l': - read_address(optarg, &lcl_addr); - laddr = 1; - break; - case 'r': - read_address(optarg, &rmt_addr); - if (rmt_addr.sin_addr.s_addr == INADDR_ANY) { - fprintf(stderr, "%s: invalid remote address: %s\n", prog, optarg); - exit(1); - } - raddr = 1; - break; - case 'p': - num_procs = atoi(optarg); - if (num_procs < 1) { - fprintf(stderr, "%s: invalid number of processes: %s\n", prog, optarg); - exit(1); - } - break; - case 'S': - /* - * Serialization decision is tricky on some platforms. For example, - * Solaris 2.6 and above has kernel sockets implementation, so supposedly - * there is no need for serialization. The ST library may be compiled - * on one OS version, but used on another, so the need for serialization - * should be determined at run time by the application. Since it's just - * an example, the serialization decision is left up to user. - * Only on platforms where the serialization is never needed on any OS - * version st_netfd_serialize_accept() is a no-op. - */ - serialize_accept = 1; - break; - case 't': - testing = strtoul(optarg, NULL, 0); - break; - case 'X': - one_process = 1; - break; - case 'h': - case '?': - fprintf(stderr, "Usage: %s [options] -l <[host]:port> -r \n", - prog); - fprintf(stderr, "options are:\n"); - fprintf(stderr, " -p number of parallel processes\n"); - fprintf(stderr, " -S serialize accepts\n"); - fprintf(stderr, " -a use alternate event system\n"); -#ifdef DEBUG - fprintf(stderr, " -t mask testing/debugging mode\n"); - fprintf(stderr, " -X one process, don't daemonize\n"); -#endif - exit(1); - } - } - if (!laddr) { - fprintf(stderr, "%s: local address required\n", prog); - exit(1); - } - if (!raddr) { - fprintf(stderr, "%s: remote address required\n", prog); - exit(1); - } - if (num_procs == 0) - num_procs = cpu_count(); - - fprintf(stderr, "%s: starting proxy daemon on %s:%d\n", prog, - inet_ntoa(lcl_addr.sin_addr), ntohs(lcl_addr.sin_port)); - - /* Start the daemon */ - if (one_process) - num_procs = 1; - else - start_daemon(); - - if (alt_ev) - st_set_eventsys(ST_EVENTSYS_ALT); - - /* Initialize the ST library */ - if (st_init() < 0) { - print_sys_error("st_init"); - exit(1); - } - - /* Create and bind listening socket */ - if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { - print_sys_error("socket"); - exit(1); - } - n = 1; - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n)) < 0) { - print_sys_error("setsockopt"); - exit(1); - } - if (bind(sock, (struct sockaddr *)&lcl_addr, sizeof(lcl_addr)) < 0) { - print_sys_error("bind"); - exit(1); - } - listen(sock, 128); - if ((srv_nfd = st_netfd_open_socket(sock)) == NULL) { - print_sys_error("st_netfd_open"); - exit(1); - } - /* See the comment regarding serialization decision above */ - if (num_procs > 1 && serialize_accept && st_netfd_serialize_accept(srv_nfd) - < 0) { - print_sys_error("st_netfd_serialize_accept"); - exit(1); - } - - /* Start server processes */ - if (!one_process) - set_concurrency(num_procs); - - for ( ; ; ) { - n = sizeof(cli_addr); - cli_nfd = st_accept(srv_nfd, (struct sockaddr *)&cli_addr, &n, - ST_UTIME_NO_TIMEOUT); - if (cli_nfd == NULL) { - print_sys_error("st_accept"); - exit(1); - } - if (st_thread_create(handle_request, cli_nfd, 0, 0) == NULL) { - print_sys_error("st_thread_create"); - exit(1); - } - } - - /* NOTREACHED */ - return 1; -} - - -static void read_address(const char *str, struct sockaddr_in *sin) -{ - char host[128], *p; - struct hostent *hp; - unsigned short port; - - strcpy(host, str); - if ((p = strchr(host, ':')) == NULL) { - fprintf(stderr, "%s: invalid address: %s\n", prog, host); - exit(1); - } - *p++ = '\0'; - port = (unsigned short) atoi(p); - if (port < 1) { - fprintf(stderr, "%s: invalid port: %s\n", prog, p); - exit(1); - } - - memset(sin, 0, sizeof(struct sockaddr_in)); - sin->sin_family = AF_INET; - sin->sin_port = htons(port); - if (host[0] == '\0') { - sin->sin_addr.s_addr = INADDR_ANY; - return; - } - sin->sin_addr.s_addr = inet_addr(host); - if (sin->sin_addr.s_addr == INADDR_NONE) { - /* not dotted-decimal */ - if ((hp = gethostbyname(host)) == NULL) { - fprintf(stderr, "%s: can't resolve address: %s\n", prog, host); - exit(1); - } - memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); - } -} - -#ifdef DEBUG -static void show_iov(const struct iovec *iov, int niov) -{ - int i; - size_t total; - - printf("iov %p has %d entries:\n", iov, niov); - total = 0; - for (i = 0; i < niov; i++) { - printf("iov[%3d] iov_base=%p iov_len=0x%lx(%lu)\n", - i, iov[i].iov_base, (unsigned long) iov[i].iov_len, - (unsigned long) iov[i].iov_len); - total += iov[i].iov_len; - } - printf("total 0x%lx(%ld)\n", (unsigned long) total, (unsigned long) total); -} - -/* - * This version is tricked out to test all the - * st_(read|write)v?(_resid)? variants. Use the non-DEBUG version for - * anything serious. st_(read|write) are all this function really - * needs. - */ -static int pass(st_netfd_t in, st_netfd_t out) -{ - char buf[IOBUFSIZE]; - struct iovec iov[IOV_COUNT]; - int ioviter, nw, nr; - - if (testing & TESTING_READV) { - for (ioviter = 0; ioviter < IOV_COUNT; ioviter++) { - iov[ioviter].iov_base = &buf[ioviter * IOV_LEN]; - iov[ioviter].iov_len = IOV_LEN; - } - if (testing & TESTING_VERBOSE) { - printf("readv(%p)...\n", in); - show_iov(iov, IOV_COUNT); - } - if (testing & TESTING_READ_RESID) { - struct iovec *riov = iov; - int riov_cnt = IOV_COUNT; - if (st_readv_resid(in, &riov, &riov_cnt, ST_UTIME_NO_TIMEOUT) == 0) { - if (testing & TESTING_VERBOSE) { - printf("resid\n"); - show_iov(riov, riov_cnt); - printf("full\n"); - show_iov(iov, IOV_COUNT); - } - nr = 0; - for (ioviter = 0; ioviter < IOV_COUNT; ioviter++) - nr += iov[ioviter].iov_len; - nr = IOBUFSIZE - nr; - } else - nr = -1; - } else - nr = (int) st_readv(in, iov, IOV_COUNT, ST_UTIME_NO_TIMEOUT); - } else { - if (testing & TESTING_READ_RESID) { - size_t resid = IOBUFSIZE; - if (st_read_resid(in, buf, &resid, ST_UTIME_NO_TIMEOUT) == 0) - nr = IOBUFSIZE - resid; - else - nr = -1; - } else - nr = (int) st_read(in, buf, IOBUFSIZE, ST_UTIME_NO_TIMEOUT); - } - if (testing & TESTING_VERBOSE) - printf("got 0x%x(%d) E=%d\n", nr, nr, errno); - - if (nr <= 0) - return 0; - - if (testing & TESTING_WRITEV) { - for (nw = 0, ioviter = 0; nw < nr; - nw += iov[ioviter].iov_len, ioviter++) { - iov[ioviter].iov_base = &buf[nw]; - iov[ioviter].iov_len = nr - nw; - if (iov[ioviter].iov_len > IOV_LEN) - iov[ioviter].iov_len = IOV_LEN; - } - if (testing & TESTING_VERBOSE) { - printf("writev(%p)...\n", out); - show_iov(iov, ioviter); - } - if (testing & TESTING_WRITE_RESID) { - struct iovec *riov = iov; - int riov_cnt = ioviter; - if (st_writev_resid(out, &riov, &riov_cnt, ST_UTIME_NO_TIMEOUT) == 0) { - if (testing & TESTING_VERBOSE) { - printf("resid\n"); - show_iov(riov, riov_cnt); - printf("full\n"); - show_iov(iov, ioviter); - } - nw = 0; - while (--ioviter >= 0) - nw += iov[ioviter].iov_len; - nw = nr - nw; - } else - nw = -1; - } else - nw = st_writev(out, iov, ioviter, ST_UTIME_NO_TIMEOUT); - } else { - if (testing & TESTING_WRITE_RESID) { - size_t resid = nr; - if (st_write_resid(out, buf, &resid, ST_UTIME_NO_TIMEOUT) == 0) - nw = nr - resid; - else - nw = -1; - } else - nw = st_write(out, buf, nr, ST_UTIME_NO_TIMEOUT); - } - if (testing & TESTING_VERBOSE) - printf("put 0x%x(%d) E=%d\n", nw, nw, errno); - - if (nw != nr) - return 0; - - return 1; -} -#else /* DEBUG */ -/* - * This version is the simple one suitable for serious use. - */ -static int pass(st_netfd_t in, st_netfd_t out) -{ - char buf[IOBUFSIZE]; - int nw, nr; - - nr = (int) st_read(in, buf, IOBUFSIZE, ST_UTIME_NO_TIMEOUT); - if (nr <= 0) - return 0; - - nw = st_write(out, buf, nr, ST_UTIME_NO_TIMEOUT); - if (nw != nr) - return 0; - - return 1; -} -#endif - -static void *handle_request(void *arg) -{ - struct pollfd pds[2]; - st_netfd_t cli_nfd, rmt_nfd; - int sock; - - cli_nfd = (st_netfd_t) arg; - pds[0].fd = st_netfd_fileno(cli_nfd); - pds[0].events = POLLIN; - - /* Connect to remote host */ - if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { - print_sys_error("socket"); - goto done; - } - if ((rmt_nfd = st_netfd_open_socket(sock)) == NULL) { - print_sys_error("st_netfd_open_socket"); - close(sock); - goto done; - } - if (st_connect(rmt_nfd, (struct sockaddr *)&rmt_addr, - sizeof(rmt_addr), ST_UTIME_NO_TIMEOUT) < 0) { - print_sys_error("st_connect"); - st_netfd_close(rmt_nfd); - goto done; - } - pds[1].fd = sock; - pds[1].events = POLLIN; - - /* - * Now just pump the data through. - * XXX This should use one thread for each direction for true full-duplex. - */ - for ( ; ; ) { - pds[0].revents = 0; - pds[1].revents = 0; - - if (st_poll(pds, 2, ST_UTIME_NO_TIMEOUT) <= 0) { - print_sys_error("st_poll"); - break; - } - - if (pds[0].revents & POLLIN) { - if (!pass(cli_nfd, rmt_nfd)) - break; - } - - if (pds[1].revents & POLLIN) { - if (!pass(rmt_nfd, cli_nfd)) - break; - } - } - st_netfd_close(rmt_nfd); - -done: - - st_netfd_close(cli_nfd); - - return NULL; -} - -static void start_daemon(void) -{ - pid_t pid; - - /* Start forking */ - if ((pid = fork()) < 0) { - print_sys_error("fork"); - exit(1); - } - if (pid > 0) - exit(0); /* parent */ - - /* First child process */ - setsid(); /* become session leader */ - - if ((pid = fork()) < 0) { - print_sys_error("fork"); - exit(1); - } - if (pid > 0) /* first child */ - exit(0); - - chdir("/"); - umask(022); -} - -/* - * Create separate processes ("virtual processors"). Since it's just an - * example, there is no watchdog - the parent just exits leaving children - * on their own. - */ -static void set_concurrency(int nproc) -{ - pid_t pid; - int i; - - if (nproc < 1) - nproc = 1; - - for (i = 0; i < nproc; i++) { - if ((pid = fork()) < 0) { - print_sys_error("fork"); - exit(1); - } - /* Child returns */ - if (pid == 0) - return; - } - - /* Parent just exits */ - exit(0); -} - -static int cpu_count(void) -{ - int n; - -#if defined (_SC_NPROCESSORS_ONLN) - n = (int) sysconf(_SC_NPROCESSORS_ONLN); -#elif defined (_SC_NPROC_ONLN) - n = (int) sysconf(_SC_NPROC_ONLN); -#elif defined (HPUX) -#include - n = mpctl(MPC_GETNUMSPUS, 0, 0); -#else - n = -1; - errno = ENOSYS; -#endif - - return n; -} - -static void print_sys_error(const char *msg) -{ - fprintf(stderr, "%s: %s: %s\n", prog, msg, strerror(errno)); -} - diff --git a/trunk/3rdparty/st-srs/examples/res.c b/trunk/3rdparty/st-srs/examples/res.c deleted file mode 100644 index 14ecd8c927..0000000000 --- a/trunk/3rdparty/st-srs/examples/res.c +++ /dev/null @@ -1,305 +0,0 @@ -/* - * Copyright (c) 1985, 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#if defined (DARWIN) -#define BIND_8_COMPAT -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "st.h" - -#define MAXPACKET 1024 - -#if !defined(NETDB_INTERNAL) && defined(h_NETDB_INTERNAL) -#define NETDB_INTERNAL h_NETDB_INTERNAL -#endif - -/* New in Solaris 7 */ -#if !defined(_getshort) && defined(ns_get16) -#define _getshort(cp) ns_get16(cp) -#endif - -typedef union { - HEADER hdr; - u_char buf[MAXPACKET]; -} querybuf_t; - - -static int parse_answer(querybuf_t *ans, int len, struct in_addr *addr) -{ - char buf[MAXPACKET]; - HEADER *ahp; - u_char *cp, *eoa; - int type, n; - - ahp = &ans->hdr; - eoa = ans->buf + len; - cp = ans->buf + sizeof(HEADER); - - while (ahp->qdcount > 0) { - ahp->qdcount--; - cp += dn_skipname(cp, eoa) + QFIXEDSZ; - } - while (ahp->ancount > 0 && cp < eoa) { - ahp->ancount--; - if ((n = dn_expand(ans->buf, eoa, cp, buf, sizeof(buf))) < 0) - break; - cp += n; - type = _getshort(cp); - cp += 8; - n = _getshort(cp); - cp += 2; - if (type == T_CNAME) { - cp += n; - continue; - } - memcpy(addr, cp, n); - return 0; - } - - h_errno = TRY_AGAIN; - return -1; -} - - -static int query_domain(st_netfd_t nfd, const char *name, struct in_addr *addr, - st_utime_t timeout) -{ - querybuf_t qbuf; - u_char *buf = qbuf.buf; - HEADER *hp = &qbuf.hdr; - int blen = sizeof(qbuf); - int i, len, id; - - for (i = 0; i < _res.nscount; i++) { - len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); - if (len <= 0) { - h_errno = NO_RECOVERY; - return -1; - } - id = hp->id; - - if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), - sizeof(struct sockaddr), timeout) != len) { - h_errno = NETDB_INTERNAL; - /* EINTR means interrupt by other thread, NOT by a caught signal */ - if (errno == EINTR) - return -1; - continue; - } - - /* Wait for reply */ - do { - len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); - if (len <= 0) - break; - } while (id != hp->id); - - if (len < HFIXEDSZ) { - h_errno = NETDB_INTERNAL; - if (len >= 0) - errno = EMSGSIZE; - else if (errno == EINTR) /* see the comment above */ - return -1; - continue; - } - - hp->ancount = ntohs(hp->ancount); - hp->qdcount = ntohs(hp->qdcount); - if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { - switch (hp->rcode) { - case NXDOMAIN: - h_errno = HOST_NOT_FOUND; - break; - case SERVFAIL: - h_errno = TRY_AGAIN; - break; - case NOERROR: - h_errno = NO_DATA; - break; - case FORMERR: - case NOTIMP: - case REFUSED: - default: - h_errno = NO_RECOVERY; - } - continue; - } - - if (parse_answer(&qbuf, len, addr) == 0) - return 0; - } - - return -1; -} - - -#define CLOSE_AND_RETURN(ret) \ - { \ - n = errno; \ - st_netfd_close(nfd); \ - errno = n; \ - return (ret); \ - } - - -int dns_getaddr(const char *host, struct in_addr *addr, st_utime_t timeout) -{ - char name[MAXDNAME], **domain; - const char *cp; - int s, n, maxlen, dots; - int trailing_dot, tried_as_is; - st_netfd_t nfd; - - if ((_res.options & RES_INIT) == 0 && res_init() == -1) { - h_errno = NETDB_INTERNAL; - return -1; - } - if (_res.options & RES_USEVC) { - h_errno = NETDB_INTERNAL; - errno = ENOSYS; - return -1; - } - if (!host || *host == '\0') { - h_errno = HOST_NOT_FOUND; - return -1; - } - - /* Create UDP socket */ - if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { - h_errno = NETDB_INTERNAL; - return -1; - } - if ((nfd = st_netfd_open_socket(s)) == NULL) { - h_errno = NETDB_INTERNAL; - n = errno; - close(s); - errno = n; - return -1; - } - - maxlen = sizeof(name) - 1; - n = 0; - dots = 0; - trailing_dot = 0; - tried_as_is = 0; - - for (cp = host; *cp && n < maxlen; cp++) { - dots += (*cp == '.'); - name[n++] = *cp; - } - if (name[n - 1] == '.') - trailing_dot = 1; - - /* - * If there are dots in the name already, let's just give it a try - * 'as is'. The threshold can be set with the "ndots" option. - */ - if (dots >= _res.ndots) { - if (query_domain(nfd, host, addr, timeout) == 0) - CLOSE_AND_RETURN(0); - if (h_errno == NETDB_INTERNAL && errno == EINTR) - CLOSE_AND_RETURN(-1); - tried_as_is = 1; - } - - /* - * We do at least one level of search if - * - there is no dot and RES_DEFNAME is set, or - * - there is at least one dot, there is no trailing dot, - * and RES_DNSRCH is set. - */ - if ((!dots && (_res.options & RES_DEFNAMES)) || - (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { - name[n++] = '.'; - for (domain = _res.dnsrch; *domain; domain++) { - strncpy(name + n, *domain, maxlen - n); - if (query_domain(nfd, name, addr, timeout) == 0) - CLOSE_AND_RETURN(0); - if (h_errno == NETDB_INTERNAL && errno == EINTR) - CLOSE_AND_RETURN(-1); - if (!(_res.options & RES_DNSRCH)) - break; - } - } - - /* - * If we have not already tried the name "as is", do that now. - * note that we do this regardless of how many dots were in the - * name or whether it ends with a dot. - */ - if (!tried_as_is) { - if (query_domain(nfd, host, addr, timeout) == 0) - CLOSE_AND_RETURN(0); - } - - CLOSE_AND_RETURN(-1); -} - diff --git a/trunk/3rdparty/st-srs/examples/server.c b/trunk/3rdparty/st-srs/examples/server.c deleted file mode 100644 index 5d5aa6d726..0000000000 --- a/trunk/3rdparty/st-srs/examples/server.c +++ /dev/null @@ -1,1025 +0,0 @@ -/* - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "st.h" - - -/****************************************************************** - * Server configuration parameters - */ - -/* Log files */ -#define PID_FILE "pid" -#define ERRORS_FILE "errors" -#define ACCESS_FILE "access" - -/* Default server port */ -#define SERV_PORT_DEFAULT 8000 - -/* Socket listen queue size */ -#define LISTENQ_SIZE_DEFAULT 256 - -/* Max number of listening sockets ("hardware virtual servers") */ -#define MAX_BIND_ADDRS 16 - -/* Max number of "spare" threads per process per socket */ -#define MAX_WAIT_THREADS_DEFAULT 8 - -/* Number of file descriptors needed to handle one client session */ -#define FD_PER_THREAD 2 - -/* Access log buffer flushing interval (in seconds) */ -#define ACCLOG_FLUSH_INTERVAL 30 - -/* Request read timeout (in seconds) */ -#define REQUEST_TIMEOUT 30 - - -/****************************************************************** - * Global data - */ - -struct socket_info { - st_netfd_t nfd; /* Listening socket */ - char *addr; /* Bind address */ - unsigned int port; /* Port */ - int wait_threads; /* Number of threads waiting to accept */ - int busy_threads; /* Number of threads processing request */ - int rqst_count; /* Total number of processed requests */ -} srv_socket[MAX_BIND_ADDRS]; /* Array of listening sockets */ - -static int sk_count = 0; /* Number of listening sockets */ - -static int vp_count = 0; /* Number of server processes (VPs) */ -static pid_t *vp_pids; /* Array of VP pids */ - -static int my_index = -1; /* Current process index */ -static pid_t my_pid = -1; /* Current process pid */ - -static st_netfd_t sig_pipe[2]; /* Signal pipe */ - -/* - * Configuration flags/parameters - */ -static int interactive_mode = 0; -static int serialize_accept = 0; -static int log_access = 0; -static char *logdir = NULL; -static char *username = NULL; -static int listenq_size = LISTENQ_SIZE_DEFAULT; -static int errfd = STDERR_FILENO; - -/* - * Thread throttling parameters (all numbers are per listening socket). - * Zero values mean use default. - */ -static int max_threads = 0; /* Max number of threads */ -static int max_wait_threads = 0; /* Max number of "spare" threads */ -static int min_wait_threads = 2; /* Min number of "spare" threads */ - - -/****************************************************************** - * Useful macros - */ - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -#define SEC2USEC(s) ((s)*1000000LL) - -#define WAIT_THREADS(i) (srv_socket[i].wait_threads) -#define BUSY_THREADS(i) (srv_socket[i].busy_threads) -#define TOTAL_THREADS(i) (WAIT_THREADS(i) + BUSY_THREADS(i)) -#define RQST_COUNT(i) (srv_socket[i].rqst_count) - - -/****************************************************************** - * Forward declarations - */ - -static void usage(const char *progname); -static void parse_arguments(int argc, char *argv[]); -static void start_daemon(void); -static void set_thread_throttling(void); -static void create_listeners(void); -static void change_user(void); -static void open_log_files(void); -static void start_processes(void); -static void wdog_sighandler(int signo); -static void child_sighandler(int signo); -static void install_sighandlers(void); -static void start_threads(void); -static void *process_signals(void *arg); -static void *flush_acclog_buffer(void *arg); -static void *handle_connections(void *arg); -static void dump_server_info(void); - -static void Signal(int sig, void (*handler)(int)); -static int cpu_count(void); - -extern void handle_session(long srv_socket_index, st_netfd_t cli_nfd); -extern void load_configs(void); -extern void logbuf_open(void); -extern void logbuf_flush(void); -extern void logbuf_close(void); - -/* Error reporting functions defined in the error.c file */ -extern void err_sys_report(int fd, const char *fmt, ...); -extern void err_sys_quit(int fd, const char *fmt, ...); -extern void err_sys_dump(int fd, const char *fmt, ...); -extern void err_report(int fd, const char *fmt, ...); -extern void err_quit(int fd, const char *fmt, ...); - - -/* - * General server example: accept a client connection and do something. - * This program just outputs a short HTML page, but can be easily adapted - * to do other things. - * - * This server creates a constant number of processes ("virtual processors" - * or VPs) and replaces them when they die. Each virtual processor manages - * its own independent set of state threads (STs), the number of which varies - * with load against the server. Each state thread listens to exactly one - * listening socket. The initial process becomes the watchdog, waiting for - * children (VPs) to die or for a signal requesting termination or restart. - * Upon receiving a restart signal (SIGHUP), all VPs close and then reopen - * log files and reload configuration. All currently active connections remain - * active. It is assumed that new configuration affects only request - * processing and not the general server parameters such as number of VPs, - * thread limits, bind addresses, etc. Those are specified as command line - * arguments, so the server has to be stopped and then started again in order - * to change them. - * - * Each state thread loops processing connections from a single listening - * socket. Only one ST runs on a VP at a time, and VPs do not share memory, - * so no mutual exclusion locking is necessary on any data, and the entire - * server is free to use all the static variables and non-reentrant library - * functions it wants, greatly simplifying programming and debugging and - * increasing performance (for example, it is safe to ++ and -- all global - * counters or call inet_ntoa(3) without any mutexes). The current thread on - * each VP maintains equilibrium on that VP, starting a new thread or - * terminating itself if the number of spare threads exceeds the lower or - * upper limit. - * - * All I/O operations on sockets must use the State Thread library's I/O - * functions because only those functions prevent blocking of the entire VP - * process and perform state thread scheduling. - */ -int main(int argc, char *argv[]) -{ - /* Parse command-line options */ - parse_arguments(argc, argv); - - /* Allocate array of server pids */ - if ((vp_pids = calloc(vp_count, sizeof(pid_t))) == NULL) - err_sys_quit(errfd, "ERROR: calloc failed"); - - /* Start the daemon */ - if (!interactive_mode) - start_daemon(); - - /* Initialize the ST library */ - if (st_init() < 0) - err_sys_quit(errfd, "ERROR: initialization failed: st_init"); - - /* Set thread throttling parameters */ - set_thread_throttling(); - - /* Create listening sockets */ - create_listeners(); - - /* Change the user */ - if (username) - change_user(); - - /* Open log files */ - open_log_files(); - - /* Start server processes (VPs) */ - start_processes(); - - /* Turn time caching on */ - st_timecache_set(1); - - /* Install signal handlers */ - install_sighandlers(); - - /* Load configuration from config files */ - load_configs(); - - /* Start all threads */ - start_threads(); - - /* Become a signal processing thread */ - process_signals(NULL); - - /* NOTREACHED */ - return 1; -} - - -/******************************************************************/ - -static void usage(const char *progname) -{ - fprintf(stderr, "Usage: %s -l []\n\n" - "Possible options:\n\n" - "\t-b : Bind to specified address. Multiple" - " addresses\n" - "\t are permitted.\n" - "\t-p Create specified number of processes.\n" - "\t-t : Specify thread limits per listening" - " socket\n" - "\t across all processes.\n" - "\t-u Change server's user id to specified" - " value.\n" - "\t-q Set max length of pending connections" - " queue.\n" - "\t-a Enable access logging.\n" - "\t-i Run in interactive mode.\n" - "\t-S Serialize all accept() calls.\n" - "\t-h Print this message.\n", - progname); - exit(1); -} - - -/******************************************************************/ - -static void parse_arguments(int argc, char *argv[]) -{ - extern char *optarg; - int opt; - char *c; - - while ((opt = getopt(argc, argv, "b:p:l:t:u:q:aiSh")) != EOF) { - switch (opt) { - case 'b': - if (sk_count >= MAX_BIND_ADDRS) - err_quit(errfd, "ERROR: max number of bind addresses (%d) exceeded", - MAX_BIND_ADDRS); - if ((c = strdup(optarg)) == NULL) - err_sys_quit(errfd, "ERROR: strdup"); - srv_socket[sk_count++].addr = c; - break; - case 'p': - vp_count = atoi(optarg); - if (vp_count < 1) - err_quit(errfd, "ERROR: invalid number of processes: %s", optarg); - break; - case 'l': - logdir = optarg; - break; - case 't': - max_wait_threads = (int) strtol(optarg, &c, 10); - if (*c++ == ':') - max_threads = atoi(c); - if (max_wait_threads < 0 || max_threads < 0) - err_quit(errfd, "ERROR: invalid number of threads: %s", optarg); - break; - case 'u': - username = optarg; - break; - case 'q': - listenq_size = atoi(optarg); - if (listenq_size < 1) - err_quit(errfd, "ERROR: invalid listen queue size: %s", optarg); - break; - case 'a': - log_access = 1; - break; - case 'i': - interactive_mode = 1; - break; - case 'S': - /* - * Serialization decision is tricky on some platforms. For example, - * Solaris 2.6 and above has kernel sockets implementation, so supposedly - * there is no need for serialization. The ST library may be compiled - * on one OS version, but used on another, so the need for serialization - * should be determined at run time by the application. Since it's just - * an example, the serialization decision is left up to user. - * Only on platforms where the serialization is never needed on any OS - * version st_netfd_serialize_accept() is a no-op. - */ - serialize_accept = 1; - break; - case 'h': - case '?': - usage(argv[0]); - } - } - - if (logdir == NULL && !interactive_mode) { - err_report(errfd, "ERROR: logging directory is required\n"); - usage(argv[0]); - } - - if (getuid() == 0 && username == NULL) - err_report(errfd, "WARNING: running as super-user!"); - - if (vp_count == 0 && (vp_count = cpu_count()) < 1) - vp_count = 1; - - if (sk_count == 0) { - sk_count = 1; - srv_socket[0].addr = "0.0.0.0"; - } -} - - -/******************************************************************/ - -static void start_daemon(void) -{ - pid_t pid; - - /* Start forking */ - if ((pid = fork()) < 0) - err_sys_quit(errfd, "ERROR: fork"); - if (pid > 0) - exit(0); /* parent */ - - /* First child process */ - setsid(); /* become session leader */ - - if ((pid = fork()) < 0) - err_sys_quit(errfd, "ERROR: fork"); - if (pid > 0) /* first child */ - exit(0); - - umask(022); - - if (chdir(logdir) < 0) - err_sys_quit(errfd, "ERROR: can't change directory to %s: chdir", logdir); -} - - -/****************************************************************** - * For simplicity, the minimal size of thread pool is considered - * as a maximum number of spare threads (max_wait_threads) that - * will be created upon server startup. The pool size can grow up - * to the max_threads value. Note that this is a per listening - * socket limit. It is also possible to limit the total number of - * threads for all sockets rather than impose a per socket limit. - */ - -static void set_thread_throttling(void) -{ - /* - * Calculate total values across all processes. - * All numbers are per listening socket. - */ - if (max_wait_threads == 0) - max_wait_threads = MAX_WAIT_THREADS_DEFAULT * vp_count; - /* Assuming that each client session needs FD_PER_THREAD file descriptors */ - if (max_threads == 0) - max_threads = (st_getfdlimit() * vp_count) / FD_PER_THREAD / sk_count; - if (max_wait_threads > max_threads) - max_wait_threads = max_threads; - - /* - * Now calculate per-process values. - */ - if (max_wait_threads % vp_count) - max_wait_threads = max_wait_threads / vp_count + 1; - else - max_wait_threads = max_wait_threads / vp_count; - if (max_threads % vp_count) - max_threads = max_threads / vp_count + 1; - else - max_threads = max_threads / vp_count; - - if (min_wait_threads > max_wait_threads) - min_wait_threads = max_wait_threads; -} - - -/******************************************************************/ - -static void create_listeners(void) -{ - int i, n, sock; - char *c; - struct sockaddr_in serv_addr; - struct hostent *hp; - unsigned short port; - - for (i = 0; i < sk_count; i++) { - port = 0; - if ((c = strchr(srv_socket[i].addr, ':')) != NULL) { - *c++ = '\0'; - port = (unsigned short) atoi(c); - } - if (srv_socket[i].addr[0] == '\0') - srv_socket[i].addr = "0.0.0.0"; - if (port == 0) - port = SERV_PORT_DEFAULT; - - /* Create server socket */ - if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) - err_sys_quit(errfd, "ERROR: can't create socket: socket"); - n = 1; - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n)) < 0) - err_sys_quit(errfd, "ERROR: can't set SO_REUSEADDR: setsockopt"); - memset(&serv_addr, 0, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - serv_addr.sin_addr.s_addr = inet_addr(srv_socket[i].addr); - if (serv_addr.sin_addr.s_addr == INADDR_NONE) { - /* not dotted-decimal */ - if ((hp = gethostbyname(srv_socket[i].addr)) == NULL) - err_quit(errfd, "ERROR: can't resolve address: %s", - srv_socket[i].addr); - memcpy(&serv_addr.sin_addr, hp->h_addr, hp->h_length); - } - srv_socket[i].port = port; - - /* Do bind and listen */ - if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) - err_sys_quit(errfd, "ERROR: can't bind to address %s, port %hu", - srv_socket[i].addr, port); - if (listen(sock, listenq_size) < 0) - err_sys_quit(errfd, "ERROR: listen"); - - /* Create file descriptor object from OS socket */ - if ((srv_socket[i].nfd = st_netfd_open_socket(sock)) == NULL) - err_sys_quit(errfd, "ERROR: st_netfd_open_socket"); - /* - * On some platforms (e.g. IRIX, Linux) accept() serialization is never - * needed for any OS version. In that case st_netfd_serialize_accept() - * is just a no-op. Also see the comment above. - */ - if (serialize_accept && st_netfd_serialize_accept(srv_socket[i].nfd) < 0) - err_sys_quit(errfd, "ERROR: st_netfd_serialize_accept"); - } -} - - -/******************************************************************/ - -static void change_user(void) -{ - struct passwd *pw; - - if ((pw = getpwnam(username)) == NULL) - err_quit(errfd, "ERROR: can't find user '%s': getpwnam failed", username); - - if (setgid(pw->pw_gid) < 0) - err_sys_quit(errfd, "ERROR: can't change group id: setgid"); - if (setuid(pw->pw_uid) < 0) - err_sys_quit(errfd, "ERROR: can't change user id: setuid"); - - err_report(errfd, "INFO: changed process user id to '%s'", username); -} - - -/******************************************************************/ - -static void open_log_files(void) -{ - int fd; - char str[32]; - - if (interactive_mode) - return; - - /* Open access log */ - if (log_access) - logbuf_open(); - - /* Open and write pid to pid file */ - if ((fd = open(PID_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) - err_sys_quit(errfd, "ERROR: can't open pid file: open"); - sprintf(str, "%d\n", (int)getpid()); - if (write(fd, str, strlen(str)) != strlen(str)) - err_sys_quit(errfd, "ERROR: can't write to pid file: write"); - close(fd); - - /* Open error log file */ - if ((fd = open(ERRORS_FILE, O_CREAT | O_WRONLY | O_APPEND, 0644)) < 0) - err_sys_quit(errfd, "ERROR: can't open error log file: open"); - errfd = fd; - - err_report(errfd, "INFO: starting the server..."); -} - - -/******************************************************************/ - -static void start_processes(void) -{ - int i, status; - pid_t pid; - sigset_t mask, omask; - - if (interactive_mode) { - my_index = 0; - my_pid = getpid(); - return; - } - - for (i = 0; i < vp_count; i++) { - if ((pid = fork()) < 0) { - err_sys_report(errfd, "ERROR: can't create process: fork"); - if (i == 0) - exit(1); - err_report(errfd, "WARN: started only %d processes out of %d", i, - vp_count); - vp_count = i; - break; - } - if (pid == 0) { - my_index = i; - my_pid = getpid(); - /* Child returns to continue in main() */ - return; - } - vp_pids[i] = pid; - } - - /* - * Parent process becomes a "watchdog" and never returns to main(). - */ - - /* Install signal handlers */ - Signal(SIGTERM, wdog_sighandler); /* terminate */ - Signal(SIGHUP, wdog_sighandler); /* restart */ - Signal(SIGUSR1, wdog_sighandler); /* dump info */ - - /* Now go to sleep waiting for a child termination or a signal */ - for ( ; ; ) { - if ((pid = wait(&status)) < 0) { - if (errno == EINTR) - continue; - err_sys_quit(errfd, "ERROR: watchdog: wait"); - } - /* Find index of the exited child */ - for (i = 0; i < vp_count; i++) { - if (vp_pids[i] == pid) - break; - } - - /* Block signals while printing and forking */ - sigemptyset(&mask); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGHUP); - sigaddset(&mask, SIGUSR1); - sigprocmask(SIG_BLOCK, &mask, &omask); - - if (WIFEXITED(status)) - err_report(errfd, "WARN: watchdog: process %d (pid %d) exited" - " with status %d", i, pid, WEXITSTATUS(status)); - else if (WIFSIGNALED(status)) - err_report(errfd, "WARN: watchdog: process %d (pid %d) terminated" - " by signal %d", i, pid, WTERMSIG(status)); - else if (WIFSTOPPED(status)) - err_report(errfd, "WARN: watchdog: process %d (pid %d) stopped" - " by signal %d", i, pid, WSTOPSIG(status)); - else - err_report(errfd, "WARN: watchdog: process %d (pid %d) terminated:" - " unknown termination reason", i, pid); - - /* Fork another VP */ - if ((pid = fork()) < 0) { - err_sys_report(errfd, "ERROR: watchdog: can't create process: fork"); - } else if (pid == 0) { - my_index = i; - my_pid = getpid(); - /* Child returns to continue in main() */ - return; - } - vp_pids[i] = pid; - - /* Restore the signal mask */ - sigprocmask(SIG_SETMASK, &omask, NULL); - } -} - - -/******************************************************************/ - -static void wdog_sighandler(int signo) -{ - int i, err; - - /* Save errno */ - err = errno; - /* Forward the signal to all children */ - for (i = 0; i < vp_count; i++) { - if (vp_pids[i] > 0) - kill(vp_pids[i], signo); - } - /* - * It is safe to do pretty much everything here because process is - * sleeping in wait() which is async-safe. - */ - switch (signo) { - case SIGHUP: - err_report(errfd, "INFO: watchdog: caught SIGHUP"); - /* Reopen log files - needed for log rotation */ - if (log_access) { - logbuf_close(); - logbuf_open(); - } - close(errfd); - if ((errfd = open(ERRORS_FILE, O_CREAT | O_WRONLY | O_APPEND, 0644)) < 0) - err_sys_quit(STDERR_FILENO, "ERROR: watchdog: open"); - break; - case SIGTERM: - /* Non-graceful termination */ - err_report(errfd, "INFO: watchdog: caught SIGTERM, terminating"); - unlink(PID_FILE); - exit(0); - case SIGUSR1: - err_report(errfd, "INFO: watchdog: caught SIGUSR1"); - break; - default: - err_report(errfd, "INFO: watchdog: caught signal %d", signo); - } - /* Restore errno */ - errno = err; -} - - -/******************************************************************/ - -static void install_sighandlers(void) -{ - sigset_t mask; - int p[2]; - - /* Create signal pipe */ - if (pipe(p) < 0) - err_sys_quit(errfd, "ERROR: process %d (pid %d): can't create" - " signal pipe: pipe", my_index, my_pid); - if ((sig_pipe[0] = st_netfd_open(p[0])) == NULL || - (sig_pipe[1] = st_netfd_open(p[1])) == NULL) - err_sys_quit(errfd, "ERROR: process %d (pid %d): can't create" - " signal pipe: st_netfd_open", my_index, my_pid); - - /* Install signal handlers */ - Signal(SIGTERM, child_sighandler); /* terminate */ - Signal(SIGHUP, child_sighandler); /* restart */ - Signal(SIGUSR1, child_sighandler); /* dump info */ - - /* Unblock signals */ - sigemptyset(&mask); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGHUP); - sigaddset(&mask, SIGUSR1); - sigprocmask(SIG_UNBLOCK, &mask, NULL); -} - - -/******************************************************************/ - -static void child_sighandler(int signo) -{ - int err, fd; - - err = errno; - fd = st_netfd_fileno(sig_pipe[1]); - - /* write() is async-safe */ - if (write(fd, &signo, sizeof(int)) != sizeof(int)) - err_sys_quit(errfd, "ERROR: process %d (pid %d): child's signal" - " handler: write", my_index, my_pid); - errno = err; -} - - -/****************************************************************** - * The "main" function of the signal processing thread. - */ - -/* ARGSUSED */ -static void *process_signals(void *arg) -{ - int signo; - - for ( ; ; ) { - /* Read the next signal from the signal pipe */ - if (st_read(sig_pipe[0], &signo, sizeof(int), - ST_UTIME_NO_TIMEOUT) != sizeof(int)) - err_sys_quit(errfd, "ERROR: process %d (pid %d): signal processor:" - " st_read", my_index, my_pid); - - switch (signo) { - case SIGHUP: - err_report(errfd, "INFO: process %d (pid %d): caught SIGHUP," - " reloading configuration", my_index, my_pid); - if (interactive_mode) { - load_configs(); - break; - } - /* Reopen log files - needed for log rotation */ - if (log_access) { - logbuf_flush(); - logbuf_close(); - logbuf_open(); - } - close(errfd); - if ((errfd = open(ERRORS_FILE, O_CREAT | O_WRONLY | O_APPEND, 0644)) < 0) - err_sys_quit(STDERR_FILENO, "ERROR: process %d (pid %d): signal" - " processor: open", my_index, my_pid); - /* Reload configuration */ - load_configs(); - break; - case SIGTERM: - /* - * Terminate ungracefully since it is generally not known how long - * it will take to gracefully complete all client sessions. - */ - err_report(errfd, "INFO: process %d (pid %d): caught SIGTERM," - " terminating", my_index, my_pid); - if (log_access) - logbuf_flush(); - exit(0); - case SIGUSR1: - err_report(errfd, "INFO: process %d (pid %d): caught SIGUSR1", - my_index, my_pid); - /* Print server info to stderr */ - dump_server_info(); - break; - default: - err_report(errfd, "INFO: process %d (pid %d): caught signal %d", - my_index, my_pid, signo); - } - } - - /* NOTREACHED */ - return NULL; -} - - -/****************************************************************** - * The "main" function of the access log flushing thread. - */ - -/* ARGSUSED */ -static void *flush_acclog_buffer(void *arg) -{ - for ( ; ; ) { - st_sleep(ACCLOG_FLUSH_INTERVAL); - logbuf_flush(); - } - - /* NOTREACHED */ - return NULL; -} - - -/******************************************************************/ - -static void start_threads(void) -{ - long i, n; - - /* Create access log flushing thread */ - if (log_access && st_thread_create(flush_acclog_buffer, NULL, 0, 0) == NULL) - err_sys_quit(errfd, "ERROR: process %d (pid %d): can't create" - " log flushing thread", my_index, my_pid); - - /* Create connections handling threads */ - for (i = 0; i < sk_count; i++) { - err_report(errfd, "INFO: process %d (pid %d): starting %d threads" - " on %s:%u", my_index, my_pid, max_wait_threads, - srv_socket[i].addr, srv_socket[i].port); - WAIT_THREADS(i) = 0; - BUSY_THREADS(i) = 0; - RQST_COUNT(i) = 0; - for (n = 0; n < max_wait_threads; n++) { - if (st_thread_create(handle_connections, (void *)i, 0, 0) != NULL) - WAIT_THREADS(i)++; - else - err_sys_report(errfd, "ERROR: process %d (pid %d): can't create" - " thread", my_index, my_pid); - } - if (WAIT_THREADS(i) == 0) - exit(1); - } -} - - -/******************************************************************/ - -static void *handle_connections(void *arg) -{ - st_netfd_t srv_nfd, cli_nfd; - struct sockaddr_in from; - int fromlen; - long i = (long) arg; - - srv_nfd = srv_socket[i].nfd; - fromlen = sizeof(from); - - while (WAIT_THREADS(i) <= max_wait_threads) { - cli_nfd = st_accept(srv_nfd, (struct sockaddr *)&from, &fromlen, - ST_UTIME_NO_TIMEOUT); - if (cli_nfd == NULL) { - err_sys_report(errfd, "ERROR: can't accept connection: st_accept"); - continue; - } - /* Save peer address, so we can retrieve it later */ - st_netfd_setspecific(cli_nfd, &from.sin_addr, NULL); - - WAIT_THREADS(i)--; - BUSY_THREADS(i)++; - if (WAIT_THREADS(i) < min_wait_threads && TOTAL_THREADS(i) < max_threads) { - /* Create another spare thread */ - if (st_thread_create(handle_connections, (void *)i, 0, 0) != NULL) - WAIT_THREADS(i)++; - else - err_sys_report(errfd, "ERROR: process %d (pid %d): can't create" - " thread", my_index, my_pid); - } - - handle_session(i, cli_nfd); - - st_netfd_close(cli_nfd); - WAIT_THREADS(i)++; - BUSY_THREADS(i)--; - } - - WAIT_THREADS(i)--; - return NULL; -} - - -/******************************************************************/ - -static void dump_server_info(void) -{ - char *buf; - int i, len; - - if ((buf = malloc(sk_count * 512)) == NULL) { - err_sys_report(errfd, "ERROR: malloc failed"); - return; - } - - len = sprintf(buf, "\n\nProcess #%d (pid %d):\n", my_index, (int)my_pid); - for (i = 0; i < sk_count; i++) { - len += sprintf(buf + len, "\nListening Socket #%d:\n" - "-------------------------\n" - "Address %s:%u\n" - "Thread limits (min/max) %d/%d\n" - "Waiting threads %d\n" - "Busy threads %d\n" - "Requests served %d\n", - i, srv_socket[i].addr, srv_socket[i].port, - max_wait_threads, max_threads, - WAIT_THREADS(i), BUSY_THREADS(i), RQST_COUNT(i)); - } - - write(STDERR_FILENO, buf, len); - free(buf); -} - - -/****************************************************************** - * Stubs - */ - -/* - * Session handling function stub. Just dumps small HTML page. - */ -void handle_session(long srv_socket_index, st_netfd_t cli_nfd) -{ - static char resp[] = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n" - "Connection: close\r\n\r\n

It worked!

\n"; - char buf[512]; - int n = sizeof(resp) - 1; - struct in_addr *from = st_netfd_getspecific(cli_nfd); - - if (st_read(cli_nfd, buf, sizeof(buf), SEC2USEC(REQUEST_TIMEOUT)) < 0) { - err_sys_report(errfd, "WARN: can't read request from %s: st_read", - inet_ntoa(*from)); - return; - } - if (st_write(cli_nfd, resp, n, ST_UTIME_NO_TIMEOUT) != n) { - err_sys_report(errfd, "WARN: can't write response to %s: st_write", - inet_ntoa(*from)); - return; - } - - RQST_COUNT(srv_socket_index)++; -} - - -/* - * Configuration loading function stub. - */ -void load_configs(void) -{ - err_report(errfd, "INFO: process %d (pid %d): configuration loaded", - my_index, my_pid); -} - - -/* - * Buffered access logging methods. - * Note that stdio functions (fopen(3), fprintf(3), fflush(3), etc.) cannot - * be used if multiple VPs are created since these functions can flush buffer - * at any point and thus write only partial log record to disk. - * Also, it is completely safe for all threads of the same VP to write to - * the same log buffer without any mutex protection (one buffer per VP, of - * course). - */ -void logbuf_open(void) -{ - -} - - -void logbuf_flush(void) -{ - -} - - -void logbuf_close(void) -{ - -} - - -/****************************************************************** - * Small utility functions - */ - -static void Signal(int sig, void (*handler)(int)) -{ - struct sigaction sa; - - sa.sa_handler = handler; - sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - sigaction(sig, &sa, NULL); -} - -static int cpu_count(void) -{ - int n; - -#if defined (_SC_NPROCESSORS_ONLN) - n = (int) sysconf(_SC_NPROCESSORS_ONLN); -#elif defined (_SC_NPROC_ONLN) - n = (int) sysconf(_SC_NPROC_ONLN); -#elif defined (HPUX) -#include - n = mpctl(MPC_GETNUMSPUS, 0, 0); -#else - n = -1; - errno = ENOSYS; -#endif - - return n; -} - -/******************************************************************/ - diff --git a/trunk/3rdparty/st-srs/extensions/Makefile b/trunk/3rdparty/st-srs/extensions/Makefile deleted file mode 100644 index fc6634f93f..0000000000 --- a/trunk/3rdparty/st-srs/extensions/Makefile +++ /dev/null @@ -1,91 +0,0 @@ -# -# Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. -# All Rights Reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# 3. Neither the name of Silicon Graphics, Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -CC = cc - -SHELL = /bin/sh -ECHO = /bin/echo - -DEPTH = .. -BUILD = -TARGETDIR = obj - -DEFINES = -OTHER_FLAGS = -CFLAGS = - -OBJDIR = $(DEPTH)/$(TARGETDIR) -INCDIR = $(DEPTH)/$(TARGETDIR) - -LIBRESOLV = -EXTRALIBS = - -SLIBRARY = $(OBJDIR)/libstx.a -OBJS = $(OBJDIR)/dnscache.o $(OBJDIR)/dnsres.o $(OBJDIR)/lrucache.o - - -CFLAGS += -Wall -I$(INCDIR) -AR = ar -ARFLAGS = rv -RANLIB = ranlib - - -########################## -# Platform section. -# - -ifeq (LINUX, $(findstring LINUX, $(OS))) -LIBRESOLV = -lresolv -endif - -ifeq ($(OS), SOLARIS) -LIBRESOLV = -lresolv -EXTRALIBS = -lsocket -lnsl -endif - -# -# End of platform section. -########################## - - -all: $(SLIBRARY) - -$(SLIBRARY): $(OBJS) - $(AR) $(ARFLAGS) $@ $(OBJS) - $(RANLIB) $@ - -$(OBJDIR)/%.o: %.c stx.h common.h - $(CC) $(CFLAGS) -c $< -o $@ - -clean: - rm -rf $(OBJS) $(SLIBRARY) - -#.DEFAULT: -# @cd $(DEPTH); $(MAKE) $@ - diff --git a/trunk/3rdparty/st-srs/extensions/README b/trunk/3rdparty/st-srs/extensions/README deleted file mode 100644 index f768aa7125..0000000000 --- a/trunk/3rdparty/st-srs/extensions/README +++ /dev/null @@ -1,42 +0,0 @@ -This directory contains extensions to the core State Threads Library -that were contributed by users. All files hereunder are not part of the -State Threads Library itself. They are provided as-is, without warranty -or support, and under whatever license terms their authors provided. To -contribute your own extensions, just mail them to the project -administrators or to one of the project's mailing lists; see -state-threads.sourceforge.net. Please indicate the license terms under -which the project may distribute your contribution. - -======================================================================== - -stx_fileio ----------- -Contributed by Jeff , 4 Nov 2002. - -Provides non-blocking random access file reading capability for -programs using the State Threads library. There is one public function: - -ssize_t stx_file_read(st_netfd_t fd, off_t offset, - void *buf, size_t nbytes, st_utime_t timeout); - -The implementation is not optimal in that the data is copied at least once -more than should be necessary. Its usefulness is limited to cases where -random access to a file is required and where starvation of other threads -is unacceptable. - -The particular application which motivated this implementation was a UDP -file transfer protocol. Because the OS does very little buffering of UDP -traffic it is important that UDP transmission threads are not starved for -periods of time which are long relative to the interval required to -maintain a steady send rate. - -Licensed under the same dual MPL/GPL as core State Threads. - -======================================================================== - -stx_dns -------- - -Documentation coming. - -======================================================================== diff --git a/trunk/3rdparty/st-srs/extensions/common.h b/trunk/3rdparty/st-srs/extensions/common.h deleted file mode 100644 index f6298ba09e..0000000000 --- a/trunk/3rdparty/st-srs/extensions/common.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef _STX_COMMON_H_ -#define _STX_COMMON_H_ - -#include -#include - - -#define STX_BEGIN_MACRO { -#define STX_END_MACRO } - - -/***************************************** - * Circular linked list definitions - */ - -typedef struct _stx_clist { - struct _stx_clist *next; - struct _stx_clist *prev; -} stx_clist_t; - -/* Insert element "_e" into the list, before "_l" */ -#define STX_CLIST_INSERT_BEFORE(_e,_l) \ - STX_BEGIN_MACRO \ - (_e)->next = (_l); \ - (_e)->prev = (_l)->prev; \ - (_l)->prev->next = (_e); \ - (_l)->prev = (_e); \ - STX_END_MACRO - -/* Insert element "_e" into the list, after "_l" */ -#define STX_CLIST_INSERT_AFTER(_e,_l) \ - STX_BEGIN_MACRO \ - (_e)->next = (_l)->next; \ - (_e)->prev = (_l); \ - (_l)->next->prev = (_e); \ - (_l)->next = (_e); \ - STX_END_MACRO - -/* Append an element "_e" to the end of the list "_l" */ -#define STX_CLIST_APPEND_LINK(_e,_l) STX_CLIST_INSERT_BEFORE(_e,_l) - -/* Remove the element "_e" from it's circular list */ -#define STX_CLIST_REMOVE_LINK(_e) \ - STX_BEGIN_MACRO \ - (_e)->prev->next = (_e)->next; \ - (_e)->next->prev = (_e)->prev; \ - STX_END_MACRO - -/* Return the head/tail of the list */ -#define STX_CLIST_HEAD(_l) (_l)->next -#define STX_CLIST_TAIL(_l) (_l)->prev - -/* Return non-zero if the given circular list "_l" is empty, */ -/* zero if the circular list is not empty */ -#define STX_CLIST_IS_EMPTY(_l) \ - ((_l)->next == (_l)) - -/* Initialize a circular list */ -#define STX_CLIST_INIT_CLIST(_l) \ - STX_BEGIN_MACRO \ - (_l)->next = (_l); \ - (_l)->prev = (_l); \ - STX_END_MACRO - - -/***************************************** - * Useful macros - */ - -#ifndef offsetof -#define offsetof(type, identifier) ((size_t)&(((type *)0)->identifier)) -#endif - -#define STX_MIN(a, b) (((a) < (b)) ? (a) : (b)) - -#endif /* !_STX_COMMON_H_ */ - diff --git a/trunk/3rdparty/st-srs/extensions/dnscache.c b/trunk/3rdparty/st-srs/extensions/dnscache.c deleted file mode 100644 index ac49166a14..0000000000 --- a/trunk/3rdparty/st-srs/extensions/dnscache.c +++ /dev/null @@ -1,190 +0,0 @@ -#include "stx.h" -#include "common.h" - - -/***************************************** - * Basic types definitions - */ - -typedef struct _stx_dns_data { - struct in_addr *addrs; - int num_addrs; - int cur; - time_t expires; -} stx_dns_data_t; - - -#define MAX_HOST_ADDRS 1024 - -static struct in_addr addr_list[MAX_HOST_ADDRS]; - -stx_cache_t *_stx_dns_cache = NULL; - -extern int _stx_dns_ttl; -extern int _stx_dns_getaddrlist(const char *hostname, struct in_addr *addrs, - int *num_addrs, st_utime_t timeout); - - -static unsigned long hash_hostname(const void *key) -{ - const char *name = (const char *)key; - unsigned long hash = 0; - - while (*name) - hash = (hash << 4) - hash + *name++; /* hash = hash * 15 + *name++ */ - - return hash; -} - -static void cleanup_entry(void *key, void *data) -{ - if (key) - free(key); - - if (data) { - if (((stx_dns_data_t *)data)->addrs) - free(((stx_dns_data_t *)data)->addrs); - free(data); - } -} - -static int lookup_entry(const char *host, struct in_addr *addrs, - int *num_addrs, int rotate) -{ - stx_cache_entry_t *entry; - stx_dns_data_t *data; - int n; - - entry = stx_cache_entry_lookup(_stx_dns_cache, host); - if (entry) { - data = (stx_dns_data_t *)stx_cache_entry_getdata(entry); - if (st_time() <= data->expires) { - if (*num_addrs == 1) { - if (rotate) { - *addrs = data->addrs[data->cur++]; - if (data->cur >= data->num_addrs) - data->cur = 0; - } else { - *addrs = data->addrs[0]; - } - } else { - n = STX_MIN(*num_addrs, data->num_addrs); - memcpy(addrs, data->addrs, n * sizeof(*addrs)); - *num_addrs = n; - } - - stx_cache_entry_release(_stx_dns_cache, entry); - return 1; - } - - /* - * Cache entry expired: decrement its refcount and purge it from cache. - */ - stx_cache_entry_release(_stx_dns_cache, entry); - stx_cache_entry_delete(_stx_dns_cache, entry); - } - - return 0; -} - -static void insert_entry(const char *host, struct in_addr *addrs, int count) -{ - stx_cache_entry_t *entry; - stx_dns_data_t *data; - char *key; - size_t n; - - if (_stx_dns_ttl > 0) { - key = strdup(host); - data = (stx_dns_data_t *)malloc(sizeof(stx_dns_data_t)); - n = count * sizeof(*addrs); - if (data) { - data->addrs = (struct in_addr *)malloc(n); - if (data->addrs) - memcpy(data->addrs, addrs, n); - data->num_addrs = count; - data->cur = 0; - data->expires = st_time() + _stx_dns_ttl; - } - entry = stx_cache_entry_create(key, data, strlen(host) + 1 + - sizeof(stx_dns_data_t) + n + - stx_cache_entry_sizeof()); - if (key && data && data->addrs && entry && - stx_cache_entry_insert(_stx_dns_cache, entry) == 0) { - stx_cache_entry_release(_stx_dns_cache, entry); - return; - } - - if (entry) - stx_cache_entry_delete(_stx_dns_cache, entry); - else - cleanup_entry(key, data); - } -} - - - -int _stx_dns_cache_getaddrlist(const char *hostname, struct in_addr *addrs, - int *num_addrs, st_utime_t timeout, - int rotate) -{ - char host[128]; - int n, count; - - if (!_stx_dns_cache) - return _stx_dns_getaddrlist(hostname, addrs, num_addrs, timeout); - - for (n = 0; n < sizeof(host) - 1 && hostname[n]; n++) { - host[n] = tolower(hostname[n]); - } - host[n] = '\0'; - - if (lookup_entry(host, addrs, num_addrs, rotate)) - return 0; - - count = MAX_HOST_ADDRS; - if (_stx_dns_getaddrlist(host, addr_list, &count, timeout) < 0) - return -1; - n = STX_MIN(*num_addrs, count); - memcpy(addrs, addr_list, n * sizeof(*addrs)); - *num_addrs = n; - - insert_entry(host, addr_list, count); - return 0; -} - - -int stx_dns_cache_init(size_t max_size, size_t max_bytes, size_t hash_size) -{ - _stx_dns_cache = stx_cache_create(max_size, max_bytes, hash_size, - hash_hostname, - (long (*)(const void *, const void *))strcmp, - cleanup_entry); - if (!_stx_dns_cache) - return -1; - - return 0; -} - -void stx_dns_cache_getinfo(stx_cache_info_t *info) -{ - if (_stx_dns_cache) - stx_cache_getinfo(_stx_dns_cache, info); - else - memset(info, 0, sizeof(stx_cache_info_t)); -} - -int stx_dns_getaddrlist(const char *hostname, struct in_addr *addrs, - int *num_addrs, st_utime_t timeout) -{ - return _stx_dns_cache_getaddrlist(hostname, addrs, num_addrs, timeout, 0); -} - -int stx_dns_getaddr(const char *hostname, struct in_addr *addr, - st_utime_t timeout) -{ - int n = 1; - - return _stx_dns_cache_getaddrlist(hostname, addr, &n, timeout, 1); -} - diff --git a/trunk/3rdparty/st-srs/extensions/dnsres.c b/trunk/3rdparty/st-srs/extensions/dnsres.c deleted file mode 100644 index 04a91ccafa..0000000000 --- a/trunk/3rdparty/st-srs/extensions/dnsres.c +++ /dev/null @@ -1,305 +0,0 @@ -/* - * Copyright (c) 1985, 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc. - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Silicon Graphics, Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "stx.h" - -#define MAXPACKET 1024 - -#if !defined(NETDB_INTERNAL) && defined(h_NETDB_INTERNAL) -#define NETDB_INTERNAL h_NETDB_INTERNAL -#endif - -/* New in Solaris 7 */ -#if !defined(_getshort) && defined(ns_get16) -#define _getshort(cp) ns_get16(cp) -#define _getlong(cp) ns_get32(cp) -#endif - -typedef union { - HEADER hdr; - u_char buf[MAXPACKET]; -} querybuf_t; - -int _stx_dns_ttl; - - -static int parse_answer(querybuf_t *ans, int len, struct in_addr *addrs, - int *num_addrs) -{ - char buf[MAXPACKET]; - HEADER *ahp; - u_char *cp, *eoa; - int type, n, i; - - ahp = &ans->hdr; - eoa = ans->buf + len; - cp = ans->buf + sizeof(HEADER); - h_errno = TRY_AGAIN; - _stx_dns_ttl = -1; - i = 0; - - while (ahp->qdcount > 0) { - ahp->qdcount--; - cp += dn_skipname(cp, eoa) + QFIXEDSZ; - } - while (ahp->ancount > 0 && cp < eoa && i < *num_addrs) { - ahp->ancount--; - if ((n = dn_expand(ans->buf, eoa, cp, buf, sizeof(buf))) < 0) - return -1; - cp += n; - if (cp + 4 + 4 + 2 >= eoa) - return -1; - type = _getshort(cp); - cp += 4; - if (type == T_A) - _stx_dns_ttl = _getlong(cp); - cp += 4; - n = _getshort(cp); - cp += 2; - if (type == T_A) { - if (n > sizeof(*addrs) || cp + n > eoa) - return -1; - memcpy(&addrs[i++], cp, n); - } - cp += n; - } - - *num_addrs = i; - return 0; -} - - -static int query_domain(st_netfd_t nfd, const char *name, - struct in_addr *addrs, int *num_addrs, - st_utime_t timeout) -{ - querybuf_t qbuf; - u_char *buf = qbuf.buf; - HEADER *hp = &qbuf.hdr; - int blen = sizeof(qbuf); - int i, len, id; - - for (i = 0; i < _res.nscount; i++) { - len = res_mkquery(QUERY, name, C_IN, T_A, NULL, 0, NULL, buf, blen); - if (len <= 0) { - h_errno = NO_RECOVERY; - return -1; - } - id = hp->id; - - if (st_sendto(nfd, buf, len, (struct sockaddr *)&(_res.nsaddr_list[i]), - sizeof(struct sockaddr), timeout) != len) { - h_errno = NETDB_INTERNAL; - /* EINTR means interrupt by other thread, NOT by a caught signal */ - if (errno == EINTR) - return -1; - continue; - } - - /* Wait for reply */ - do { - len = st_recvfrom(nfd, buf, blen, NULL, NULL, timeout); - if (len <= 0) - break; - } while (id != hp->id); - - if (len < HFIXEDSZ) { - h_errno = NETDB_INTERNAL; - if (len >= 0) - errno = EMSGSIZE; - else if (errno == EINTR) /* see the comment above */ - return -1; - continue; - } - - hp->ancount = ntohs(hp->ancount); - hp->qdcount = ntohs(hp->qdcount); - if ((hp->rcode != NOERROR) || (hp->ancount == 0)) { - switch (hp->rcode) { - case NXDOMAIN: - h_errno = HOST_NOT_FOUND; - break; - case SERVFAIL: - h_errno = TRY_AGAIN; - break; - case NOERROR: - h_errno = NO_DATA; - break; - case FORMERR: - case NOTIMP: - case REFUSED: - default: - h_errno = NO_RECOVERY; - } - continue; - } - - if (parse_answer(&qbuf, len, addrs, num_addrs) == 0) - return 0; - } - - return -1; -} - - -#define CLOSE_AND_RETURN(ret) \ - { \ - n = errno; \ - st_netfd_close(nfd); \ - errno = n; \ - return (ret); \ - } - - -int _stx_dns_getaddrlist(const char *host, struct in_addr *addrs, - int *num_addrs, st_utime_t timeout) -{ - char name[MAXDNAME], **domain; - const char *cp; - int s, n, maxlen, dots; - int trailing_dot, tried_as_is; - st_netfd_t nfd; - - if ((_res.options & RES_INIT) == 0 && res_init() == -1) { - h_errno = NETDB_INTERNAL; - return -1; - } - if (_res.options & RES_USEVC) { - h_errno = NETDB_INTERNAL; - errno = ENOSYS; - return -1; - } - if (!host || *host == '\0') { - h_errno = HOST_NOT_FOUND; - return -1; - } - - /* Create UDP socket */ - if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { - h_errno = NETDB_INTERNAL; - return -1; - } - if ((nfd = st_netfd_open_socket(s)) == NULL) { - h_errno = NETDB_INTERNAL; - n = errno; - close(s); - errno = n; - return -1; - } - - maxlen = sizeof(name) - 1; - n = 0; - dots = 0; - trailing_dot = 0; - tried_as_is = 0; - - for (cp = host; *cp && n < maxlen; cp++) { - dots += (*cp == '.'); - name[n++] = *cp; - } - if (name[n - 1] == '.') - trailing_dot = 1; - - /* - * If there are dots in the name already, let's just give it a try - * 'as is'. The threshold can be set with the "ndots" option. - */ - if (dots >= _res.ndots) { - if (query_domain(nfd, host, addrs, num_addrs, timeout) == 0) - CLOSE_AND_RETURN(0); - if (h_errno == NETDB_INTERNAL && errno == EINTR) - CLOSE_AND_RETURN(-1); - tried_as_is = 1; - } - - /* - * We do at least one level of search if - * - there is no dot and RES_DEFNAME is set, or - * - there is at least one dot, there is no trailing dot, - * and RES_DNSRCH is set. - */ - if ((!dots && (_res.options & RES_DEFNAMES)) || - (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { - name[n++] = '.'; - for (domain = _res.dnsrch; *domain; domain++) { - strncpy(name + n, *domain, maxlen - n); - if (query_domain(nfd, name, addrs, num_addrs, timeout) == 0) - CLOSE_AND_RETURN(0); - if (h_errno == NETDB_INTERNAL && errno == EINTR) - CLOSE_AND_RETURN(-1); - if (!(_res.options & RES_DNSRCH)) - break; - } - } - - /* - * If we have not already tried the name "as is", do that now. - * note that we do this regardless of how many dots were in the - * name or whether it ends with a dot. - */ - if (!tried_as_is) { - if (query_domain(nfd, host, addrs, num_addrs, timeout) == 0) - CLOSE_AND_RETURN(0); - } - - CLOSE_AND_RETURN(-1); -} - diff --git a/trunk/3rdparty/st-srs/extensions/lrucache.c b/trunk/3rdparty/st-srs/extensions/lrucache.c deleted file mode 100644 index 33494fee62..0000000000 --- a/trunk/3rdparty/st-srs/extensions/lrucache.c +++ /dev/null @@ -1,343 +0,0 @@ -#include "stx.h" -#include "common.h" - - -/***************************************** - * Basic types definitions - */ - -struct _stx_centry { - void *key; /* key for doing lookups */ - void *data; /* data in the cache */ - size_t weight; /* "weight" of this entry */ - struct _stx_centry *next; /* next entry */ - struct _stx_centry **pthis; - stx_clist_t lru_link; /* for putting this entry on LRU list */ - int ref_count; /* use count for this entry */ - int delete_pending; /* pending delete flag */ -}; - -struct _stx_cache { - size_t max_size; /* max size of cache */ - size_t cur_size; /* current size of cache */ - - size_t max_weight; /* cache capacity */ - size_t cur_weight; /* current total "weight" of all entries */ - - size_t hash_size; /* size of hash table */ - stx_cache_entry_t **table; /* hash table for this cache */ - - stx_clist_t lru_list; /* least-recently-used list */ - - /* Cache stats */ - unsigned long hits; /* num cache hits */ - unsigned long lookups; /* num cache lookups */ - unsigned long inserts; /* num inserts */ - unsigned long deletes; /* num deletes */ - - /* Functions */ - unsigned long (*key_hash_fn)(const void *); - long (*key_cmp_fn)(const void *, const void *); - void (*cleanup_fn)(void *, void *); -}; - - -#define STX_CACHE_ENTRY_PTR(_qp) \ - ((stx_cache_entry_t *)((char *)(_qp) - offsetof(stx_cache_entry_t, lru_link))) - - -/***************************************** - * Cache methods - */ - -stx_cache_t *stx_cache_create(size_t max_size, size_t max_weight, - size_t hash_size, - unsigned long (*key_hash_fn)(const void *key), - long (*key_cmp_fn)(const void *key1, - const void *key2), - void (*cleanup_fn)(void *key, void *data)) -{ - stx_cache_t *newcache; - - newcache = (stx_cache_t *)calloc(1, sizeof(stx_cache_t)); - if (newcache == NULL) - return NULL; - newcache->table = (stx_cache_entry_t **)calloc(hash_size, - sizeof(stx_cache_entry_t *)); - if (newcache->table == NULL) { - free(newcache); - return NULL; - } - - newcache->max_size = max_size; - newcache->max_weight = max_weight; - newcache->hash_size = hash_size; - STX_CLIST_INIT_CLIST(&(newcache->lru_list)); - newcache->key_hash_fn = key_hash_fn; - newcache->key_cmp_fn = key_cmp_fn; - newcache->cleanup_fn = cleanup_fn; - - return newcache; -} - - -void stx_cache_empty(stx_cache_t *cache) -{ - size_t i; - stx_cache_entry_t *entry, *next_entry; - - for (i = 0; i < cache->hash_size; i++) { - entry = cache->table[i]; - while (entry) { - next_entry = entry->next; - stx_cache_entry_delete(cache, entry); - entry = next_entry; - } - } -} - - -void stx_cache_traverse(stx_cache_t *cache, - void (*callback)(void *key, void *data)) -{ - size_t i; - stx_cache_entry_t *entry; - - for (i = 0; i < cache->hash_size; i++) { - for (entry = cache->table[i]; entry; entry = entry->next) { - if (!entry->delete_pending) - (*callback)(entry->key, entry->data); - } - } -} - - -void stx_cache_traverse_lru(stx_cache_t *cache, - void (*callback)(void *key, void *data), - unsigned int n) -{ - stx_clist_t *q; - stx_cache_entry_t *entry; - - for (q = STX_CLIST_HEAD(&cache->lru_list); q != &cache->lru_list && n; - q = q->next, n--) { - entry = STX_CACHE_ENTRY_PTR(q); - (*callback)(entry->key, entry->data); - } -} - - -void stx_cache_traverse_mru(stx_cache_t *cache, - void (*callback)(void *key, void *data), - unsigned int n) -{ - stx_clist_t *q; - stx_cache_entry_t *entry; - - for (q = STX_CLIST_TAIL(&cache->lru_list); q != &cache->lru_list && n; - q = q->prev, n--) { - entry = STX_CACHE_ENTRY_PTR(q); - (*callback)(entry->key, entry->data); - } -} - - -size_t stx_cache_getsize(stx_cache_t *cache) -{ - return cache->cur_size; -} - - -size_t stx_cache_getweight(stx_cache_t *cache) -{ - return cache->cur_weight; -} - - -void stx_cache_getinfo(stx_cache_t *cache, stx_cache_info_t *info) -{ - info->max_size = cache->max_size; - info->max_weight = cache->max_weight; - info->hash_size = cache->hash_size; - info->cur_size = cache->cur_size; - info->cur_weight = cache->cur_weight; - info->hits = cache->hits; - info->lookups = cache->lookups; - info->inserts = cache->inserts; - info->deletes = cache->deletes; -} - - -/***************************************** - * Cache entry methods - */ - -stx_cache_entry_t *stx_cache_entry_create(void *key, void *data, - size_t weight) -{ - stx_cache_entry_t *newentry; - - newentry = (stx_cache_entry_t *)calloc(1, sizeof(stx_cache_entry_t)); - if (newentry == NULL) - return NULL; - - newentry->key = key; - newentry->data = data; - newentry->weight = weight; - - return newentry; -} - - -void stx_cache_entry_delete(stx_cache_t *cache, stx_cache_entry_t *entry) -{ - entry->delete_pending = 1; - - if (entry->ref_count > 0) - return; - - if (entry->pthis) { - *entry->pthis = entry->next; - if (entry->next) - entry->next->pthis = entry->pthis; - - cache->cur_size--; - cache->cur_weight -= entry->weight; - cache->deletes++; - STX_CLIST_REMOVE_LINK(&(entry->lru_link)); - } - - if (cache->cleanup_fn) - cache->cleanup_fn(entry->key, entry->data); - - entry->pthis = NULL; - entry->key = NULL; - entry->data = NULL; - free(entry); -} - - -stx_cache_entry_t *stx_cache_entry_lookup(stx_cache_t *cache, const void *key) -{ - unsigned long bucket; - stx_cache_entry_t *entry; - - cache->lookups++; - bucket = cache->key_hash_fn(key) % cache->hash_size; - for (entry = cache->table[bucket]; entry; entry = entry->next) { - if (!entry->delete_pending && cache->key_cmp_fn(key, entry->key) == 0) - break; - } - if (entry) { - cache->hits++; - if (entry->ref_count == 0) - STX_CLIST_REMOVE_LINK(&(entry->lru_link)); - entry->ref_count++; - } - - return entry; -} - - -void stx_cache_entry_release(stx_cache_t *cache, stx_cache_entry_t *entry) -{ - if (entry->ref_count == 0) - return; - - entry->ref_count--; - - if (entry->ref_count == 0) { - STX_CLIST_APPEND_LINK(&(entry->lru_link), &(cache->lru_list)); - if (entry->delete_pending) - stx_cache_entry_delete(cache, entry); - } -} - - -int stx_cache_entry_insert(stx_cache_t *cache, stx_cache_entry_t *entry) -{ - stx_cache_entry_t *old_entry; - unsigned long bucket; - - /* - * If cache capacity is exceeded, try to remove LRU entries till there is - * enough room or LRU list is empty. - */ - while (cache->cur_weight + entry->weight > cache->max_weight) { - old_entry = stx_cache_entry_getlru(cache); - if (!old_entry) { - /* cache capacity is exceeded and all entries are in use */ - return -1; - } - stx_cache_entry_delete(cache, old_entry); - } - - /* If cache size is exceeded, remove LRU entry */ - if (cache->cur_size >= cache->max_size) { - old_entry = stx_cache_entry_getlru(cache); - if (!old_entry) { - /* cache size is exceeded and all entries are in use */ - return -1; - } - stx_cache_entry_delete(cache, old_entry); - } - - /* Don't add duplicate entries in the cache */ - bucket = cache->key_hash_fn(entry->key) % cache->hash_size; - for (old_entry = cache->table[bucket]; old_entry; - old_entry = old_entry->next) { - if (!old_entry->delete_pending && - cache->key_cmp_fn(entry->key, old_entry->key) == 0) - break; - } - if (old_entry) - stx_cache_entry_delete(cache, old_entry); - - /* Insert in the hash table */ - entry->next = cache->table[bucket]; - cache->table[bucket] = entry; - entry->pthis = &cache->table[bucket]; - if (entry->next) - entry->next->pthis = &entry->next; - entry->ref_count++; - - cache->inserts++; - cache->cur_size++; - cache->cur_weight += entry->weight; - - return 0; -} - - -stx_cache_entry_t *stx_cache_entry_getlru(stx_cache_t *cache) -{ - if (STX_CLIST_IS_EMPTY(&(cache->lru_list))) - return NULL; - - return STX_CACHE_ENTRY_PTR(STX_CLIST_HEAD(&(cache->lru_list))); -} - - -int stx_cache_entry_sizeof(void) -{ - return (int)sizeof(stx_cache_entry_t); -} - - -void *stx_cache_entry_getdata(stx_cache_entry_t *entry) -{ - return entry->data; -} - - -void *stx_cache_entry_getkey(stx_cache_entry_t *entry) -{ - return entry->key; -} - - -size_t stx_cache_entry_getweight(stx_cache_entry_t *entry) -{ - return entry->weight; -} - diff --git a/trunk/3rdparty/st-srs/extensions/print_stk.patch b/trunk/3rdparty/st-srs/extensions/print_stk.patch deleted file mode 100644 index f7451c7b07..0000000000 --- a/trunk/3rdparty/st-srs/extensions/print_stk.patch +++ /dev/null @@ -1,367 +0,0 @@ -Michael Abd-El-Malek contributed this patch. He wrote: ----------------------------------------- -Hello, - -This is a patch that enables programmatically dumping the stack of -every thread. This has been useful in debugging deadlocks, etc... -Our usage model is that the SIGUSR2 handler calls the new -_st_print_thread_stacks function, which dumps the stack for all -threads. A convenient feature is that for thread stacks that are the -same (which is common for application with a lot of worker threads -waiting for work), only one stack trace is printed, along with a -count of how many threads have that same stack. - -I use the glibc backtrace function to get the backtrace, and then use -popen to execute addr2line and convert memory addresses to file -names, function names, and line numbers. If glibc isn't available, -_st_print_thread_stacks just prints a warning. And this feature is -only available if DEBUG is turned on. - -We've found this feature extremely helpful when debugging. - -The patch can be a bit more robust (it assumes addr2line exists). -But I didn't want to go through the hassle of doing this, if the -StateThreads community doesn't want to use this patch. (In our -environment, addr2line will always be there.) - -Cheers, -Mike ----------------------------------------- -Invoking complex functions from a signal handler is not recommended, -plus this patch changes the behavior of existing API hooks. It will -not become part of State Threads proper but you may find it useful -nonetheless. This patch applies to st-1.5.2. - -diff -Nur Makefile.1.5.2 Makefile ---- Makefile.1.5.2 Wed Sep 7 14:19:50 2005 -+++ Makefile Wed Sep 7 14:33:08 2005 -@@ -255,7 +255,8 @@ - $(TARGETDIR)/stk.o \ - $(TARGETDIR)/sync.o \ - $(TARGETDIR)/key.o \ -- $(TARGETDIR)/io.o -+ $(TARGETDIR)/io.o \ -+ $(TARGETDIR)/backtrace.o - OBJS += $(EXTRA_OBJS) - HEADER = $(TARGETDIR)/st.h - SLIBRARY = $(TARGETDIR)/libst.a -diff -Nur backtrace.c.1.5.2 backtrace.c ---- backtrace.c.1.5.2 Wed Dec 31 16:00:00 1969 -+++ backtrace.c Wed Sep 7 13:40:21 2005 -@@ -0,0 +1,211 @@ -+/* -+ * 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. -+ * -+ * Contributor(s): Michael Abd-El-Malek (mabdelmalek@cmu.edu) -+ * Carnegie Mellon University -+ * -+ * 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 contains routines for printing a stack trace of all threads. -+ * Only works when DEBUG is defined and where glibc is available, since it -+ * provides the backtrace() function. -+ */ -+ -+#define _GNU_SOURCE /* to get program_invocation_name */ -+ -+#include -+#include -+ -+ -+#if defined(DEBUG) && defined(__GLIBC__) -+ -+#include -+#include "common.h" -+#include -+#include -+#include -+ -+ -+/* The maximum number of frames to get a stack trace for. If a thread has more -+ * frames than this, then we only show the latest X frames. */ -+#define MAX_NUM_FRAMES 64 -+ -+ -+typedef struct thread_stack_s { -+ uint32_t num_frames; -+ void* addresses[MAX_NUM_FRAMES]; /* frame pointers */ -+ char* locations[MAX_NUM_FRAMES]; /* file/function/line numbers */ -+ uint32_t num_matches; -+ -+ struct thread_stack_s* next; -+} thread_stack_t; -+ -+static thread_stack_t* stacks = NULL; -+ -+ -+/* Converts the function's memory addresses to function names, file names, and -+ * line numbers. Calls binutil's addr2line program. */ -+static void get_symbol_names(thread_stack_t *stack) -+{ -+ char program_to_run[1024], function[256], filename_lineno[256], temp[19]; -+ FILE* output; -+ int num_bytes_left; -+ uint32_t i; -+ -+ /* Construct the arguments to addr2line */ -+ num_bytes_left = sizeof(program_to_run); -+ num_bytes_left -= snprintf(program_to_run, sizeof(program_to_run), -+ "addr2line -fCe %s", program_invocation_name); -+ for (i = 0; i < stack->num_frames && num_bytes_left > 0; ++i) { -+ num_bytes_left -= snprintf(temp, sizeof(temp), " %p", stack->addresses[i]); -+ strncat(program_to_run, temp, num_bytes_left); -+ } -+ -+ /* Use popen to execute addr2line and read its ouput */ -+ output = popen(program_to_run, "r"); -+ for (i = 0; i < stack->num_frames; ++i) { -+ char* function_listing = (char*) malloc(512); -+ fscanf(output, "%255s\n", function); -+ fscanf(output, "%255s\n", filename_lineno); -+ snprintf(function_listing, 512, "%s at %s", function, filename_lineno); -+ stack->locations[i] = function_listing; -+ } -+ pclose(output); -+} -+ -+ -+static void print_stack(thread_stack_t* stack) -+{ -+ int skip_offset = 0, cmp_len; -+ uint32_t i; -+ -+ /* Get the function names/filenames/line numbers */ -+ get_symbol_names(stack); -+ -+ cmp_len = strlen("_st_iterate_threads_helper"); -+ -+ /* Print the backtrace */ -+ for (i = 0; i < stack->num_frames; ++i) { -+ /* Skip frames we don't have location info for */ -+ if (!strncmp(stack->locations[i], "??", 2)) { -+ continue; -+ } -+ -+ /* Skip the frames that are used for printing the stack trace */ -+ if (skip_offset) { -+ printf("\t#%2d %s %p\n", i - skip_offset, stack->locations[i], -+ stack->addresses[i]); -+ } else if (!strncmp(stack->locations[i], "_st_iterate_threads_helper", -+ cmp_len)) { -+ skip_offset = i + 1; -+ } -+ } -+} -+ -+ -+static void add_current_thread_stack(void) -+{ -+ thread_stack_t *new_stack = malloc(sizeof(thread_stack_t)); -+ thread_stack_t *search; -+ -+ /* Call glibc function to get the backtrace */ -+ new_stack->num_frames = backtrace(new_stack->addresses, MAX_NUM_FRAMES); -+ -+ /* Check if we have another stacks that is equivalent. If so, then coaelsce -+ * two stacks into one, to minimize output to user. */ -+ search = stacks; -+ while (search) { -+ if (search->num_frames == new_stack->num_frames && -+ !memcmp(search->addresses, new_stack->addresses, -+ search->num_frames * sizeof(void*))) { -+ /* Found an existing stack that is the same as this thread's stack */ -+ ++search->num_matches; -+ free(new_stack); -+ return; -+ } else { -+ search = search->next; -+ } -+ } -+ -+ /* This is a new stack. Add it to the list of stacks. */ -+ new_stack->num_matches = 1; -+ new_stack->next = stacks; -+ stacks = new_stack; -+} -+ -+static void print_stack_frames(void) -+{ -+ while (stacks) { -+ printf("\n%u thread(s) with this backtrace:\n", stacks->num_matches); -+ print_stack(stacks); -+ stacks = stacks->next; -+ } -+ printf("\n"); -+} -+ -+static void free_stacks(void) -+{ -+ uint32_t i; -+ while (stacks) { -+ thread_stack_t *next = stacks->next; -+ for (i = 0; i < stacks->num_frames; ++i) { -+ free(stacks->locations[i]); -+ } -+ free(stacks); -+ stacks = next; -+ } -+ stacks = NULL; -+} -+ -+ -+static void st_print_thread_stack(_st_thread_t *thread, int start_flag, -+ int end_flag) -+{ -+ if (end_flag == 0) { -+ add_current_thread_stack(); -+ } else { -+ print_stack_frames(); -+ } -+} -+ -+ -+void _st_print_thread_stacks(int ignore) -+{ -+ _st_iterate_threads_flag = 1; -+ _st_iterate_threads_helper(st_print_thread_stack); -+ _st_iterate_threads_flag = 0; -+ -+ /* Deallocate memory */ -+ free_stacks(); -+} -+ -+#else /* defined(DEBUG) && defined(__GLIBC__) */ -+ -+void _st_print_thread_stacks(int ignore) -+{ -+ printf("%s: need DEBUG mode and glibc-specific functions to read stack.\n", -+ __FUNCTION__); -+} -+#endif /* defined(DEBUG) && defined(__GLIBC__) */ -diff -Nur common.h.1.5.2 common.h ---- common.h.1.5.2 Wed Sep 7 14:18:37 2005 -+++ common.h Wed Sep 7 14:35:36 2005 -@@ -371,8 +371,18 @@ - */ - - #ifdef DEBUG --void _st_iterate_threads(void); --#define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads() -+typedef void(*_st_func_ptr_t)(_st_thread_t *thread, -+ int start_flag, -+ int end_flag); -+/* Pointer to function that will be called on thread switch */ -+extern _st_func_ptr_t _st_iterate_func_ptr; -+extern int _st_iterate_threads_flag; -+/* Thread iteration function that will call an arbitrary function */ -+extern void _st_iterate_threads_helper(_st_func_ptr_t func); -+#define ST_DEBUG_ITERATE_THREADS() \ -+ if (_st_iterate_func_ptr) { \ -+ _st_iterate_threads_helper(_st_iterate_func_ptr); \ -+ } - #else - #define ST_DEBUG_ITERATE_THREADS() - #endif -diff -Nur public.h.1.5.2 public.h ---- public.h.1.5.2 Wed Sep 7 11:46:58 2005 -+++ public.h Wed Sep 7 13:38:46 2005 -@@ -171,8 +171,10 @@ - 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_show_thread_stack(st_thread_t thread, int start_flag, -+ int end_flag); - extern void _st_iterate_threads(void); -+extern void _st_print_thread_stacks(int ignore); - #endif - - #ifdef __cplusplus -diff -Nur sched.c.1.5.2 sched.c ---- sched.c.1.5.2 Wed Sep 7 10:48:05 2005 -+++ sched.c Wed Sep 7 13:38:46 2005 -@@ -919,16 +919,13 @@ - - - #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; -+/* Thread iteration function that will call an arbitrary function */ -+_st_func_ptr_t _st_iterate_func_ptr = NULL; - --void _st_iterate_threads(void) -+/* This function iterates over all threads, calling "func" for each thread. */ -+void _st_iterate_threads_helper(_st_func_ptr_t func) - { - static _st_thread_t *thread = NULL; - static jmp_buf orig_jb, save_jb; -@@ -944,16 +941,20 @@ - - if (thread) { - memcpy(thread->context, save_jb, sizeof(jmp_buf)); -- _st_show_thread_stack(thread, NULL); -+ func(thread, 0, 0); - } else { - if (MD_SETJMP(orig_jb)) { - _st_iterate_threads_flag = 0; -+ _st_iterate_func_ptr = NULL; - thread = NULL; -- _st_show_thread_stack(thread, "Iteration completed"); -+ /* Last thread to iterate through */ -+ func(thread, 0, 1); - return; - } -+ /* First thread to iterate through */ - thread = _ST_CURRENT_THREAD(); -- _st_show_thread_stack(thread, "Iteration started"); -+ _st_iterate_func_ptr = func; -+ func(thread, 1, 0); - } - - q = thread->tlink.next; -@@ -966,5 +967,17 @@ - memcpy(save_jb, thread->context, sizeof(jmp_buf)); - MD_LONGJMP(thread->context, 1); - } -+ -+/* ARGSUSED */ -+void _st_show_thread_stack(_st_thread_t *thread, int start_flag, int end_flag) -+{ -+} -+ -+/* Iterate over threads inside debugger; see st/README */ -+void _st_iterate_threads(void) -+{ -+ _st_iterate_threads_helper(_st_show_thread_stack); -+} -+ - #endif /* DEBUG */ - diff --git a/trunk/3rdparty/st-srs/extensions/stx.h b/trunk/3rdparty/st-srs/extensions/stx.h deleted file mode 100644 index 8371e0d93c..0000000000 --- a/trunk/3rdparty/st-srs/extensions/stx.h +++ /dev/null @@ -1,91 +0,0 @@ -#ifndef _STX_H_ -#define _STX_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "st.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -/***************************************** - * Basic types definitions - */ - -typedef struct _stx_centry stx_cache_entry_t; -typedef struct _stx_cache stx_cache_t; - -/* This is public type */ -typedef struct _stx_cache_info { - size_t max_size; - size_t max_weight; - size_t hash_size; - size_t cur_size; - size_t cur_weight; - unsigned long hits; - unsigned long lookups; - unsigned long inserts; - unsigned long deletes; -} stx_cache_info_t; - - -/***************************************** - * Cache and cache entry methods - */ - -stx_cache_t *stx_cache_create(size_t max_size, size_t max_weight, - size_t hash_size, - unsigned long (*key_hash_fn)(const void *key), - long (*key_cmp_fn)(const void *key1, - const void *key2), - void (*cleanup_fn)(void *key, void *data)); -void stx_cache_empty(stx_cache_t *cache); -void stx_cache_traverse(stx_cache_t *cache, - void (*callback)(void *key, void *data)); -void stx_cache_traverse_lru(stx_cache_t *, void (*)(void *, void *), - unsigned int); -void stx_cache_traverse_mru(stx_cache_t *, void (*)(void *, void *), - unsigned int); -void stx_cache_getinfo(stx_cache_t *cache, stx_cache_info_t *info); -size_t stx_cache_getsize(stx_cache_t *cache); -size_t stx_cache_getweight(stx_cache_t *cache); - - -stx_cache_entry_t *stx_cache_entry_create(void *key, void *data, - size_t weight); -void stx_cache_entry_delete(stx_cache_t *cache, stx_cache_entry_t *entry); -stx_cache_entry_t *stx_cache_entry_lookup(stx_cache_t *cache, const void *key); -void stx_cache_entry_release(stx_cache_t *, stx_cache_entry_t *); -int stx_cache_entry_insert(stx_cache_t *cache, stx_cache_entry_t *entry); -stx_cache_entry_t *stx_cache_entry_getlru(stx_cache_t *cache); -int stx_cache_entry_sizeof(void); -void *stx_cache_entry_getdata(stx_cache_entry_t *entry); -void *stx_cache_entry_getkey(stx_cache_entry_t *entry); -size_t stx_cache_entry_getweight(stx_cache_entry_t *entry); - - -int stx_dns_cache_init(size_t max_size, size_t max_bytes, size_t hash_size); -void stx_dns_cache_getinfo(stx_cache_info_t *info); -int stx_dns_getaddrlist(const char *hostname, struct in_addr *addrs, - int *num_addrs, st_utime_t timeout); -int stx_dns_getaddr(const char *hostname, struct in_addr *addr, - st_utime_t timeout); - -#ifdef __cplusplus -} -#endif - -#endif /* !_STX_H_ */ - diff --git a/trunk/3rdparty/st-srs/extensions/stx_fileio.c b/trunk/3rdparty/st-srs/extensions/stx_fileio.c deleted file mode 100644 index cb24346e8a..0000000000 --- a/trunk/3rdparty/st-srs/extensions/stx_fileio.c +++ /dev/null @@ -1,197 +0,0 @@ -/* - * File I/O extension to the State Threads Library. - */ - -/* - * 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 file I/O extension to the State Threads Library. - * - * The Initial Developer of the Original Code is Jeff - * . Portions created by the Initial - * Developer are Copyright (C) 2002 the Initial Developer. All Rights - * Reserved. - * - * Contributor(s): (none) - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -#include - -#include "stx_fileio.h" - -#define STX_FILEIO_SIGNUM SIGUSR2 - -typedef struct { - st_netfd_t data_fd; - st_netfd_t control_fd; - pid_t pid; -} fileio_data_t; - -#define FILEREADER_MAX_READ 1024 - -typedef struct { - off_t offset; - ssize_t nbytes; -} file_reader_cb_t; - -/** - * Fork a process to read a file and return its pid. Receives - * offset/length commands from control stream and sends corresponding data - * to out stream. A zero length on the control stream signals an end. - * - * @param fd stream from which to read - * @param control_out receives the file descriptor to which control commands can be sent - * @param fd_out receives the file descriptor from which the output of the command can be read. - * @return PID of the process created to execute the command - */ -pid_t -file_reader(int fd, int *fd_control, int *fd_out) -{ - pid_t pid; - int control_pipe[2], out_pipe[2]; - - if (pipe(control_pipe) < 0 || pipe(out_pipe) < 0) - return (pid_t)-1; - - pid = fork(); - if (pid == (pid_t) -1) - { - close(control_pipe[0]); - close(control_pipe[1]); - close(out_pipe[0]); - close(out_pipe[1]); - return pid; - } - else if (pid == (pid_t) 0) - { - // child - off_t pos = 0; - file_reader_cb_t cb; - char buf[FILEREADER_MAX_READ]; - if (fd == -1) - _exit(EXIT_FAILURE); - - while (sizeof(cb) == read(control_pipe[0], &cb, sizeof(cb))) { - ssize_t nb; - if (0 >= cb.nbytes) - goto clean_exit; - if (pos != cb.offset) { - pos = lseek(fd, cb.offset, SEEK_SET); - if (pos == (off_t)-1) - break; - } - nb = read(fd, buf, cb.nbytes); - if (nb == (ssize_t)-1) - break; - pos += nb; - write(out_pipe[1], (char *)&nb, sizeof(nb)); - write(out_pipe[1], buf, nb); - } - perror("ERROR: file_reader: "); - clean_exit: - close(control_pipe[0]); - close(control_pipe[1]); - close(out_pipe[0]); - close(out_pipe[1]); - _exit(EXIT_SUCCESS); - } - - // parent - close(out_pipe[1]); - close(control_pipe[0]); - *fd_out = out_pipe[0]; - *fd_control = control_pipe[1]; - return pid; -} - -/** - * fileio_data_t destructor callback - */ -static void -fileio_data_destructor(void *dat_in) -{ - if (dat_in) { - fileio_data_t *dat = (fileio_data_t *)dat_in; - file_reader_cb_t cb; - cb.offset = 0; - cb.nbytes = 0; - st_write(dat->control_fd, (char *)&cb, sizeof(cb), - ST_UTIME_NO_TIMEOUT); - waitpid(dat->pid, NULL, 0); - st_netfd_close(dat->control_fd); - st_netfd_close(dat->data_fd); - free(dat_in); - } -} - -/** - * Retrieve fileio_data_t struct from an st descriptor. Create and store - * a new one if needed. - */ -static fileio_data_t *get_fileio_data(st_netfd_t fd) -{ - fileio_data_t *dat = (fileio_data_t *)st_netfd_getspecific(fd); - if (!dat) { - int fd_control, fd_out; - pid_t pid = file_reader(st_netfd_fileno(fd), &fd_control, &fd_out); - if (pid != (pid_t)-1) { - dat = (fileio_data_t *)calloc(1, sizeof(fileio_data_t)); - dat->control_fd = st_netfd_open(fd_control); - dat->data_fd = st_netfd_open(fd_out); - dat->pid = pid; - st_netfd_setspecific(fd, dat, fileio_data_destructor); - } - } - return dat; -} - -/** - * Read data from the specified section of a file. Uses a forked - * file_reader process to do the actual reading so as to avoid causing all - * State Threads to block. - * - * @param fd must refer to a seekable file. - * @param offset absolute offset within the file - * @param buf output buffer - * @param nbytes size of the output buffer - * @param timeout - */ -ssize_t -stx_file_read(st_netfd_t fd, off_t offset, void *buf, size_t nbytes, st_utime_t timeout) -{ - fileio_data_t *dat = get_fileio_data(fd); - if (dat) { - file_reader_cb_t cb; - ssize_t ret = (ssize_t)-1; - cb.offset = offset; - cb.nbytes = nbytes; - st_write(dat->control_fd, (char *)&cb, sizeof(cb), timeout); - if (sizeof(ret) == st_read(dat->data_fd, (char *)&ret, sizeof(ret), timeout) && 0 < ret && ret <= nbytes) { - return st_read(dat->data_fd, buf, ret, timeout); - } else { - return ret; - } - } - - return (ssize_t)-1; -} diff --git a/trunk/3rdparty/st-srs/extensions/stx_fileio.h b/trunk/3rdparty/st-srs/extensions/stx_fileio.h deleted file mode 100644 index b6bec190b7..0000000000 --- a/trunk/3rdparty/st-srs/extensions/stx_fileio.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * File I/O extension to the State Threads Library. - */ - -/* - * 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 file I/O extension to the State Threads Library. - * - * The Initial Developer of the Original Code is Jeff - * . Portions created by the Initial - * Developer are Copyright (C) 2002 the Initial Developer. All Rights - * Reserved. - * - * Contributor(s): (none) - * - * 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 __STX_FILEIO_H__ -#define __STX_FILEIO_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -extern ssize_t stx_file_read(st_netfd_t fd, off_t offset, void *buf, size_t nbytes, st_utime_t timeout); - -#ifdef __cplusplus -} -#endif -#endif /* !__STX_FILEIO_H__ */ diff --git a/trunk/3rdparty/st-srs/extensions/testdns.c b/trunk/3rdparty/st-srs/extensions/testdns.c deleted file mode 100644 index aa896b25e5..0000000000 --- a/trunk/3rdparty/st-srs/extensions/testdns.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "stx.h" -#include -#include - - -#define MAX_ADDRS 128 -#define TIMEOUT (4*1000000LL) - -static void do_resolve(const char *host) -{ - struct in_addr addrs[MAX_ADDRS]; - int i, n = MAX_ADDRS; - - if (stx_dns_getaddrlist(host, addrs, &n, TIMEOUT) < 0) { - fprintf(stderr, "stx_dns_getaddrlist: can't resolve %s: ", host); - if (h_errno == NETDB_INTERNAL) - perror(""); - else - herror(""); - } else { - if (n > 0) - printf("%-40s %s\n", (char *)host, inet_ntoa(addrs[0])); - for (i = 1; i < n; i++) - printf("%-40s %s\n", "", inet_ntoa(addrs[i])); - } -} - -static void show_info(void) -{ - stx_cache_info_t info; - - stx_dns_cache_getinfo(&info); - printf("DNS cache info:\n\n"); - printf("max_size: %8d\n", (int)info.max_size); - printf("capacity: %8d bytes\n", (int)info.max_weight); - printf("hash_size: %8d\n", (int)info.hash_size); - printf("cur_size: %8d\n" - "cur_mem: %8d bytes\n" - "hits: %8d\n" - "lookups: %8d\n" - "inserts: %8d\n" - "deletes: %8d\n", - (int)info.cur_size, (int)info.cur_weight, (int)info.hits, - (int)info.lookups, (int)info.inserts, (int)info.deletes); -} - -extern stx_cache_t *_stx_dns_cache; - -static void printhost(void *host, void *data) -{ - printf("%s\n", (char *)host); -} - -static void show_lru(void) -{ - printf("LRU hosts:\n\n"); - stx_cache_traverse_lru(_stx_dns_cache, printhost, 10); -} - -static void show_mru(void) -{ - printf("MRU hosts:\n\n"); - stx_cache_traverse_mru(_stx_dns_cache, printhost, 10); -} - -static void flush_cache(void) -{ - stx_cache_empty(_stx_dns_cache); - printf("DNS cache is empty\n"); -} - - -int main() -{ - char line[256]; - char str[sizeof(line)]; - - st_init(); - stx_dns_cache_init(100, 10000, 101); - - for ( ; ; ) { - fputs("> ", stdout); - fflush(stdout); - if (!fgets(line, sizeof(line), stdin)) - break; - if (sscanf(line, "%s", str) != 1) - continue; - if (strcmp(str, "exit") == 0 || strcmp(str, "quit") == 0) - break; - if (strcmp(str, "info") == 0) { - show_info(); - continue; - } - if (strcmp(str, "lru") == 0) { - show_lru(); - continue; - } - if (strcmp(str, "mru") == 0) { - show_mru(); - continue; - } - if (strcmp(str, "flush") == 0) { - flush_cache(); - continue; - } - - do_resolve(str); - } - - return 0; -} - diff --git a/trunk/3rdparty/st-srs/io.c b/trunk/3rdparty/st-srs/io.c deleted file mode 100644 index 912de0b286..0000000000 --- a/trunk/3rdparty/st-srs/io.c +++ /dev/null @@ -1,769 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - - -#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/winlinvip/simple-rtmp-server/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; -} - - -#ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT -/* 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; -} - -#else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ -/* - * On some platforms accept() calls from different processes - * on the same listen socket must be serialized. - * The following code serializes accept()'s without process blocking. - * A pipe is used as an inter-process semaphore. - */ -int st_netfd_serialize_accept(_st_netfd_t *fd) -{ - _st_netfd_t **p; - int osfd[2], err; - - if (fd->aux_data) { - errno = EINVAL; - return -1; - } - if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL) - return -1; - if (pipe(osfd) < 0) { - free(p); - return -1; - } - if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) { - fd->aux_data = p; - return 0; - } - /* Error */ - err = errno; - if (p[0]) - st_netfd_free(p[0]); - if (p[1]) - st_netfd_free(p[1]); - close(osfd[0]); - close(osfd[1]); - free(p); - errno = err; - - return -1; -} - -static void _st_netfd_free_aux_data(_st_netfd_t *fd) -{ - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data; - - st_netfd_close(p[0]); - st_netfd_close(p[1]); - free(p); - 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; - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data; - ssize_t n; - char c; - - for ( ; ; ) { - if (p == NULL) { - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen); - } else { - /* Get the lock */ - n = st_read(p[0], &c, 1, timeout); - if (n < 0) - return NULL; - ST_ASSERT(n == 1); - /* Got the lock */ - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen); - /* Unlock */ - err = errno; - n = st_write(p[1], &c, 1, timeout); - ST_ASSERT(n == 1); - errno = err; - } - if (osfd >= 0) - break; - 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; -} -#endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ - - -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; - - while ((n = read(fd->osfd, buf, nbyte)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* 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; - - while ((n = readv(fd->osfd, iov, iov_size)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* 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; - - 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; - } - } - /* 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; - - 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; - } - /* 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; - - while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* 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; - - while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* 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; - - while ((n = recvmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* 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; - - while ((n = sendmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) - continue; - if (!_IO_NOT_READY_ERROR) - return -1; - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) - return -1; - } - - return n; -} - - - -/* - * To open FIFOs or other special files. - */ -_st_netfd_t *st_open(const char *path, int oflags, mode_t mode) -{ - int osfd, err; - _st_netfd_t *newfd; - - while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) { - if (errno != EINTR) - return NULL; - } - - newfd = _st_netfd_new(osfd, 0, 0); - if (!newfd) { - err = errno; - close(osfd); - errno = err; - } - - return newfd; -} - diff --git a/trunk/3rdparty/st-srs/key.c b/trunk/3rdparty/st-srs/key.c deleted file mode 100644 index 5e64022c04..0000000000 --- a/trunk/3rdparty/st-srs/key.c +++ /dev/null @@ -1,121 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include "common.h" - - -/* - * Destructor table for per-thread private data - */ -static _st_destructor_t _st_destructors[ST_KEYS_MAX]; -static int key_max = 0; - - -/* - * Return a key to be used for thread specific data - */ -int st_key_create(int *keyp, _st_destructor_t destructor) -{ - if (key_max >= ST_KEYS_MAX) { - errno = EAGAIN; - return -1; - } - - *keyp = key_max++; - _st_destructors[*keyp] = destructor; - - return 0; -} - - -int st_key_getlimit(void) -{ - return ST_KEYS_MAX; -} - - -int st_thread_setspecific(int key, void *value) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (key < 0 || key >= key_max) { - errno = EINVAL; - return -1; - } - - if (value != me->private_data[key]) { - /* free up previously set non-NULL data value */ - if (me->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(me->private_data[key]); - } - me->private_data[key] = value; - } - - return 0; -} - - -void *st_thread_getspecific(int key) -{ - if (key < 0 || key >= key_max) - return NULL; - - return ((_ST_CURRENT_THREAD())->private_data[key]); -} - - -/* - * Free up all per-thread private data - */ -void _st_thread_cleanup(_st_thread_t *thread) -{ - int key; - - for (key = 0; key < key_max; key++) { - if (thread->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(thread->private_data[key]); - thread->private_data[key] = NULL; - } - } -} - diff --git a/trunk/3rdparty/st-srs/libst.def b/trunk/3rdparty/st-srs/libst.def deleted file mode 100644 index 6eaf149a97..0000000000 --- a/trunk/3rdparty/st-srs/libst.def +++ /dev/null @@ -1,51 +0,0 @@ -EXPORTS - st_accept @62 - st_cond_broadcast @63 - st_cond_destroy @64 - st_cond_new @65 - st_cond_signal @66 - st_cond_timedwait @67 - st_cond_wait @68 - st_connect @69 - st_getfdlimit @70 - st_init @71 - st_key_create @72 - st_key_getlimit @73 - st_mutex_destroy @74 - st_mutex_lock @75 - st_mutex_new @76 - st_mutex_trylock @77 - st_mutex_unlock @78 - st_netfd_close @79 - st_netfd_fileno @80 - st_netfd_free @81 - st_netfd_getspecific @82 - st_netfd_open @83 - st_netfd_open_socket @84 - st_netfd_poll @85 - st_netfd_serialize_accept @86 - st_netfd_setspecific @87 - st_open @88 - st_poll @89 - st_randomize_stacks @90 - st_read @91 - st_read_fully @92 - st_read_resid @93 - st_recvfrom @94 - st_sendto @95 - st_sleep @96 - st_thread_create @97 - st_thread_exit @98 - st_thread_getspecific @99 - st_thread_interrupt @100 - st_thread_join @101 - st_thread_self @102 - st_thread_setspecific @103 - st_time @104 - st_timecache_set @105 - st_usleep @106 - st_utime @107 - st_utime_last_clock @108 - st_write @109 - st_write_resid @110 - st_writev @111 diff --git a/trunk/3rdparty/st-srs/md.S b/trunk/3rdparty/st-srs/md.S deleted file mode 100644 index 2ef9c41f75..0000000000 --- a/trunk/3rdparty/st-srs/md.S +++ /dev/null @@ -1,644 +0,0 @@ - -/* 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(__ia64__) - - /****************************************************************/ - - /* - * The internal __jmp_buf layout is different from one used - * by setjmp()/longjmp(). - * - * Offset Description - * ------ ----------- - * 0x000 stack pointer (r12) - * 0x008 gp (r1) - * 0x010 caller's unat - * 0x018 fpsr - * 0x020 r4 - * 0x028 r5 - * 0x030 r6 - * 0x038 r7 - * 0x040 rp (b0) - * 0x048 b1 - * 0x050 b2 - * 0x058 b3 - * 0x060 b4 - * 0x068 b5 - * 0x070 ar.pfs - * 0x078 ar.lc - * 0x080 pr - * 0x088 ar.bsp - * 0x090 ar.unat - * 0x098 &__jmp_buf - * 0x0a0 ar.rsc - * 0x0a8 ar.rnat - * 0x0b0 f2 - * 0x0c0 f3 - * 0x0d0 f4 - * 0x0e0 f5 - * 0x0f0 f16 - * 0x100 f17 - * 0x110 f18 - * 0x120 f19 - * 0x130 f20 - * 0x130 f21 - * 0x140 f22 - * 0x150 f23 - * 0x160 f24 - * 0x170 f25 - * 0x180 f26 - * 0x190 f27 - * 0x1a0 f28 - * 0x1b0 f29 - * 0x1c0 f30 - * 0x1d0 f31 - * - * Note that the address of __jmp_buf is saved but not used: we assume - * that the jmp_buf data structure is never moved around in memory. - */ - - /* - * Implemented according to "IA-64 Software Conventions and Runtime - * Architecture Guide", Chapter 10: "Context Management". - */ - - .text - .psr abi64 - .psr lsb - .lsb - - /* _st_md_cxt_save(__jmp_buf env) */ - .align 32 - .global _st_md_cxt_save - .proc _st_md_cxt_save - _st_md_cxt_save: - alloc r14 = ar.pfs,1,0,0,0 - mov r16 = ar.unat - ;; - mov r17 = ar.fpsr - mov r2 = in0 - add r3 = 8,in0 - ;; - st8.spill.nta [r2] = sp,16 // r12 (sp) - ;; - st8.spill.nta [r3] = gp,16 // r1 (gp) - ;; - st8.nta [r2] = r16,16 // save caller's unat - st8.nta [r3] = r17,16 // save fpsr - add r8 = 0xb0,in0 - ;; - st8.spill.nta [r2] = r4,16 // r4 - ;; - st8.spill.nta [r3] = r5,16 // r5 - add r9 = 0xc0,in0 - ;; - stf.spill.nta [r8] = f2,32 - stf.spill.nta [r9] = f3,32 - mov r15 = rp - ;; - stf.spill.nta [r8] = f4,32 - stf.spill.nta [r9] = f5,32 - mov r17 = b1 - ;; - stf.spill.nta [r8] = f16,32 - stf.spill.nta [r9] = f17,32 - mov r18 = b2 - ;; - stf.spill.nta [r8] = f18,32 - stf.spill.nta [r9] = f19,32 - mov r19 = b3 - ;; - stf.spill.nta [r8] = f20,32 - stf.spill.nta [r9] = f21,32 - mov r20 = b4 - ;; - stf.spill.nta [r8] = f22,32 - stf.spill.nta [r9] = f23,32 - mov r21 = b5 - ;; - stf.spill.nta [r8] = f24,32 - stf.spill.nta [r9] = f25,32 - mov r22 = ar.lc - ;; - stf.spill.nta [r8] = f26,32 - stf.spill.nta [r9] = f27,32 - mov r24 = pr - ;; - stf.spill.nta [r8] = f28,32 - stf.spill.nta [r9] = f29,32 - ;; - stf.spill.nta [r8] = f30 - stf.spill.nta [r9] = f31 - - st8.spill.nta [r2] = r6,16 // r6 - ;; - st8.spill.nta [r3] = r7,16 // r7 - ;; - mov r23 = ar.bsp - mov r25 = ar.unat - - st8.nta [r2] = r15,16 // b0 - st8.nta [r3] = r17,16 // b1 - ;; - st8.nta [r2] = r18,16 // b2 - st8.nta [r3] = r19,16 // b3 - mov r26 = ar.rsc - ;; - st8.nta [r2] = r20,16 // b4 - st8.nta [r3] = r21,16 // b5 - ;; - st8.nta [r2] = r14,16 // ar.pfs - st8.nta [r3] = r22,16 // ar.lc - ;; - st8.nta [r2] = r24,16 // pr - st8.nta [r3] = r23,16 // ar.bsp - ;; - st8.nta [r2] = r25,16 // ar.unat - st8.nta [r3] = in0,16 // &__jmp_buf (just in case) - ;; - st8.nta [r2] = r26 // ar.rsc - ;; - flushrs // flush dirty regs to backing store - ;; - and r27 = ~0x3,r26 // clear ar.rsc.mode - ;; - mov ar.rsc = r27 // put RSE in enforced lazy mode - ;; - mov r28 = ar.rnat - ;; - st8.nta [r3] = r28 // ar.rnat - mov ar.rsc = r26 // restore ar.rsc - ;; - mov r8 = 0 - br.ret.sptk.few b0 - .endp _st_md_cxt_save - - - /****************************************************************/ - - /* _st_md_cxt_restore(__jmp_buf env, int val) */ - .global _st_md_cxt_restore - .proc _st_md_cxt_restore - _st_md_cxt_restore: - alloc r8 = ar.pfs,2,0,0,0 - add r2 = 0x88,in0 // r2 <- &jmpbuf.ar_bsp - mov r16 = ar.rsc - ;; - flushrs // flush dirty regs to backing store - ;; - and r17 = ~0x3,r16 // clear ar.rsc.mode - ;; - mov ar.rsc = r17 // put RSE in enforced lazy mode - ;; - invala // invalidate the ALAT - ;; - ld8 r23 = [r2],8 // r23 <- jmpbuf.ar_bsp - ;; - mov ar.bspstore = r23 // write BSPSTORE - ld8 r25 = [r2],24 // r25 <- jmpbuf.ar_unat - ;; - ld8 r26 = [r2],-8 // r26 <- jmpbuf.ar_rnat - ;; - mov ar.rnat = r26 // write RNAT - ld8 r27 = [r2] // r27 <- jmpbuf.ar_rsc - ;; - mov ar.rsc = r27 // write RSE control - mov r2 = in0 - ;; - mov ar.unat = r25 // write ar.unat - add r3 = 8,in0 - ;; - ld8.fill.nta sp = [r2],16 // r12 (sp) - ld8.fill.nta gp = [r3],16 // r1 (gp) - ;; - ld8.nta r16 = [r2],16 // caller's unat - ld8.nta r17 = [r3],16 // fpsr - ;; - ld8.fill.nta r4 = [r2],16 // r4 - ld8.fill.nta r5 = [r3],16 // r5 - ;; - ld8.fill.nta r6 = [r2],16 // r6 - ld8.fill.nta r7 = [r3],16 // r7 - ;; - mov ar.unat = r16 // restore caller's unat - mov ar.fpsr = r17 // restore fpsr - ;; - ld8.nta r16 = [r2],16 // b0 - ld8.nta r17 = [r3],16 // b1 - ;; - ld8.nta r18 = [r2],16 // b2 - ld8.nta r19 = [r3],16 // b3 - ;; - ld8.nta r20 = [r2],16 // b4 - ld8.nta r21 = [r3],16 // b5 - ;; - ld8.nta r11 = [r2],16 // ar.pfs - ld8.nta r22 = [r3],72 // ar.lc - ;; - ld8.nta r24 = [r2],48 // pr - mov b0 = r16 - ;; - ldf.fill.nta f2 = [r2],32 - ldf.fill.nta f3 = [r3],32 - mov b1 = r17 - ;; - ldf.fill.nta f4 = [r2],32 - ldf.fill.nta f5 = [r3],32 - mov b2 = r18 - ;; - ldf.fill.nta f16 = [r2],32 - ldf.fill.nta f17 = [r3],32 - mov b3 = r19 - ;; - ldf.fill.nta f18 = [r2],32 - ldf.fill.nta f19 = [r3],32 - mov b4 = r20 - ;; - ldf.fill.nta f20 = [r2],32 - ldf.fill.nta f21 = [r3],32 - mov b5 = r21 - ;; - ldf.fill.nta f22 = [r2],32 - ldf.fill.nta f23 = [r3],32 - mov ar.lc = r22 - ;; - ldf.fill.nta f24 = [r2],32 - ldf.fill.nta f25 = [r3],32 - cmp.eq p6,p7 = 0,in1 - ;; - ldf.fill.nta f26 = [r2],32 - ldf.fill.nta f27 = [r3],32 - mov ar.pfs = r11 - ;; - ldf.fill.nta f28 = [r2],32 - ldf.fill.nta f29 = [r3],32 - ;; - ldf.fill.nta f30 = [r2] - ldf.fill.nta f31 = [r3] - (p6) mov r8 = 1 - (p7) mov r8 = in1 - - mov pr = r24,-1 - br.ret.sptk.few b0 - .endp _st_md_cxt_restore - - /****************************************************************/ - - - - - - - - - -#elif 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 - - /****************************************************************/ - -#endif - -#endif diff --git a/trunk/3rdparty/st-srs/md.h b/trunk/3rdparty/st-srs/md.h deleted file mode 100644 index dc4ef54c90..0000000000 --- a/trunk/3rdparty/st-srs/md.h +++ /dev/null @@ -1,641 +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 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 (AIX) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_SYSV_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #ifndef MD_HAVE_SOCKLEN_T - #define MD_HAVE_SOCKLEN_T - #define socklen_t unsigned long - #endif - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - (_thread)->context[3] = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - timebasestruct_t rt; \ - (void) read_real_time(&rt, TIMEBASE_SZ); \ - (void) time_base_to_time(&rt, TIMEBASE_SZ); \ - return (rt.tb_high * 1000000LL + rt.tb_low / 1000) - -#elif defined (CYGWIN) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_NOT_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) setjmp(env) - #define MD_LONGJMP(env, val) longjmp(env, val) - - #define MD_JB_SP 7 - - #define MD_GET_SP(_t) (_t)->context[MD_JB_SP] - - #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 - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (DARWIN) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - #define MD_HAVE_SOCKLEN_T - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #if defined(__ppc__) - #define MD_JB_SP 0 - #elif defined(__i386__) - #define MD_JB_SP 9 - #elif defined(__x86_64__) - #define MD_JB_SP 4 - #else - #error Unknown CPU architecture - #endif - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - *((long *)&((_thread)->context[MD_JB_SP])) = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (FREEBSD) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #if defined(__i386__) - #define MD_JB_SP 2 - #elif defined(__alpha__) - #define MD_JB_SP 34 - #elif defined(__amd64__) - #define MD_JB_SP 2 - #else - #error Unknown CPU architecture - #endif - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - (_thread)->context[0]._jb[MD_JB_SP] = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (HPUX) - - #define MD_STACK_GROWS_UP - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #ifndef __LP64__ - /* 32-bit mode (ILP32 data model) */ - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - ((long *)((_thread)->context))[1] = (long) (_sp); \ - ST_END_MACRO - #else - /* 64-bit mode (LP64 data model) */ - #define MD_STACK_PAD_SIZE 256 - /* Last stack frame must be preserved */ - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - memcpy((char *)(_sp) - MD_STACK_PAD_SIZE, \ - ((char **)((_thread)->context))[1] - MD_STACK_PAD_SIZE, \ - MD_STACK_PAD_SIZE); \ - ((long *)((_thread)->context))[1] = (long) (_sp); \ - ST_END_MACRO - #endif /* !__LP64__ */ - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (IRIX) - - #include - - #define MD_STACK_GROWS_DOWN - #define MD_USE_SYSV_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) setjmp(env) - #define MD_LONGJMP(env, val) longjmp(env, val) - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - (void) MD_SETJMP((_thread)->context); \ - (_thread)->context[JB_SP] = (long) (_sp); \ - (_thread)->context[JB_PC] = (long) _main; \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - static int inited = 0; \ - static clockid_t clock_id = CLOCK_SGI_CYCLE; \ - struct timespec ts; \ - if (!inited) { \ - if (syssgi(SGI_CYCLECNTR_SIZE) < 64) \ - clock_id = CLOCK_REALTIME; \ - inited = 1; \ - } \ - (void) clock_gettime(clock_id, &ts); \ - return (ts.tv_sec * 1000000LL + ts.tv_nsec / 1000) - - /* - * Cap the stack by zeroing out the saved return address register - * value. This allows libexc, used by SpeedShop, to know when to stop - * backtracing since it won't find main, start, or any other known - * stack root function in a state thread's stack. Without this libexc - * traces right off the stack and crashes. - * The function preamble stores ra at 8(sp), this stores zero there. - * N.B. This macro is compiler/ABI dependent. It must change if ANY more - * automatic variables are added to the _st_thread_main() routine, because - * the address where ra is stored will change. - */ - #if !defined(__GNUC__) && defined(_MIPS_SIM) && _MIPS_SIM != _ABIO32 - #define MD_CAP_STACK(var_addr) \ - (((volatile __uint64_t *)(var_addr))[1] = 0) - #endif - -#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 - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - /* - * 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 defined(__ia64__) - #define MD_STACK_GROWS_DOWN - - /* - * IA-64 architecture. Besides traditional memory call stack, IA-64 - * uses general register stack. Thus each thread needs a backing store - * for register stack in addition to memory stack. Standard - * setjmp()/longjmp() cannot be used for thread context switching - * because their implementation implicitly assumes that only one - * register stack exists. - */ - #ifdef USE_LIBC_SETJMP - #undef USE_LIBC_SETJMP - #endif - #define MD_USE_BUILTIN_SETJMP - - #define MD_STACK_PAD_SIZE 128 - /* Last register stack frame must be preserved */ - #define MD_INIT_CONTEXT(_thread, _sp, _bsp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - memcpy((char *)(_bsp) - MD_STACK_PAD_SIZE, \ - (char *)(_thread)->context[0].__jmpbuf[17] - MD_STACK_PAD_SIZE, \ - MD_STACK_PAD_SIZE); \ - (_thread)->context[0].__jmpbuf[0] = (long) (_sp); \ - (_thread)->context[0].__jmpbuf[17] = (long) (_bsp); \ - ST_END_MACRO - - #elif defined(__mips__) - #define MD_STACK_GROWS_DOWN - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - MD_SETJMP((_thread)->context); \ - _thread->context[0].__jmpbuf[0].__pc = (__ptr_t) _main; \ - _thread->context[0].__jmpbuf[0].__sp = _sp; \ - ST_END_MACRO - - #else /* Not IA-64 or mips */ - - /* - * 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(__powerpc__) - #define MD_STACK_GROWS_DOWN - - #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) - #ifndef JB_GPR1 - #define JB_GPR1 0 - #endif - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_GPR1] - #else - /* not an error but certainly cause for caution */ - #error "Untested use of old glibc on powerpc" - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__misc[0] - #endif /* glibc 2.1 or later */ - - #elif defined(__alpha) - #define MD_STACK_GROWS_DOWN - - #if defined(__GLIBC__) && __GLIBC__ >= 2 - #ifndef JB_SP - #define JB_SP 8 - #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 alpha" - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp - #endif - - #elif defined(__mc68000__) - #define MD_STACK_GROWS_DOWN - - /* m68k still uses old style sigjmp_buf */ - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp - - #elif defined(__sparc__) - #define MD_STACK_GROWS_DOWN - - #if defined(__GLIBC__) && __GLIBC__ >= 2 - #ifndef JB_SP - #define JB_SP 0 - #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 glic on sparc -- also using odd mozilla derived __fp" - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__fp - #endif - - #elif defined(__i386__) - #define MD_STACK_GROWS_DOWN - #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_STACK_GROWS_DOWN - #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_STACK_GROWS_DOWN - #define MD_USE_BUILTIN_SETJMP - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[13] - - #elif defined(__arm__) - #define MD_STACK_GROWS_DOWN - /* 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(__s390__) - #define MD_STACK_GROWS_DOWN - - /* There is no JB_SP in glibc at this time. (glibc 2.2.5) - */ - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__gregs[9] - - #elif defined(__hppa__) - #define MD_STACK_GROWS_UP - - /* yes, this is gross, unfortunately at the moment (2002/08/01) there is - * a bug in hppa's glibc header definition for JB_SP, so we can't - * use that... - */ - #define MD_GET_SP(_t) (*(long *)(((char *)&(_t)->context[0].__jmpbuf[0]) + 76)) - - #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 (NETBSD) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - #define MD_HAVE_SOCKLEN_T - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #if defined(__i386__) - #define MD_JB_SP 2 - #elif defined(__alpha__) - #define MD_JB_SP 34 - #elif defined(__sparc__) - #define MD_JB_SP 0 - #elif defined(__vax__) - #define MD_JB_SP 2 - #else - #error Unknown CPU architecture - #endif - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - (_thread)->context[MD_JB_SP] = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (OPENBSD) - - #define MD_STACK_GROWS_DOWN - #define MD_USE_BSD_ANON_MMAP - #define MD_ACCEPT_NB_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #if defined(__i386__) - #define MD_JB_SP 2 - #elif defined(__alpha__) - #define MD_JB_SP 34 - #elif defined(__sparc__) - #define MD_JB_SP 0 - #elif defined(__amd64__) - #define MD_JB_SP 6 - #else - #error Unknown CPU architecture - #endif - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - (_thread)->context[MD_JB_SP] = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (OSF1) - - #include - - #define MD_STACK_GROWS_DOWN - #define MD_USE_SYSV_ANON_MMAP - #define MD_ACCEPT_NB_NOT_INHERITED - #define MD_ALWAYS_UNSERIALIZED_ACCEPT - - #define MD_SETJMP(env) _setjmp(env) - #define MD_LONGJMP(env, val) _longjmp(env, val) - - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - ((struct sigcontext *)((_thread)->context))->sc_sp = (long) (_sp); \ - ST_END_MACRO - - #define MD_GET_UTIME() \ - struct timeval tv; \ - (void) gettimeofday(&tv, NULL); \ - return (tv.tv_sec * 1000000LL + tv.tv_usec) - -#elif defined (SOLARIS) - - #include - extern int getpagesize(void); - - #define MD_STACK_GROWS_DOWN - #define MD_USE_SYSV_ANON_MMAP - #define MD_ACCEPT_NB_NOT_INHERITED - - #define MD_SETJMP(env) setjmp(env) - #define MD_LONGJMP(env, val) longjmp(env, val) - - #if defined(sparc) || defined(__sparc) - #ifdef _LP64 - #define MD_STACK_PAD_SIZE 4095 - #endif - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - (void) MD_SETJMP((_thread)->context); \ - (_thread)->context[1] = (long) (_sp); \ - (_thread)->context[2] = (long) _main; \ - ST_END_MACRO - #elif defined(i386) || defined(__i386) - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - (void) MD_SETJMP((_thread)->context); \ - (_thread)->context[4] = (long) (_sp); \ - (_thread)->context[5] = (long) _main; \ - ST_END_MACRO - #elif defined(__amd64__) - #define MD_INIT_CONTEXT(_thread, _sp, _main) \ - ST_BEGIN_MACRO \ - if (MD_SETJMP((_thread)->context)) \ - _main(); \ - (_thread)->context[6] = (long) (_sp); \ - ST_END_MACRO - #else - #error Unknown CPU architecture - #endif - - #define MD_GET_UTIME() \ - return (gethrtime() / 1000) - -#else - #error Unknown OS -#endif /* OS */ - -#if !defined(MD_HAVE_POLL) && !defined(MD_DONT_HAVE_POLL) - #define MD_HAVE_POLL -#endif - -#ifndef MD_STACK_PAD_SIZE - #define MD_STACK_PAD_SIZE 128 -#endif - -#if !defined(MD_HAVE_SOCKLEN_T) && !defined(socklen_t) - #define socklen_t int -#endif - -#ifndef MD_CAP_STACK - #define MD_CAP_STACK(var_addr) -#endif - -#endif /* !__ST_MD_H__ */ - diff --git a/trunk/3rdparty/st-srs/osguess.sh b/trunk/3rdparty/st-srs/osguess.sh deleted file mode 100644 index 531681efe6..0000000000 --- a/trunk/3rdparty/st-srs/osguess.sh +++ /dev/null @@ -1,45 +0,0 @@ -# -# This script can be used to automatically guess target OS. -# It requires the config.guess utility which is a part of GNU Autoconf. -# GNU Autoconf can be downloaded from ftp://ftp.gnu.org/gnu/autoconf/ -# -# Use "default" as a make target for automatic builds. -# - - -# Specify path to the config.guess utility (unless set via environment) -#CONFIG_GUESS_PATH= - - -if [ x"$CONFIG_GUESS_PATH" = x ]; then - echo "Error: CONFIG_GUESS_PATH variable is not set" - exit 1 -fi - -if [ ! -f "$CONFIG_GUESS_PATH/config.guess" ]; then - echo "Can't find $CONFIG_GUESS_PATH/config.guess utility. Wrong path?" - exit 1 -fi - -sys_info=`/bin/sh $CONFIG_GUESS_PATH/config.guess` - -echo "Building for $sys_info" - -case "$sys_info" in - *-ibm-aix4* ) OS=AIX ;; - *-freebsd* ) OS=FREEBSD ;; - hppa*-hp-hpux11*) OS=HPUX ;; - *-sgi-irix6* ) OS=IRIX ;; - *-linux* ) OS=LINUX ;; - *-netbsd* ) OS=NETBSD ;; - *-openbsd* ) OS=OPENBSD ;; - *-dec-osf* ) OS=OSF1 ;; - *-solaris2* ) OS=SOLARIS ;; - *-darwin* ) OS=DARWIN ;; - * ) OS= - echo "Sorry, unsupported OS" - exit 1 ;; -esac - -echo "Making with OS=$OS" - diff --git a/trunk/3rdparty/st-srs/public.h b/trunk/3rdparty/st-srs/public.h deleted file mode 100644 index 20b09407b2..0000000000 --- a/trunk/3rdparty/st-srs/public.h +++ /dev/null @@ -1,166 +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. - */ - -#ifndef __ST_THREAD_H__ -#define __ST_THREAD_H__ - -#include -#include -#include -#include -#include -#include -#include - -#define ST_VERSION "1.9" -#define ST_VERSION_MAJOR 1 -#define ST_VERSION_MINOR 9 - -/* Undefine this to remove the context switch callback feature. */ -#define ST_SWITCH_CB - -#ifndef ETIME - #define ETIME ETIMEDOUT -#endif - -#ifndef ST_UTIME_NO_TIMEOUT - #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) -#endif - -#ifndef ST_UTIME_NO_WAIT - #define ST_UTIME_NO_WAIT 0 -#endif - -#define ST_EVENTSYS_DEFAULT 0 -#define ST_EVENTSYS_SELECT 1 -#define ST_EVENTSYS_POLL 2 -#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 st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); -extern int st_randomize_stacks(int on); -extern int st_set_utime_function(st_utime_t (*func)(void)); - -extern st_utime_t st_utime(void); -extern st_utime_t st_utime_last_clock(void); -extern int st_timecache_set(int on); -extern time_t st_time(void); -extern int st_usleep(st_utime_t usecs); -extern int st_sleep(int secs); -extern st_cond_t st_cond_new(void); -extern int st_cond_destroy(st_cond_t cvar); -extern int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout); -extern int st_cond_wait(st_cond_t cvar); -extern int st_cond_signal(st_cond_t cvar); -extern int st_cond_broadcast(st_cond_t cvar); -extern st_mutex_t st_mutex_new(void); -extern int st_mutex_destroy(st_mutex_t lock); -extern int st_mutex_lock(st_mutex_t lock); -extern int st_mutex_unlock(st_mutex_t lock); -extern int st_mutex_trylock(st_mutex_t lock); - -extern int st_key_create(int *keyp, void (*destructor)(void *)); -extern int st_key_getlimit(void); -extern int st_thread_setspecific(int key, void *value); -extern void *st_thread_getspecific(int key); - -extern st_netfd_t st_netfd_open(int osfd); -extern st_netfd_t st_netfd_open_socket(int osfd); -extern void st_netfd_free(st_netfd_t fd); -extern int st_netfd_close(st_netfd_t fd); -extern int st_netfd_fileno(st_netfd_t fd); -extern void st_netfd_setspecific(st_netfd_t fd, void *value, void (*destructor)(void *)); -extern void *st_netfd_getspecific(st_netfd_t fd); -extern int st_netfd_serialize_accept(st_netfd_t fd); -extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout); - -extern int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); -extern st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout); -extern int st_connect(st_netfd_t fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout); -extern ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); -extern ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); -extern int st_read_resid(st_netfd_t fd, void *buf, size_t *resid, st_utime_t timeout); -extern ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); -extern int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); -extern ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte, st_utime_t timeout); -extern int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid, st_utime_t timeout); -extern ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); -extern int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); -extern int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout); -extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout); -extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout); -extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout); -extern st_netfd_t st_open(const char *path, int oflags, mode_t mode); - -#ifdef DEBUG -extern void _st_show_thread_stack(st_thread_t thread, const char *messg); -extern void _st_iterate_threads(void); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* !__ST_THREAD_H__ */ - diff --git a/trunk/3rdparty/st-srs/sched.c b/trunk/3rdparty/st-srs/sched.c deleted file mode 100644 index 87515827e9..0000000000 --- a/trunk/3rdparty/st-srs/sched.c +++ /dev/null @@ -1,705 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -/* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND -#include -#endif - - -/* Global 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) { - /* Pull thread off of the run queue */ - thread = _ST_THREAD_PTR(_ST_RUNQ.next); - _ST_DEL_RUNQ(thread); - } else { - /* 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 elapsed, now; - - now = st_utime(); - elapsed = now - _ST_LAST_CLOCK; - _ST_LAST_CLOCK = now; - - 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; - _ST_ADD_RUNQ(thread); - } -} - - -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; -#ifdef __ia64__ - char *bsp; -#endif - - /* 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 */ -#if defined (MD_STACK_GROWS_DOWN) - sp = stack->stk_top; -#ifdef __ia64__ - /* - * The stack segment is split in the middle. The upper half is used - * as backing store for the register stack which grows upward. - * The lower half is used for the traditional memory stack which - * grows downward. Both stacks start in the middle and grow outward - * from each other. - */ - sp -= (stk_size >> 1); - bsp = sp; - /* Make register stack 64-byte aligned */ - if ((unsigned long)bsp & 0x3f) - bsp = bsp + (0x40 - ((unsigned long)bsp & 0x3f)); - stack->bsp = bsp + _ST_STACK_PAD_SIZE; -#endif - 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; -#elif defined (MD_STACK_GROWS_UP) - sp = stack->stk_bottom; - thread = (_st_thread_t *) sp; - sp = sp + sizeof(_st_thread_t); - ptds = (void **) sp; - sp = sp + (ST_KEYS_MAX * sizeof(void *)); - - /* Make stack 64-byte aligned */ - if ((unsigned long)sp & 0x3f) - sp = sp + (0x40 - ((unsigned long)sp & 0x3f)); - stack->sp = sp + _ST_STACK_PAD_SIZE; -#else -#error Unknown OS -#endif - - 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; - -#ifndef __ia64__ - /* 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 -#else - _ST_INIT_CONTEXT(thread, stack->sp, stack->bsp, _st_thread_main); -#endif - - /* If thread is joinable, allocate a termination condition variable */ - if (joinable) { - thread->term = st_cond_new(); - if (thread->term == NULL) { - _st_stack_free(thread->stack); - return NULL; - } - } - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _st_active_count++; - _ST_ADD_RUNQ(thread); -#ifdef DEBUG - _ST_ADD_THREADQ(thread); -#endif - - /* merge from https://github.com/toffaletti/state-threads/commit/7f57fc9acc05e657bca1223f1e5b9b1a45ed929b */ -#ifndef NVALGRIND - if (!(thread->flags & _ST_FL_PRIMORDIAL)) { - thread->stack->valgrind_stack_id = VALGRIND_STACK_REGISTER(thread->stack->stk_top, thread->stack->stk_bottom); - } -#endif - - return thread; -} - - -_st_thread_t *st_thread_self(void) -{ - return _ST_CURRENT_THREAD(); -} - - -#ifdef DEBUG -/* ARGSUSED */ -void _st_show_thread_stack(_st_thread_t *thread, const char *messg) -{ - -} - -/* To be set from debugger */ -int _st_iterate_threads_flag = 0; - -void _st_iterate_threads(void) -{ - static _st_thread_t *thread = NULL; - static jmp_buf orig_jb, save_jb; - _st_clist_t *q; - - if (!_st_iterate_threads_flag) { - if (thread) { - memcpy(thread->context, save_jb, sizeof(jmp_buf)); - MD_LONGJMP(orig_jb, 1); - } - return; - } - - if (thread) { - memcpy(thread->context, save_jb, sizeof(jmp_buf)); - _st_show_thread_stack(thread, NULL); - } else { - if (MD_SETJMP(orig_jb)) { - _st_iterate_threads_flag = 0; - thread = NULL; - _st_show_thread_stack(thread, "Iteration completed"); - return; - } - thread = _ST_CURRENT_THREAD(); - _st_show_thread_stack(thread, "Iteration started"); - } - - q = thread->tlink.next; - if (q == &_ST_THREADQ) - q = q->next; - ST_ASSERT(q != &_ST_THREADQ); - thread = _ST_THREAD_THREADQ_PTR(q); - if (thread == _ST_CURRENT_THREAD()) - MD_LONGJMP(orig_jb, 1); - memcpy(save_jb, thread->context, sizeof(jmp_buf)); - MD_LONGJMP(thread->context, 1); -} -#endif /* DEBUG */ - diff --git a/trunk/3rdparty/st-srs/st.pc.in b/trunk/3rdparty/st-srs/st.pc.in deleted file mode 100644 index 46c39ec522..0000000000 --- a/trunk/3rdparty/st-srs/st.pc.in +++ /dev/null @@ -1,10 +0,0 @@ -prefix=@prefix@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include - -Name: libst -Description: State Thread Library -Version: @VERSION@ -Libs: -L${libdir} -lst -Cflags: -I${includedir} diff --git a/trunk/3rdparty/st-srs/st.spec b/trunk/3rdparty/st-srs/st.spec deleted file mode 100644 index 4914aa1961..0000000000 --- a/trunk/3rdparty/st-srs/st.spec +++ /dev/null @@ -1,79 +0,0 @@ -Summary: State Threads Library -Name: st -Version: 1.9 -Release: 1 -Copyright: MPL 1.2 or GPL 2+ -Packager: Wesley W. Terpstra -Source: http://prdownloads.sourceforge.net/state-threads/st-%{version}.tar.gz -Prefix: /usr -BuildRoot: /tmp/%{name}-%{version}-build -Group: Development/Libraries - -%description -The State Threads library has an interface similar to POSIX threads. - -However, the threads are actually all run in-process. This type of -threading allows for controlled schedualing points. It is highly useful -for designing robust and extremely scalable internet applications since -there is no resource contention and locking is generally unnecessary. - -It can be combined with traditional threading or multiple process -parallelism to take advantage of multiple processors. - -See: for further -information about how state threads improve performance. - -%package -n libst-devel -Summary: State Threads Library - Development Files -Group: Development/Libraries -Requires: libst1 - -%description -n libst-devel -Development headers and documentation for libst - -%package -n libst1 -Summary: State Threads Library - Shared Libs Major 1 -Group: System/Libraries - -%description -n libst1 -Shared libraries for running applications linked against api version 1. - -%prep -%setup -q - -%build -make CONFIG_GUESS_PATH=/usr/share/automake default-optimized - -%install -if [ -d ${RPM_BUILD_ROOT} ]; then rm -rf ${RPM_BUILD_ROOT}; fi - -mkdir -m 0755 -p ${RPM_BUILD_ROOT}/%{prefix}/lib/pkgconfig -mkdir -m 0755 -p ${RPM_BUILD_ROOT}/%{prefix}/include -mkdir -m 0755 -p ${RPM_BUILD_ROOT}/%{prefix}/share/doc/libst-devel -cp -a obj/libst.* ${RPM_BUILD_ROOT}/%{prefix}/lib -cp -a obj/st.h ${RPM_BUILD_ROOT}/%{prefix}/include -sed "s*@prefix@*%{prefix}*g" ${RPM_BUILD_ROOT}/%{prefix}/lib/pkgconfig/st.pc -cp -a docs/* ${RPM_BUILD_ROOT}/%{prefix}/share/doc/libst-devel/ -cp -a examples ${RPM_BUILD_ROOT}/%{prefix}/share/doc/libst-devel/ - -%post -n libst1 -/sbin/ldconfig %{prefix}/lib - -%files -n libst1 -%defattr(-,root,root) -%{prefix}/lib/lib*.so.* - -%files -n libst-devel -%defattr(-,root,root) -%{prefix}/include/* -%{prefix}/lib/lib*.a -%{prefix}/lib/lib*.so -%{prefix}/lib/pkgconfig/st.pc -%{prefix}/share/doc/libst-devel/* - -%clean -if [ -d ${RPM_BUILD_ROOT} ]; then rm -rf ${RPM_BUILD_ROOT}; fi - -%changelog -* Wed Dec 26 2001 Wesley W. Terpstra -- first rpms for libst-1.3.tar.gz diff --git a/trunk/3rdparty/st-srs/stk.c b/trunk/3rdparty/st-srs/stk.c deleted file mode 100644 index 3e681e5954..0000000000 --- a/trunk/3rdparty/st-srs/stk.c +++ /dev/null @@ -1,173 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include "common.h" - - -/* How much space to leave between the stacks, at each end */ -#define REDZONE _ST_PAGE_SIZE - -_st_clist_t _st_free_stacks = ST_INIT_STATIC_CLIST(&_st_free_stacks); -int _st_num_free_stacks = 0; -int _st_randomize_stacks = 0; - -static char *_st_new_stk_segment(int size); - -_st_stack_t *_st_stack_new(int stack_size) -{ - _st_clist_t *qp; - _st_stack_t *ts; - int extra; - - for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) { - ts = _ST_THREAD_STACK_PTR(qp); - if (ts->stk_size >= stack_size) { - /* Found a stack that is big enough */ - ST_REMOVE_LINK(&ts->links); - _st_num_free_stacks--; - ts->links.next = NULL; - ts->links.prev = NULL; - return ts; - } - } - - /* Make a new thread stack object. */ - if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL) - return NULL; - extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; - ts->vaddr_size = stack_size + 2*REDZONE + extra; - ts->vaddr = _st_new_stk_segment(ts->vaddr_size); - if (!ts->vaddr) { - free(ts); - return NULL; - } - ts->stk_size = stack_size; - ts->stk_bottom = ts->vaddr + REDZONE; - ts->stk_top = ts->stk_bottom + stack_size; - -#ifdef DEBUG - mprotect(ts->vaddr, REDZONE, PROT_NONE); - mprotect(ts->stk_top + extra, REDZONE, PROT_NONE); -#endif - - if (extra) { - long offset = (random() % extra) & ~0xf; - - ts->stk_bottom += offset; - ts->stk_top += offset; - } - - return ts; -} - - -/* - * Free the stack for the current thread - */ -void _st_stack_free(_st_stack_t *ts) -{ - if (!ts) - return; - - /* Put the stack on the free list */ - ST_APPEND_LINK(&ts->links, _st_free_stacks.prev); - _st_num_free_stacks++; -} - - -static char *_st_new_stk_segment(int size) -{ -#ifdef MALLOC_STACK - void *vaddr = malloc(size); -#else - static int zero_fd = -1; - int mmap_flags = MAP_PRIVATE; - void *vaddr; - -#if defined (MD_USE_SYSV_ANON_MMAP) - if (zero_fd < 0) { - if ((zero_fd = open("/dev/zero", O_RDWR, 0)) < 0) - return NULL; - fcntl(zero_fd, F_SETFD, FD_CLOEXEC); - } -#elif defined (MD_USE_BSD_ANON_MMAP) - mmap_flags |= MAP_ANON; -#else -#error Unknown OS -#endif - - vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, mmap_flags, zero_fd, 0); - if (vaddr == (void *)MAP_FAILED) - return NULL; - -#endif /* MALLOC_STACK */ - - return (char *)vaddr; -} - - -/* Not used */ -#if 0 -void _st_delete_stk_segment(char *vaddr, int size) -{ -#ifdef MALLOC_STACK - free(vaddr); -#else - (void) munmap(vaddr, size); -#endif -} -#endif - -int st_randomize_stacks(int on) -{ - int wason = _st_randomize_stacks; - - _st_randomize_stacks = on; - if (on) - srandom((unsigned int) st_utime()); - - return wason; -} diff --git a/trunk/3rdparty/st-srs/sync.c b/trunk/3rdparty/st-srs/sync.c deleted file mode 100644 index 907fdfac3c..0000000000 --- a/trunk/3rdparty/st-srs/sync.c +++ /dev/null @@ -1,368 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include "common.h" - - -extern time_t _st_curr_time; -extern st_utime_t _st_last_tset; -extern int _st_active_count; - -static st_utime_t (*_st_utime)(void) = NULL; - - -/***************************************** - * Time functions - */ - -st_utime_t st_utime(void) -{ - if (_st_utime == NULL) { -#ifdef MD_GET_UTIME - MD_GET_UTIME(); -#else -#error Unknown OS -#endif - } - - return (*_st_utime)(); -} - - -int st_set_utime_function(st_utime_t (*func)(void)) -{ - if (_st_active_count) { - errno = EINVAL; - return -1; - } - - _st_utime = func; - - return 0; -} - - -st_utime_t st_utime_last_clock(void) -{ - return _ST_LAST_CLOCK; -} - - -int st_timecache_set(int on) -{ - int wason = (_st_curr_time) ? 1 : 0; - - if (on) { - _st_curr_time = time(NULL); - _st_last_tset = st_utime(); - } else - _st_curr_time = 0; - - return wason; -} - - -time_t st_time(void) -{ - if (_st_curr_time) - return _st_curr_time; - - return time(NULL); -} - - -int st_usleep(st_utime_t usecs) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (usecs != ST_UTIME_NO_TIMEOUT) { - me->state = _ST_ST_SLEEPING; - _ST_ADD_SLEEPQ(me, usecs); - } else - me->state = _ST_ST_SUSPENDED; - - _ST_SWITCH_CONTEXT(me); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - - -int st_sleep(int secs) -{ - return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT); -} - - -/***************************************** - * Condition variable functions - */ - -_st_cond_t *st_cond_new(void) -{ - _st_cond_t *cvar; - - cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t)); - if (cvar) { - ST_INIT_CLIST(&cvar->wait_q); - } - - return cvar; -} - - -int st_cond_destroy(_st_cond_t *cvar) -{ - if (cvar->wait_q.next != &cvar->wait_q) { - errno = EBUSY; - return -1; - } - - free(cvar); - - return 0; -} - - -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - int rv; - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - /* Put caller thread on the condition variable's wait queue */ - me->state = _ST_ST_COND_WAIT; - ST_APPEND_LINK(&me->wait_links, &cvar->wait_q); - - if (timeout != ST_UTIME_NO_TIMEOUT) - _ST_ADD_SLEEPQ(me, timeout); - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - rv = 0; - - if (me->flags & _ST_FL_TIMEDOUT) { - me->flags &= ~_ST_FL_TIMEDOUT; - errno = ETIME; - rv = -1; - } - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - rv = -1; - } - - return rv; -} - - -int st_cond_wait(_st_cond_t *cvar) -{ - return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT); -} - - -static int _st_cond_signal(_st_cond_t *cvar, int broadcast) -{ - _st_thread_t *thread; - _st_clist_t *q; - - for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) { - thread = _ST_THREAD_WAITQ_PTR(q); - if (thread->state == _ST_ST_COND_WAIT) { - if (thread->flags & _ST_FL_ON_SLEEPQ) - _ST_DEL_SLEEPQ(thread); - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - if (!broadcast) - break; - } - } - - return 0; -} - - -int st_cond_signal(_st_cond_t *cvar) -{ - return _st_cond_signal(cvar, 0); -} - - -int st_cond_broadcast(_st_cond_t *cvar) -{ - return _st_cond_signal(cvar, 1); -} - - -/***************************************** - * Mutex functions - */ - -_st_mutex_t *st_mutex_new(void) -{ - _st_mutex_t *lock; - - lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t)); - if (lock) { - ST_INIT_CLIST(&lock->wait_q); - lock->owner = NULL; - } - - return lock; -} - - -int st_mutex_destroy(_st_mutex_t *lock) -{ - if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) { - errno = EBUSY; - return -1; - } - - free(lock); - - return 0; -} - - -int st_mutex_lock(_st_mutex_t *lock) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (lock->owner == NULL) { - /* Got the mutex */ - lock->owner = me; - return 0; - } - - if (lock->owner == me) { - errno = EDEADLK; - return -1; - } - - /* Put caller thread on the mutex's wait queue */ - me->state = _ST_ST_LOCK_WAIT; - ST_APPEND_LINK(&me->wait_links, &lock->wait_q); - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - - if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - - -int st_mutex_unlock(_st_mutex_t *lock) -{ - _st_thread_t *thread; - _st_clist_t *q; - - if (lock->owner != _ST_CURRENT_THREAD()) { - errno = EPERM; - return -1; - } - - for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) { - thread = _ST_THREAD_WAITQ_PTR(q); - if (thread->state == _ST_ST_LOCK_WAIT) { - lock->owner = thread; - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - return 0; - } - } - - /* No threads waiting on this mutex */ - lock->owner = NULL; - - return 0; -} - - -int st_mutex_trylock(_st_mutex_t *lock) -{ - if (lock->owner != NULL) { - errno = EBUSY; - return -1; - } - - /* Got the mutex */ - lock->owner = _ST_CURRENT_THREAD(); - - return 0; -} - diff --git a/trunk/auto/depends.sh b/trunk/auto/depends.sh index a4c8730dfe..843439357e 100755 --- a/trunk/auto/depends.sh +++ b/trunk/auto/depends.sh @@ -212,34 +212,23 @@ if [[ $OS_IS_UBUNTU = NO && $OS_IS_CENTOS = NO && $SRS_EXPORT_LIBRTMP_PROJECT = fi ##################################################################################### -# state-threads +# libco ##################################################################################### if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then - # check the cross build flag file, if flag changed, need to rebuild the st. - _ST_MAKE=linux-debug && _ST_EXTRA_CFLAGS="-DMD_HAVE_EPOLL" - if [[ $SRS_VALGRIND == YES ]]; then - _ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS -DMD_VALGRIND" - fi - # Pass the global extra flags. - if [[ $SRS_EXTRA_FLAGS != '' ]]; then - _ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS $SRS_EXTRA_FLAGS" - fi - # Patched ST from https://github.com/ossrs/state-threads/tree/srs - if [[ -f ${SRS_OBJS}/st/libst.a ]]; then - echo "The state-threads is ok."; + if [[ -f ${SRS_OBJS}/co/libcolib.a ]]; then + echo "The libco is ok."; else - echo "Building state-threads."; + echo "Building libco."; ( - rm -rf ${SRS_OBJS}/st-srs && cd ${SRS_OBJS} && - ln -sf ../3rdparty/st-srs && cd st-srs && - make clean && make ${_ST_MAKE} EXTRA_CFLAGS="${_ST_EXTRA_CFLAGS}" \ - CC=${SRS_TOOL_CC} AR=${SRS_TOOL_AR} LD=${SRS_TOOL_LD} RANDLIB=${SRS_TOOL_RANDLIB} && - cd .. && rm -f st && ln -sf st-srs/obj st + rm -rf ${SRS_OBJS}/co && cd ${SRS_OBJS} && + ln -sf ../3rdparty/libco && cd libco && + make clean && make colib && + cd .. && rm -f co && ln -sf libco/ co ) fi # check status - ret=$?; if [[ $ret -ne 0 ]]; then echo "Build state-threads failed, ret=$ret"; exit $ret; fi - if [ ! -f ${SRS_OBJS}/st/libst.a ]; then echo "Build state-threads static lib failed."; exit -1; fi + ret=$?; if [[ $ret -ne 0 ]]; then echo "Build libco failed, ret=$ret"; exit $ret; fi + if [ ! -f ${SRS_OBJS}/co/lib/libcolib.a ]; then echo "Build libco static lib failed."; exit -1; fi fi ##################################################################################### diff --git a/trunk/configure b/trunk/configure index d233f66dbb..e67644a200 100755 --- a/trunk/configure +++ b/trunk/configure @@ -142,12 +142,12 @@ END ##################################################################################### # Libraries, external library to build in srs, -# header(.h): add to ModuleLibIncs if need the specified library. for example, LibSTRoot -# library(.a): add to ModuleLibFiles if binary need the specifeid library. for example, LibSTfile +# header(.h): add to ModuleLibIncs if need the specified library. for example, LibCoRoot +# library(.a): add to ModuleLibFiles if binary need the specifeid library. for example, LibCofile # -# st(state-threads) the basic network library for SRS. -LibSTRoot="${SRS_OBJS_DIR}/st"; LibSTfile="${LibSTRoot}/libst.a" -if [[ $SRS_SHARED_ST == YES ]]; then LibSTfile="-lst"; fi +# libco the basic network library for SRS. +LibcoRoot="${SRS_OBJS_DIR}/co"; Libcofile="${LibcoRoot}/lib/libcolib.a -lpthread" +if [[ $SRS_SHARED_LIBCO == YES ]]; then Libcofile="-lcolib -lpthread"; fi # openssl-1.1.0e, for the RTMP complex handshake. LibSSLRoot="";LibSSLfile="" if [[ $SRS_SSL == YES && $SRS_USE_SYS_SSL == NO ]]; then @@ -233,7 +233,7 @@ fi if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then MODULE_ID="SERVICE" MODULE_DEPENDS=("CORE" "KERNEL" "PROTOCOL") - ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibSSLRoot}) + ModuleLibIncs=(${LibcoRoot} ${SRS_OBJS_DIR} ${LibSSLRoot}) MODULE_FILES=("srs_service_log" "srs_service_st" "srs_service_http_client" "srs_service_http_conn" "srs_service_rtmp_conn" "srs_service_utility" "srs_service_conn") @@ -246,7 +246,7 @@ fi if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then MODULE_ID="APP" MODULE_DEPENDS=("CORE" "KERNEL" "PROTOCOL" "SERVICE") - ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibSSLRoot}) + ModuleLibIncs=(${LibcoRoot} ${SRS_OBJS_DIR} ${LibSSLRoot}) MODULE_FILES=("srs_app_server" "srs_app_conn" "srs_app_rtmp_conn" "srs_app_source" "srs_app_refer" "srs_app_hls" "srs_app_forward" "srs_app_encoder" "srs_app_http_stream" "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" @@ -284,7 +284,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then if [[ $SRS_SRT == YES ]]; then MODULE_DEPENDS+=("SRT") fi - ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) + ModuleLibIncs=(${LibcoRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) if [[ $SRS_SRT == YES ]]; then ModuleLibIncs+=("${LibSRTRoot[*]}") fi @@ -297,7 +297,7 @@ fi if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then MODULE_ID="MAIN" MODULE_DEPENDS=("CORE" "KERNEL" "PROTOCOL" "SERVICE") - ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) + ModuleLibIncs=(${LibcoRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) MODULE_FILES=() DEFINES="" # add each modules for main @@ -324,24 +324,24 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then done # # all depends libraries - ModuleLibFiles=(${LibSTfile} ${LibSSLfile} ${LibGperfFile}) + ModuleLibFiles=(${Libcofile} ${LibSSLfile} ${LibGperfFile}) if [[ $SRS_SRT == YES ]]; then ModuleLibFiles+=("${LibSRTfile[*]}") fi # all depends objects MODULE_OBJS="${CORE_OBJS[@]} ${KERNEL_OBJS[@]} ${PROTOCOL_OBJS[@]} ${SERVICE_OBJS[@]} ${APP_OBJS[@]} ${SERVER_OBJS[@]}" - ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) + ModuleLibIncs=(${LibcoRoot} ${SRS_OBJS_DIR} ${LibGperfRoot} ${LibSSLRoot}) if [[ $SRS_SRT == YES ]]; then MODULE_OBJS="${MODULE_OBJS} ${SRT_OBJS[@]}" fi LINK_OPTIONS="${SrsLinkOptions}${SrsGprofLink}${SrsGperfLink}" # - # srs: srs(simple rtmp server) over st(state-threads) + # srs: srs(simple rtmp server) over co(libco) BUILD_KEY="srs" APP_MAIN="srs_main_server" APP_NAME="srs" . auto/apps.sh # # For modules, without the app module. MODULE_OBJS="${CORE_OBJS[@]} ${KERNEL_OBJS[@]} ${PROTOCOL_OBJS[@]} ${SERVICE_OBJS[@]} ${MAIN_OBJS[@]}" - ModuleLibFiles=(${LibSTfile} ${LibSSLfile} ${LibGperfFile}) + ModuleLibFiles=(${Libcofile} ${LibSSLfile} ${LibGperfFile}) # for SRS_MODULE in ${SRS_MODULES[*]}; do . $SRS_MODULE/config @@ -361,11 +361,11 @@ if [ $SRS_UTEST = YES ]; then MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_protocol" "srs_utest_kernel" "srs_utest_core" "srs_utest_config" "srs_utest_rtmp" "srs_utest_http" "srs_utest_avc" "srs_utest_reload" "srs_utest_mp4" "srs_utest_service" "srs_utest_app") - ModuleLibIncs=(${SRS_OBJS_DIR} ${LibSTRoot} ${LibSSLRoot}) + ModuleLibIncs=(${SRS_OBJS_DIR} ${LibcoRoot} ${LibSSLRoot}) if [[ $SRS_SRT == YES ]]; then ModuleLibIncs+=("${LibSRTRoot[*]}") fi - ModuleLibFiles=(${LibSTfile} ${LibSSLfile}) + ModuleLibFiles=(${Libcofile} ${LibSSLfile}) if [[ $SRS_SRT == YES ]]; then ModuleLibFiles+=("${LibSRTfile[*]}") fi @@ -413,7 +413,7 @@ help: @echo "Usage: make |||||||" @echo " help display this help menu" @echo " clean cleanup project" - @echo " server build the srs(simple rtmp server) over st(state-threads)" + @echo " server build the srs(simple rtmp server) over co(libco)" @echo " librtmp build the client publish/play library, and samples" @echo " utest build the utest for srs" @echo " install install srs to the prefix path" @@ -451,7 +451,7 @@ END else cat << END >> ${SRS_WORKDIR}/${SRS_MAKEFILE} server: _prepare_dir - @echo "Build the srs(simple rtmp server) over ST(state-threads)" + @echo "Build the srs(simple rtmp server) over co(libco)" \$(MAKE) -f ${SRS_OBJS_DIR}/${SRS_MAKEFILE} srs END diff --git a/trunk/research/st/Makefile b/trunk/research/st/Makefile deleted file mode 100755 index 0b24bb80c2..0000000000 --- a/trunk/research/st/Makefile +++ /dev/null @@ -1,100 +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. - -########################## -# Target dir and cc: -CC = cc -TARGETDIR = objs - -########################## -# Supported OSes: -OS = LINUX - -ifneq ($(shell test -f /usr/include/sys/epoll.h && echo yes), yes) -default: - @echo "epoll not found" - @exit 1 -endif - -EXTRA_OBJS = $(TARGETDIR)/md.o - -CFLAGS = -OTHER_FLAGS += -Wall -g -O0 -DEFINES = -D$(OS) -DDEBUG -DMD_HAVE_EPOLL -DMALLOC_STACK - -########################## -# Other possible defines: -# 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= -# -# Note that you can also add these defines by specifying them as -# make/gmake arguments (without editing this Makefile). For example: -# -# make EXTRA_CFLAGS=-UMD_HAVE_EPOLL -# -########################## - -CFLAGS += $(DEFINES) $(OTHER_FLAGS) $(EXTRA_CFLAGS) - -OBJS = $(TARGETDIR)/sched.o \ - $(TARGETDIR)/stk.o \ - $(TARGETDIR)/sync.o \ - $(TARGETDIR)/key.o \ - $(TARGETDIR)/io.o \ - $(TARGETDIR)/event.o \ - $(TARGETDIR)/srs.o -OBJS += $(EXTRA_OBJS) -SRS = $(TARGETDIR)/srs - -linux-debug: all -all: $(TARGETDIR) $(SRS) - -$(TARGETDIR): - if [ ! -d $(TARGETDIR) ]; then mkdir $(TARGETDIR); fi - -$(SRS): $(OBJS) - $(CC) $(CFLAGS) -o $@ $(OBJS) - -$(TARGETDIR)/md.o: md.S - $(CC) $(CFLAGS) -c $< -o $@ - -$(TARGETDIR)/%.o: %.c common.h md.h Makefile - $(CC) $(CFLAGS) -c $< -o $@ - -clean: - rm -rf $(TARGETDIR) diff --git a/trunk/research/st/common.h b/trunk/research/st/common.h deleted file mode 100644 index b83e575caf..0000000000 --- a/trunk/research/st/common.h +++ /dev/null @@ -1,445 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#ifndef __ST_COMMON_H__ -#define __ST_COMMON_H__ - -#include -#include -#include -#include -#include - -/* Enable assertions only if DEBUG is defined */ -#ifndef DEBUG - #define NDEBUG -#endif -#include -#define ST_ASSERT(expr) assert(expr) - -#define ST_BEGIN_MACRO { -#define ST_END_MACRO } - -#ifdef DEBUG - #define ST_HIDDEN /*nothing*/ -#else - #define ST_HIDDEN static -#endif - -#include "public.h" -#include "md.h" - -/***************************************** - * 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 */ -} _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_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 (64*1024) - -#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 - -/* - * Number of bytes reserved under the stack "bottom" - */ -#define _ST_STACK_PAD_SIZE MD_STACK_PAD_SIZE - - -/***************************************** - * Forward declarations - */ - -void _st_vp_schedule(void); -void _st_vp_check_clock(void); -void *_st_idle_thread_start(void *arg); -void _st_thread_main(void); -void _st_thread_cleanup(_st_thread_t *thread); -void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout); -void _st_del_sleep_q(_st_thread_t *thread); -_st_stack_t *_st_stack_new(int stack_size); -void _st_stack_free(_st_stack_t *ts); -int _st_io_init(void); - -st_utime_t st_utime(void); -_st_cond_t *st_cond_new(void); -int st_cond_destroy(_st_cond_t *cvar); -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout); -int st_cond_signal(_st_cond_t *cvar); -ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout); -ssize_t st_write(_st_netfd_t *fd, const void *buf, size_t nbyte, st_utime_t timeout); -int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size); - -#endif /* !__ST_COMMON_H__ */ - diff --git a/trunk/research/st/event.c b/trunk/research/st/event.c deleted file mode 100644 index 5704f520a9..0000000000 --- a/trunk/research/st/event.c +++ /dev/null @@ -1,483 +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. - * Yahoo! Inc. - * - * Alternatively, the contents of this file may be used under the - * terms of the GNU General Public License Version 2 or later (the - * "GPL"), in which case the provisions of the GPL are applicable - * instead of those above. If you wish to allow use of your - * version of this file only under the terms of the GPL and not to - * allow others to use your version of this file under the MPL, - * indicate your decision by deleting the provisions above and - * replace them with the notice and other provisions required by - * the GPL. If you do not delete the provisions above, a recipient - * may use your version of this file under either the MPL or the - * GPL. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -#ifdef USE_POLL - #error "Not support USE_POLL" -#endif -#ifdef MD_HAVE_KQUEUE - #error "Not support MD_HAVE_KQUEUE" -#endif -#ifdef MD_HAVE_POLL - #error "Not support MD_HAVE_POLL" -#endif -#ifndef MD_HAVE_EPOLL - #error "Only support MD_HAVE_EPOLL" -#endif - -#include - -typedef struct _epoll_fd_data { - int rd_ref_cnt; - int wr_ref_cnt; - int ex_ref_cnt; - int revents; -} _epoll_fd_data_t; - -static struct _st_epolldata { - _epoll_fd_data_t *fd_data; - struct epoll_event *evtlist; - int fd_data_size; - int evtlist_size; - int evtlist_cnt; - int fd_hint; - int epfd; - pid_t pid; -} *_st_epoll_data; - -#ifndef ST_EPOLL_EVTLIST_SIZE -/* Not a limit, just a hint */ -#define ST_EPOLL_EVTLIST_SIZE 4096 -#endif - -#define _ST_EPOLL_READ_CNT(fd) (_st_epoll_data->fd_data[fd].rd_ref_cnt) -#define _ST_EPOLL_WRITE_CNT(fd) (_st_epoll_data->fd_data[fd].wr_ref_cnt) -#define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt) -#define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents) - -#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0) -#define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0) -#define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0) -#define _ST_EPOLL_EVENTS(fd) \ - (_ST_EPOLL_READ_BIT(fd)|_ST_EPOLL_WRITE_BIT(fd)|_ST_EPOLL_EXCEP_BIT(fd)) - -_st_eventsys_t *_st_eventsys = NULL; - -/***************************************** - * epoll event system - */ - -ST_HIDDEN int _st_epoll_init(void) -{ - int fdlim; - int err = 0; - int rv = 0; - - _st_epoll_data = (struct _st_epolldata *) calloc(1, sizeof(*_st_epoll_data)); - if (!_st_epoll_data) { - return -1; - } - - fdlim = st_getfdlimit(); - _st_epoll_data->fd_hint = (fdlim > 0 && fdlim < ST_EPOLL_EVTLIST_SIZE) ? fdlim : ST_EPOLL_EVTLIST_SIZE; - - if ((_st_epoll_data->epfd = epoll_create(_st_epoll_data->fd_hint)) < 0) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - fcntl(_st_epoll_data->epfd, F_SETFD, FD_CLOEXEC); - _st_epoll_data->pid = getpid(); - - /* Allocate file descriptor data array */ - _st_epoll_data->fd_data_size = _st_epoll_data->fd_hint; - _st_epoll_data->fd_data = (_epoll_fd_data_t *)calloc(_st_epoll_data->fd_data_size, sizeof(_epoll_fd_data_t)); - if (!_st_epoll_data->fd_data) { - err = errno; - rv = -1; - goto cleanup_epoll; - } - - /* Allocate event lists */ - _st_epoll_data->evtlist_size = _st_epoll_data->fd_hint; - _st_epoll_data->evtlist = (struct epoll_event *)malloc(_st_epoll_data->evtlist_size * sizeof(struct epoll_event)); - if (!_st_epoll_data->evtlist) { - err = errno; - rv = -1; - } - - cleanup_epoll: - if (rv < 0) { - if (_st_epoll_data->epfd >= 0) { - close(_st_epoll_data->epfd); - } - free(_st_epoll_data->fd_data); - free(_st_epoll_data->evtlist); - free(_st_epoll_data); - _st_epoll_data = NULL; - errno = err; - } - - return rv; -} - -ST_HIDDEN int _st_epoll_fd_data_expand(int maxfd) -{ - _epoll_fd_data_t *ptr; - int n = _st_epoll_data->fd_data_size; - - while (maxfd >= n) { - n <<= 1; - } - - ptr = (_epoll_fd_data_t *)realloc(_st_epoll_data->fd_data, n * sizeof(_epoll_fd_data_t)); - if (!ptr) { - return -1; - } - - memset(ptr + _st_epoll_data->fd_data_size, 0, (n - _st_epoll_data->fd_data_size) * sizeof(_epoll_fd_data_t)); - - _st_epoll_data->fd_data = ptr; - _st_epoll_data->fd_data_size = n; - - return 0; -} - -ST_HIDDEN void _st_epoll_evtlist_expand(void) -{ - struct epoll_event *ptr; - int n = _st_epoll_data->evtlist_size; - - while (_st_epoll_data->evtlist_cnt > n) { - n <<= 1; - } - - ptr = (struct epoll_event *)realloc(_st_epoll_data->evtlist, n * sizeof(struct epoll_event)); - if (ptr) { - _st_epoll_data->evtlist = ptr; - _st_epoll_data->evtlist_size = n; - } -} - -ST_HIDDEN void _st_epoll_pollset_del(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - struct pollfd *pd; - struct pollfd *epd = pds + npds; - int old_events, events, op; - - /* - * It's more or less OK if deleting fails because a descriptor - * will either be closed or deleted in dispatch function after - * it fires. - */ - for (pd = pds; pd < epd; pd++) { - old_events = _ST_EPOLL_EVENTS(pd->fd); - - if (pd->events & POLLIN) { - _ST_EPOLL_READ_CNT(pd->fd)--; - } - if (pd->events & POLLOUT) { - _ST_EPOLL_WRITE_CNT(pd->fd)--; - } - if (pd->events & POLLPRI) { - _ST_EPOLL_EXCEP_CNT(pd->fd)--; - } - - events = _ST_EPOLL_EVENTS(pd->fd); - /* - * The _ST_EPOLL_REVENTS check below is needed so we can use - * this function inside dispatch(). Outside of dispatch() - * _ST_EPOLL_REVENTS is always zero for all descriptors. - */ - if (events != old_events && _ST_EPOLL_REVENTS(pd->fd) == 0) { - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = pd->fd; - if (epoll_ctl(_st_epoll_data->epfd, op, pd->fd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds) -{ - struct epoll_event ev; - int i, fd; - int old_events, events, op; - - /* Do as many checks as possible up front */ - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - if (fd < 0 || !pds[i].events || (pds[i].events & ~(POLLIN | POLLOUT | POLLPRI))) { - errno = EINVAL; - return -1; - } - if (fd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(fd) < 0) { - return -1; - } - } - - for (i = 0; i < npds; i++) { - fd = pds[i].fd; - old_events = _ST_EPOLL_EVENTS(fd); - - if (pds[i].events & POLLIN) { - _ST_EPOLL_READ_CNT(fd)++; - } - if (pds[i].events & POLLOUT) { - _ST_EPOLL_WRITE_CNT(fd)++; - } - if (pds[i].events & POLLPRI) { - _ST_EPOLL_EXCEP_CNT(fd)++; - } - - events = _ST_EPOLL_EVENTS(fd); - if (events != old_events) { - op = old_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; - ev.events = events; - ev.data.fd = fd; - if (epoll_ctl(_st_epoll_data->epfd, op, fd, &ev) < 0 && (op != EPOLL_CTL_ADD || errno != EEXIST)) { - break; - } - if (op == EPOLL_CTL_ADD) { - _st_epoll_data->evtlist_cnt++; - if (_st_epoll_data->evtlist_cnt > _st_epoll_data->evtlist_size) { - _st_epoll_evtlist_expand(); - } - } - } - } - - if (i < npds) { - /* Error */ - int err = errno; - /* Unroll the state */ - _st_epoll_pollset_del(pds, i + 1); - errno = err; - return -1; - } - - return 0; -} - -ST_HIDDEN void _st_epoll_dispatch(void) -{ - st_utime_t min_timeout; - _st_clist_t *q; - _st_pollq_t *pq; - struct pollfd *pds, *epds; - struct epoll_event ev; - int timeout, nfd, i, osfd, notify; - int events, op; - short revents; - - if (_ST_SLEEPQ == NULL) { - timeout = -1; - } else { - min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK); - timeout = (int) (min_timeout / 1000); - } - - if (_st_epoll_data->pid != getpid()) { - // WINLIN: remove it for bug introduced. - // @see: https://github.com/ossrs/srs/issues/193 - exit(-1); - } - - /* Check for I/O operations */ - nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout); - - if (nfd > 0) { - for (i = 0; i < nfd; i++) { - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = _st_epoll_data->evtlist[i].events; - if (_ST_EPOLL_REVENTS(osfd) & (EPOLLERR | EPOLLHUP)) { - /* Also set I/O bits on error */ - _ST_EPOLL_REVENTS(osfd) |= _ST_EPOLL_EVENTS(osfd); - } - } - - for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) { - pq = _ST_POLLQUEUE_PTR(q); - notify = 0; - epds = pq->pds + pq->npds; - - for (pds = pq->pds; pds < epds; pds++) { - if (_ST_EPOLL_REVENTS(pds->fd) == 0) { - pds->revents = 0; - continue; - } - osfd = pds->fd; - events = pds->events; - revents = 0; - if ((events & POLLIN) && (_ST_EPOLL_REVENTS(osfd) & EPOLLIN)) { - revents |= POLLIN; - } - if ((events & POLLOUT) && (_ST_EPOLL_REVENTS(osfd) & EPOLLOUT)) { - revents |= POLLOUT; - } - if ((events & POLLPRI) && (_ST_EPOLL_REVENTS(osfd) & EPOLLPRI)) { - revents |= POLLPRI; - } - if (_ST_EPOLL_REVENTS(osfd) & EPOLLERR) { - revents |= POLLERR; - } - if (_ST_EPOLL_REVENTS(osfd) & EPOLLHUP) { - revents |= POLLHUP; - } - - pds->revents = revents; - if (revents) { - notify = 1; - } - } - if (notify) { - ST_REMOVE_LINK(&pq->links); - pq->on_ioq = 0; - /* - * Here we will only delete/modify descriptors that - * didn't fire (see comments in _st_epoll_pollset_del()). - */ - _st_epoll_pollset_del(pq->pds, pq->npds); - - if (pq->thread->flags & _ST_FL_ON_SLEEPQ) { - _ST_DEL_SLEEPQ(pq->thread); - } - pq->thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(pq->thread); - } - } - - for (i = 0; i < nfd; i++) { - /* Delete/modify descriptors that fired */ - osfd = _st_epoll_data->evtlist[i].data.fd; - _ST_EPOLL_REVENTS(osfd) = 0; - events = _ST_EPOLL_EVENTS(osfd); - op = events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; - ev.events = events; - ev.data.fd = osfd; - if (epoll_ctl(_st_epoll_data->epfd, op, osfd, &ev) == 0 && op == EPOLL_CTL_DEL) { - _st_epoll_data->evtlist_cnt--; - } - } - } -} - -ST_HIDDEN int _st_epoll_fd_new(int osfd) -{ - if (osfd >= _st_epoll_data->fd_data_size && _st_epoll_fd_data_expand(osfd) < 0) { - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_close(int osfd) -{ - if (_ST_EPOLL_READ_CNT(osfd) || _ST_EPOLL_WRITE_CNT(osfd) || _ST_EPOLL_EXCEP_CNT(osfd)) { - errno = EBUSY; - return -1; - } - - return 0; -} - -ST_HIDDEN int _st_epoll_fd_getlimit(void) -{ - /* zero means no specific limit */ - return 0; -} - -/* - * Check if epoll functions are just stubs. - */ -ST_HIDDEN int _st_epoll_is_supported(void) -{ - struct epoll_event ev; - - ev.events = EPOLLIN; - ev.data.ptr = NULL; - /* Guaranteed to fail */ - epoll_ctl(-1, EPOLL_CTL_ADD, -1, &ev); - - return (errno != ENOSYS); -} - -static _st_eventsys_t _st_epoll_eventsys = { - "epoll", - ST_EVENTSYS_ALT, - _st_epoll_init, - _st_epoll_dispatch, - _st_epoll_pollset_add, - _st_epoll_pollset_del, - _st_epoll_fd_new, - _st_epoll_fd_close, - _st_epoll_fd_getlimit -}; - -/***************************************** - * Public functions - */ - -int st_set_eventsys(int eventsys) -{ - if (_st_eventsys) { - errno = EBUSY; - return -1; - } - - switch (eventsys) { - case ST_EVENTSYS_DEFAULT: - case ST_EVENTSYS_ALT: - default: - if (_st_epoll_is_supported()) { - _st_eventsys = &_st_epoll_eventsys; - break; - } - errno = EINVAL; - return -1; - } - - return 0; -} - -int st_get_eventsys(void) -{ - return _st_eventsys ? _st_eventsys->val : -1; -} - -const char *st_get_eventsys_name(void) -{ - return _st_eventsys ? _st_eventsys->name : ""; -} - diff --git a/trunk/research/st/io.c b/trunk/research/st/io.c deleted file mode 100644 index bc77dc8e10..0000000000 --- a/trunk/research/st/io.c +++ /dev/null @@ -1,792 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - -#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; - } - 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; -} - -#ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT -/* 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; -} - -#else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ -/* - * On some platforms accept() calls from different processes - * on the same listen socket must be serialized. - * The following code serializes accept()'s without process blocking. - * A pipe is used as an inter-process semaphore. - */ -int st_netfd_serialize_accept(_st_netfd_t *fd) -{ - _st_netfd_t **p; - int osfd[2], err; - - if (fd->aux_data) { - errno = EINVAL; - return -1; - } - if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL) { - return -1; - } - if (pipe(osfd) < 0) { - free(p); - return -1; - } - if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) { - fd->aux_data = p; - return 0; - } - /* Error */ - err = errno; - if (p[0]) { - st_netfd_free(p[0]); - } - if (p[1]) { - st_netfd_free(p[1]); - } - close(osfd[0]); - close(osfd[1]); - free(p); - errno = err; - - return -1; -} - -static void _st_netfd_free_aux_data(_st_netfd_t *fd) -{ - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data; - - st_netfd_close(p[0]); - st_netfd_close(p[1]); - free(p); - 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; - _st_netfd_t **p = (_st_netfd_t **) fd->aux_data; - ssize_t n; - char c; - - for ( ; ; ) { - if (p == NULL) { - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen); - } else { - /* Get the lock */ - n = st_read(p[0], &c, 1, timeout); - if (n < 0) { - return NULL; - } - ST_ASSERT(n == 1); - /* Got the lock */ - osfd = accept(fd->osfd, addr, (socklen_t *)addrlen); - /* Unlock */ - err = errno; - n = st_write(p[1], &c, 1, timeout); - ST_ASSERT(n == 1); - errno = err; - } - if (osfd >= 0) { - break; - } - 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; -} -#endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */ - -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; - - while ((n = read(fd->osfd, buf, nbyte)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* 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; - - while ((n = readv(fd->osfd, iov, iov_size)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* 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; - - 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; - } - } - /* 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; - - 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; - } - /* 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; - - while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* 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; - - while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* 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; - - while ((n = recvmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* 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; - - while ((n = sendmsg(fd->osfd, msg, flags)) < 0) { - if (errno == EINTR) { - continue; - } - if (!_IO_NOT_READY_ERROR) { - return -1; - } - /* Wait until the socket becomes writable */ - if (st_netfd_poll(fd, POLLOUT, timeout) < 0) { - return -1; - } - } - - return n; -} - -/* - * To open FIFOs or other special files. - */ -_st_netfd_t *st_open(const char *path, int oflags, mode_t mode) -{ - int osfd, err; - _st_netfd_t *newfd; - - while ((osfd = open(path, oflags | O_NONBLOCK, mode)) < 0) { - if (errno != EINTR) { - return NULL; - } - } - - newfd = _st_netfd_new(osfd, 0, 0); - if (!newfd) { - err = errno; - close(osfd); - errno = err; - } - - return newfd; -} - diff --git a/trunk/research/st/key.c b/trunk/research/st/key.c deleted file mode 100644 index 49d778a187..0000000000 --- a/trunk/research/st/key.c +++ /dev/null @@ -1,116 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include "common.h" - -/* - * Destructor table for per-thread private data - */ -static _st_destructor_t _st_destructors[ST_KEYS_MAX]; -static int key_max = 0; - -/* - * Return a key to be used for thread specific data - */ -int st_key_create(int *keyp, _st_destructor_t destructor) -{ - if (key_max >= ST_KEYS_MAX) { - errno = EAGAIN; - return -1; - } - - *keyp = key_max++; - _st_destructors[*keyp] = destructor; - - return 0; -} - -int st_key_getlimit(void) -{ - return ST_KEYS_MAX; -} - -int st_thread_setspecific(int key, void *value) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (key < 0 || key >= key_max) { - errno = EINVAL; - return -1; - } - - if (value != me->private_data[key]) { - /* free up previously set non-NULL data value */ - if (me->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(me->private_data[key]); - } - me->private_data[key] = value; - } - - return 0; -} - -void *st_thread_getspecific(int key) -{ - if (key < 0 || key >= key_max) { - return NULL; - } - - return ((_ST_CURRENT_THREAD())->private_data[key]); -} - -/* - * Free up all per-thread private data - */ -void _st_thread_cleanup(_st_thread_t *thread) -{ - int key; - - for (key = 0; key < key_max; key++) { - if (thread->private_data[key] && _st_destructors[key]) { - (*_st_destructors[key])(thread->private_data[key]); - thread->private_data[key] = NULL; - } - } -} - diff --git a/trunk/research/st/md.S b/trunk/research/st/md.S deleted file mode 100755 index 883da302b2..0000000000 --- a/trunk/research/st/md.S +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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 - -/****************************************************************/ - -#endif - diff --git a/trunk/research/st/md.h b/trunk/research/st/md.h deleted file mode 100644 index bf82f4d55c..0000000000 --- a/trunk/research/st/md.h +++ /dev/null @@ -1,193 +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 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 (LINUX) - /* linux ok, defined bellow */ -#elif defined (AIX) - #error "AIX not supported" -#elif defined (CYGWIN) - #error "CYGWIN not supported" -#elif defined (DARWIN) - #error "DARWIN not supported" -#elif defined (FREEBSD) - #error "FREEBSD not supported" -#elif defined (HPUX) - #error "HPUX not supported" -#elif defined (IRIX) - #error "IRIX not supported" -#elif defined (NETBSD) - #error "NETBSD not supported" -#elif defined (OPENBSD) - #error "OPENBSD not supported" -#elif defined (OSF1) - #error "OSF1 not supported" -#elif defined (SOLARIS) - #error "SOLARIS not supported" -#else - #error "Unknown OS" -#endif /* OS */ - -/* linux only, defined bellow */ -/* - * 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 -#define MD_ALWAYS_UNSERIALIZED_ACCEPT -/* - * 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 defined(__mips__) - #define MD_STACK_GROWS_DOWN -#else /* Not or mips */ - /* - * 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_STACK_GROWS_DOWN - #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_STACK_GROWS_DOWN - #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(__arm__) - #define MD_STACK_GROWS_DOWN - - #if defined(__GLIBC__) && __GLIBC__ >= 2 - #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[8] - #else - #error "ARM/Linux pre-glibc2 not supported yet" - #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */ - #else - #error "Unknown CPU architecture" - #endif /* Cases with common MD_INIT_CONTEXT and different SP locations */ -#endif /* Cases with different MD_INIT_CONTEXT */ - -#if defined(MD_USE_BUILTIN_SETJMP) && !defined(USE_LIBC_SETJMP) - /* i386/x86_64 */ - #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 - /* arm/mips */ - #define MD_SETJMP(env) setjmp(env) - #define MD_LONGJMP(env, val) longjmp(env, val) -#endif - -/***************************************** - * Other defines - */ -#ifndef MD_STACK_PAD_SIZE - #define MD_STACK_PAD_SIZE 128 -#endif - -#if !defined(MD_HAVE_SOCKLEN_T) && !defined(socklen_t) - #define socklen_t int -#endif - -#ifndef MD_CAP_STACK - #define MD_CAP_STACK(var_addr) -#endif - -#endif /* !__ST_MD_H__ */ - diff --git a/trunk/research/st/public.h b/trunk/research/st/public.h deleted file mode 100644 index 3275191bc7..0000000000 --- a/trunk/research/st/public.h +++ /dev/null @@ -1,164 +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. - */ - -#ifndef __ST_THREAD_H__ -#define __ST_THREAD_H__ - -#include -#include -#include -#include -#include -#include -#include - -#define ST_VERSION "1.9" -#define ST_VERSION_MAJOR 1 -#define ST_VERSION_MINOR 9 - -/* Undefine this to remove the context switch callback feature. */ -#define ST_SWITCH_CB - -#ifndef ETIME - #define ETIME ETIMEDOUT -#endif - -#ifndef ST_UTIME_NO_TIMEOUT - #define ST_UTIME_NO_TIMEOUT ((st_utime_t) -1LL) -#endif - -#ifndef ST_UTIME_NO_WAIT - #define ST_UTIME_NO_WAIT 0 -#endif - -#define ST_EVENTSYS_DEFAULT 0 -#define ST_EVENTSYS_SELECT 1 -#define ST_EVENTSYS_POLL 2 -#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 trd, void **retvalp); - extern void st_thread_interrupt(st_thread_t trd); - extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); - extern int st_randomize_stacks(int on); - extern int st_set_utime_function(st_utime_t (*func)(void)); - - extern st_utime_t st_utime(void); - extern st_utime_t st_utime_last_clock(void); - extern int st_timecache_set(int on); - extern time_t st_time(void); - extern int st_usleep(st_utime_t usecs); - extern int st_sleep(int secs); - extern st_cond_t st_cond_new(void); - extern int st_cond_destroy(st_cond_t cvar); - extern int st_cond_timedwait(st_cond_t cvar, st_utime_t timeout); - extern int st_cond_wait(st_cond_t cvar); - extern int st_cond_signal(st_cond_t cvar); - extern int st_cond_broadcast(st_cond_t cvar); - extern st_mutex_t st_mutex_new(void); - extern int st_mutex_destroy(st_mutex_t lock); - extern int st_mutex_lock(st_mutex_t lock); - extern int st_mutex_unlock(st_mutex_t lock); - extern int st_mutex_trylock(st_mutex_t lock); - - extern int st_key_create(int *keyp, void (*destructor)(void *)); - extern int st_key_getlimit(void); - extern int st_thread_setspecific(int key, void *value); - extern void *st_thread_getspecific(int key); - - extern st_netfd_t st_netfd_open(int osfd); - extern st_netfd_t st_netfd_open_socket(int osfd); - extern void st_netfd_free(st_netfd_t fd); - extern int st_netfd_close(st_netfd_t fd); - extern int st_netfd_fileno(st_netfd_t fd); - extern void st_netfd_setspecific(st_netfd_t fd, void *value, void (*destructor)(void *)); - extern void *st_netfd_getspecific(st_netfd_t fd); - extern int st_netfd_serialize_accept(st_netfd_t fd); - extern int st_netfd_poll(st_netfd_t fd, int how, st_utime_t timeout); - - extern int st_poll(struct pollfd *pds, int npds, st_utime_t timeout); - extern st_netfd_t st_accept(st_netfd_t fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout); - extern int st_connect(st_netfd_t fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout); - extern ssize_t st_read(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); - extern ssize_t st_read_fully(st_netfd_t fd, void *buf, size_t nbyte, st_utime_t timeout); - extern int st_read_resid(st_netfd_t fd, void *buf, size_t *resid, st_utime_t timeout); - extern ssize_t st_readv(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); - extern int st_readv_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); - extern ssize_t st_write(st_netfd_t fd, const void *buf, size_t nbyte, st_utime_t timeout); - extern int st_write_resid(st_netfd_t fd, const void *buf, size_t *resid, st_utime_t timeout); - extern ssize_t st_writev(st_netfd_t fd, const struct iovec *iov, int iov_size, st_utime_t timeout); - extern int st_writev_resid(st_netfd_t fd, struct iovec **iov, int *iov_size, st_utime_t timeout); - extern int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout); - extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout); - extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout); - extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout); - extern st_netfd_t st_open(const char *path, int oflags, mode_t mode); - - #ifdef DEBUG - extern void _st_show_thread_stack(st_thread_t thread, const char *messg); - extern void _st_iterate_threads(void); - #endif -#ifdef __cplusplus -} -#endif - -#endif /* !__ST_THREAD_H__ */ - diff --git a/trunk/research/st/sched.c b/trunk/research/st/sched.c deleted file mode 100755 index 66095cfd1b..0000000000 --- a/trunk/research/st/sched.c +++ /dev/null @@ -1,680 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include -#include "common.h" - -/* 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 *trd; - - if (_ST_RUNQ.next != &_ST_RUNQ) { - /* Pull thread off of the run queue */ - trd = _ST_THREAD_PTR(_ST_RUNQ.next); - _ST_DEL_RUNQ(trd); - } else { - /* If there are no threads to run, switch to the idle thread */ - trd = _st_this_vp.idle_thread; - } - ST_ASSERT(trd->state == _ST_ST_RUNNABLE); - - /* Resume the thread */ - trd->state = _ST_ST_RUNNING; - _ST_RESTORE_CONTEXT(trd); -} - -/* - * Initialize this Virtual Processor - */ -int st_init(void) -{ - _st_thread_t *trd; - - 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 - */ - trd = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + - (ST_KEYS_MAX * sizeof(void *))); - if (!trd) { - return -1; - } - trd->private_data = (void **) (trd + 1); - trd->state = _ST_ST_RUNNING; - trd->flags = _ST_FL_PRIMORDIAL; - _ST_SET_CURRENT_THREAD(trd); - _st_active_count++; -#ifdef DEBUG - _ST_ADD_THREADQ(trd); -#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 *trd = _ST_CURRENT_THREAD(); - - trd->retval = retval; - _st_thread_cleanup(trd); - _st_active_count--; - if (trd->term) { - /* Put thread on the zombie queue */ - trd->state = _ST_ST_ZOMBIE; - _ST_ADD_ZOMBIEQ(trd); - - /* Notify on our termination condition variable */ - st_cond_signal(trd->term); - - /* Switch context and come back later */ - _ST_SWITCH_CONTEXT(trd); - - /* Continue the cleanup */ - st_cond_destroy(trd->term); - trd->term = NULL; - } - -#ifdef DEBUG - _ST_DEL_THREADQ(trd); -#endif - - if (!(trd->flags & _ST_FL_PRIMORDIAL)) { - _st_stack_free(trd->stack); - } - - /* Find another thread to run */ - _ST_SWITCH_CONTEXT(trd); - /* Not going to land here */ -} - -int st_thread_join(_st_thread_t *trd, void **retvalp) -{ - _st_cond_t *term = trd->term; - - /* Can't join a non-joinable thread */ - if (term == NULL) { - errno = EINVAL; - return -1; - } - if (_ST_CURRENT_THREAD() == trd) { - 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 (trd->state != _ST_ST_ZOMBIE) { - if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) { - return -1; - } - } - - if (retvalp) { - *retvalp = trd->retval; - } - - /* - * Remove target thread from the zombie queue and make it runnable. - * When it gets scheduled later, it will do the clean up. - */ - trd->state = _ST_ST_RUNNABLE; - _ST_DEL_ZOMBIEQ(trd); - _ST_ADD_RUNQ(trd); - - return 0; -} - -void _st_thread_main(void) -{ - _st_thread_t *trd = _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(&trd); - - /* Run thread main */ - trd->retval = (*trd->start)(trd->arg); - - /* All done, time to go away */ - st_thread_exit(trd->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 *trd) -{ - int target = trd->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 (trd->due < (*p)->due) { - _st_thread_t *t = *p; - trd->left = t->left; - trd->right = t->right; - *p = trd; - trd->heap_index = index; - trd = t; - } - index <<= 1; - if (target & (1 << bit)) { - p = &((*p)->right); - index |= 1; - } else { - p = &((*p)->left); - } - } - - trd->heap_index = index; - *p = trd; - trd->left = trd->right = NULL; - - return p; -} - -/* - * Delete "thread" from the timeout heap. - */ -static void heap_delete(_st_thread_t *trd) -{ - _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 != trd) { - /* - * Insert the unlinked last element in place of the element we are deleting - */ - t->heap_index = trd->heap_index; - p = heap_insert(t); - t = *p; - t->left = trd->left; - t->right = trd->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; - } - } - } - - trd->left = trd->right = NULL; -} - -void _st_add_sleep_q(_st_thread_t *trd, st_utime_t timeout) -{ - trd->due = _ST_LAST_CLOCK + timeout; - trd->flags |= _ST_FL_ON_SLEEPQ; - trd->heap_index = ++_ST_SLEEPQ_SIZE; - heap_insert(trd); -} - -void _st_del_sleep_q(_st_thread_t *trd) -{ - heap_delete(trd); - trd->flags &= ~_ST_FL_ON_SLEEPQ; -} - -void _st_vp_check_clock(void) -{ - _st_thread_t *trd; - st_utime_t elapsed, now; - - now = st_utime(); - elapsed = now - _ST_LAST_CLOCK; - _ST_LAST_CLOCK = now; - - if (_st_curr_time && now - _st_last_tset > 999000) { - _st_curr_time = time(NULL); - _st_last_tset = now; - } - - while (_ST_SLEEPQ != NULL) { - trd = _ST_SLEEPQ; - ST_ASSERT(trd->flags & _ST_FL_ON_SLEEPQ); - if (trd->due > now) { - break; - } - _ST_DEL_SLEEPQ(trd); - - /* If thread is waiting on condition variable, set the time out flag */ - if (trd->state == _ST_ST_COND_WAIT) { - trd->flags |= _ST_FL_TIMEDOUT; - } - - /* Make thread runnable */ - ST_ASSERT(!(trd->flags & _ST_FL_IDLE_THREAD)); - trd->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(trd); - } -} - -void st_thread_interrupt(_st_thread_t* trd) -{ - /* If thread is already dead */ - if (trd->state == _ST_ST_ZOMBIE) { - return; - } - - trd->flags |= _ST_FL_INTERRUPT; - - if (trd->state == _ST_ST_RUNNING || trd->state == _ST_ST_RUNNABLE) { - return; - } - - if (trd->flags & _ST_FL_ON_SLEEPQ) { - _ST_DEL_SLEEPQ(trd); - } - - /* Make thread runnable */ - trd->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(trd); -} - -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size) -{ - _st_thread_t *trd; - _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 */ -#if defined (MD_STACK_GROWS_DOWN) - sp = stack->stk_top; - /* - * The stack segment is split in the middle. The upper half is used - * as backing store for the register stack which grows upward. - * The lower half is used for the traditional memory stack which - * grows downward. Both stacks start in the middle and grow outward - * from each other. - */ - /** - The below comments is by winlin: - The Stack public structure: - +--------------------------------------------------------------+ - | stack | - +--------------------------------------------------------------+ - bottom top - The code bellow use the stack as: - +-----------------+-----------------+-------------+------------+ - | stack of thread |pad+align(128B+) |thread(336B) | keys(128B) | - +-----------------+-----------------+-------------+------------+ - bottom sp trd ptds top - (context[0].__jmpbuf.sp) (private_data) - */ - sp = sp - (ST_KEYS_MAX * sizeof(void *)); - ptds = (void **) sp; - sp = sp - sizeof(_st_thread_t); - trd = (_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; -#else - #error "Only Supports Stack Grown Down" -#endif - - memset(trd, 0, sizeof(_st_thread_t)); - memset(ptds, 0, ST_KEYS_MAX * sizeof(void *)); - - /* Initialize thread */ - trd->private_data = ptds; - trd->stack = stack; - trd->start = start; - trd->arg = arg; - -// by winlin, expand macro MD_INIT_CONTEXT -#if defined(__mips__) - MD_SETJMP((trd)->context); - trd->context[0].__jmpbuf[0].__pc = (__ptr_t) _st_thread_main; - trd->context[0].__jmpbuf[0].__sp = stack->sp; -#else - if (MD_SETJMP((trd)->context)) { - _st_thread_main(); - } - MD_GET_SP(trd) = (long) (stack->sp); -#endif - - /* If thread is joinable, allocate a termination condition variable */ - if (joinable) { - trd->term = st_cond_new(); - if (trd->term == NULL) { - _st_stack_free(trd->stack); - return NULL; - } - } - - /* Make thread runnable */ - trd->state = _ST_ST_RUNNABLE; - _st_active_count++; - _ST_ADD_RUNQ(trd); -#ifdef DEBUG - _ST_ADD_THREADQ(trd); -#endif - - return trd; -} - -_st_thread_t *st_thread_self(void) -{ - return _ST_CURRENT_THREAD(); -} - -#ifdef DEBUG -/* ARGSUSED */ -void _st_show_thread_stack(_st_thread_t *trd, const char *messg) -{ -} - -/* To be set from debugger */ -int _st_iterate_threads_flag = 0; - -void _st_iterate_threads(void) -{ - static _st_thread_t *trd = NULL; - static jmp_buf orig_jb, save_jb; - _st_clist_t *q; - - if (!_st_iterate_threads_flag) { - if (trd) { - memcpy(trd->context, save_jb, sizeof(jmp_buf)); - MD_LONGJMP(orig_jb, 1); - } - return; - } - - if (trd) { - memcpy(trd->context, save_jb, sizeof(jmp_buf)); - _st_show_thread_stack(trd, NULL); - } else { - if (MD_SETJMP(orig_jb)) { - _st_iterate_threads_flag = 0; - trd = NULL; - _st_show_thread_stack(trd, "Iteration completed"); - return; - } - trd = _ST_CURRENT_THREAD(); - _st_show_thread_stack(trd, "Iteration started"); - } - - q = trd->tlink.next; - if (q == &_ST_THREADQ) { - q = q->next; - } - ST_ASSERT(q != &_ST_THREADQ); - trd = _ST_THREAD_THREADQ_PTR(q); - if (trd == _ST_CURRENT_THREAD()) { - MD_LONGJMP(orig_jb, 1); - } - memcpy(save_jb, trd->context, sizeof(jmp_buf)); - MD_LONGJMP(trd->context, 1); -} -#endif /* DEBUG */ - diff --git a/trunk/research/st/srs.c b/trunk/research/st/srs.c deleted file mode 100644 index 09bc5eacca..0000000000 --- a/trunk/research/st/srs.c +++ /dev/null @@ -1,497 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "public.h" - -#define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") - -int io_port = 1990; -int sleep_ms = 100; - -void stack_print(long int previous_sp, int level) -{ - if (level <= 0) { - return; - } - - register long int rsp asm("sp"); - char buf[level * 1024]; - - stack_print(rsp, level - 1); - - srs_trace("%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)", - level, previous_sp, rsp, (int)(previous_sp - rsp), - (int)(previous_sp - rsp - sizeof(buf)), (int)(sizeof(buf) / 1024)); -} - -int huge_stack_test() -{ - srs_trace("==================================================="); - srs_trace("huge_stack test: start"); - - register long int rsp asm("sp"); - stack_print(rsp, 10); - - srs_trace("huge_stack test: end"); - - return 0; -} - -int sleep_test() -{ - srs_trace("==================================================="); - srs_trace("sleep test: start"); - - srs_trace("1. sleep..."); - st_utime_t start = st_utime(); - st_usleep(sleep_ms * 1000); - st_utime_t end = st_utime(); - - srs_trace("2. sleep ok, sleep=%dus, deviation=%dus", - (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000)); - - srs_trace("sleep test: end"); - - return 0; -} - -void* sleep2_func0(void* arg) -{ - int sleep_ms = 100; - st_utime_t start = st_utime(); - st_usleep(sleep_ms * 1000); - st_utime_t end = st_utime(); - - srs_trace("sleep ok, sleep=%dus, deviation=%dus", - (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000)); - - return NULL; -} - -void* sleep2_func1(void* arg) -{ - int sleep_ms = 250; - st_utime_t start = st_utime(); - st_usleep(sleep_ms * 1000); - st_utime_t end = st_utime(); - - srs_trace("sleep ok, sleep=%dus, deviation=%dus", - (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000)); - - return NULL; -} - -int sleep2_test() -{ - srs_trace("==================================================="); - srs_trace("sleep2 test: start"); - - st_thread_t trd0 = st_thread_create(sleep2_func0, NULL, 1, 0); - st_thread_t trd1 = st_thread_create(sleep2_func1, NULL, 1, 0); - st_thread_join(trd0, NULL); - st_thread_join(trd1, NULL); - - srs_trace("sleep test: end"); - - return 0; -} - -st_mutex_t sleep_work_cond = NULL; -void* sleep_deviation_func(void* arg) -{ - st_mutex_lock(sleep_work_cond); - srs_trace("2. work thread start."); - - int64_t i; - for (i = 0; i < 3000000000ULL; i++) { - } - - st_mutex_unlock(sleep_work_cond); - srs_trace("3. work thread end."); - - return NULL; -} - -int sleep_deviation_test() -{ - srs_trace("==================================================="); - srs_trace("sleep deviation test: start"); - - sleep_work_cond = st_mutex_new(); - - st_thread_create(sleep_deviation_func, NULL, 0, 0); - st_mutex_lock(sleep_work_cond); - - srs_trace("1. sleep..."); - st_utime_t start = st_utime(); - - // other thread to do some complex work. - st_mutex_unlock(sleep_work_cond); - st_usleep(1000 * 1000); - - st_utime_t end = st_utime(); - - srs_trace("4. sleep ok, sleep=%dus, deviation=%dus", - (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000)); - - st_mutex_lock(sleep_work_cond); - srs_trace("sleep deviation test: end"); - - st_mutex_destroy(sleep_work_cond); - - return 0; -} - -void* thread_func(void* arg) -{ - srs_trace("1. thread run"); - st_usleep(sleep_ms * 1000); - srs_trace("2. thread completed"); - return NULL; -} - -int thread_test() -{ - srs_trace("==================================================="); - srs_trace("thread test: start"); - - st_thread_t trd = st_thread_create(thread_func, NULL, 1, 0); - if (trd == NULL) { - srs_trace("st_thread_create failed"); - return -1; - } - - st_thread_join(trd, NULL); - srs_trace("3. thread joined"); - - srs_trace("thread test: end"); - - return 0; -} - -st_mutex_t sync_start = NULL; -st_cond_t sync_cond = NULL; -st_mutex_t sync_mutex = NULL; -st_cond_t sync_end = NULL; - -void* sync_master(void* arg) -{ - // wait for main to sync_start this thread. - st_mutex_lock(sync_start); - st_mutex_unlock(sync_start); - - st_usleep(sleep_ms * 1000); - st_cond_signal(sync_cond); - - st_mutex_lock(sync_mutex); - srs_trace("2. st mutex is ok"); - st_mutex_unlock(sync_mutex); - - st_usleep(sleep_ms * 1000); - srs_trace("3. st thread is ok"); - st_cond_signal(sync_cond); - - return NULL; -} - -void* sync_slave(void* arg) -{ - // lock mutex to control thread. - st_mutex_lock(sync_mutex); - - // wait for main to sync_start this thread. - st_mutex_lock(sync_start); - st_mutex_unlock(sync_start); - - // wait thread to ready. - st_cond_wait(sync_cond); - srs_trace("1. st cond is ok"); - - // release mutex to control thread - st_usleep(sleep_ms * 1000); - st_mutex_unlock(sync_mutex); - - // wait thread to exit. - st_cond_wait(sync_cond); - srs_trace("4. st is ok"); - - st_cond_signal(sync_end); - - return NULL; -} - -int sync_test() -{ - srs_trace("==================================================="); - srs_trace("sync test: start"); - - if ((sync_start = st_mutex_new()) == NULL) { - srs_trace("st_mutex_new sync_start failed"); - return -1; - } - st_mutex_lock(sync_start); - - if ((sync_cond = st_cond_new()) == NULL) { - srs_trace("st_cond_new cond failed"); - return -1; - } - - if ((sync_end = st_cond_new()) == NULL) { - srs_trace("st_cond_new end failed"); - return -1; - } - - if ((sync_mutex = st_mutex_new()) == NULL) { - srs_trace("st_mutex_new mutex failed"); - return -1; - } - - if (!st_thread_create(sync_master, NULL, 0, 0)) { - srs_trace("st_thread_create failed"); - return -1; - } - - if (!st_thread_create(sync_slave, NULL, 0, 0)) { - srs_trace("st_thread_create failed"); - return -1; - } - - // run all threads. - st_mutex_unlock(sync_start); - - st_cond_wait(sync_end); - srs_trace("sync test: end"); - - return 0; -} - -void* io_client(void* arg) -{ - - int fd; - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - srs_trace("create linux socket error."); - return NULL; - } - srs_trace("6. client create linux socket success. fd=%d", fd); - - st_netfd_t stfd; - if ((stfd = st_netfd_open_socket(fd)) == NULL){ - srs_trace("st_netfd_open_socket open socket failed."); - return NULL; - } - srs_trace("7. client st open socket success. fd=%d", fd); - - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(io_port); - addr.sin_addr.s_addr = INADDR_ANY; - if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1) { - srs_trace("bind socket error."); - return NULL; - } - - char buf[1024]; - if (st_read_fully(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { - srs_trace("st_read_fully failed"); - return NULL; - } - if (st_write(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { - srs_trace("st_write failed"); - return NULL; - } - - st_netfd_close(stfd); - - return NULL; -} - -int io_test() -{ - srs_trace("==================================================="); - srs_trace("io test: start, port=%d", io_port); - - int fd; - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - srs_trace("create linux socket error."); - return -1; - } - srs_trace("1. server create linux socket success. fd=%d", fd); - - int reuse_socket = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) { - srs_trace("setsockopt reuse-addr error."); - return -1; - } - srs_trace("2. server setsockopt reuse-addr success. fd=%d", fd); - - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(io_port); - addr.sin_addr.s_addr = INADDR_ANY; - if (bind(fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) { - srs_trace("bind socket error."); - return -1; - } - srs_trace("3. server bind socket success. fd=%d", fd); - - if (listen(fd, 10) == -1) { - srs_trace("listen socket error."); - return -1; - } - srs_trace("4. server listen socket success. fd=%d", fd); - - st_netfd_t stfd; - if ((stfd = st_netfd_open_socket(fd)) == NULL){ - srs_trace("st_netfd_open_socket open socket failed."); - return -1; - } - srs_trace("5. server st open socket success. fd=%d", fd); - - if (!st_thread_create(io_client, NULL, 0, 0)) { - srs_trace("st_thread_create failed"); - return -1; - } - - st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT); - srs_trace("8. server get a client. fd=%d", st_netfd_fileno(client_stfd)); - - char buf[1024]; - if (st_write(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { - srs_trace("st_write failed"); - return -1; - } - if (st_read_fully(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) { - srs_trace("st_read_fully failed"); - return -1; - } - srs_trace("9. server io completed."); - - st_netfd_close(stfd); - st_netfd_close(client_stfd); - - srs_trace("io test: end"); - return 0; -} - -int pipe_test() -{ - srs_trace("==================================================="); - srs_trace("pipe test: start"); - - int fds[2]; - if (pipe(fds) < 0) { - srs_trace("pipe failed"); - return -1; - } - srs_trace("1. pipe ok, %d=>%d", fds[1], fds[0]); - - st_netfd_t fdw; - if ((fdw = st_netfd_open_socket(fds[1])) == NULL) { - srs_trace("st_netfd_open_socket open socket failed."); - return -1; - } - srs_trace("2. open write fd ok"); - - st_netfd_t fdr; - if ((fdr = st_netfd_open_socket(fds[0])) == NULL) { - srs_trace("st_netfd_open_socket open socket failed."); - return -1; - } - srs_trace("3. open read fd ok"); - - char buf[1024]; - if (st_write(fdw, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) < 0) { - srs_trace("st_write socket failed."); - return -1; - } - srs_trace("4. write to pipe ok"); - - if (st_read(fdr, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) < 0) { - srs_trace("st_read socket failed."); - return -1; - } - srs_trace("5. read from pipe ok"); - - st_netfd_close(fdw); - st_netfd_close(fdr); - - srs_trace("pipe test: end"); - return 0; -} - -int main(int argc, char** argv) -{ - srs_trace("ETIME=%d", ETIME); - - if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { - srs_trace("st_set_eventsys failed"); - return -1; - } - - if (st_init() < 0) { - srs_trace("st_init failed"); - return -1; - } - - if (sleep2_test() < 0) { - srs_trace("sleep2_test failed"); - return -1; - } - - if (sleep_test() < 0) { - srs_trace("sleep_test failed"); - return -1; - } - - if (sleep_deviation_test() < 0) { - srs_trace("sleep_deviation_test failed"); - return -1; - } - - if (huge_stack_test() < 0) { - srs_trace("huge_stack_test failed"); - return -1; - } - - if (thread_test() < 0) { - srs_trace("thread_test failed"); - return -1; - } - - if (sync_test() < 0) { - srs_trace("sync_test failed"); - return -1; - } - - if (io_test() < 0) { - srs_trace("io_test failed"); - return -1; - } - - if (pipe_test() < 0) { - srs_trace("pipe_test failed"); - return -1; - } - - // cleanup. - srs_trace("wait for all thread completed"); - st_thread_exit(NULL); - // the following never enter, - // the above code will exit when all thread exit, - // current is a primordial st-thread, when all thread exit, - // the st idle thread will exit(0), see _st_idle_thread_start() - srs_trace("all thread completed"); - - return 0; -} - diff --git a/trunk/research/st/st/init b/trunk/research/st/st/init deleted file mode 100644 index 61604b75fb..0000000000 --- a/trunk/research/st/st/init +++ /dev/null @@ -1,3 +0,0 @@ -#ifndef _st_icpp_init_stub -#define _st_icpp_init_stub -#endif diff --git a/trunk/research/st/st/st.upp b/trunk/research/st/st/st.upp deleted file mode 100755 index dab6d49589..0000000000 --- a/trunk/research/st/st/st.upp +++ /dev/null @@ -1,18 +0,0 @@ -file - main readonly separator, - ..\srs.c, - st readonly separator, - ..\common.h, - ..\event.c, - ..\io.c, - ..\key.c, - ..\md.h, - ..\md.S, - ..\public.h, - ..\sched.c, - ..\stk.c, - ..\sync.c; - -mainconfig - "" = "MAIN"; - diff --git a/trunk/research/st/stk.c b/trunk/research/st/stk.c deleted file mode 100644 index c26223ba5f..0000000000 --- a/trunk/research/st/stk.c +++ /dev/null @@ -1,169 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include -#include -#include "common.h" - -/* How much space to leave between the stacks, at each end */ -#define REDZONE _ST_PAGE_SIZE - -_st_clist_t _st_free_stacks = ST_INIT_STATIC_CLIST(&_st_free_stacks); -int _st_num_free_stacks = 0; -int _st_randomize_stacks = 0; - -static char *_st_new_stk_segment(int size); - -/** -The below comments is by winlin: -The stack memory struct: - | REDZONE | stack | extra | REDZONE | - +---------+------------------------+---------+---------+ - | 4k | | 4k/0 | 4k | - +---------+------------------------+---------+---------+ - vaddr bottom top -When _st_randomize_stacks is on, by st_randomize_stacks(), -the bottom and top will random movided in the extra: - long offset = (random() % extra) & ~0xf; - ts->stk_bottom += offset; - ts->stk_top += offset; -Both REDZONE are protected by mprotect when DEBUG is on. -*/ -_st_stack_t *_st_stack_new(int stack_size) -{ - _st_clist_t *qp; - _st_stack_t *ts; - int extra; - - // TODO: WINLIN: remove the stack reuse. - 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; - -#ifdef DEBUG - 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 - #error "Only Supports Malloc Stack" -#endif - - return (char *)vaddr; -} - -/* Not used */ -#if 0 -void _st_delete_stk_segment(char *vaddr, int size) -{ -#ifdef MALLOC_STACK - free(vaddr); -#else - #error Unknown Stack Malloc -#endif -} -#endif - -int st_randomize_stacks(int on) -{ - int wason = _st_randomize_stacks; - - _st_randomize_stacks = on; - if (on) { - srandom((unsigned int) st_utime()); - } - - return wason; -} diff --git a/trunk/research/st/sync.c b/trunk/research/st/sync.c deleted file mode 100644 index 3e5324084a..0000000000 --- a/trunk/research/st/sync.c +++ /dev/null @@ -1,352 +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 file is derived directly from Netscape Communications Corporation, - * and consists of extensive modifications made during the year(s) 1999-2000. - */ - -#include -#include -#include -#include "common.h" - -extern time_t _st_curr_time; -extern st_utime_t _st_last_tset; -extern int _st_active_count; - -static st_utime_t (*_st_utime)(void) = NULL; - -/***************************************** - * Time functions - */ - -st_utime_t st_utime(void) -{ - if (_st_utime == NULL) { -#ifdef MD_GET_UTIME - MD_GET_UTIME(); -#else - #error Unknown OS -#endif - } - - return (*_st_utime)(); -} - -int st_set_utime_function(st_utime_t (*func)(void)) -{ - if (_st_active_count) { - errno = EINVAL; - return -1; - } - - _st_utime = func; - - return 0; -} - -st_utime_t st_utime_last_clock(void) -{ - return _ST_LAST_CLOCK; -} - -int st_timecache_set(int on) -{ - int wason = (_st_curr_time) ? 1 : 0; - - if (on) { - _st_curr_time = time(NULL); - _st_last_tset = st_utime(); - } else { - _st_curr_time = 0; - } - - return wason; -} - -time_t st_time(void) -{ - if (_st_curr_time) { - return _st_curr_time; - } - - return time(NULL); -} - -int st_usleep(st_utime_t usecs) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (usecs != ST_UTIME_NO_TIMEOUT) { - me->state = _ST_ST_SLEEPING; - _ST_ADD_SLEEPQ(me, usecs); - } else { - me->state = _ST_ST_SUSPENDED; - } - - _ST_SWITCH_CONTEXT(me); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - -int st_sleep(int secs) -{ - return st_usleep((secs >= 0) ? secs * (st_utime_t) 1000000LL : ST_UTIME_NO_TIMEOUT); -} - -/***************************************** - * Condition variable functions - */ -_st_cond_t *st_cond_new(void) -{ - _st_cond_t *cvar; - - cvar = (_st_cond_t *) calloc(1, sizeof(_st_cond_t)); - if (cvar) { - ST_INIT_CLIST(&cvar->wait_q); - } - - return cvar; -} - -int st_cond_destroy(_st_cond_t *cvar) -{ - if (cvar->wait_q.next != &cvar->wait_q) { - errno = EBUSY; - return -1; - } - - free(cvar); - - return 0; -} - -int st_cond_timedwait(_st_cond_t *cvar, st_utime_t timeout) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - int rv; - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - /* Put caller thread on the condition variable's wait queue */ - me->state = _ST_ST_COND_WAIT; - ST_APPEND_LINK(&me->wait_links, &cvar->wait_q); - - if (timeout != ST_UTIME_NO_TIMEOUT) { - _ST_ADD_SLEEPQ(me, timeout); - } - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - rv = 0; - - if (me->flags & _ST_FL_TIMEDOUT) { - me->flags &= ~_ST_FL_TIMEDOUT; - errno = ETIME; - rv = -1; - } - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - rv = -1; - } - - return rv; -} - -int st_cond_wait(_st_cond_t *cvar) -{ - return st_cond_timedwait(cvar, ST_UTIME_NO_TIMEOUT); -} - -static int _st_cond_signal(_st_cond_t *cvar, int broadcast) -{ - _st_thread_t *thread; - _st_clist_t *q; - - for (q = cvar->wait_q.next; q != &cvar->wait_q; q = q->next) { - thread = _ST_THREAD_WAITQ_PTR(q); - if (thread->state == _ST_ST_COND_WAIT) { - if (thread->flags & _ST_FL_ON_SLEEPQ) { - _ST_DEL_SLEEPQ(thread); - } - - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - if (!broadcast) { - break; - } - } - } - - return 0; -} - -int st_cond_signal(_st_cond_t *cvar) -{ - return _st_cond_signal(cvar, 0); -} - -int st_cond_broadcast(_st_cond_t *cvar) -{ - return _st_cond_signal(cvar, 1); -} - -/***************************************** - * Mutex functions - */ -_st_mutex_t *st_mutex_new(void) -{ - _st_mutex_t *lock; - - lock = (_st_mutex_t *) calloc(1, sizeof(_st_mutex_t)); - if (lock) { - ST_INIT_CLIST(&lock->wait_q); - lock->owner = NULL; - } - - return lock; -} - -int st_mutex_destroy(_st_mutex_t *lock) -{ - if (lock->owner != NULL || lock->wait_q.next != &lock->wait_q) { - errno = EBUSY; - return -1; - } - - free(lock); - - return 0; -} - -int st_mutex_lock(_st_mutex_t *lock) -{ - _st_thread_t *me = _ST_CURRENT_THREAD(); - - if (me->flags & _ST_FL_INTERRUPT) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - if (lock->owner == NULL) { - /* Got the mutex */ - lock->owner = me; - return 0; - } - - if (lock->owner == me) { - errno = EDEADLK; - return -1; - } - - /* Put caller thread on the mutex's wait queue */ - me->state = _ST_ST_LOCK_WAIT; - ST_APPEND_LINK(&me->wait_links, &lock->wait_q); - - _ST_SWITCH_CONTEXT(me); - - ST_REMOVE_LINK(&me->wait_links); - - if ((me->flags & _ST_FL_INTERRUPT) && lock->owner != me) { - me->flags &= ~_ST_FL_INTERRUPT; - errno = EINTR; - return -1; - } - - return 0; -} - -int st_mutex_unlock(_st_mutex_t *lock) -{ - _st_thread_t *thread; - _st_clist_t *q; - - if (lock->owner != _ST_CURRENT_THREAD()) { - errno = EPERM; - return -1; - } - - for (q = lock->wait_q.next; q != &lock->wait_q; q = q->next) { - thread = _ST_THREAD_WAITQ_PTR(q); - if (thread->state == _ST_ST_LOCK_WAIT) { - lock->owner = thread; - /* Make thread runnable */ - thread->state = _ST_ST_RUNNABLE; - _ST_ADD_RUNQ(thread); - return 0; - } - } - - /* No threads waiting on this mutex */ - lock->owner = NULL; - - return 0; -} - -int st_mutex_trylock(_st_mutex_t *lock) -{ - if (lock->owner != NULL) { - errno = EBUSY; - return -1; - } - - /* Got the mutex */ - lock->owner = _ST_CURRENT_THREAD(); - - return 0; -} - diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index b33b4f6f8e..7ebb10b146 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -72,7 +72,7 @@ SrsUdpListener::SrsUdpListener(ISrsUdpHandler* h, string i, int p) handler = h; ip = i; port = p; - lfd = NULL; + lfd = -1; nb_buf = SRS_UDP_MAX_PACKET_SIZE; buf = new char[nb_buf]; @@ -148,7 +148,7 @@ SrsTcpListener::SrsTcpListener(ISrsTcpHandler* h, string i, int p) ip = i; port = p; - lfd = NULL; + lfd = -1; trd = new SrsDummyCoroutine(); } @@ -161,7 +161,7 @@ SrsTcpListener::~SrsTcpListener() int SrsTcpListener::fd() { - return srs_netfd_fileno(lfd);; + return srs_netfd_fileno(lfd); } srs_error_t SrsTcpListener::listen() @@ -191,10 +191,10 @@ srs_error_t SrsTcpListener::cycle() } srs_netfd_t fd = srs_accept(lfd, NULL, NULL, SRS_UTIME_NO_TIMEOUT); - if(fd == NULL){ + if(fd < 0){ return srs_error_new(ERROR_SOCKET_ACCEPT, "accept at fd=%d", srs_netfd_fileno(lfd)); } - + if ((err = srs_fd_closeexec(srs_netfd_fileno(fd))) != srs_success) { return srs_error_wrap(err, "set closeexec"); } diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 61eeb23094..e42f8314b2 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -32,6 +32,8 @@ #include using namespace std; +#include + #include #include #include @@ -344,7 +346,7 @@ SrsSignalManager::SrsSignalManager(SrsServer* s) server = s; sig_pipe[0] = sig_pipe[1] = -1; trd = new SrsSTCoroutine("signal", this); - signal_read_stfd = NULL; + signal_read_stfd = -1; } SrsSignalManager::~SrsSignalManager() @@ -368,7 +370,7 @@ srs_error_t SrsSignalManager::initialize() return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "create pipe"); } - if ((signal_read_stfd = srs_netfd_open(sig_pipe[0])) == NULL) { + if ((signal_read_stfd = srs_netfd_open(sig_pipe[0])) < 0) { return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "open pipe"); } @@ -794,7 +796,7 @@ srs_error_t SrsServer::ingest() srs_error_t SrsServer::cycle() { srs_error_t err = do_cycle(); - + #ifdef SRS_AUTO_GPERF_MC destroy(); @@ -885,6 +887,10 @@ srs_error_t SrsServer::do_cycle() // the daemon thread, update the time cache // TODO: FIXME: use SrsHourGlass. + + // FIXME: libco will take over user's event loop + co_eventloop(co_get_epoll_ct(), NULL, NULL); + while (true) { if (handler && (err = handler->on_cycle()) != srs_success) { return srs_error_wrap(err, "handle callback"); diff --git a/trunk/src/app/srs_app_st.cpp b/trunk/src/app/srs_app_st.cpp index 831a0613a7..5c4c7b87bf 100755 --- a/trunk/src/app/srs_app_st.cpp +++ b/trunk/src/app/srs_app_st.cpp @@ -23,7 +23,7 @@ #include -#include +#include #include using namespace std; @@ -79,13 +79,24 @@ int SrsDummyCoroutine::cid() return 0; } -_ST_THREAD_CREATE_PFN _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)st_thread_create; +void* co_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size) { + (void)joinable; + (void)stack_size; + + stCoRoutine_t *co = NULL; + co_create(&co, NULL, start, arg); + co_resume(co); + return co; +} + +_ST_THREAD_CREATE_PFN _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)co_thread_create; SrsSTCoroutine::SrsSTCoroutine(string n, ISrsCoroutineHandler* h, int cid) { name = n; handler = h; context = cid; + term = co_cond_alloc(); trd = NULL; trd_err = srs_success; started = interrupted = disposed = cycle_done = false; @@ -94,7 +105,8 @@ SrsSTCoroutine::SrsSTCoroutine(string n, ISrsCoroutineHandler* h, int cid) SrsSTCoroutine::~SrsSTCoroutine() { stop(); - + + co_cond_free(term); srs_freep(trd_err); } @@ -142,8 +154,8 @@ void SrsSTCoroutine::stop() // When not started, the rd is NULL. if (trd) { void* res = NULL; - int r0 = st_thread_join((st_thread_t)trd, &res); - srs_assert(!r0); + + co_cond_timedwait(term, -1); srs_error_t err_res = (srs_error_t)res; if (err_res != srs_success) { @@ -171,8 +183,6 @@ void SrsSTCoroutine::interrupt() if (trd_err == srs_success) { trd_err = srs_error_new(ERROR_THREAD_INTERRUPED, "interrupted"); } - - st_thread_interrupt((st_thread_t)trd); } srs_error_t SrsSTCoroutine::pull() @@ -220,6 +230,9 @@ void* SrsSTCoroutine::pfn(void* arg) p->trd_err = err; } + // FIXME: + //co_cond_signal(term); + return (void*)err; } diff --git a/trunk/src/app/srs_app_st.hpp b/trunk/src/app/srs_app_st.hpp index 956e00b42b..f158d5ba63 100644 --- a/trunk/src/app/srs_app_st.hpp +++ b/trunk/src/app/srs_app_st.hpp @@ -102,6 +102,8 @@ class SrsDummyCoroutine : public SrsCoroutine typedef void* (*_ST_THREAD_CREATE_PFN)(void *(*start)(void *arg), void *arg, int joinable, int stack_size); extern _ST_THREAD_CREATE_PFN _pfn_st_thread_create; +struct stCoCond_t; + // A ST-coroutine is a lightweight thread, just like the goroutine. // But the goroutine maybe run on different thread, while ST-coroutine only // run in single thread, because it use setjmp and longjmp, so it may cause @@ -120,6 +122,7 @@ class SrsSTCoroutine : public SrsCoroutine std::string name; ISrsCoroutineHandler* handler; private: + stCoCond_t* term; srs_thread_t trd; int context; srs_error_t trd_err; diff --git a/trunk/src/service/srs_service_st.cpp b/trunk/src/service/srs_service_st.cpp index f63cd42794..dba7adbe8d 100644 --- a/trunk/src/service/srs_service_st.cpp +++ b/trunk/src/service/srs_service_st.cpp @@ -23,10 +23,11 @@ #include -#include +#include #include #include #include +#include using namespace std; #include @@ -41,6 +42,17 @@ using namespace std; #ifdef __linux__ #include +static int set_fd_nonblock(int fd) +{ + int flags; + + flags = fcntl(fd, F_GETFL, 0); + flags |= O_NONBLOCK; + flags |= O_NDELAY; + int ret = fcntl(fd, F_SETFL, flags); + return ret; +} + bool srs_st_epoll_is_supported(void) { struct epoll_event ev; @@ -56,37 +68,12 @@ bool srs_st_epoll_is_supported(void) srs_error_t srs_st_init() { -#ifdef __linux__ - // check epoll, some old linux donot support epoll. - // @see https://github.com/ossrs/srs/issues/162 - if (!srs_st_epoll_is_supported()) { - return srs_error_new(ERROR_ST_SET_EPOLL, "linux epoll disabled"); - } -#endif - - // Select the best event system available on the OS. In Linux this is - // epoll(). On BSD it will be kqueue. - if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) { - return srs_error_new(ERROR_ST_SET_EPOLL, "st enable st failed, current is %s", st_get_eventsys_name()); - } - - int r0 = 0; - if((r0 = st_init()) != 0){ - return srs_error_new(ERROR_ST_INITIALIZE, "st initialize failed, r0=%d", r0); - } - srs_trace("st_init success, use %s", st_get_eventsys_name()); - return srs_success; } void srs_close_stfd(srs_netfd_t& stfd) { - if (stfd) { - // we must ensure the close is ok. - int err = st_netfd_close((st_netfd_t)stfd); - srs_assert(err != -1); - stfd = NULL; - } + ::close(stfd); } srs_error_t srs_fd_closeexec(int fd) @@ -144,18 +131,17 @@ srs_error_t srs_fd_keepalive(int fd) srs_thread_t srs_thread_self() { - return (srs_thread_t)st_thread_self(); + return (srs_thread_t)co_self(); } srs_error_t srs_tcp_connect(string server, int port, srs_utime_t tm, srs_netfd_t* pstfd) { - st_utime_t timeout = ST_UTIME_NO_TIMEOUT; + srs_utime_t timeout = SRS_UTIME_NO_TIMEOUT; if (tm != SRS_UTIME_NO_TIMEOUT) { timeout = tm; } - - *pstfd = NULL; - srs_netfd_t stfd = NULL; + + (void)timeout; char sport[8]; snprintf(sport, sizeof(sport), "%d", port); @@ -175,20 +161,15 @@ srs_error_t srs_tcp_connect(string server, int port, srs_utime_t tm, srs_netfd_t if(sock == -1){ return srs_error_new(ERROR_SOCKET_CREATE, "create socket"); } + + *pstfd = sock; - srs_assert(!stfd); - stfd = st_netfd_open_socket(sock); - if(stfd == NULL){ - ::close(sock); - return srs_error_new(ERROR_ST_OPEN_SOCKET, "open socket"); - } - - if (st_connect((st_netfd_t)stfd, r->ai_addr, r->ai_addrlen, timeout) == -1){ - srs_close_stfd(stfd); + // TODO: timeout + if (connect(sock, r->ai_addr, r->ai_addrlen) == -1) { + srs_close_stfd(sock); return srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", server.c_str(), port); } - *pstfd = stfd; return srs_success; } @@ -214,7 +195,7 @@ srs_error_t do_srs_tcp_listen(int fd, addrinfo* r, srs_netfd_t* pfd) return srs_error_wrap(err, "set reuseport"); } - if (bind(fd, r->ai_addr, r->ai_addrlen) == -1) { + if (::bind(fd, r->ai_addr, r->ai_addrlen) == -1) { return srs_error_new(ERROR_SOCKET_BIND, "bind"); } @@ -222,10 +203,6 @@ srs_error_t do_srs_tcp_listen(int fd, addrinfo* r, srs_netfd_t* pfd) return srs_error_new(ERROR_SOCKET_LISTEN, "listen"); } - if ((*pfd = srs_netfd_open_socket(fd)) == NULL){ - return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open"); - } - return err; } @@ -255,11 +232,15 @@ srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd) r->ai_family, r->ai_socktype, r->ai_protocol); } + set_fd_nonblock(fd); + if ((err = do_srs_tcp_listen(fd, r, pfd)) != srs_success) { ::close(fd); return srs_error_wrap(err, "fd=%d", fd); } + *pfd = fd; + return err; } @@ -283,10 +264,6 @@ srs_error_t do_srs_udp_listen(int fd, addrinfo* r, srs_netfd_t* pfd) return srs_error_new(ERROR_SOCKET_BIND, "bind"); } - if ((*pfd = srs_netfd_open_socket(fd)) == NULL){ - return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open"); - } - return err; } @@ -326,85 +303,121 @@ srs_error_t srs_udp_listen(std::string ip, int port, srs_netfd_t* pfd) srs_cond_t srs_cond_new() { - return (srs_cond_t)st_cond_new(); + return (srs_cond_t)co_cond_alloc(); } int srs_cond_destroy(srs_cond_t cond) { - return st_cond_destroy((st_cond_t)cond); + return co_cond_free((stCoCond_t*)cond); } int srs_cond_wait(srs_cond_t cond) { - return st_cond_wait((st_cond_t)cond); + return co_cond_timedwait((stCoCond_t*)cond, -1); } int srs_cond_timedwait(srs_cond_t cond, srs_utime_t timeout) { - return st_cond_timedwait((st_cond_t)cond, (st_utime_t)timeout); + return co_cond_timedwait((stCoCond_t*)cond, timeout); } int srs_cond_signal(srs_cond_t cond) { - return st_cond_signal((st_cond_t)cond); + return co_cond_signal((stCoCond_t*)cond); } srs_mutex_t srs_mutex_new() { - return (srs_mutex_t)st_mutex_new(); + return NULL; } int srs_mutex_destroy(srs_mutex_t mutex) { - if (!mutex) { - return 0; - } - return st_mutex_destroy((st_mutex_t)mutex); + return 0; } int srs_mutex_lock(srs_mutex_t mutex) { - return st_mutex_lock((st_mutex_t)mutex); + return 0; } int srs_mutex_unlock(srs_mutex_t mutex) { - return st_mutex_unlock((st_mutex_t)mutex); + return 0; } int srs_netfd_fileno(srs_netfd_t stfd) { - return st_netfd_fileno((st_netfd_t)stfd); + return stfd; } int srs_usleep(srs_utime_t usecs) { - return st_usleep((st_utime_t)usecs); + // XXX: libco has no API like co_sleep, use co_cond_timedwait instead + stCoCond_t* cond = co_cond_alloc(); + co_cond_timedwait(cond, usecs/1000.0); + + return 0; } srs_netfd_t srs_netfd_open_socket(int osfd) { - return (srs_netfd_t)st_netfd_open_socket(osfd); + set_fd_nonblock(osfd); + return osfd; } srs_netfd_t srs_netfd_open(int osfd) { - return (srs_netfd_t)st_netfd_open(osfd); + set_fd_nonblock(osfd); + return osfd; } int srs_recvfrom(srs_netfd_t stfd, void *buf, int len, struct sockaddr *from, int *fromlen, srs_utime_t timeout) { - return st_recvfrom((st_netfd_t)stfd, buf, len, from, fromlen, (st_utime_t)timeout); + // TODO: timeout + return recvfrom(stfd, buf, len, 0, from, (socklen_t*)fromlen); } srs_netfd_t srs_accept(srs_netfd_t stfd, struct sockaddr *addr, int *addrlen, srs_utime_t timeout) { - return (srs_netfd_t)st_accept((st_netfd_t)stfd, addr, addrlen, (st_utime_t)timeout); + struct pollfd pf = { 0 }; + pf.fd = stfd; + pf.events = (POLLIN | POLLERR | POLLHUP); + + srs_utime_t atm = timeout; + if (atm != SRS_UTIME_NO_TIMEOUT) + atm /= 1000; + + int client_fd; + while ((client_fd = accept(stfd, addr, (socklen_t*)addrlen)) < 0) { + if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { + return -1; + } + + co_poll(co_get_epoll_ct(), &pf, 1, atm); + } + + set_fd_nonblock(client_fd); + + return client_fd; } ssize_t srs_read(srs_netfd_t stfd, void *buf, size_t nbyte, srs_utime_t timeout) { - return st_read((st_netfd_t)stfd, buf, nbyte, (st_utime_t)timeout); + struct pollfd pf = { 0 }; + pf.fd = stfd; + pf.events = (POLLIN | POLLERR | POLLHUP); + + int n; + while ((n = ::read(stfd, buf, nbyte)) < 0) { + if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { + return -1; + } + + co_poll(co_get_epoll_ct(), &pf, 1, timeout); + } + + return n; } bool srs_is_never_timeout(srs_utime_t tm) @@ -414,7 +427,6 @@ bool srs_is_never_timeout(srs_utime_t tm) SrsStSocket::SrsStSocket() { - stfd = NULL; stm = rtm = SRS_UTIME_NO_TIMEOUT; rbytes = sbytes = 0; } @@ -465,15 +477,15 @@ srs_error_t SrsStSocket::read(void* buf, size_t size, ssize_t* nread) ssize_t nb_read; if (rtm == SRS_UTIME_NO_TIMEOUT) { - nb_read = st_read((st_netfd_t)stfd, buf, size, ST_UTIME_NO_TIMEOUT); + nb_read = srs_read(stfd, buf, size, -1); } else { - nb_read = st_read((st_netfd_t)stfd, buf, size, rtm); + nb_read = srs_read(stfd, buf, size, rtm / 1000); } if (nread) { *nread = nb_read; } - + // On success a non-negative integer indicating the number of bytes actually read is returned // (a value of 0 means the network connection is closed or end of file is reached). // Otherwise, a value of -1 is returned and errno is set to indicate the error. @@ -499,13 +511,25 @@ srs_error_t SrsStSocket::read_fully(void* buf, size_t size, ssize_t* nread) { srs_error_t err = srs_success; - ssize_t nb_read; - if (rtm == SRS_UTIME_NO_TIMEOUT) { - nb_read = st_read_fully((st_netfd_t)stfd, buf, size, ST_UTIME_NO_TIMEOUT); - } else { - nb_read = st_read_fully((st_netfd_t)stfd, buf, size, rtm); + ssize_t nb_read = 0; + + int wait_read_bytes = size; + while (wait_read_bytes > 0) { + int bytes = ::read(stfd, buf, wait_read_bytes); + if (bytes > 0) { + nb_read += bytes; + wait_read_bytes -= bytes; + if (nb_read == (ssize_t)size) { + break; + } + } else if (bytes == 0) { + break; + } else { + if (errno != EWOULDBLOCK && errno != EAGAIN && errno != EINTR) + break; + } } - + if (nread) { *nread = nb_read; } @@ -535,11 +559,30 @@ srs_error_t SrsStSocket::write(void* buf, size_t size, ssize_t* nwrite) { srs_error_t err = srs_success; - ssize_t nb_write; - if (stm == SRS_UTIME_NO_TIMEOUT) { - nb_write = st_write((st_netfd_t)stfd, buf, size, ST_UTIME_NO_TIMEOUT); - } else { - nb_write = st_write((st_netfd_t)stfd, buf, size, stm); + ssize_t nb_write = 0; + + struct pollfd pf = { 0 }; + pf.fd = stfd; + pf.events = (POLLOUT | POLLERR | POLLHUP); + + srs_utime_t wtm = stm; + if (wtm != SRS_UTIME_NO_TIMEOUT) + wtm = stm / 1000; + + int wait_write_bytes = size; + while (wait_write_bytes > 0) { + int n = 0; + if ((n = ::write(stfd, buf, size)) < 0) { + if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { + break; + } + + co_poll(co_get_epoll_ct(), &pf, 1, wtm); + continue; + } + + wait_write_bytes -= n; + nb_write += n; } if (nwrite) { @@ -566,11 +609,44 @@ srs_error_t SrsStSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite) { srs_error_t err = srs_success; - ssize_t nb_write; - if (stm == SRS_UTIME_NO_TIMEOUT) { - nb_write = st_writev((st_netfd_t)stfd, iov, iov_size, ST_UTIME_NO_TIMEOUT); - } else { - nb_write = st_writev((st_netfd_t)stfd, iov, iov_size, stm); + srs_utime_t tm = stm; + if (tm != SRS_UTIME_NO_TIMEOUT) + tm = stm / 1000; + + int wait_write_bytes = 0; + for (int i = 0; i < iov_size; ++i) + wait_write_bytes += iov[i].iov_len; + + ssize_t nb_write = 0; + iovec* cur_iov = (iovec*)iov; + int cur_iov_size = iov_size; + + while (wait_write_bytes > 0) { + int n = 0; + if ((n = ::writev(stfd, cur_iov, cur_iov_size)) < 0) { + if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { + break; + } + + struct pollfd pf = {0}; + pf.fd = stfd; + pf.events = (POLLOUT | POLLERR | POLLHUP); + + co_poll(co_get_epoll_ct(), &pf, 1, tm); + continue; + } + + wait_write_bytes -= n; + nb_write += n; + + while (n >= (int)cur_iov->iov_len) { + n -= cur_iov->iov_len; + --cur_iov_size; + ++cur_iov; + } + // FIXME: no modify iov + (*cur_iov).iov_base = (void*)((char*)(*cur_iov).iov_base + n); + (*cur_iov).iov_len -= n; } if (nwrite) { @@ -595,7 +671,7 @@ srs_error_t SrsStSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite) SrsTcpClient::SrsTcpClient(string h, int p, srs_utime_t tm) { - stfd = NULL; + stfd = -1; io = new SrsStSocket(); host = h; @@ -616,7 +692,7 @@ srs_error_t SrsTcpClient::connect() close(); - srs_assert(stfd == NULL); + srs_assert(stfd == -1); if ((err = srs_tcp_connect(host, port, timeout, &stfd)) != srs_success) { return srs_error_wrap(err, "tcp: connect %s:%d to=%dms", host.c_str(), port, srsu2msi(timeout)); } diff --git a/trunk/src/service/srs_service_st.hpp b/trunk/src/service/srs_service_st.hpp index 510b9ba8ac..ca6da3f7cc 100644 --- a/trunk/src/service/srs_service_st.hpp +++ b/trunk/src/service/srs_service_st.hpp @@ -31,7 +31,7 @@ #include // Wrap for coroutine. -typedef void* srs_netfd_t; +typedef int srs_netfd_t; typedef void* srs_thread_t; typedef void* srs_cond_t; typedef void* srs_mutex_t;