From owner-freebsd-arch Fri Sep 27 8:35:53 2002 Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A1E3637B404 for ; Fri, 27 Sep 2002 08:35:49 -0700 (PDT) Received: from mail.speakeasy.net (mail17.speakeasy.net [216.254.0.217]) by mx1.FreeBSD.org (Postfix) with ESMTP id 36DBF43E65 for ; Fri, 27 Sep 2002 08:35:49 -0700 (PDT) (envelope-from jhb@FreeBSD.org) Received: (qmail 2446 invoked from network); 27 Sep 2002 15:35:48 -0000 Received: from unknown (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender ) by mail17.speakeasy.net (qmail-ldap-1.03) with DES-CBC3-SHA encrypted SMTP for ; 27 Sep 2002 15:35:48 -0000 Received: from laptop.baldwin.cx (gw1.twc.weather.com [216.133.140.1]) by server.baldwin.cx (8.12.5/8.12.5) with ESMTP id g8RFZkBv095315; Fri, 27 Sep 2002 11:35:47 -0400 (EDT) (envelope-from jhb@FreeBSD.org) Message-ID: X-Mailer: XFMail 1.5.2 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <200209271257.g8RCvmwf032794@grimreaper.grondar.org> Date: Fri, 27 Sep 2002 11:35:49 -0400 (EDT) From: John Baldwin To: Mark Murray Subject: Re: [patch] module-with-thread exit routine. Cc: arch@FreeBSD.org Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On 27-Sep-2002 Mark Murray wrote: >> > Attached is a patch to the kthread code. I've been running this >> > for at least three months. >> > >> > Comments? >> >> You don't need it. Look at the crash module I have. It has a thread >> and I committed an extra wakeup() inside of exit1() for kthread's a >> while ago to handle just this case. Here's an excerpt from the crash >> module code: > > This looks good. But I have some questions: > >> In the kthread's main loop (it usually gets events via a sysctl from >> userland): >> >> static void >> crash_thread(void *arg) >> { >> int ev; >> >> while (1) { >> mtx_lock(&event_mtx); >> while ((ev = event) == 0) >> cv_wait(&event_cv, &event_mtx); >> event = 0; >> mtx_unlock(&event_mtx); > > What is the cv_wait for? For the random_kthread, can I just ignore it? Well, the way this module works is it has a kthread that executes events that are signaled to them via the 'event' variable. The user can use a sysctl to write a value that gets put in 'event' and then the sysctl handler does a 'cv_signal'. The unload() function is similar. Basically, MOD_UNLOAD needs to poke the thread somehow to tell it to commit suicide. For the crash module, I use a special value of the 'event' variable. >> ... >> switch (ev) { >> case -1: >> mtx_lock(&Giant); >> kthread_exit(0); >> break; >> ... >> } > > This looks like I can just call kthread_exit() directly with no shenanigans. > Am I on the right track? > >> Here's the unload() function that the module calls for MOD_UNLOAD: >> >> static int >> unload(void *arg) >> { >> >> mtx_lock(&event_mtx); >> event = -1; >> cv_signal(&event_cv); >> msleep(kthread, &event_mtx, PWAIT, "crshun", 0); >> mtx_unlock(&event_mtx); >> mtx_destroy(&event_mtx); >> cv_destroy(&event_cv); > > I'm not sure of the event_cv relevance. Can I not just set the event (or > equivalent) variable and wait for event_mtx to become available? Think of the cv as a wakeup to the kthread to tell it to go examine 'event' and see that it needs to die. The trick here is you need to make sure you don't miss the wakeup. Thus, you need to atomically signal the thread to die and then block waiting for it to die. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message