#!/usr/bin/env perl
use v5.16.3;
use threads (exit => 'threads_only');
use threads::shared;
use IO::Select();
use FCGI;
my $listen = ':5000';
my $socket = FCGI::OpenSocket($listen, 100);
$SIG{INT} = sub {
map { $_->kill('TERM') } threads->list(threads::all);
};
threads->create(\&thread, $socket);
threads->create(\&thread, $socket);
threads->create(\&thread, $socket);
while (threads->list(threads::all)) { sleep 1 } #wait until all thr
+eads finished
sub thread {
my $socket = shift;
$SIG{TERM} = sub {
threads->detach;
threads->exit;
};
my $env = {};
my $req = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, $env, $socket
+, 1);
my $sel = IO::Select->new($socket);
while (1) {
if ($sel->can_read(1) && $req->Accept() >= 0) {
print "200\x0D\x0A";
print "Content-Type: text/plain\x0D\x0A";
print "\x0D\x0A";
print threads->tid . "\x0D\x0A";
$req->Finish;
}
}
return;
}
Problem with blocking $req->Accept seems to be solved. As you can see, I use IO::Select to make non-blocking check if socket ready to accept connection.
Non-blocking accept is needed to give process possibility to process received signals, because signals in threads is not OS level, but perl emulated and don't break system calls.
$sel->can_read(1) return true in all threads, when new connection is ready to be accepted.
I don't know, can system call $req->Accept() return positive value for several callers simultaneously?
If it can - this code need to be modified.
Now, in my tests, it return 0+int to first caller, who really accept connection, and negative value for all others. |