Fixed up parlib pthreads/harts
authorAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 05:43:47 +0000 (21:43 -0800)
committerAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 05:43:47 +0000 (21:43 -0800)
Added a hart_current_harts() routine, fixed hart_yield()
to decrement that value; I leverage this in pthreads.

user/parlib/inc/hart.h
user/parlib/src/hart.c
user/parlib/src/pthread.c

index a80c089..7bd751a 100644 (file)
@@ -57,6 +57,7 @@ size_t hart_self();
 error_t hart_request(size_t k);
 void hart_yield();
 size_t hart_max_harts();
+size_t hart_current_harts();
 
 #ifdef __cplusplus
 }
index c6e6948..881dbc3 100644 (file)
@@ -94,6 +94,9 @@ error_t hart_request(size_t k)
 
 void hart_yield()
 {
+       hart_lock_lock(&_hart_lock);
+       _hart_current_harts--;
+       hart_lock_unlock(&_hart_lock);
        syscall(SYS_yield,0,0,0,0,0);
 }
 
@@ -102,6 +105,11 @@ size_t hart_max_harts()
        return procinfo.max_harts < HART_MAX_MAX_HARTS ? procinfo.max_harts : HART_MAX_MAX_HARTS;
 }
 
+size_t hart_current_harts()
+{
+       return _hart_current_harts;
+}
+
 // MCS locks!!
 void hart_lock_init(hart_lock_t* lock)
 {
index 46e67cb..75c9db0 100644 (file)
@@ -5,7 +5,6 @@
 #include <assert.h>
 
 int threads_active = 1;
-int current_harts = 1;
 hart_lock_t work_queue_lock = HART_LOCK_INIT;
 pthread_t work_queue_head = 0;
 pthread_t work_queue_tail = 0;
@@ -79,20 +78,15 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr,
   (*thread)->finished = 0;
   (*thread)->detached = 0;
 
-  int request_hart = 0;
-
   hart_lock_lock(&work_queue_lock);
   {
     threads_active++;
-    if(threads_active > current_harts)
-      request_hart = 1;
-
     queue_insert(&work_queue_head,&work_queue_tail,*thread);
+    // don't return until we get a hart
+    while(threads_active > hart_current_harts() && hart_request(1));
   }
   hart_lock_unlock(&work_queue_lock);
 
-  // don't return until we get a hart
-  while(request_hart && hart_request(1));
   return 0;
 }