8000 Merge pull request #2 from maxdjohnson/cs194_master · kevints/ruby@4dc5e82 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4dc5e82

Browse files
author
andrewmains12
committed
Merge pull request #2 from maxdjohnson/cs194_master
added locking logic for the global queue
2 parents a7b313b + 85962a3 commit 4dc5e82

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

gc.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <setjmp.h>
2626
#include <sys/types.h>
2727

28+
#include <pthread.h>
29+
2830
#ifdef HAVE_SYS_TIME_H
2931
#include <sys/time.h>
3032
#endif
@@ -529,6 +531,56 @@ typedef struct mark_queue_struct {
529531

530532
mark_queue_t mark_queue = {NULL, NULL, 0};
531533

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+
532584
void gc_mark_defer(rb_objspace_t *objspace, VALUE ptr, int lev);
533585
int gc_mark_pop();
534586

0 commit comments

Comments
 (0)
0