00001 /* Part of the culibs project, <http://www.eideticdew.org/culibs/>. 00002 * Copyright (C) 2006--2007 Petter Urkedal <urkedal@nbi.dk> 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef CUFLOW_CACHECONF_H 00019 #define CUFLOW_CACHECONF_H 00020 00021 #if 0 00022 #include <cuflow/cache.h> 00023 #endif 00024 #include <cu/inherit.h> 00025 #include <cu/dlink.h> 00026 #include <time.h> 00027 #include <atomic_ops.h> 00028 00029 CU_BEGIN_DECLARATIONS 00030 /** \defgroup cuflow_cacheconf_h cuflow/cacheconf.h: Cache Configuration 00031 ** @{ \ingroup cuflow_x_mod 00032 ** 00033 ** This is used by \ref cuflow_cached_h "cuflow/cached.h" and \ref 00034 ** cuflow_cache_h "cuflow/cache.h". */ 00035 00036 struct cuflow_cacheconf 00037 { 00038 /* Set by this library. */ 00039 #if 0 00040 pthread_mutex_t cache_link_mutex; 00041 struct cu_dlink cache_link; 00042 #endif 00043 AO_t current_ticks; 00044 struct timespec target_time; 00045 00046 /* Set by manager. */ 00047 unsigned int byte_cost_per_tick; 00048 struct timespec tick_period; 00049 00050 cu_bool_t (*manager)(cuflow_cacheconf_t conf, struct timespec *t_now); 00051 }; 00052 00053 /** Construct a cache configuration. \a manager is a function which will be 00054 ** called once during this construction, then occationally afterwards. If the 00055 ** parameters were not set prior to this call, then \a manager must set them 00056 ** on it's first invokation. It may also update other parameters contituously 00057 ** to implement heuristics for controlling memory consumption. 00058 ** 00059 ** \a manager should normally always return true, but if there is need to shut 00060 ** down the cache configuration, false may be returned. After that, cache 00061 ** clock will stop and cached objects will no longer be freed. Therefore, the 00062 ** client should make sure to destruct associated caches with \ref 00063 ** cuflow_cache_deinit in conjuction with a false return from \a manager. */ 00064 void cuflow_cacheconf_init(cuflow_cacheconf_t conf, 00065 cu_bool_t (*manager)(cuflow_cacheconf_t conf, 00066 struct timespec *t_now)); 00067 00068 /** A predefined cache configuration. Note that calling this the first time 00069 ** will spawn one worker thread if there are none running, see \ref 00070 ** cuflow_workers_h. */ 00071 cuflow_cacheconf_t cuflow_default_cacheconf(void); 00072 00073 #if 0 00074 CU_SINLINE void cuflow_cacheconf_lock_cache_range(cuflow_cacheconf_t conf) 00075 { cu_mutex_lock(&conf->cache_link_mutex); } 00076 00077 CU_SINLINE void cuflow_cacheconf_unlock_cache_range(cuflow_cacheconf_t conf) 00078 { cu_mutex_unlock(&conf->cache_link_mutex); } 00079 00080 CU_SINLINE cuflow_cache_t cuflow_cacheconf_first_cache(cuflow_cacheconf_t conf) 00081 { return cu_from(cuflow_cache, cu_dlink, conf->cache_link.next); } 00082 00083 CU_SINLINE cuflow_cache_t cuflow_cacheconf_end_cache(cuflow_cacheconf_t conf) 00084 { return cu_from(cuflow_cache, cu_dlink, &conf->cache_link); } 00085 00086 CU_SINLINE cuflow_cache_t cuflow_cacheconf_next_cache(cuflow_cache_t cache) 00087 { return cu_from(cuflow_cache, cu_dlink, cu_to(cu_dlink, cache)->next); } 00088 #endif 00089 00090 /** @} */ 00091 CU_END_DECLARATIONS 00092 00093 #endif