Applications of Telos
Total Page:16
File Type:pdf, Size:1020Kb
LISP AND SYMBOLIC COMPUTATION An International Journal c Kluwer Academic Publishers Manufactured in The Netherlands Applications of Telos PETER BROADBERY pabmathsbathacuk CHRISTOPHER BURDORF cbmathsbathacuk School of Mathematical Sciences University of Bath Bath BA AY United Kingdom Keywords Eulisp Telos p ersistence nalizatio n virtual shared memory Abstract EULISP has an integrated ob ject system with reective capabiliti es We discuss some example application s which use these facilities to exp eriment with some advanced and p owerful concepts namely nalization virtual shared memory and p ersis tence A secondary goal is to attempt to illustrate the additional p ossibili ties of metaob ject programming over nonmetalevel techniques Intro duction EULISP provides an ob ject system called Telos which is fully inte grated with the rest of the language and includes a metaob ject proto col MOP which allows programs to reect on the structure and inheri tance relationships b etween classes Reection here is the pro cess of taking a system ob ject such as a class and transforming it into a userlevel ob ject which a program can read and p erhaps mo dify Using this structure a pro gram is free to change the representation and computation of these asp ects to obtain new b ehaviour by sub classing existing classes and metaclasses These new classes have the same status as the systemdened classes so the extensions b ecome a part of the original languageno sp ecial co de is needed to use them This pap er comes in ve parts First we intro duce the Telos MOP from a users p oint of view with a particular emphasis on the slot access pro to col After this we lo ok at a fairly simple extension of the slot access proto col to add nalization section Two larger scale applications are virtual shared memory mo del section and p ersistent ob jects section The last section diers from the earlier ones in that instead of b eing illustrative fragments of metaob ject programming it describ es a complete application written using EULISP and Telos which dep ends heavily on Telos in order to create an interface into an ob ject store for simulation programs A secondary asp ect of this last part is that the system in question was originally written in CLOS and had rst to b e p orted to EULISP b efore the BROADBERY AND BURDORF extensions describ ed here were made Metaob ject proto cols and Telos A detailed description and rationale for the design of Telos app ears in Two principles of the design are i a program should not pay for the cost of a feature that it do es not use also known as dont use dont lose ii as large a prop ortion as p ossible of the metaob ject level op erations should b e done when classes are created rather then when they are usedin eect a form of compiletime versus runtime tradeo A consequence of this second prop erty is that the creation and access routines can b e extended without imp osing overheads on other programs and with minimal overheads on the client program There are four comp onents to the Telos MOP Class denition and inheritance Slot accessor creation and invo cation Generic function dispatch Ob ject allo cation and initialization Each of these consists of a numb er of generic functions which have dened semantics and are guaranteed to b e called at sp ecic p oints in the proto col Of course these proto cols are not entirely separateeach relies on the existence of the other three to function but the actual details of each proto col are largely indep endent of one another New b ehaviour is obtained by sub classing the classes sp ecied in the denition and sp ecializi ng the appropriate parts of the MOP for the new classes Sp ecializi ng slot access The Telos slot creation and access proto col diers from the CLOS proto col in a numb er of imp ortant ways but primarily the balance of work is shifted from the access proto col to the creation proto col The slot accessor creation routine has four phases Figure Create slotdescription ob jects Finalize the details of the ob ject representation Create the slot accessor functions Create sp ecialized slot accessors APPLICATIONS OF TELOS computeinheritedslotdescriptions computeslotdescriptions computeandensureslotaccessors ensureslotreader computeprimitivereaderusingclass computeprimitivereaderusingslotdescription ensureslotwriter computeprimitivewriterusingclass computeprimitivewriterusingslotdescription Figure slot access proto col Each phase allows the programmer to sp ecialize the slot in dierent ways add extra slots to the class in the rst and move slots and allo cate space for hidden slots in the second However the last two are probably the most commonly used one can change the function used to dispatch slot access in the third phase whilst the last enables arbitrary functions to b e called at slot access time Where the slot description is an instance of localslotdescription the default case accessing a slot is just an indexed reference or up date op eration However this can b e sp ecialized at will The exibility of this approach can b e used to build complex systems from the primitive functions provided by EULISP In the following sections we describ e some applications that use this technique Finalization In many applications it is required to do some p ostpro cessing when it can b e established that an ob ject will no longer b e needed This pro cess is known as nalization For example many systems require that close should b e called on a le ob ject b efore a program is exited otherwise the stored version of the le may b e inconsistent with the version held by the program due to buering by either the op erating system or the application program The problem is that a program may lose a p ointer to such an ob ject and never b e able to run the nalization co de on it b efore the ob ject is recycled by garbage collection after which nalization is imp ossible To circumvent this situation we need to b e able to to note when an ob ject b ecomes inaccessible recover its slots from some hidden storage and invoke a tidying op eration on the ob ject The Bath implementation of EULISP provides two extensions to the lan guage that simplied the implementation the system allows the user to BROADBERY AND BURDORF defclass file create a class with a slot to be used in the nalization method file initarg file accessor fileinternal slotclass finalizablesd class finalizableclass initargs openargs constructor opensafefile openargs open a le and set the actual hand le defmethod initialize x file lst let new callnextmethod setter fileinternal new apply open findkey openargs lst new tidy up the le defmethod finalize x file close fileinternal x Figure Finalization of a le handle install a function which is called directly after each garbage collection a p ostGC ho ok and secondly a new class of ob jectweak wrapp ers It is guaranteed that the p ost garbage collection function is never called during the execution of a previous nalize and always runs on the thread which in voked the garbage collection pro cess The purp ose of these rules is to avoid problems with innite lo ops and concurrency resp ectively Weak wrapp ers are ob jects with a single slot which initially contains some ob ject but is set to nil if the ob ject is garbage collected during a garbage collection references from weak p ointers are not followed therefore the referenced ob 1 ject may b e garbage collected These two extensions and the facilities of Telos allow the implementation of a simple nalization scheme The idea is to store the slot values of an ob ject that are needed for nalization somewhere safe so that when the space o ccupied by the ob ject is recovered by garbage collection the values that were stored in the nal ization slots are still available Under the proto col for this implementation of nalization each class which needs this facility is required to nominate a proxyclass with similarly named slots for the values needed for the nalization metho d The proxy class defaults to the class itself When the 1 One do es not absolutely need the p ost GC callback as one could use the wait prim itive with a timeout but the GC callback is more ecient APPLICATIONS OF TELOS defclass finalizableclass class count accessor finalizableslotcount proxy accessor proxyclass initargs proxy class initialization defmethod initialize cl finalizableclass lst let cl callnextmethod let slotposn classinstancesize cl setter classinstancesize cl slotposn setter finalizablehandleposn cl slotposn cl instance al location defmethod allocate cl finalizableclass lst let handle makevector finalizableslotcount cl obj callnextmethod setter primitiveslotref cl finalizablehandleposn cl handle obj Constructing new proxy objects defun makeproxyobject class values let newcl proxyclass class let obj allocate newcl proxy t do lambda sd slotdescriptionslotwriter findslotdescription newcl slotdescriptionname sd obj vectorref values slotdescriptionposition sd classslotdescriptions class obj Figure Fragment of nalization co de original instance is garbage collected the change of status can b e detected in the weak wrapp er and the slot values can b e recovered The naliza tion metho d is then executed on an instance of the proxyclass with the proviso that this particular instance cannot b e nalized From the co de in Figure you will see that a slot is added to the instance to b e nalized when it is allo cated This slot is initialized with a vector in which the slot BROADBERY AND BURDORF values needed for nalization are stored The vector