The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Saying goodbye is not hard

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Saying goodbye is not hard Posted: Dec 20, 2005 11:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Saying goodbye is not hard
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Travis Griggs - Blog

Advertisement

Two evenings ago, Boris Popov showed up in the #smalltalk irc channel, and we talked about ephemerons. I had forgotten how these things worked, but once upon a time, I knew when I wrote Weaklings. It's pretty straightforward. When you create a class whose indexType: is #ephemeron, the first ivar slot is weak. And when the object you're holding is no longer referenced strongly, you get a sent a mourn message, which by default does nothing. So you can choose to let go of it too. If you don't it won't actually go away. The Weaklings package has two variants on this pattern: one a "ValueHolder" like thing, and the other a Proxy. I've used the asWeakling quite a bit in some of our code. It's a great way to have a process loop which runs for the life of an object, but does not contribute to the object staying around:

| weakString |
hardReference := String withAll: 'Hello World'.
weakString := hardReference asWeakling.

[[weakString value isNil] whileFalse: 
		[weakString value out.
		(Delay forSeconds: 0.5) wait].
'All done' out] 
		fork.

[(Delay forSeconds: 2) wait.
hardReference := Object new.
ObjectMemory globalGarbageCollect] 
		fork

What Boris wanted though was some of the "instance based finalization" that ephemerons purport to offer. The thing about an ephemeron is that it doesn't make the weak thing itself have anything special happen to it when it's being gotten rid of; rather the thing that holds it weakly gets a chance to know about it. Boris had a DLLCC reference which needed to be "managed" the associated library when it was ready to go away. This got me thinking that it would be nice if there were a simple way of plugging in "finalization actions" for these weakling things.

So I sat down this morning and wrote some tests, and then made some code work. There's a new version of Weaklings in the OR, as well as a WeaklingsTests. The basic idea is to add a 'lastRites:' message to Weakling. It's a single arg block that can do whatever you want with the object before it goes away. In this way, should Boris so choose, he can do something like:

pointer := someCLibrary makeNewPointerAPI asWeakling lastRites: [:ptr | someCLibrary releasePointerAPI: ptr]

He'll have to use value to get at the actual pointer inside the weakling. But it's all just automatic now. He could use a WeakProxy to hide the value thing:

pointer := someClibrary makeNewPointerAPI weakProxyWithLastRites: [:ptr | someCLibrary releasePointer: ptr]

A final plug. For SymbolValue. Since it's a single arg block, if the receiver is capable of doing it's own cleanup, it could just be something like:

^'file' asFilename writeStream asWeakling lastRites: #close.
When the stream returned is no longer used, it'll automatically clean up.

Read: Saying goodbye is not hard

Topic: Who controls the vertical? Previous Topic   Next Topic Topic: How to kill real voices

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use