Ported to pthread. cache_mutex and cn_mutex are now recursive mutexes.
[pwmd.git] / src / cache.h
blobe01b1f66643b447e0ea4b7585f5ee51a5771996c
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 #ifndef CACHE_H
20 #define CACHE_H
22 #include <pthread.h>
24 #ifdef DEBUG
25 #define CACHE_LOCK_DEBUG log_write(N_("%s(%i): LOCK tid=%p"), __FUNCTION__, __LINE__, pthread_self());
26 #define CACHE_UNLOCK_DEBUG log_write(N_("%s(%i): UNLOCK tid=%p"), __FUNCTION__, __LINE__, pthread_self());
27 #else
28 #define CACHE_LOCK_DEBUG
29 #define CACHE_UNLOCK_DEBUG
30 #endif
32 #define CACHE_TRYLOCK(ctx) if (pthread_mutex_trylock(&cache_mutex) == EBUSY) {\
33 log_write(N_("%s(%i): cache mutex LOCKED"), __FUNCTION__, __LINE__); \
34 if (ctx) \
35 send_status(ctx, STATUS_LOCKED, NULL); \
36 pthread_mutex_lock(&cache_mutex); \
39 #define CACHE_LOCK(ctx) CACHE_LOCK_DEBUG CACHE_TRYLOCK(ctx)
40 #define CACHE_UNLOCK CACHE_UNLOCK_DEBUG pthread_mutex_unlock(&cache_mutex)
42 typedef struct {
43 guchar filename[16]; /* MD5 hash. */
44 guchar key[32]; /* SHA256/AES256/gcrykeysize */
45 glong timeout; /* adjust_cache_time_thread(). */
46 glong reset; /* To reset .timeout. */
47 pthread_mutex_t *mutex; /* Not related to cache_mutex. */
48 gint refcount; /* The number of associated clients. */
49 } file_cache_t;
51 GSList *key_cache;
52 pthread_mutex_t cache_mutex;
54 void cache_adjust_timer(void);
55 gboolean cache_set_timeout(const guchar *md5filename, glong timeout);
56 gboolean cache_reset_timeout(const guchar *md5filename, glong timeout);
57 gboolean cache_iscached(const guchar *md5filename);
58 gboolean cache_clear(const guchar *md5filename, gint which);
59 gboolean cache_add_file(const guchar *md5file, const guchar *shakey);
60 gboolean cache_get_key(const guchar *md5file, guchar *shakey);
61 gboolean cache_update_key(const guchar *md5filename, const guchar *shakey);
62 gint cache_file_count(void);
63 gboolean cache_has_file(const guchar *md5file);
64 void send_status_all(status_msg_t which);
65 gboolean cache_get_mutex(const guchar *md5filename, pthread_mutex_t **result);
66 gboolean cache_decr_refcount(const guchar *md5filename);
67 gboolean cache_incr_refcount(const guchar *md5filename);
68 void cache_free();
70 #endif