Check-in [56b1190a30]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added Thread support for standalone mode of Rivet-Starkit
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:56b1190a303693d77ff35c4b9adbfacfbc33487e
User & Date: rkeene 2009-04-27 21:16:32
Context
2009-04-27
23:13
Added a --maxthreads option check-in: e534d82216 user: rkeene tags: trunk
21:16
Added Thread support for standalone mode of Rivet-Starkit check-in: 56b1190a30 user: rkeene tags: trunk
21:10
Made ::rivet::reset reset all of rivet check-in: 88e46bf0b5 user: rkeene tags: trunk
Changes

Changes to rivet-starkit/main.tcl.

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
173
174
175
176
177
178
179


180
181
182
183
184
185
186
...
741
742
743
744
745
746
747

748
749
750
751
752
753
754









755
756
757
758
759
760
761
...
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
...
827
828
829
830
831
832
833
834

835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863









































































































864
865
866
867
868
869
870
871

872
873

874
875
876
877
878
879


880
881
882

883
884
885
886
887
888
889
890
891
892
893
....
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
			}
		}
		set targetfile $chk_targetfile
	}
	
	# Check the path to ensure that is inside the starkit
	set targetfile [file normalize $targetfile]
	set srcdir [file dirname [file normalize [info script]]]
	if {![string match "$srcdir/*" $targetfile]} {
		set targetfile "__RIVETSTARKIT_FORBIDDEN__"
	}
	
	# Check every component of the pathname for a ".htaccess" file, and stop processing if one is found
	set chk_srcdir $srcdir
	set work [file split $targetfile]
