|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library 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 GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#ifndef _CACHE_ENTRY_HPP_ |
| 22 | +#define _CACHE_ENTRY_HPP_ |
| 23 | + |
| 24 | +#include <pthread.h> |
| 25 | +#include <set> |
| 26 | +#include "details/http_response_ptr.hpp" |
| 27 | + |
| 28 | +namespace httpserver |
| 29 | +{ |
| 30 | +namespace details |
| 31 | +{ |
| 32 | + |
| 33 | +struct pthread_t_comparator |
| 34 | +{ |
| 35 | + bool operator()(const pthread_t& t1, const pthread_t& t2) const |
| 36 | + { |
| 37 | + return pthread_equal(t1, t2); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +struct cache_entry |
| 42 | +{ |
| 43 | + long ts; |
| 44 | + int validity; |
| 45 | + details::http_response_ptr response; |
| 46 | + pthread_rwlock_t elem_guard; |
| 47 | + pthread_mutex_t lock_guard; |
| 48 | + std::set<pthread_t, pthread_t_comparator> lockers; |
| 49 | + |
| 50 | + cache_entry(): |
| 51 | + ts(-1), |
| 52 | + validity(-1) |
| 53 | + { |
| 54 | + pthread_rwlock_init(&elem_guard, NULL); |
| 55 | + pthread_mutex_init(&lock_guard, NULL); |
| 56 | + } |
| 57 | + |
| 58 | + ~cache_entry() |
| 59 | + { |
| 60 | + pthread_rwlock_destroy(&elem_guard); |
| 61 | + pthread_mutex_destroy(&lock_guard); |
| 62 | + } |
| 63 | + |
| 64 | + cache_entry(const cache_entry& b): |
| 65 | + ts(b.ts), |
| 66 | + validity(b.validity), |
| 67 | + response(b.response), |
| 68 | + elem_guard(b.elem_guard), |
| 69 | + lock_guard(b.lock_guard) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + void operator= (const cache_entry& b) |
| 74 | + { |
| 75 | + ts = b.ts; |
| 76 | + validity = b.validity; |
| 77 | + response = b.response; |
| 78 | + pthread_rwlock_destroy(&elem_guard); |
| 79 | + pthread_mutex_destroy(&lock_guard); |
| 80 | + elem_guard = b.elem_guard; |
| 81 | + } |
| 82 | + |
| 83 | + cache_entry( |
| 84 | + details::http_response_ptr response, |
| 85 | + long ts = -1, |
| 86 | + int validity = -1 |
| 87 | + ): |
| 88 | + ts(ts), |
| 89 | + validity(validity), |
| 90 | + response(response) |
| 91 | + { |
| 92 | + pthread_rwlock_init(&elem_guard, NULL); |
| 93 | + pthread_mutex_init(&lock_guard, NULL); |
| 94 | + } |
| 95 | + |
| 96 | + void lock(bool write = false) |
| 97 | + { |
| 98 | + pthread_mutex_lock(&lock_guard); |
| 99 | + pthread_t tid = pthread_self(); |
| 100 | + if(!lockers.count(tid)) |
| 101 | + { |
| 102 | + if(write) |
| 103 | + { |
| 104 | + lockers.insert(tid); |
| 105 | + pthread_mutex_unlock(&lock_guard); |
| 106 | + pthread_rwlock_wrlock(&elem_guard); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + lockers.insert(tid); |
| 111 | + pthread_mutex_unlock(&lock_guard); |
| 112 | + pthread_rwlock_rdlock(&elem_guard); |
| 113 | + } |
| 114 | + } |
| 115 | + else |
| 116 | + pthread_mutex_unlock(&lock_guard); |
| 117 | + } |
| 118 | + |
| 119 | + void unlock() |
| 120 | + { |
| 121 | + pthread_mutex_lock(&lock_guard); |
| 122 | + { |
| 123 | + pthread_t tid = pthread_self(); |
| 124 | + if(lockers.count(tid)) |
| 125 | + { |
| 126 | + lockers.erase(tid); |
| 127 | + pthread_rwlock_unlock(&elem_guard); |
| 128 | + } |
| 129 | + } |
| 130 | + pthread_mutex_unlock(&lock_guard); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +} //details |
| 135 | +} //httpserver |
| 136 | + |
| 137 | +#endif //_CACHE_ENTRY_HPP_ |
0 commit comments