Debian Codegraph @

lib/request_canceler.c

raw symbols SCIP
1 /*
2 Copyright(C) 2014-2016 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_ctx.h"
20 #include "grn_ctx_impl.h"
21 #include "grn_request_canceler.h"
22
23 typedef struct _grn_request_canceler grn_request_canceler;
24 struct _grn_request_canceler {
25 grn_hash *entries;
26 grn_mutex mutex;
27 };
28
29 typedef struct _grn_request_canceler_entry grn_request_canceler_entry;
30 struct _grn_request_canceler_entry {
31 grn_ctx *ctx;
32 };
33
34 static grn_ctx grn_the_request_canceler_ctx;
35 static grn_request_canceler *grn_the_request_canceler = NULL;
36
37 bool
38 grn_request_canceler_init(void)
39 {
40 grn_ctx *ctx = &grn_the_request_canceler_ctx;
41
42 grn_ctx_init(ctx, 0);
43
44 grn_the_request_canceler = GRN_CALLOC(sizeof(grn_request_canceler));
45 if (!grn_the_request_canceler) {
46 ERR(GRN_NO_MEMORY_AVAILABLE,
47 "[request-canceler] failed to allocate the global request canceler");
48 return false;
49 }
50
51 grn_the_request_canceler->entries =
52 grn_hash_create(ctx, NULL, GRN_TABLE_MAX_KEY_SIZE,
53 sizeof(grn_request_canceler_entry), GRN_OBJ_KEY_VAR_SIZE);
54 if (!grn_the_request_canceler->entries) {
55 return false;
56 }
57 MUTEX_INIT(grn_the_request_canceler->mutex);
58
59 return true;
60 }
61
62 void
63 grn_request_canceler_register(grn_ctx *ctx,
64 const char *request_id, unsigned int size)
65 {
66 MUTEX_LOCK(grn_the_request_canceler->mutex);
67 {
68 grn_hash *entries = grn_the_request_canceler->entries;
69 grn_id id;
70 void *value;
71 id = grn_hash_add(&grn_the_request_canceler_ctx,
72 entries, request_id, size, &value, NULL);
73 if (id) {
74 grn_request_canceler_entry *entry = value;
75 entry->ctx = ctx;
76 }
77 }
78 MUTEX_UNLOCK(grn_the_request_canceler->mutex);
79 }
80
81 void
82 grn_request_canceler_unregister(grn_ctx *ctx,
83 const char *request_id, unsigned int size)
84 {
85 MUTEX_LOCK(grn_the_request_canceler->mutex);
86 {
87 grn_hash *entries = grn_the_request_canceler->entries;
88 grn_hash_delete(&grn_the_request_canceler_ctx,
89 entries, request_id, size, NULL);
90 }
91 MUTEX_UNLOCK(grn_the_request_canceler->mutex);
92
93 if (ctx->rc == GRN_CANCEL) {
94 ERRSET(ctx, GRN_LOG_NOTICE, ctx->rc,
95 "[request-canceler] a request is canceled: <%.*s>",
96 size, request_id);
97 }
98 }
99
100 static bool
101 grn_request_canceler_cancel_entry(grn_request_canceler_entry *entry)
102 {
103 if (entry->ctx->rc == GRN_SUCCESS) {
104 entry->ctx->rc = GRN_CANCEL;
105 if (entry->ctx->impl->current_request_timer_id) {
106 void *timer_id = entry->ctx->impl->current_request_timer_id;
107 entry->ctx->impl->current_request_timer_id = NULL;
108 grn_request_timer_unregister(timer_id);
109 }
110 return true;
111 }
112 return false;
113 }
114
115 bool
116 grn_request_canceler_cancel(const char *request_id, unsigned int size)
117 {
118 bool canceled = false;
119 MUTEX_LOCK(grn_the_request_canceler->mutex);
120 {
121 grn_ctx *ctx = &grn_the_request_canceler_ctx;
122 grn_hash *entries = grn_the_request_canceler->entries;
123 void *value;
124 if (grn_hash_get(ctx, entries, request_id, size, &value)) {
125 grn_request_canceler_entry *entry = value;
126 if (grn_request_canceler_cancel_entry(entry)) {
127 canceled = true;
128 }
129 }
130 }
131 MUTEX_UNLOCK(grn_the_request_canceler->mutex);
132 return canceled;
133 }
134
135 bool
136 grn_request_canceler_cancel_all(void)
137 {
138 bool canceled = false;
139 MUTEX_LOCK(grn_the_request_canceler->mutex);
140 {
141 grn_ctx *ctx = &grn_the_request_canceler_ctx;
142 grn_hash *entries = grn_the_request_canceler->entries;
143 grn_hash_cursor *cursor;
144
145 cursor = grn_hash_cursor_open(ctx, entries,
146 NULL, 0, NULL, 0,
147 0, -1, 0);
148 if (cursor) {
149 while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) {
150 void *value;
151 if (grn_hash_cursor_get_value(ctx, cursor, &value) > 0) {
152 grn_request_canceler_entry *entry = value;
153 if (grn_request_canceler_cancel_entry(entry)) {
154 canceled = true;
155 }
156 }
157 }
158 grn_hash_cursor_close(ctx, cursor);
159 }
160 }
161 MUTEX_UNLOCK(grn_the_request_canceler->mutex);
162 return canceled;
163 }
164
165 void
166 grn_request_canceler_fin(void)
167 {
168 grn_ctx *ctx = &grn_the_request_canceler_ctx;
169
170 grn_hash_close(ctx, grn_the_request_canceler->entries);
171 MUTEX_FIN(grn_the_request_canceler->mutex);
172 GRN_FREE(grn_the_request_canceler);
173 grn_the_request_canceler = NULL;
174 grn_ctx_fin(ctx);
175 }