................................................................................
				interp delete $myinterp
			} else {
				if {$useenv ne ""} {
					unset -nocomplain ::env
					array set ::env [array get env] 
				}



				if {[catch {
					parse $targetfile
				} err]} {
					rivet_error

					rivet_flush

................................................................................
}

proc rivet_cgi_server {addr port foreground initscp logfile errorlogfile} {
	catch {
		package require Tclx
	}


	set canfork 0
	catch {
		set canfork [infox have_waitpid]
	}
	if {$::tcl_platform(platform) == "windows"} {
		set canfork 0
	}










	if {$initscp != ""} {
		uplevel #0 $initscp
	}

	switch -- $logfile {
		"-" {
................................................................................
	if {$elogfd == "stderr" || $logfd == "stdout"} {
		catch {
			console show
		}
	}

	if {$addr == "ALL"} {
		set ::rivetstarkit::masterfd [socket -server [list rivet_cgi_server_request $port $logfd $elogfd $canfork] $port]
	} else {
		set ::rivetstarkit::masterfd [socket -server [list rivet_cgi_server_request $port $logfd $elogfd $canfork] -myaddr $addr $port]
	}

	if {!$foreground} {
		if {$canfork} {
			set mypid [fork]
			if {$mypid != 0} {
				# Parent
				wait
				exit
			}

................................................................................
			tcl_puts stderr "Can't become a daemon, forking unavailable."
		}
	}

	vwait __FOREVER__
}

proc rivet_cgi_server_request {hostport logfd elogfd canfork sock addr port} {

	if {$canfork} {
		# Flush log descriptor, so buffer doesn't contain any extra data.
		if {$logfd != ""} {
			flush $logfd
		}
		if {$elogfd != ""} {
			flush $elogfd
		}

		# Reap up to 10 children per request
		for {set i 0} {$i < 10} {incr i} {
			if {[catch {
				wait -nohang
			}]} {
				break
			}
		}

		# Fork off a child to handle the request
		set mypid [fork]
		if {$mypid != 0} {
			catch {
				close $sock
			}
			return
		}

		catch {
			close $::rivetstarkit::masterfd









































































































		}
	}

	# Cleanup socket information
	unset -nocomplain ::rivetstarkit::sockinfo($sock)
	set ::rivetstarkit::sockinfo($sock) [list state NEW]
	set ::rivetstarkit::finished($sock) 0


	# Handle connection from data
	fileevent $sock readable [list rivet_cgi_server_request_data $sock $addr $hostport $logfd $elogfd $canfork]


	vwait ::rivetstarkit::finished($sock)

	catch {
		close $sock
	}



	if {$canfork} {
		exit

	}
}

proc rivet_cgi_server_request_data {sock addr hostport logfd elogfd canfork} {
	array set sockinfo $::rivetstarkit::sockinfo($sock)

	gets $sock line

	if {$line == "" && [eof $sock]} {
		# Tell the event loop that we're done here.
		set ::rivetstarkit::finished($sock) 1
................................................................................
		# Call "call_page" with the new enivronment
		if {[catch {
			if {$elogfd != ""} {
				tcl_puts $elogfd "($sock/$addr/[pid]) Debug: [array get myenv] ++ headers = [array get headers]"
				flush $elogfd
			}

			if {$canfork} {
				call_page [array get myenv] 0
			} else {
				call_page [array get myenv] 1
			}

			if {$logfd != ""} {
				tcl_puts $logfd "$addr - - \[[clock format [clock seconds] -format {%d/%b/%Y:%H:%M:%S %z}]\] \"$sockinfo(requestline)\" 200 0 \"-\" \"$ua\""
				flush $logfd
			}
		} err]} {







|







 







>
>







 







>







>
>
>
>
>
>
>
>
>







 







|

|



|







 







|
>
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|
|
|
<
|
>
|
|
>
|
<

|
<
|
>
>
|
<
<
>



|







 







|
|

|







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
...
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
...
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

988
989
990
991
992
993

994
995

996
997
998
999


1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
....
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
			}
		}
		set targetfile $chk_targetfile
	}
	
	# Check the path to ensure that is inside the starkit
	set targetfile [file normalize $targetfile]
	set srcdir $::starkit::topdir
	if {![string match "$srcdir/*" $targetfile]} {
		set targetfile "__RIVETSTARKIT_FORBIDDEN__"
	}
	
	# Check every component of the pathname for a ".htaccess" file, and stop processing if one is found
	set chk_srcdir $srcdir
	set work [file split $targetfile]
................................................................................
				interp delete $myinterp
			} else {
				if {$useenv ne ""} {
					unset -nocomplain ::env
					array set ::env [array get env] 
				}

				::rivet::reset
 
				if {[catch {
					parse $targetfile
				} err]} {
					rivet_error

					rivet_flush

................................................................................
}

proc rivet_cgi_server {addr port foreground initscp logfile errorlogfile} {
	catch {
		package require Tclx
	}

	set process_model "flat"
	set canfork 0
	catch {
		set canfork [infox have_waitpid]
	}
	if {$::tcl_platform(platform) == "windows"} {
		set canfork 0
	}
	if {$canfork} {
		set process_model "fork"
	} else {
		catch {
			package require Thread
			set process_model "thread"
		}
	}
	unset canfork

	if {$initscp != ""} {
		uplevel #0 $initscp
	}

	switch -- $logfile {
		"-" {
................................................................................
	if {$elogfd == "stderr" || $logfd == "stdout"} {
		catch {
			console show
		}
	}

	if {$addr == "ALL"} {
		set ::rivetstarkit::masterfd [socket -server [list rivet_cgi_server_request $port $logfd $elogfd $process_model] $port]
	} else {
		set ::rivetstarkit::masterfd [socket -server [list rivet_cgi_server_request $port $logfd $elogfd $process_model] -myaddr $addr $port]
	}

	if {!$foreground} {
		if {$process_model == "fork"} {
			set mypid [fork]
			if {$mypid != 0} {
				# Parent
				wait
				exit
			}

................................................................................
			tcl_puts stderr "Can't become a daemon, forking unavailable."
		}
	}

	vwait __FOREVER__
}

proc rivet_cgi_server_request {hostport logfd elogfd pmodel sock addr port {threadId ""}} {
	switch -- $pmodel {
		"fork" {
			# Flush log descriptor, so buffer doesn't contain any extra data.
			if {$logfd != ""} {
				flush $logfd
			}
			if {$elogfd != ""} {
				flush $elogfd
			}

			# Reap up to 10 children per request
			for {set i 0} {$i < 10} {incr i} {
				if {[catch {
					wait -nohang
				}]} {
					break
				}
			}

			# Fork off a child to handle the request
			set mypid [fork]
			if {$mypid != 0} {
				catch {
					close $sock
				}
				return
			}

			catch {
				close $::rivetstarkit::masterfd
			}
		}
		"thread" {
			# Find a free thread...
			if {![info exists ::rivetstarkit::threadinfo]} {
				array set ::rivetstarkit::threadinfo {}
			}

			unset -nocomplain threadId
			foreach {thread isInUse} [array get ::rivetstarkit::threadinfo] {
				if {!$isInUse} {
					tcl_puts $elogfd "Using existing thread: $thread"

					set threadId $thread
					break
				}
			}

			# If none found, create one...
			if {![info exists threadId]} {
				# Create an empty interpreter in a new thread
				set threadId [thread::create]
				tcl_puts $elogfd "Creating thread: $threadId"

				# Load the needed packages in the new thread
				thread::send $threadId [list package require tclrivet]
				thread::send $threadId [list set ::rivet::parsestack [info script]]

				# Copy the appropriate namespaces to the new thread
				foreach ns [list ::rivetstarkit] {
					thread::send $threadId [list namespace eval $ns ""]
				}

				# Copy the appropriate variables to the new thread
				foreach var [list ::starkit::topdir] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}

					thread::send $threadId [list set $var [set $var]]
				}

				# Copy the appropriate procedures to the new thread
				foreach proc [list rivet_cgi_server_request_data rivet_cgi_server_request call_page] {
					if {[namespace qualifiers $var] != ""} {
						thread::send $threadId [list namespace eval [namespace qualifiers $var] ""]
					}

					set procargs [list]
					foreach arg [info args $proc] {
						if {[info default $proc $arg defval]} {
							lappend procargs [list $arg $defval]
						} else {
							lappend procargs $arg
						}
					}

					thread::send $threadId [list proc $proc $procargs [info body $proc]]
				}

				tcl_puts $elogfd " ... done creating thread ($threadId)"
			}

			# Mark the specified thread as in-use
			set ::rivetstarkit::threadinfo($threadId) 1

			# Perform the bottom-half of the processing (we are required to re-enter the event loop)
			after idle [list rivet_cgi_server_request $hostport $logfd $elogfd "thread-parent" $sock $addr $port $threadId]

			return
		}
		"thread-parent" {
			set pmodel "thread"

			# Transfer the socket to the thread, and specify our thread Id
			thread::transfer $threadId $sock

			tcl_puts $elogfd "Calling child thread to handle request ($threadId) in background"
			thread::send -async $threadId [list rivet_cgi_server_request $hostport "" "" "thread-child" $sock $addr $port [thread::id]]
			tcl_puts $elogfd " ... done ($threadId)."
			return
		}
		"thread-child" {
			set pmodel "thread"

			set parentThreadId $threadId
			unset threadId
		}
		"flat" {
		}
	}

	catch {
		# Cleanup socket information
		unset -nocomplain ::rivetstarkit::sockinfo($sock)
		set ::rivetstarkit::sockinfo($sock) [list state NEW]
		set ::rivetstarkit::finished($sock) 0

		# Handle connection from data
		fileevent $sock readable [list rivet_cgi_server_request_data $sock $addr $hostport $logfd $elogfd $pmodel]

		vwait ::rivetstarkit::finished($sock)

		catch {
			close $sock
		}
	}

	switch -- $pmodel {
		"fork" {
			exit

		}
		"thread" {
			# Mark the thread as free
			if {$elogfd != ""} {
				tcl_puts $elogfd "Marking thread as free ([thread::id])"
			}


			thread::send -async $parentThreadId [list set ::rivetstarkit::threadinfo([thread::id]) 0]


			if {$elogfd != ""} {
				tcl_puts $elogfd " ... done ([thread::id])."
			}


		}
	}
}

proc rivet_cgi_server_request_data {sock addr hostport logfd elogfd pmodel} {
	array set sockinfo $::rivetstarkit::sockinfo($sock)

	gets $sock line

	if {$line == "" && [eof $sock]} {
		# Tell the event loop that we're done here.
		set ::rivetstarkit::finished($sock) 1
................................................................................
		# Call "call_page" with the new enivronment
		if {[catch {
			if {$elogfd != ""} {
				tcl_puts $elogfd "($sock/$addr/[pid]) Debug: [array get myenv] ++ headers = [array get headers]"
				flush $elogfd
			}

			if {$pmodel == "flat"} {
				call_page [array get myenv] 1
			} else {
				call_page [array get myenv] 0
			}

			if {$logfd != ""} {
				tcl_puts $logfd "$addr - - \[[clock format [clock seconds] -format {%d/%b/%Y:%H:%M:%S %z}]\] \"$sockinfo(requestline)\" 200 0 \"-\" \"$ua\""
				flush $logfd
			}
		} err]} {