1 # Copyright 2012-2015 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
17 # The in-memory cache.
18 array set gdb_data_cache {}
20 # A helper for gdb_caching_proc that handles the caching.
22 proc gdb_do_cache {name} {
23 global gdb_data_cache objdir
26 # See if some other process wrote the cache file. Cache value per
27 # "board" to handle runs with multiple options
28 # (e.g. unix/{-m32,-64}) correctly. We use "file join" here
29 # because we later use this in a real filename.
30 set cache_name [file join [target_info name] $name]
32 if {[info exists gdb_data_cache($cache_name)]} {
33 verbose "$name: returning '$gdb_data_cache($cache_name)' from cache" 2
34 return $gdb_data_cache($cache_name)
37 if {[info exists GDB_PARALLEL]} {
38 set cache_filename [file join $objdir cache $cache_name]
39 if {[file exists $cache_filename]} {
40 set fd [open $cache_filename]
41 set gdb_data_cache($cache_name) [read -nonewline $fd]
43 verbose "$name: returning '$gdb_data_cache($cache_name)' from file cache" 2
44 return $gdb_data_cache($cache_name)
48 set real_name gdb_real__$name
49 set gdb_data_cache($cache_name) [uplevel 1 $real_name]
51 if {[info exists GDB_PARALLEL]} {
52 verbose "$name: returning '$gdb_data_cache($cache_name)' and writing file" 2
53 file mkdir [file dirname $cache_filename]
54 # Make sure to write the results file atomically.
55 set fd [open $cache_filename.[pid] w]
56 puts $fd $gdb_data_cache($cache_name)
58 file rename -force -- $cache_filename.[pid] $cache_filename
60 return $gdb_data_cache($cache_name)
63 # Define a new proc named NAME that takes no arguments. BODY is the
64 # body of the proc. The proc will evaluate BODY and cache the
65 # results, both in memory and, if GDB_PARALLEL is defined, in the
66 # filesystem for use across invocations of dejagnu.
68 proc gdb_caching_proc {name body} {
69 # Define the underlying proc that we'll call.
70 set real_name gdb_real__$name
71 proc $real_name {} $body
73 # Define the advertised proc.
74 proc $name {} [list gdb_do_cache $name]