summaryrefslogtreecommitdiffstats
path: root/server/red_client_shared_cache.h
blob: 1d3c45284b0afac39cca61d91f6528e5731deb42 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#if defined(CLIENT_PIXMAPS_CACHE)

#define CACHE PixmapCache

#define CACHE_NAME bits_cache
#define CACHE_HASH_KEY BITS_CACHE_HASH_KEY
#define CACHE_HASH_SIZE BITS_CACHE_HASH_SIZE
#define PIPE_ITEM_TYPE PIPE_ITEM_TYPE_INVAL_PIXMAP
#define FUNC_NAME(name) pixmap_cache_##name
#define PRIVATE_FUNC_NAME(name) __pixmap_cache_##name
#define CHANNEL DisplayChannel
#define CACH_GENERATION pixmap_cache_generation
#define INVAL_ALL_VERB RED_DISPLAY_INVAL_ALL_PIXMAPS
#else

#error "no cache type."

#endif


static int FUNC_NAME(hit)(CACHE *cache, uint64_t id, CHANNEL *channel)
{
    NewCacheItem *item;
    uint64_t serial;

    serial = channel_message_serial((RedChannel *)channel);
    pthread_mutex_lock(&cache->lock);
    item = cache->hash_table[CACHE_HASH_KEY(id)];

    while (item) {
        if (item->id == id) {
            ring_remove(&item->lru_link);
            ring_add(&cache->lru, &item->lru_link);
            ASSERT(channel->base.id < MAX_CACHE_CLIENTS)
            item->sync[channel->base.id] = serial;
            cache->sync[channel->base.id] = serial;
            break;
        }
        item = item->next;
    }
    pthread_mutex_unlock(&cache->lock);

    return !!item;
}

static int FUNC_NAME(add)(CACHE *cache, uint64_t id, uint32_t size, CHANNEL *channel)
{
    NewCacheItem *item;
    uint64_t serial;
    int key;

    ASSERT(size > 0);

    item = malloc(sizeof(*item));
    if (!item) {
        return FALSE;
    }
    serial = channel_message_serial((RedChannel *)channel);

    pthread_mutex_lock(&cache->lock);

    if (cache->generation != channel->CACH_GENERATION) {
        if (!channel->pending_pixmaps_sync) {
            red_pipe_add_type((RedChannel *)channel, PIPE_ITEM_TYPE_PIXMAP_SYNC);
            channel->pending_pixmaps_sync = TRUE;
        }
        pthread_mutex_unlock(&cache->lock);
        free(item);
        return FALSE;
    }

    cache->available -= size;
    while (cache->available < 0) {
        NewCacheItem *tail;
        NewCacheItem **now;

        if (!(tail = (NewCacheItem *)ring_get_tail(&cache->lru)) ||
                                                   tail->sync[channel->base.id] == serial) {
            cache->available += size;
            pthread_mutex_unlock(&cache->lock);
            free(item);
            return FALSE;
        }

        now = &cache->hash_table[CACHE_HASH_KEY(tail->id)];
        for (;;) {
            ASSERT(*now);
            if (*now == tail) {
                *now = tail->next;
                break;
            }
            now = &(*now)->next;
        }
        ring_remove(&tail->lru_link);
        cache->items--;
        cache->available += tail->size;
        cache->sync[channel->base.id] = serial;
        display_channel_push_release(channel, RED_RES_TYPE_PIXMAP, tail->id, tail->sync);
        free(tail);
    }
    ++cache->items;
    item->next = cache->hash_table[(key = CACHE_HASH_KEY(id))];
    cache->hash_table[key] = item;
    ring_item_init(&item->lru_link);
    ring_add(&cache->lru, &item->lru_link);
    item->id = id;
    item->size = size;
    memset(item->sync, 0, sizeof(item->sync));
    item->sync[channel->base.id] = serial;
    cache->sync[channel->base.id] = serial;
    pthread_mutex_unlock(&cache->lock);
    return TRUE;
}

static void PRIVATE_FUNC_NAME(clear)(CACHE *cache)
{
    NewCacheItem *item;

    if (cache->freezed) {
        cache->lru.next = cache->freezed_head;
        cache->lru.prev = cache->freezed_tail;
        cache->freezed = FALSE;
    }

    while ((item = (NewCacheItem *)ring_get_head(&cache->lru))) {
        ring_remove(&item->lru_link);
        free(item);
    }
    memset(cache->hash_table, 0, sizeof(*cache->hash_table) * CACHE_HASH_SIZE);

    cache->available = cache->size;
    cache->items = 0;
}

static void FUNC_NAME(reset)(CACHE *cache, CHANNEL *channel, RedWaitForChannels* sync_data)
{
    uint8_t wait_count;
    uint64_t serial;
    uint32_t i;

    serial = channel_message_serial((RedChannel *)channel);
    pthread_mutex_lock(&cache->lock);
    PRIVATE_FUNC_NAME(clear)(cache);

    channel->CACH_GENERATION = ++cache->generation;
    cache->generation_initiator.client = channel->base.id;
    cache->generation_initiator.message = serial;
    cache->sync[channel->base.id] = serial;

    wait_count = 0;
    for (i = 0; i < MAX_CACHE_CLIENTS; i++) {
        if (cache->sync[i] && i != channel->base.id) {
            sync_data->wait_list[wait_count].channel_type = RED_CHANNEL_DISPLAY;
            sync_data->wait_list[wait_count].channel_id = i;
            sync_data->wait_list[wait_count++].message_serial = cache->sync[i];
        }
    }
    sync_data->wait_count = wait_count;
    pthread_mutex_unlock(&cache->lock);
}

static int FUNC_NAME(freeze)(CACHE *cache)
{
    pthread_mutex_lock(&cache->lock);

    if (cache->freezed) {
        pthread_mutex_unlock(&cache->lock);
        return FALSE;
    }

    cache->freezed_head = cache->lru.next;
    cache->freezed_tail = cache->lru.prev;
    ring_init(&cache->lru);
    memset(cache->hash_table, 0, sizeof(*cache->hash_table) * CACHE_HASH_SIZE);
    cache->available = -1;
    cache->freezed = TRUE;

    pthread_mutex_unlock(&cache->lock);
    return TRUE;
}

static void FUNC_NAME(destroy)(CACHE *cache)
{
    ASSERT(cache);

    pthread_mutex_lock(&cache->lock);
    PRIVATE_FUNC_NAME(clear)(cache);
    pthread_mutex_unlock(&cache->lock);
}

#undef CACHE_NAME
#undef CACHE_HASH_KEY
#undef CACHE_HASH_SIZE
#undef CACHE_INVAL_TYPE
#undef CACHE_MAX_CLIENT_SIZE
#undef FUNC_NAME
#undef VAR_NAME
#undef CHANNEL