thrtest.c
Go to the documentation of this file.
00001 #include <thread.h>
00002 #include <synch.h>
00003 #include <math.h>
00004 #include <sys/times.h>
00005 #include <limits.h>
00006 
00007 mutex_t test_lock;
00008 mutex_t count_lock;
00009 sema_t  gosem,retsem;
00010 
00011 int count=0;
00012 float sinx=0.0;
00013 
00014 dofunc(n)
00015 int n;
00016 { int i;
00017   thread_t self;
00018   self=thr_self();
00019   for (i=0; i<n; i++) {
00020     mutex_lock(&count_lock); 
00021     count++; sinx += sin(i % 6);
00022     mutex_unlock(&count_lock); 
00023 /*    thr_yield(); */
00024     }
00025   printf("id=%d count=%d sinx=%f\n", self, count, sinx);
00026   }
00027 
00028 
00029 semtest(n)
00030 int n;
00031 { register int i;
00032   for (i=0; i<n; i++) {
00033     sema_wait(&gosem);
00034     sema_post(&retsem);}
00035   printf("sematest exit\n");}
00036   
00037 
00038 float ustime(t1, t2)    /*returns time in milli second*/
00039 struct tms *t1, *t2;
00040 { return((1000.0/CLK_TCK) *
00041           ((t2->tms_utime-t1->tms_utime) + (t2->tms_stime-t1->tms_stime)));}
00042 
00043 main(argc,argv)
00044 int argc;
00045 char *argv[];
00046 {
00047   thread_t thrid[10001];
00048   int stat, *statp, stat2, s=1, exitstat, N;
00049   struct tms t1,t2,t3,t4,t5,t6;
00050   register int i;
00051   int concurrency;
00052 
00053   stat=thr_self();
00054   stat2=thr_min_stack();
00055   if (argc>=2) sscanf(argv[1],"%d", &concurrency);
00056   else concurrency=4;
00057   if (argc>=3) sscanf(argv[2], "%d", &N);
00058   else N=1000;
00059 
00060   thr_setconcurrency(concurrency);
00061   printf("self=%d  minstack=%x concurrency=%d CLK_TCK=%d\n\n",
00062         stat, stat2, thr_getconcurrency(), CLK_TCK);
00063 
00064   times(&t1);
00065   for (i=0; i<N; i++) {
00066     stat=thr_create(0,0,thr_exit,&exitstat,THR_BOUND,&thrid[i]);
00067     if (i % 100 ==0) {printf("%d stat=%d\n", i, stat);}}
00068   times(&t2);
00069   printf("creation time=%f max_thrid=%d\n", ustime(&t1, &t2)/(float)N,thrid[N-1]);
00070   times(&t2);
00071   for(i=0; i<N; i++) { thr_join(thrid[i], 0, 0);}
00072   times(&t3);
00073   printf("join time=%f\n", ustime(&t2, &t3)/(float)N);
00074   
00075   sema_init(&gosem, 0, USYNC_THREAD, 0);
00076   sema_init(&retsem, 0, USYNC_THREAD, 0);
00077   stat=thr_create(0,0,semtest,N,0,&thrid[0]);
00078   times(&t3);
00079   for (i=0; i<N; i++) {
00080     sema_post(&gosem);
00081     sema_wait(&retsem);}
00082   times(&t4);
00083   printf("sema switch time=%f\n", ustime(&t3, &t4)/(float)N);
00084 
00085   for (i=0; i<N; i++) {
00086     mutex_lock(&test_lock);
00087     mutex_unlock(&test_lock);
00088     mutex_lock(&test_lock);
00089     mutex_unlock(&test_lock);
00090     mutex_lock(&test_lock);
00091     mutex_unlock(&test_lock);
00092     mutex_lock(&test_lock);
00093     mutex_unlock(&test_lock);
00094     mutex_lock(&test_lock);
00095     mutex_unlock(&test_lock);}
00096   times(&t5);
00097   printf("mutex_lock & unlock time=%f\n", ustime(&t4, &t5)/((float)N*5.0));
00098 
00099 
00100   for (s=0; s<5; s++) {
00101     stat=thr_create(0,0, dofunc, 10000, 0, &thrid[s]);
00102 /*    printf("stat=%x thrid=%x\n", stat, thrid[s]); */
00103     if (stat!=0) exit(1);}
00104   statp=&stat;
00105   for (s=0; s<5; s++) 
00106     stat2=thr_join(thrid[s], 0, &statp);
00107   printf("stat2=%x stat=%x concurrency=%d\n", stat2, stat, thr_getconcurrency());}
00108 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 18:05:53