-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar_worker.c
76 lines (63 loc) · 1.33 KB
/
astar_worker.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
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include "astar.h"
#include "astar_worker.h"
#include "level.h"
#include "worker.h"
static struct worker s_astar_worker;
struct astar_job
{
struct level_t *level;
struct point a;
struct point b;
astar_callback callback;
void *data;
};
static void astar_worker(void *arg)
{
struct astar_job *job = arg;
if (job->callback != NULL)
{
struct point *path = as_find(job->level, &job->a, &job->b);
if (job->callback != NULL)
{
job->callback(job->level, path, job->data);
}
else
{
free(path);
}
}
level_inuse(job->level, false);
free(job);
}
void astar_worker_init(void)
{
worker_init(&s_astar_worker, "astar", 30000, 5, astar_worker);
}
void astar_worker_deinit(void)
{
worker_deinit(&s_astar_worker);
}
void *astar_queue(struct level_t *level, const struct point *a, const struct point *b, astar_callback callback, void *data)
{
assert(level != NULL);
assert(a != NULL);
assert(b != NULL);
assert(callback != NULL);
if (!level_inuse(level, true)) return NULL;
struct astar_job *job = malloc(sizeof *job);
job->level = level;
job->a = *a;
job->b = *b;
job->callback = callback;
job->data = data;
worker_queue(&s_astar_worker, job);
return job;
}
void astar_cancel(void *data)
{
struct astar_job *job = data;
if (job != NULL) job->callback = NULL;
}