[ENH] sockets and GC
Lex Spoon
lex at cc.gatech.edu
Fri Sep 24 14:55:36 UTC 1999
Squeak does finalization now on sockets which is nice. However, it
would be nicer if socket allocation would force a GC when necessary.
That way, you could have a style of programming where you never bother
to destroy sockets when you are finished with them; you could just
let them become garbage, much like any other memory.
Of course, it would also turn some buggy programs into correct ones,
if they currently leak a socket here and there :)
Anyway, the below changeset does this. It adds a method retryWithGCIf:
which can be used to try an operation, and, if it fails, to do a GC
and then try it again. (A cool improvement would be to first try
an incremental GC, and only if the routine fails again to try
a full GC. I don't know how to do this, though).
The changeset then inserts this routine in Socket's newTCP, newUDP,
and acceptFrom: primitives.
Lex
'From Squeak 2.5 of August 6, 1999 on 24 September 1999 at 9:45:51 am'!
"Change Set: SocketGC
Date: 24 September 1999
Author: Lex Spoon
If socket creation fails, do a garbage collect and then try again
"!
!BlockContext methodsFor: 'controlling' stamp: 'ls 9/24/1999 09:45'!
repeatWithGCIf: testBlock
| ans |
"run the receiver, and if testBlock returns true, garbage collect and run the receiver again"
ans _ self value.
(testBlock value: ans) ifTrue: [ Smalltalk garbageCollect. ans _ self value ].
^ans! !
!Socket class methodsFor: 'instance creation' stamp: 'ls 9/24/1999 09:45'!
acceptFrom: aSocket
^[ super new acceptFrom: aSocket ]
repeatWithGCIf: [ :sock | sock isValid not ]! !
!Socket class methodsFor: 'instance creation' stamp: 'ls 9/24/1999 09:44'!
newTCP
"Create a socket and initialise it for UDP"
^[ super new initialize: UDPSocketType ]
repeatWithGCIf: [ :socket | socket isValid not ]! !
!Socket class methodsFor: 'instance creation' stamp: 'ls 9/24/1999 09:44'!
newUDP
"Create a socket and initialise it for UDP"
^[ super new initialize: UDPSocketType ]
repeatWithGCIf: [ :socket | socket isValid not ]! !
More information about the Squeak-dev
mailing list
|