-
Notifications
You must be signed in to change notification settings - Fork 86
/
operationqueue.c
255 lines (214 loc) · 7.03 KB
/
operationqueue.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* libmypaint - The MyPaint Brush Library
* Copyright (C) 2012 Jon Nordby <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#if MYPAINT_CONFIG_USE_GLIB
#include <glib.h>
#else // not MYPAINT_CONFIG_USE_GLIB
#include "mypaint-glib-compat.h"
#endif
#include "operationqueue.h"
#include "fifo.h"
struct OperationQueue {
TileMap *tile_map;
TileIndex *dirty_tiles;
int dirty_tiles_n;
};
/* For use with queue_delete */
void
operation_delete_func(void *user_data) {
if (user_data) {
free(user_data);
}
}
void
free_fifo(void *item) {
Fifo *op_queue = item;
if (op_queue) {
fifo_free(op_queue, operation_delete_func);
}
}
gboolean
operation_queue_resize(OperationQueue *self, int new_size)
{
if (new_size == 0) {
if (self->tile_map) {
assert(self->dirty_tiles);
tile_map_free(self->tile_map, TRUE);
self->tile_map = NULL;
free(self->dirty_tiles);
self->dirty_tiles = NULL;
self->dirty_tiles_n = 0;
}
return TRUE;
} else {
TileMap *new_tile_map = tile_map_new(new_size, sizeof(Fifo *), free_fifo);
const int new_map_size = new_size*2*new_size*2;
TileIndex *new_dirty_tiles = (TileIndex *)malloc(new_map_size*sizeof(TileIndex));
if (self->tile_map) {
tile_map_copy_to(self->tile_map, new_tile_map);
for(int i = 0; i < self->dirty_tiles_n; i++) {
new_dirty_tiles[i] = self->dirty_tiles[i];
}
tile_map_free(self->tile_map, FALSE);
free(self->dirty_tiles);
}
self->tile_map = new_tile_map;
self->dirty_tiles = new_dirty_tiles;
return FALSE;
}
}
OperationQueue *
operation_queue_new(void)
{
OperationQueue *self = (OperationQueue *)malloc(sizeof(OperationQueue));
self->tile_map = NULL;
self->dirty_tiles_n = 0;
self->dirty_tiles = NULL;
#ifdef HEAVY_DEBUG
operation_queue_resize(self, 1);
#else
operation_queue_resize(self, 10);
#endif
return self;
}
void
operation_queue_free(OperationQueue *self)
{
operation_queue_resize(self, 0); // free the tile map data
free(self);
}
int
tile_equal(TileIndex a, TileIndex b)
{
return (a.x == b.x && a.y == b.y);
}
size_t
remove_duplicate_tiles(TileIndex *array, size_t length)
{
if (length < 2) {
// There cannot be any duplicates
return length;
}
size_t new_length = 1;
size_t i, j;
for (i = 1; i < length; i++) {
for (j = 0; j < new_length; j++) {
if (tile_equal(array[j], array[i])) {
break;
}
}
if (j == new_length) {
array[new_length++] = array[i];
}
}
return new_length;
}
/* Returns all tiles that are have operations queued
* The consumer that actually does the processing should iterate over this list
* of tiles, and use operation_queue_pop() to pop all the operations.
*
* Concurrency: This function is not thread-safe on the same @self instance. */
int
operation_queue_get_dirty_tiles(OperationQueue *self, TileIndex** tiles_out)
{
self->dirty_tiles_n = remove_duplicate_tiles(self->dirty_tiles, self->dirty_tiles_n);
*tiles_out = self->dirty_tiles;
return self->dirty_tiles_n;
}
/* Clears the list of dirty tiles
* Consumers should call this after having processed all the tiles.
*
* Concurrency: This function is not thread-safe on the same @self instance. */
void
operation_queue_clear_dirty_tiles(OperationQueue *self)
{
// operation_queue_add will overwrite the invalid tiles as new dirty tiles comes in
self->dirty_tiles_n = 0;
}
/* Add an operation to the queue for tile @index
* Note: if an operation affects more than one tile, it must be added once per tile.
*
* Concurrency: This function is not thread-safe on the same @self instance. */
void
operation_queue_add(OperationQueue *self, TileIndex index, OperationDataDrawDab *op)
{
while (!tile_map_contains(self->tile_map, index)) {
#ifdef HEAVY_DEBUG
operation_queue_resize(self, self->tile_map->size+1);
#else
operation_queue_resize(self, self->tile_map->size*2);
#endif
}
Fifo **queue_pointer = (Fifo **)tile_map_get(self->tile_map, index);
Fifo *op_queue = *queue_pointer;
if (op_queue == NULL) {
// Lazy initialization
op_queue = fifo_new();
*queue_pointer = op_queue;
}
if (fifo_peek_first(op_queue) == NULL) {
// Critical section, not thread-safe
if (!(self->dirty_tiles_n < self->tile_map->size*2*self->tile_map->size*2)) {
// Prune duplicate tiles that cause us to almost exceed max
self->dirty_tiles_n = remove_duplicate_tiles(self->dirty_tiles, self->dirty_tiles_n);
}
assert(self->dirty_tiles_n < self->tile_map->size*2*self->tile_map->size*2);
self->dirty_tiles[self->dirty_tiles_n++] = index;
}
fifo_push(op_queue, (void *)op);
}
/* Pop an operation off the queue for tile @index
* The user of this function is responsible for freeing the result using free()
*
* Concurrency: This function is reentrant (and lock-free) on different @index */
OperationDataDrawDab *
operation_queue_pop(OperationQueue *self, TileIndex index)
{
OperationDataDrawDab *op = NULL;
if (!tile_map_contains(self->tile_map, index)) {
return NULL;
}
Fifo **queue_pointer = (Fifo **)tile_map_get(self->tile_map, index);
Fifo *op_queue = *queue_pointer;
if (!op_queue) {
return NULL;
}
op = (OperationDataDrawDab *)fifo_pop(op_queue);
if (!op) {
// Queue empty
fifo_free(op_queue, operation_delete_func);
*queue_pointer = NULL;
}
return op;
}
OperationDataDrawDab *
operation_queue_peek_first(OperationQueue *self, TileIndex index) {
if (!tile_map_contains(self->tile_map, index)) {
return NULL;
}
Fifo *op_queue = (Fifo *)*tile_map_get(self->tile_map, index);
return (!op_queue) ? NULL : (OperationDataDrawDab *)fifo_peek_first(op_queue);
}
OperationDataDrawDab *
operation_queue_peek_last(OperationQueue *self, TileIndex index) {
if (!tile_map_contains(self->tile_map, index)) {
return NULL;
}
Fifo *op_queue = (Fifo *)*tile_map_get(self->tile_map, index);
return (!op_queue) ? NULL : (OperationDataDrawDab *)fifo_peek_last(op_queue);
}