1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: temp_objects.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
19 inherit "/kernel/module";
22 class temp_objects : public module{
30 Thread.Mutex mutex = Thread.Mutex();
31 Thread.Queue deleteQueue = Thread.Queue();
34 * Callback function to initialize module. Sets the save and restore function.
41 add_data_storage(STORE_TEMPOBJ, retrieve_temp_objects,
42 restore_temp_objects);
46 void queued_destruct()
53 * Thread to check for temporary objects and deletes out of date objects.
54 * It also handles a deletion queue to prevent objects from being
55 * destructed while still cleaning up.
58 void check_temp_objects()
62 while ( deleteQueue->size() > 0 ) {
63 object obj = deleteQueue->read();
65 err = catch(obj->drop());
67 FATAL("Error while deleting:\n"+sprintf("%O\n", err));
71 object l = mutex->lock();
73 array idx = indices(mTempObjects);
74 foreach ( idx, object tmp ) {
75 LOG("Checking " + tmp->get_identifier() + " for deletion ("+
76 time()+":"+mTempObjects[tmp]+")!\n");
77 if ( time() > mTempObjects[tmp] ) {
80 m_delete(mTempObjects, tmp);
83 require_save(STORE_TEMPOBJ);
90 * Callback function when the module is loaded starts the
91 * thread to check for objects to be deleted.
93 * @see check_temp_objects
97 start_thread(check_temp_objects);
101 * Add a temporary object with a given timestamp when it should be deleted.
103 * @param object obj - the temporary object.
104 * @param int validT - the timestamp for that the object wont be deleted.
106 void add_temp_object(object obj, int validT)
108 if ( _SECURITY->access_delete(0, obj, CALLER) ) {
109 object l = mutex->lock();
110 mTempObjects[obj] = validT;
111 require_save(STORE_TEMPOBJ);
117 * Callback function to retrieve all temporary objects.
119 * @return mapping of temporary objects.
121 * @see restore_temp_objects
124 final mapping retrieve_temp_objects()
126 if ( CALLER != _Database )
127 THROW("Invalid call to retrieve_temp_objects()", E_ACCESS);
128 return ([ "tempObjects": mTempObjects, ]);
134 * Restore the temporary objects from the database.
136 * @param mapping data - saved data of temp objects to be restored.
138 * @see retrieve_temp_objects
141 final void restore_temp_objects(mapping data)
143 if ( CALLER != _Database )
144 THROW("Invalid call to restore_temp_objects()", E_ACCESS);
145 mTempObjects = data["tempObjects"];
151 * Get the identifier of this module.
153 * @return the identifier "temp_objects"
155 string get_identifier() { return "temp_objects"; }