History

20110104-1043

forsyth committed 15 years ago

Commit ce0a4511 parent 5d565f21 file · +45 −112Browse files

Unified · Split

emu/MacOSX/os.c+45−112
@@ -47,6 +47,13 @@ enum · 46 unmodified lines
4747 char *hosttype = "MacOSX";
4848 char *cputype = OBJTYPE;
4949
50+typedef struct Sem Sem;
51+struct Sem {
52+ pthread_cond_t c;
53+ pthread_mutex_t m;
54+ int v;
55+};
56+
5057 static pthread_key_t prdakey;
5158
5259 extern int dflag;
@@ -60,8 +67,9 @@ getup(void) · 7 unmodified lines
6067 void
6168 pexit(char *msg, int t)
6269 {
63− Osenv * e;
64− Proc * p;
70+ Osenv *e;
71+ Proc *p;
72+ Sem *sem;
6573
6674 USED(t);
6775 USED(msg);
@@ -91,6 +99,12 @@ pexit(char *msg, int t) · 23 unmodified lines
9199 }
92100 free(e->user);
93101 free(p->prog);
102+ sem = p->os;
103+ if(sem != nil){
104+ pthread_cond_destroy(&sem->c);
105+ pthread_mutex_destroy(&sem->m);
106+ }
107+ free(p->os);
94108 free(p);
95109 pthread_exit(0);
96110 }
@@ -223,16 +237,23 @@ int · 126 unmodified lines
223237 kproc(char *name, void (*func)(void*), void *arg, int flags)
224238 {
225239 pthread_t thread;
226− Proc * p;
227− Pgrp * pg;
228− Fgrp * fg;
229− Egrp * eg;
230−
240+ Proc *p;
241+ Pgrp *pg;
242+ Fgrp *fg;
243+ Egrp *eg;
231244 pthread_attr_t attr;
245+ Sem *sem;
232246
233247 p = newproc();
234248 if(p == nil)
235249 panic("kproc: no memory");
250+ sem = malloc(sizeof(*sem));
251+ if(sem == nil)
252+ panic("can't allocate semaphore");
253+ pthread_cond_init(&sem->c, NULL);
254+ pthread_mutex_init(&sem->m, NULL);
255+ sem->v = 0;
256+ p->os = sem;
236257
237258 if(flags & KPDUPPG) {
238259 pg = up->env->pgrp;
@@ -309,21 +330,33 @@ segflush(void *va, ulong len) · 70 unmodified lines
309330 void
310331 oshostintr(Proc *p)
311332 {
333+fprint(2, "oshostintr %p\n", p);
312334 pthread_kill((pthread_t)p->sigid, SIGUSR1);
313335 }
314336
315−static ulong erendezvous(void*, ulong);
316−
317337 void
318338 osblock(void)
319339 {
320− erendezvous(up, 0);
340+ Sem *sem;
341+
342+ sem = up->os;
343+ pthread_mutex_lock(&sem->m);
344+ while(sem->v == 0)
345+ pthread_cond_wait(&sem->c, &sem->m);
346+ sem->v--;
347+ pthread_mutex_unlock(&sem->m);
321348 }
322349
323350 void
324351 osready(Proc *p)
325352 {
326− erendezvous(p, 0);
353+ Sem *sem;
354+
355+ sem = p->os;
356+ pthread_mutex_lock(&sem->m);
357+ sem->v++;
358+ pthread_cond_signal(&sem->c);
359+ pthread_mutex_unlock(&sem->m);
327360 }
328361
329362 void
@@ -397,14 +430,11 @@ getnobody() · 67 unmodified lines
397430 }
398431 }
399432
400−static pthread_mutex_t rendezvouslock;
401−static pthread_mutexattr_t *pthread_mutexattr_default = NULL;
402−
403433 void
404434 libinit(char *imod)
405435 {
406436 struct passwd *pw;
407− Proc * p;
437+ Proc *p;
408438 char sys[64];
409439
410440 setsid();
@@ -419,9 +449,6 @@ libinit(char *imod) · 8 unmodified lines
419449
420450 setsigs();
421451
422− if(pthread_mutex_init(&rendezvouslock, pthread_mutexattr_default))
423− panic("pthread_mutex_init");
424−
425452 if(pthread_key_create(&prdakey, NULL))
426453 print("key_create failed\n");
427454
@@ -466,92 +493,6 @@ readkbd(void) · 38 unmodified lines
466493 return buf[0];
467494 }
468495
469−
470−
471−enum
472−{
473− NHLOG = 7,
474− NHASH = (1<<NHLOG)
475−};
476−
477−typedef struct Tag Tag;
478−struct Tag
479−{
480− void* tag;
481− ulong val;
482− pthread_cond_t cv;
483− Tag* next;
484−};
485−
486−static Tag* ht[NHASH];
487−static Tag* ft;
488−//static Lock hlock;
489−
490−static ulong
491−erendezvous(void *tag, ulong value)
492−{
493− int h;
494− ulong rval;
495− Tag * t, **l, *f;
496−
497− h = (ulong)tag & (NHASH - 1);
498−
499− // lock(&hlock);
500− pthread_mutex_lock(&rendezvouslock);
501− l = &ht[h];
502− for(t = ht[h]; t; t = t->next) {
503− if(t->tag == tag) {
504− rval = t->val;
505− t->val = value;
506− t->tag = 0;
507− pthread_mutex_unlock(&rendezvouslock);
508− // unlock(&hlock);
509− if(pthread_cond_signal(&(t->cv)))
510− panic("pthread_cond_signal");
511− return rval;
512− }
513− }
514−
515− t = ft;
516− if(t == 0) {
517− t = malloc(sizeof(Tag));
518− if(t == nil)
519− panic("rendezvous: no memory");
520− if(pthread_cond_init(&(t->cv), NULL)) {
521− print("pthread_cond_init (errno: %s) \n", strerror(errno));
522− panic("pthread_cond_init");
523− }
524− } else
525− ft = t->next;
526−
527− t->tag = tag;
528− t->val = value;
529− t->next = *l;
530− *l = t;
531− // pthread_mutex_unlock(&rendezvouslock);
532− // unlock(&hlock);
533−
534− while(t->tag != nil)
535− pthread_cond_wait(&(t->cv), &rendezvouslock);
536−
537− // pthread_mutex_lock(&rendezvouslock);
538− // lock(&hlock);
539− rval = t->val;
540− for(f = *l; f; f = f->next) {
541− if(f == t) {
542− *l = f->next;
543− break;
544− }
545− l = &f->next;
546− }
547− t->next = ft;
548− ft = t;
549− pthread_mutex_unlock(&rendezvouslock);
550− // unlock(&hlock);
551−
552− return rval;
553−}
554−
555496 /*
556497 * Return an abitrary millisecond clock time
557498 */
@@ -570,8 +511,6 @@ osmillisec(void) · 12 unmodified lines
570511 return((t.tv_sec - sec0) * 1000 + (t.tv_usec - usec0 + 500) / 1000);
571512 }
572513
573−
574−
575514 /*
576515 * Return the time since the epoch in nanoseconds and microseconds
577516 * The epoch is defined at 1 Jan 1970
@@ -593,10 +532,6 @@ osusectime(void) · 15 unmodified lines
593532 gettimeofday(&t, nil);
594533 return (vlong)t.tv_sec * 1000000 + t.tv_usec;
595534 }
596−
597−
598−
599−
600535 int
601536 osmillisleep(ulong milsec)
602537 {
@@ -607,8 +542,6 @@ osmillisleep(ulong milsec) · 4 unmodified lines
607542 return 0;
608543 }
609544
610−
611−
612545 int
613546 limbosleep(ulong milsec)
614547 {