-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.h
131 lines (113 loc) · 3.1 KB
/
cache.h
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
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include "pstring.h"
#ifndef _CACHE_H
#define _CACHE_H
struct cache_entry;
typedef struct cache_entry_reader {
void (*callback)(struct cache_entry*, void*);
void* arg;
struct cache_entry_reader* next;
} cache_entry_reader_t;
typedef struct cache_entry {
char* url;
volatile bool finished;
volatile bool invalid;
pstring_t data;
cache_entry_reader_t* readers;
pthread_rwlock_t lock;
struct cache_entry* next;
} cache_entry_t;
typedef struct cache {
pthread_mutex_t global_lock;
cache_entry_t* list;
} cache_t;
/**
* Init cache.
*
* @return {@code 0} if success.
*/
int cache_init(void);
/**
* Finds stored cache entry or creates new, if not exists.
*
* @param url Entry name.
* @param entry Returning entry.
*
* @return {@code 0} if entry found, {@code 1} if not found and
* created and {@code -1} if error occured.
*/
int cache_find_or_create(char* url, cache_entry_t** entry);
/**
* Create reader for entry.
*
* @param entry Target entry.
* @param callback Callback for cache updates.
* @param arg Argument for callback.
*
* @return Created reader or {@code NULL}.
*/
cache_entry_reader_t* cache_entry_subscribe(cache_entry_t* entry,
void (*callback)(cache_entry_t*,
void*),
void* arg);
/**
* Unsubscribes reader associated with cache entry.
*
* @param entry Target entry.
* @param reader Target reader.
*
* @return {@code true} if success.
*/
bool cache_entry_unsubscribe(cache_entry_t* entry,
cache_entry_reader_t* reader);
/**
* Extracts data part from cache entry.
*
* @param entry Target entry.
* @param offset Offset from entry data beginning.
* @param buffer Storage buffer.
* @param len Storage buffer length.
*
* @return Amount of bytes stored to buffer or {@code -1} if error occured.
*/
ssize_t cache_entry_extract(cache_entry_t* entry,
size_t offset,
char* buffer,
size_t len);
/**
* Appends new data to cache entry and notify all subscribers.
*
* @param entry Target entry.
* @param data Appending data.
* @param len Length of appending data.
*
* @return {@code true} if success.
*/
bool cache_entry_append(cache_entry_t* entry, const char* data, size_t len);
/**
* Marks cache entry as successfully finished and notify all subscribers.
*
* @param entry Target entry.
*/
void cache_entry_mark_finished(cache_entry_t* entry);
/**
* Marks cache entry as invalid.
* If entry marked as invalid, new readers will not connect to this entry.
*
* @param entry Target entry.
*/
void cache_entry_mark_invalid(cache_entry_t* entry);
/**
* Marks cache entry as finished and invalid and notify all subscibers.
* If entry marked as invalid, new readers will not connect to this entry.
*
* @param entry Target entry.
*/
void cache_entry_mark_invalid_and_finished(cache_entry_t* entry);
/**
* Cleanup all cache entries.
*/
void cache_free(void);
#endif