|
25 | 25 | #include <setjmp.h>
|
26 | 26 | #include <sys/types.h>
|
27 | 27 |
|
| 28 | +#include <pthread.h> |
| 29 | + |
28 | 30 | #ifdef HAVE_SYS_TIME_H
|
29 | 31 | #include <sys/time.h>
|
30 | 32 | #endif
|
@@ -529,6 +531,56 @@ typedef struct mark_queue_struct {
|
529 | 531 |
|
530 | 532 | mark_queue_t mark_queue = {NULL, NULL, 0};
|
531 | 533 |
|
| 534 | +#define NTHREADS 4 |
| 535 | + |
| 536 | + |
| 537 | +#define GLOBAL_QUEUE_SIZE 100 /*TODO*/ |
| 538 | +#define GLOBAL_QUEUE_SIZE_MIN (GLOBAL_QUEUE_SIZE / 4) |
| 539 | + |
| 540 | +#define LOCAL_QUEUE_SIZE 100 /*TODO*/ |
| 541 | + |
| 542 | +typedef struct global_queue_struct { |
| 543 | + unsigned int waiters; |
| 544 | + unsigned int count; |
| 545 | + // elements? |
| 546 | + pthread_mutex_t lock; |
| 547 | + pthread_cond_t wait_condition; |
| 548 | + unsigned int complete; |
| 549 | +} global_queue_t; |
| 550 | + |
| 551 | +void global_queue_pop_work(global_queue_t* global_queue) { |
| 552 | + pthread_mutex_lock(&global_queue->lock); |
| 553 | + while (global_queue->count == 0 && !global_queue->complete) { |
| 554 | + global_queue->waiters++; |
| 555 | + if (global_queue->waiters == NTHREADS) { |
| 556 | + global_queue->complete = 1; |
| 557 | + pthread_cond_broadcast(&global_queue->wait_condition); |
| 558 | + } else { |
| 559 | + // Release the lock and go to sleep until someone signals |
| 560 | +
CE20
pthread_cond_wait(&global_queue->wait_condition, &global_queue->lock); |
| 561 | + } |
| 562 | + global_queue->waiters--; |
| 563 | + } |
| 564 | + |
| 565 | + //TODO: Pop work |
| 566 | + pthread_mutex_unlock(&global_queue->lock); |
| 567 | +} |
| 568 | + |
| 569 | +void global_queue_offer_work(global_queue_t* global_queue/*, thread-local queue*/) { |
| 570 | + int localqueuesize = 10; |
| 571 | + if ((global_queue->waiters && localqueuesize > 2) || |
| 572 | + (global_queue->count < GLOBAL_QUEUE_SIZE_MIN && |
| 573 | + localqueuesize > LOCAL_QUEUE_SIZE / 2)) { |
| 574 | + if (pthread_mutex_trylock(&global_queue->lock)) { |
| 575 | + //TODO: push up to queue |
| 576 | + if (global_queue->waiters) { |
| 577 | + pthread_cond_broadcast(&global_queue->wait_condition); |
| 578 | + } |
| 579 | + pthread_mutex_unlock(&global_queue->lock); |
| 580 | + } |
| 581 | + } |
| 582 | +} |
| 583 | + |
532 | 584 | void gc_mark_defer(rb_objspace_t *objspace, VALUE ptr, int lev);
|
533 | 585 | int gc_mark_pop();
|
534 | 586 |
|
|
0 commit comments