X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..3ac664ec654230b9ad26ab12242c1db6f636a26f:/lcoreselect.pas diff --git a/lcoreselect.pas b/lcoreselect.pas index e0351eb..38da6ba 100755 --- a/lcoreselect.pas +++ b/lcoreselect.pas @@ -6,12 +6,12 @@ For conditions of distribution and use, see copyright notice in zlib_license.txt which is included in the package ----------------------------------------------------------------------------- } - -{$ifdef fpc} - {$ifndef ver1_0} - {$define useinline} - {$endif} -{$endif} + +{$ifdef fpc} + {$ifndef ver1_0} + {$define useinline} + {$endif} +{$endif} unit lcoreselect; @@ -21,7 +21,7 @@ uses {$ifdef VER1_0} linux, {$else} - baseunix,unix,unixutil, + baseunix,unix,unixutil,sockets, {$endif} fd_utils; var @@ -31,18 +31,24 @@ var function getfdsrmaster : fdset; {$ifdef useinline}inline;{$endif} function getfdswmaster : fdset; {$ifdef useinline}inline;{$endif} +procedure lcoreinit; + implementation uses lcore,sysutils, classes,pgtypes,bfifo, {$ifndef nosignal} - lsignal; + lsignal, {$endif} + ltimevalstuff; {$include unixstuff.inc} -{$include ltimevalstuff.inc} + +const + absoloutemaxs_select = (sizeof(fdset)*8)-1; + var - fdreverse:array[0..absoloutemaxs] of tlasio; + fdreverse:array[0..absoloutemaxs_select] of tlasio; type tselecteventcore=class(teventcore) public @@ -58,7 +64,7 @@ type procedure processtimers;inline; var - tv ,tvnow : ttimeval ; + tvnow : ttimeval ; currenttimer : tltimer ; temptimer : tltimer ; @@ -78,17 +84,18 @@ begin end; temptimer := currenttimer; currenttimer := currenttimer.nexttimer; - if temptimer.released then temptimer.free; end; end; procedure processasios(var fdsr,fdsw:fdset);//inline; var currentsocket : tlasio ; - tempsocket : tlasio ; socketcount : integer ; // for debugging perposes :) dw,bt:integer; + currentfdword:fdword; + fd : integer; begin + //writeln('entering processasios'); { inc(lcoretestcount);} //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed @@ -97,23 +104,26 @@ begin {------- test optimised loop} socketcount := 0; - for dw := (maxs shr 5) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin - for bt := 0 to 31 do if (fdsr[dw] or fdsw[dw]) and (1 shl bt) <> 0 then begin + for dw := (maxs shr fdwordshift) downto 0 do if (fdsr[dw] or fdsw[dw]) <> 0 then begin + currentfdword := (fdsr[dw] or fdsw[dw]); + for bt := fdwordmaxbit downto 0 do if currentfdword and (1 shl bt) <> 0 then begin inc(socketcount); - currentsocket := fdreverse[dw shl 5 or bt]; + fd := dw shl fdwordshift or bt; + //writeln('reversing fd ',fd); + currentsocket := fdreverse[fd]; {if not assigned(currentsocket) then raise exception.create('currentsocket not assigned'); if currentsocket.fdhandlein < 0 then raise exception.create('currentsocket.fdhandlein out of range');} {i've seen the out of range case actually happening, so it can happen. test: just close the fd - beware} if not assigned(currentsocket) then begin - fdclose(dw shl 5 or bt); + fdclose(fd); continue end; if currentsocket.fdhandlein < 0 then begin - fdclose(dw shl 5 or bt); + fdclose(fd); continue end; try - currentsocket.handlefdtrigger(fd_isset(currentsocket.fdhandlein,fdsr),fd_isset(currentsocket.fdhandleout,fdsw)); + currentsocket.handlefdtrigger(fd_isset(fd,fdsr),fd_isset(fd,fdsw)); except on E: exception do begin currentsocket.HandleBackGroundException(e); @@ -129,17 +139,6 @@ begin end; end; - if asinreleaseflag then begin - asinreleaseflag := false; - currentsocket := firstasin; - while assigned(currentsocket) do begin - tempsocket := currentsocket; - currentsocket := currentsocket.nextasin; - if tempsocket.released then begin - tempsocket.free; - end; - end; - end; { !!! issues: - sockets which are released may not be freed because theyre never processed by the loop @@ -178,6 +177,7 @@ begin end; end; *) { debugout('socketcount='+inttostr(socketcount));} + //writeln('leaving processasios'); end; procedure tselecteventcore.processmessages; @@ -209,7 +209,7 @@ var var fdsrmaster , fdswmaster : fdset ; - + function getfdsrmaster : fdset; {$ifdef fpc}inline;{$endif} begin result := fdsrmaster; @@ -218,8 +218,8 @@ function getfdswmaster : fdset; {$ifdef fpc}inline;{$endif} begin result := fdswmaster; end; - - + + Function doSelect(timeOut:PTimeVal):longint;//inline; var localtimeval : ttimeval; @@ -289,25 +289,6 @@ begin repeat //the message loop will exit if all lasio's and ltimer's and lsignal's are destroyed - if (not assigned(firstasin)) and (not assigned(firsttimer)) {$ifndef nosignal} and (not assigned(firstsignal)){$endif} then exit; - {fd_zero(FDSR); - fd_zero(FDSW); - currentsocket := firstasin; - if not assigned(currentsocket) then exit; //the message loop will exit if all lsockets are destroyed - - repeat - if (not currentsocket.released) and (currentsocket.state<>wsclosed) then fd_set(currentsocket.fdhandlein,fdsr); - if (not currentsocket.released) and (currentsocket.state=wsconnecting) then fd_set(currentsocket.fdhandleout,fdsw); - if currentsocket is tlsocket then begin - if (not currentsocket.released) and (currentsocket.state=wsconnected) and(tlsocket(currentsocket).sendq <> '') then fd_set(currentsocket.fdhandleout,fdsw); - end; - tempsocket := currentsocket; - currentsocket := currentsocket.nextasin; - if tempsocket.released then begin - tempsocket.free; - end; - until not assigned(currentsocket); - } processtasks; //currenttask := nil; {beware} @@ -352,6 +333,7 @@ end; procedure tselecteventcore.rmasterset(fd : integer;islistensocket : boolean); begin + //writeln('rmasterset called with fd ',fd); if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range'); if fd > maxs then maxs := fd; if fd_isset(fd,fdsrmaster) then exit; @@ -361,6 +343,7 @@ end; procedure tselecteventcore.rmasterclr(fd: integer); begin + //writeln('rmasterclr called with fd ',fd); if not fd_isset(fd,fdsrmaster) then exit; fd_clr(fd,fdsrmaster); @@ -369,6 +352,7 @@ end; procedure tselecteventcore.wmasterset(fd : integer); begin + //writeln('wmasterset called with fd ',fd); if fd > absoloutemaxs then raise esocketexception.create('file discriptor out of range'); if fd > maxs then maxs := fd; @@ -379,6 +363,7 @@ end; procedure tselecteventcore.wmasterclr(fd: integer); begin + //writeln('wmasterclr called with fd ',fd); if not fd_isset(fd,fdswmaster) then exit; fd_clr(fd,fdswmaster); end; @@ -388,12 +373,20 @@ begin fdreverse[fd] := reverseto; end; +var + inited:boolean; - +procedure lcoreinit; begin + if inited then exit; + inited := true; eventcore := tselecteventcore.create; + absoloutemaxs := absoloutemaxs_select; + maxs := 0; fd_zero(fdsrmaster); fd_zero(fdswmaster); +end; + end.