File: modulecache.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (172 lines) | stat: -rw-r--r-- 3,575 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
   FALCON - The Falcon Programming Language.
   FILE: modulecache.cpp

   Cache-sensible module loader.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sun, 30 May 2010 15:12:30 +0200

   -------------------------------------------------------------------
   (C) Copyright 2010: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/modulecache.h>
#include <falcon/traits.h>

#include <falcon/basealloc.h>
#include <falcon/timestamp.h>
#include <falcon/sys.h>


namespace Falcon {

class CacheEntry: public BaseAlloc
{
public:
   CacheEntry( Module* mod, const TimeStamp& tsDate ):
      m_module( mod ),
      m_ts( tsDate )
      {}

   ~CacheEntry()
   {
      m_module->decref();
   }

   void change( Module* mod, const TimeStamp& tsDate )
   {
      m_module->decref();
      mod->incref();
      m_module = mod;
      m_ts = tsDate;
   }

   Module* m_module;
   TimeStamp m_ts;
};

ModuleCache::ModuleCache():
      m_modMap( &traits::t_string(), &traits::t_voidp() )
{}

ModuleCache::~ModuleCache()
{
   MapIterator iter = m_modMap.begin();
   while( iter.hasCurrent() )
   {
      CacheEntry* mod = *(CacheEntry**) iter.currentValue();
      delete mod;
      iter.next();
   }
}

Module* ModuleCache::add( const String& muri, Module* module )
{
   FileStat fm;
   bool gotStats = Sys::fal_stats( muri, fm );

   m_mtx.lock();
   void* data = m_modMap.find( &muri );
   if( data != 0 )
   {
      CacheEntry* mod_cache = *(CacheEntry**) data;

      // New module?
      // -- if I can't get the stats, in doubt, change it
      if( ! gotStats )
      {
         // inserts a null timestamp so any future entry will change it.
         mod_cache->change( module, TimeStamp() );
         m_mtx.unlock();

         return module;
      }
      else if( fm.m_mtime->compare( mod_cache->m_ts ) > 0 )
      {
         mod_cache->change( module, *fm.m_mtime );
         m_mtx.unlock();

         return module;
      }
      else
      {
         Module* mod1 = mod_cache->m_module;
         mod1->incref();
         m_mtx.unlock();

         module->decref();
         return mod1;
      }
   }
   else
   {
      // had we been able to get the stats?
      if( gotStats )
      {
         m_modMap.insert( &muri, new CacheEntry( module, *fm.m_mtime ) );
      }
      else
      {
         // insert the module with a null timestamp; any other timestamp
         // read later from the system
         m_modMap.insert( &muri, new CacheEntry( module, TimeStamp() ) );
      }
      module->incref();
      m_mtx.unlock();
      return module;
   }
}

bool ModuleCache::remove( const String& muri )
{
   MapIterator iter;

   m_mtx.lock();
   if( m_modMap.find( &muri, iter ) )
   {
      CacheEntry* mod = *(CacheEntry**) iter.currentValue();
      m_modMap.erase( iter );
      m_mtx.unlock();

      delete mod;
      return true;
   }

   m_mtx.unlock();
   return false;
}

Module* ModuleCache::find( const String& muri )
{
   FileStat fm;
   bool gotStats = Sys::fal_stats( muri, fm );

   m_mtx.lock();
   void* data = m_modMap.find( &muri );
   if( data != 0 )
   {
      CacheEntry* emod = *(CacheEntry**) data;
      if ( !gotStats || fm.m_mtime->compare( emod->m_ts ) > 0 )
      {
         // ignore the find
         m_mtx.unlock();
         return 0;
      }

      Module* mod = emod->m_module;
      mod->incref();
      m_mtx.unlock();

      return mod;
   }

   m_mtx.unlock();
   return 0;
}

}

/* end of modulecache.cpp */