|
28 | 28 | #include "riscv_private.h" |
29 | 29 | #include "utils.h" |
30 | 30 | #if RV32_HAS(JIT) |
| 31 | +#if RV32_HAS(T2C) |
| 32 | +#include <pthread.h> |
| 33 | +#endif |
31 | 34 | #include "cache.h" |
32 | 35 | #include "jit.h" |
33 | 36 | #define CODE_CACHE_SIZE (4 * 1024 * 1024) |
@@ -184,6 +187,27 @@ IO_HANDLER_IMPL(byte, write_b, W) |
184 | 187 | #undef R |
185 | 188 | #undef W |
186 | 189 |
|
| 190 | +#if RV32_HAS(T2C) |
| 191 | +static pthread_t t2c_thread; |
| 192 | +static void *t2c_runloop(void *arg) |
| 193 | +{ |
| 194 | + riscv_t *rv = (riscv_t *) arg; |
| 195 | + while (rv->quit) { |
| 196 | + if (!list_empty(&rv->wait_queue)) { |
| 197 | + queue_entry_t *entry = |
| 198 | + list_last_entry(&rv->wait_queue, queue_entry_t, list); |
| 199 | + pthread_mutex_lock(&rv->wait_queue_lock); |
| 200 | + list_del_init(&entry->list); |
| 201 | + pthread_mutex_unlock(&rv->wait_queue_lock); |
| 202 | + t2c_compile(entry->block, |
| 203 | + (uint64_t) ((memory_t *) PRIV(rv)->mem)->mem_base); |
| 204 | + free(entry); |
| 205 | + } |
| 206 | + } |
| 207 | + return NULL; |
| 208 | +} |
| 209 | +#endif |
| 210 | + |
187 | 211 | riscv_t *rv_create(riscv_user_t rv_attr) |
188 | 212 | { |
189 | 213 | assert(rv_attr); |
@@ -269,6 +293,14 @@ riscv_t *rv_create(riscv_user_t rv_attr) |
269 | 293 | rv->jit_state = jit_state_init(CODE_CACHE_SIZE); |
270 | 294 | rv->block_cache = cache_create(BLOCK_MAP_CAPACITY_BITS); |
271 | 295 | assert(rv->block_cache); |
| 296 | +#if RV32_HAS(T2C) |
| 297 | + rv->quit = false; |
| 298 | + /* prepare wait queue. */ |
| 299 | + pthread_mutex_init(&rv->wait_queue_lock, NULL); |
| 300 | + INIT_LIST_HEAD(&rv->wait_queue); |
| 301 | + /* activate the background compilation thread. */ |
| 302 | + pthread_create(&t2c_thread, NULL, t2c_runloop, rv); |
| 303 | +#endif |
272 | 304 | #endif |
273 | 305 |
|
274 | 306 | return rv; |
@@ -353,6 +385,11 @@ void rv_delete(riscv_t *rv) |
353 | 385 | memory_delete(attr->mem); |
354 | 386 | block_map_destroy(rv); |
355 | 387 | #else |
| 388 | +#if RV32_HAS(T2C) |
| 389 | + rv->quit = true; |
| 390 | + pthread_join(t2c_thread, NULL); |
| 391 | + pthread_mutex_destroy(&rv->wait_queue_lock); |
| 392 | +#endif |
356 | 393 | mpool_destroy(rv->chain_entry_mp); |
357 | 394 | jit_state_exit(rv->jit_state); |
358 | 395 | cache_free(rv->block_cache); |
|
0 commit comments