int main(void) { printf(1,"testing mutex yield\n"); mutexA = kthread_mutex_alloc(); mutexB = kthread_mutex_alloc(); kthread_mutex_lock(mutexA); void* stk1 = malloc(MAX_STACK_SIZE); void* stk2 = malloc(MAX_STACK_SIZE); int id1 = kthread_create(waitingThread, stk1, MAX_STACK_SIZE); int id2 = kthread_create(signalingThread, stk2, MAX_STACK_SIZE); kthread_join(id2); kthread_mutex_unlock(mutexA); kthread_join(id1); printf(1,"done\n"); kthread_exit(); return 0; }
static int shmif_unclone(struct ifnet *ifp) { struct shmif_sc *sc = ifp->if_softc; shmif_stop(ifp, 1); if_down(ifp); finibackend(sc); mutex_enter(&sc->sc_mtx); sc->sc_dying = true; cv_broadcast(&sc->sc_cv); mutex_exit(&sc->sc_mtx); if (sc->sc_rcvl) kthread_join(sc->sc_rcvl); sc->sc_rcvl = NULL; vmem_xfree(shmif_units, sc->sc_unit+1, 1); ether_ifdetach(ifp); if_detach(ifp); cv_destroy(&sc->sc_cv); mutex_destroy(&sc->sc_mtx); kmem_free(sc, sizeof(*sc)); return 0; }
void selftest_mobj_waiter_short() { int tids[ARRAY_SIZE(conds)]; int w_tid; retry: mobj_se_test_signal_counter = 0; mobj_se_test_assumption_failed = false; for (size_t i = 0; i < ARRAY_SIZE(conds); i++) { kcond_init(&conds[i]); tids[i] = kthread_create(&mobj_waiter_sig_thread, (void*) i); VERIFY(tids[i] > 0); } w_tid = kthread_create(&mobj_waiter_wait_thread, NULL); VERIFY(w_tid > 0); kthread_join_all(tids, ARRAY_SIZE(tids)); kthread_join(w_tid); for (size_t i = 0; i < ARRAY_SIZE(conds); i++) kcond_destory(&conds[i]); if (mobj_se_test_assumption_failed) goto retry; regular_self_test_end(); }
void rumptest_busypage() { struct lwp *newl; int rv; cv_init(&tcv, "napina"); uobj = uao_create(1, 0); mutex_enter(uobj->vmobjlock); testpg = uvm_pagealloc(uobj, 0, NULL, 0); mutex_exit(uobj->vmobjlock); if (testpg == NULL) panic("couldn't create vm page"); rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL, thread, NULL, &newl, "jointest"); if (rv) panic("thread creation failed: %d", rv); mutex_enter(uobj->vmobjlock); while (!threadrun) cv_wait(&tcv, uobj->vmobjlock); uvm_page_unbusy(&testpg, 1); mutex_exit(uobj->vmobjlock); rv = kthread_join(newl); if (rv) panic("thread join failed: %d", rv); }
void selftest_kthread_med(void) { int tid = kthread_create(simple_test_kthread, (void *)1); if (tid < 0) panic("Unable to create the simple test kthread"); kthread_join(tid); regular_self_test_end(); }
void selftest_join_med() { int tid; printk("[selftest join] create the simple thread\n"); if ((tid = kthread_create(simple_test_kthread, (void *)0xAA0011FF)) < 0) panic("Unable to create simple_test_kthread"); printk("[selftest join] join()\n"); kthread_join(tid); printk("[selftest join] kernel thread exited\n"); regular_self_test_end(); }
void npf_worker_sysfini(void) { lwp_t *l = worker_lwp; /* Notify the worker and wait for the exit. */ mutex_enter(&worker_lock); worker_lwp = NULL; cv_broadcast(&worker_cv); mutex_exit(&worker_lock); kthread_join(l); /* LWP has exited, destroy the structures. */ cv_destroy(&worker_cv); cv_destroy(&worker_event_cv); mutex_destroy(&worker_lock); }
void rumptest_tsleep() { struct lwp *notbigl[NTHREADS]; int rv, i; mutex_init(&mymtx, MUTEX_DEFAULT, IPL_NONE); for (i = 0; i < NTHREADS; i++) { rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN| KTHREAD_MPSAFE, NULL, tinythread, (void *)(uintptr_t)i, ¬bigl[i], "nb"); if (rv) panic("thread create failed: %d", rv); } for (i = 0; i < NTHREADS; i++) { kthread_join(notbigl[i]); } }
/* * Free the variable sized parts of the softc. */ static void fss_softc_free(struct fss_softc *sc) { int i; if ((sc->sc_flags & FSS_BS_THREAD) != 0) { mutex_enter(&sc->sc_slock); sc->sc_flags &= ~FSS_BS_THREAD; cv_signal(&sc->sc_work_cv); mutex_exit(&sc->sc_slock); kthread_join(sc->sc_bs_lwp); disk_detach(sc->sc_dkdev); } if (sc->sc_copied != NULL) kmem_free(sc->sc_copied, howmany(sc->sc_clcount, NBBY)); sc->sc_copied = NULL; if (sc->sc_cache != NULL) { for (i = 0; i < sc->sc_cache_size; i++) if (sc->sc_cache[i].fc_data != NULL) { cv_destroy(&sc->sc_cache[i].fc_state_cv); kmem_free(sc->sc_cache[i].fc_data, FSS_CLSIZE(sc)); } kmem_free(sc->sc_cache, sc->sc_cache_size*sizeof(struct fss_cache)); } sc->sc_cache = NULL; if (sc->sc_indir_valid != NULL) kmem_free(sc->sc_indir_valid, howmany(sc->sc_indir_size, NBBY)); sc->sc_indir_valid = NULL; if (sc->sc_indir_data != NULL) kmem_free(sc->sc_indir_data, FSS_CLSIZE(sc)); sc->sc_indir_data = NULL; }
void npf_test_conc(bool st, unsigned nthreads) { uint64_t total = 0; int error; lwp_t **l; printf("THREADS\tPKTS\n"); stateful = st; done = false; run = false; npackets = kmem_zalloc(sizeof(uint64_t) * nthreads, KM_SLEEP); l = kmem_zalloc(sizeof(lwp_t *) * nthreads, KM_SLEEP); for (unsigned i = 0; i < nthreads; i++) { const int flags = KTHREAD_MUSTJOIN | KTHREAD_MPSAFE; error = kthread_create(PRI_NONE, flags, NULL, worker, (void *)(uintptr_t)i, &l[i], "npfperf"); KASSERT(error == 0); } /* Let them spin! */ run = true; kpause("perf", false, NSECS * hz, NULL); done = true; /* Wait until all threads exit and sum the counts. */ for (unsigned i = 0; i < nthreads; i++) { kthread_join(l[i]); total += npackets[i]; } kmem_free(npackets, sizeof(uint64_t) * nthreads); kmem_free(l, sizeof(lwp_t *) * nthreads); printf("%u\t%" PRIu64 "\n", nthreads, total / NSECS); }
static int bthidev_detach(device_t self, int flags) { struct bthidev_softc *sc = device_private(self); struct bthidev *hidev; mutex_enter(bt_lock); sc->sc_flags = 0; /* disable reconnecting */ /* release interrupt listen */ if (sc->sc_int_l != NULL) { l2cap_detach_pcb(&sc->sc_int_l); sc->sc_int_l = NULL; } /* release control listen */ if (sc->sc_ctl_l != NULL) { l2cap_detach_pcb(&sc->sc_ctl_l); sc->sc_ctl_l = NULL; } /* close interrupt channel */ if (sc->sc_int != NULL) { l2cap_disconnect_pcb(sc->sc_int, 0); l2cap_detach_pcb(&sc->sc_int); sc->sc_int = NULL; } /* close control channel */ if (sc->sc_ctl != NULL) { l2cap_disconnect_pcb(sc->sc_ctl, 0); l2cap_detach_pcb(&sc->sc_ctl); sc->sc_ctl = NULL; } callout_halt(&sc->sc_reconnect, bt_lock); callout_destroy(&sc->sc_reconnect); mutex_exit(bt_lock); pmf_device_deregister(self); /* kill off the input processor */ if (sc->sc_lwp != NULL) { mutex_enter(&sc->sc_lock); sc->sc_detach = 1; cv_signal(&sc->sc_cv); mutex_exit(&sc->sc_lock); kthread_join(sc->sc_lwp); sc->sc_lwp = NULL; } /* detach children */ while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) { LIST_REMOVE(hidev, sc_next); config_detach(hidev->sc_dev, flags); } MBUFQ_DRAIN(&sc->sc_inq); cv_destroy(&sc->sc_cv); mutex_destroy(&sc->sc_lock); sockopt_destroy(&sc->sc_mode); return 0; }
int main(int argc, char *argv[]) { int studentNum, graderId, res, i; int studentId[100]; void *stack; void*(*start_func)(); if (argc != 3){ printf(1, "please enter 2 arguments- number of students, and number of slots the grader adds each time\n"); exit(); } printf(1, "main- parsing args\n"); studentNum = atoi(argv[1]); slotsPerRound = atoi(argv[2]); printf(1, "main- got n-%d ; m-%d\n", studentNum, slotsPerRound); monitor = mesa_slots_monitor_alloc(); printf(1, "main- monitor allocated- address %p\n", monitor); stack = (void*)malloc(STACK_SIZE); if (stack <= 0){ goto bad_alloc; } start_func = &grader; graderId = kthread_create(start_func, stack, STACK_SIZE); if (graderId <= 0){ goto bad_grader_create; } printf(1, "main- grader allocated\n"); start_func = &student; for (i = 0; i < studentNum; i++){ stack = (void*)malloc(STACK_SIZE); if (stack <= 0){ goto bad_alloc; } studentId[i] = kthread_create(start_func, stack, STACK_SIZE); if (studentId[i] <= 0){ goto bad_student_create; } printf(1, "main- student %d created\n", studentId[i]); } printf(1, "main- waiting for students\n"); i = 0; while (i < studentNum){ res = kthread_join(studentId[i]); if (res > -1){ i++; } } printf(1, "main- killing grader\n"); while ((res = mesa_slots_monitor_stopadding(monitor) < 0)); printf(1, "main- waiting for grader\n"); while ((res = kthread_join(graderId) < 0)); printf(1, "main- done!!\n"); if (mesa_slots_monitor_dealloc(monitor) < 0){ printf(1, "main- couldn't dealloc monitor! exiting\n"); } else { printf(1, "main- exiting\n"); } exit(); bad_grader_create: printf(1, "main- failed to create grader thread, exiting\n"); exit(); bad_student_create: printf(1, "main- failed to create students threads, exiting\n"); exit(); bad_alloc: printf(1, "main- failed to allocate enough memory, exiting\n"); exit(); }