🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #1760

another KERNEL BUG : SLOW THREADS

#1760
From: Patrick Stein
Date: Tue, 06 Oct 1992 18:36
113 lines
3627 bytes
/*

Hiho again from munich B)


I posted the mach kernel bug , but nobody answered B(

So if you don't believe me ... test this program under NS2.1 and NS3.0
It's unbelievable slow under 3.O. WHY ???
Didn't NeXT remove the DEBUG Messages ?????
My first thought was, that the mutex_unlock,mutex_lock and the conditon_*
functions are so slow : There are not slow, the cthread_detach(cthread_fork...
is the major speed problem. 
I need a fast thread execution time, so i will be very very very inter-
ested in hints or improvements to solve that situation.

					Jolly




post scriptum : the program can be found in the DL too ( only the number of
		threads changed )


*/

#include <stdio.h> 
#include <cthreads.h>

int         count;    /* number of slave threads active */ 
mutex_t     lock;     /* mutual exclusion for count */ 
mutex_t     print;    /* mutual exclusion for printfs */ 
condition_t done;     /* signaled each time a slave thread finishes */

void init() 
{ 
    /* Allocate mutex variables "lock" and "print". */     
    lock = mutex_alloc();    
    print = mutex_alloc(); 

    /* Allocate condition variable "done". */     
    done = condition_alloc(); 

    count = 0; 
}

/*
 * Each slave just loops, yielding the processor on each 
 * iteration.  When it's finished, it decrements the global 
 * count and signals that it's done.  
 */ 
void slave(int n) 
{ 
    int i;

    for (i = 0; i < 100; i += 1) 
        cthread_yield(); 

    /*
     * If any thread wants to access the count variable, it      
     * first locks the mutex.  When the mutex is locked, any      
     * other thread wanting the count variable must wait until      
     * the mutex is unlocked.  
     */ 
    mutex_lock(lock); 
    count -= 1; 
    mutex_lock(print);
    printf("Slave %d finished.\n", n); 
    mutex_unlock(print);
    /* Signal that this slave has finished. */ 
    condition_signal(done);    
    mutex_unlock(lock); 
}

/*
 * The master spawns a given number of slaves and then waits 
 * for them all to finish.  
 */ 
void master(int nslaves) 
{ 
    int i;

    for (i = 1; i <= nslaves; i++) { 
        mutex_lock(lock); 
        /* Increment count with the creation of each slave thread.  */ 
        count += 1; 
        /* Fork a slave and detach it.  */ 
        cthread_detach(cthread_fork((cthread_fn_t)slave, (any_t)i)); 
        mutex_unlock(lock); 
    } 

    mutex_lock(lock); 
    /*
     * Master thread loops waiting on the condition done.  Each      
     * time the master thread is signaled by a condition_signal      
     * call, it tests the count for a value of zero.  
     */ 
    while (count != 0) 
        condition_wait(done, lock); 
    mutex_unlock(lock); 

    mutex_lock(print);
    printf("All %d slaves have finished.\n", nslaves); 
    mutex_unlock(print);
    cthread_exit(0); 
}

main() 
{ 
    init(); 
    master(150);  /* Create master thread and 15 slaves. */ 
}
Technical Details
Message-ID: <1992Oct6.183610.17142@news.lrz-muenchen.de>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!darwin.sura.net!jvnc.net!gmd.de!Germany.EU.net!rrz.uni-koeln.de!unidui!math.fu-berlin.de!fauern!LRZnews!news

Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.next.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!darwin.sura.net!jvnc.net!gmd.de!Germany.EU.net!rrz.uni-koeln.de!unidui!math.fu-berlin.de!fauern!LRZnews!news
From: jolly@next3.cis.uni-muenchen.de (Patrick Stein)
Subject: another KERNEL BUG : SLOW THREADS
Message-ID: <1992Oct6.183610.17142@news.lrz-muenchen.de>
Keywords: kernel, threads, mach, thread, bug, operating system, os
Sender: news@news.lrz-muenchen.de (Mr. News)
Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
Date: Tue, 6 Oct 1992 18:36:10 GMT
Lines: 115