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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cache.h"
#define MAX_RANDOM_ENTRIES 32
struct key_record {
char *key;
char *value;
};
int generate_random_entry(struct key_record **entry);
int generate_random_string(char **dst, const size_t len);
void free_random_entry(void *entry);
void *producer(void *arg)
{
struct foo_cache *cache = arg;
int i;
for (i = 0; i < MAX_RANDOM_ENTRIES; i++) {
struct key_record *entry = NULL;
if (generate_random_entry(&entry)) {
fprintf(stderr, "generate_random_entry() failed\n");
continue;
}
#if defined(DEBUG)
printf("Random Entry:\n");
printf(" key: %s\n", entry->key);
printf(" Key: %s\n", entry->value);
#else
printf("inserted %s (%d)\n", entry->key,
(int)strlen(entry->key));
#endif
if (foo_cache_insert(cache, entry->key, entry)) {
fprintf(stderr, "foo_cache_insert() failed\n");
continue;
}
}
pthread_exit(NULL);
}
void *consumer(void *arg)
{
struct foo_cache *cache = arg;
struct key_record *result = NULL;
char *buffer = malloc(64);
char key[33];
int stop = 0;
if (!buffer)
goto out;
/* give producer time to populate the cache */
sleep(2);
printf("\n\n");
do {
memset(key, 0, 64);
result = NULL;
printf("Enter key for lookup: ");
fgets(buffer, sizeof(key), stdin);
sscanf(buffer, "%s\n", key);
/* read '\n' from stdin */
getchar();
if (strncmp(key, "exit", 4) == 0) {
stop = 1;
continue;
}
printf("Got key %s (%d)\n", key, (int)strlen(key));
if (foo_cache_lookup(cache, key, &result)) {
fprintf(stderr, "Could not retrieve key %s\n", key);
continue;
}
if (!result) {
printf("MISS\n");
continue;
}
printf("HIT\n");
printf("key: %s\n", result->key);
printf("key : %s\n", result->value);
} while (!stop);
out:
if (buffer)
free(buffer);
pthread_exit(NULL);
}
int main()
{
int rv;
struct foo_cache *cache = NULL;
pthread_t workers[2];
rv = foo_cache_create(&cache, MAX_RANDOM_ENTRIES / 2,
free_random_entry);
if (rv) {
fprintf(stderr, "Could not create cache\n");
exit(1);
}
(void)pthread_create(&workers[0], NULL, producer, (void *)cache);
(void)pthread_create(&workers[1], NULL, consumer, (void *)cache);
pthread_join(workers[0], NULL);
pthread_join(workers[1], NULL);
(void)foo_cache_delete(cache, 0);
return 0;
}
int generate_random_entry(struct key_record **entry)
{
struct key_record *new = NULL;
char *key = NULL;
char *value = NULL;
int rv;
if (!entry)
return EINVAL;
rv = generate_random_string(&key, 33);
if (rv)
return rv;
rv = generate_random_string(&value, 129);
if (rv)
return rv;
if ((new = malloc(sizeof(*new))) == NULL) {
free(key);
free(value);
return ENOMEM;
}
new->key = key;
new->value = value;
*entry = new;
return 0;
}
int generate_random_string(char **dst, const size_t len)
{
static const char alphanum[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
size_t i;
char *s;
if (!dst || len == 0)
return EINVAL;
if ((s = malloc(len)) == NULL)
return ENOMEM;
for (i = 0; i < len - 1; i++) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len - 1] = '\0';
*dst = s;
return 0;
}
void free_random_entry(void *entry)
{
#if defined(DEBUG)
fprintf(stderr, "In %s: entry @ %p\n", __func__, entry);
#endif
struct key_record *record = entry;
if (!record)
return;
if (record->key)
free(record->key);
if (record->value)
free(record->value);
free(record);
record = NULL;
}
|