thrtest.c
Go to the documentation of this file.
1 #include <thread.h>
2 #include <synch.h>
3 #include <math.h>
4 #include <sys/times.h>
5 #include <limits.h>
6 
7 mutex_t test_lock;
8 mutex_t count_lock;
10 
11 int count=0;
12 float sinx=0.0;
13 
15 int n;
16 { int i;
17  thread_t self;
18  self=thr_self();
19  for (i=0; i<n; i++) {
20  mutex_lock(&count_lock);
21  count++; sinx += sin(i % 6);
22  mutex_unlock(&count_lock);
23 /* thr_yield(); */
24  }
25  printf("id=%d count=%d sinx=%f\n", self, count, sinx);
26  }
27 
28 
30 int n;
31 { register int i;
32  for (i=0; i<n; i++) {
33  sema_wait(&gosem);
34  sema_post(&retsem);}
35  printf("sematest exit\n");}
36 
37 
38 float ustime(t1, t2) /*returns time in milli second*/
39 struct tms *t1, *t2;
40 { return((1000.0/CLK_TCK) *
41  ((t2->tms_utime-t1->tms_utime) + (t2->tms_stime-t1->tms_stime)));}
42 
43 main(argc,argv)
44 int argc;
45 char *argv[];
46 {
47  thread_t thrid[10001];
48  int stat, *statp, stat2, s=1, exitstat, N;
49  struct tms t1,t2,t3,t4,t5,t6;
50  register int i;
51  int concurrency;
52 
53  stat=thr_self();
54  stat2=thr_min_stack();
55  if (argc>=2) sscanf(argv[1],"%d", &concurrency);
56  else concurrency=4;
57  if (argc>=3) sscanf(argv[2], "%d", &N);
58  else N=1000;
59 
60  thr_setconcurrency(concurrency);
61  printf("self=%d minstack=%x concurrency=%d CLK_TCK=%d\n\n",
62  stat, stat2, thr_getconcurrency(), CLK_TCK);
63 
64  times(&t1);
65  for (i=0; i<N; i++) {
66  stat=thr_create(0,0,thr_exit,&exitstat,THR_BOUND,&thrid[i]);
67  if (i % 100 ==0) {printf("%d stat=%d\n", i, stat);}}
68  times(&t2);
69  printf("creation time=%f max_thrid=%d\n", ustime(&t1, &t2)/(float)N,thrid[N-1]);
70  times(&t2);
71  for(i=0; i<N; i++) { thr_join(thrid[i], 0, 0);}
72  times(&t3);
73  printf("join time=%f\n", ustime(&t2, &t3)/(float)N);
74 
75  sema_init(&gosem, 0, USYNC_THREAD, 0);
76  sema_init(&retsem, 0, USYNC_THREAD, 0);
77  stat=thr_create(0,0,semtest,N,0,&thrid[0]);
78  times(&t3);
79  for (i=0; i<N; i++) {
80  sema_post(&gosem);
81  sema_wait(&retsem);}
82  times(&t4);
83  printf("sema switch time=%f\n", ustime(&t3, &t4)/(float)N);
84 
85  for (i=0; i<N; i++) {
86  mutex_lock(&test_lock);
87  mutex_unlock(&test_lock);
88  mutex_lock(&test_lock);
89  mutex_unlock(&test_lock);
90  mutex_lock(&test_lock);
91  mutex_unlock(&test_lock);
92  mutex_lock(&test_lock);
93  mutex_unlock(&test_lock);
94  mutex_lock(&test_lock);
95  mutex_unlock(&test_lock);}
96  times(&t5);
97  printf("mutex_lock & unlock time=%f\n", ustime(&t4, &t5)/((float)N*5.0));
98 
99 
100  for (s=0; s<5; s++) {
101  stat=thr_create(0,0, dofunc, 10000, 0, &thrid[s]);
102 /* printf("stat=%x thrid=%x\n", stat, thrid[s]); */
103  if (stat!=0) exit(1);}
104  statp=&stat;
105  for (s=0; s<5; s++)
106  stat2=thr_join(thrid[s], 0, &statp);
107  printf("stat2=%x stat=%x concurrency=%d\n", stat2, stat, thr_getconcurrency());}
108 
sema_t retsem
Definition: thrtest.c:9
int sema_init(sema_t *, unsigned int, int, void *)
Definition: pthreads.c:111
double sin()
int thr_join(int tid, int *depature, void **status)
Definition: pthreads.c:75
GLfloat n[6][3]
Definition: cube.c:15
main(int argc, argv)
Definition: thrtest.c:43
static int argc
Definition: transargv.c:56
mutex_t count_lock
Definition: thrtest.c:8
semtest(int n)
Definition: thrtest.c:29
float ustime(struct tms *t1, struct tms *t2)
Definition: thrtest.c:38
Definition: eus_thr.h:96
mutex_t test_lock
Definition: thrtest.c:7
int sema_wait(sema_t *)
Definition: pthreads.c:124
short s
Definition: structsize.c:2
dofunc(int n)
Definition: thrtest.c:14
sema_t gosem
Definition: thrtest.c:9
int thr_create(void *, size_t, void(*)(), void *, long, int *)
Definition: pthreads.c:43
int count
Definition: thrtest.c:11
float sinx
Definition: thrtest.c:12
int sema_post(sema_t *sem)
Definition: pthreads.c:148
unsigned int thr_self()
Definition: eus.c:25


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:28