Res ThreadRegister(Thread *threadReturn, Arena arena) { Res res; Thread thread; Ring ring; void *p; AVER(threadReturn != NULL); res = ControlAlloc(&p, arena, sizeof(ThreadStruct)); if (res != ResOK) return res; thread = (Thread)p; thread->arena = arena; RingInit(&thread->arenaRing); thread->serial = arena->threadSerial; ++arena->threadSerial; thread->alive = TRUE; thread->forking = FALSE; thread->port = mach_thread_self(); AVER(MACH_PORT_VALID(thread->port)); thread->sig = ThreadSig; AVERT(Thread, thread); ProtThreadRegister(); ring = ArenaThreadRing(arena); RingAppend(ring, &thread->arenaRing); *threadReturn = thread; return ResOK; }
Res ThreadRegister(Thread *threadReturn, Arena arena) { Res res; Thread thread; Ring ring; void *p; AVER(threadReturn != NULL); res = ControlAlloc(&p, arena, sizeof(ThreadStruct), /* withReservoirPermit */ FALSE); if(res != ResOK) return res; thread = (Thread)p; thread->arena = arena; RingInit(&thread->arenaRing); thread->serial = arena->threadSerial; ++arena->threadSerial; thread->port = mach_thread_self(); thread->sig = ThreadSig; AVERT(Thread, thread); ProtThreadRegister(FALSE); ring = ArenaThreadRing(arena); RingAppend(ring, &thread->arenaRing); *threadReturn = thread; return ResOK; }
static void protSetupInner(void) { kern_return_t kr; int pr; pthread_t excThread; mach_port_t self; /* Create a port to send and receive exceptions. */ self = mach_task_self(); AVER(MACH_PORT_VALID(self)); kr = mach_port_allocate(self, MACH_PORT_RIGHT_RECEIVE, &protExcPort); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS mach_port_allocate", kr); /* .trans.must */ AVER(MACH_PORT_VALID(protExcPort)); /* Allow me to send exceptions on this port. */ /* TODO: Find out why this is necessary. */ self = mach_task_self(); AVER(MACH_PORT_VALID(self)); kr = mach_port_insert_right(self, protExcPort, protExcPort, MACH_MSG_TYPE_MAKE_SEND); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS mach_port_insert_right", kr); /* .trans.must */ ProtThreadRegister(TRUE); /* Launch the exception handling thread. We use pthread_create because it's much simpler than setting up a thread from scratch using Mach, and that's basically what it does. See [Libc] <http://www.opensource.apple.com/source/Libc/Libc-825.26/pthreads/pthread.c> */ pr = pthread_create(&excThread, NULL, protCatchThread, NULL); AVER(pr == 0); if (pr != 0) fprintf(stderr, "ERROR: MPS pthread_create: %d\n", pr); /* .trans.must */ }