kern_kthread.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* $OpenBSD: kern_kthread.c,v 1.38 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: kern_kthread.c,v 1.3 1998/12/22 21:21:36 kleink Exp $ */
  3. /*-
  4. * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/kthread.h>
  35. #include <sys/proc.h>
  36. #include <sys/wait.h>
  37. #include <sys/malloc.h>
  38. #include <sys/queue.h>
  39. /*
  40. * note that stdarg.h and the ansi style va_start macro is used for both
  41. * ansi and traditional c compilers.
  42. * XXX: this requires that stdarg.h define: va_alist and va_dcl
  43. */
  44. #include <sys/stdarg.h>
  45. int kthread_create_now;
  46. /*
  47. * Fork a kernel thread. Any process can request this to be done.
  48. * The VM space and limits, etc. will be shared with proc0.
  49. */
  50. int
  51. kthread_create(void (*func)(void *), void *arg,
  52. struct proc **newpp, const char *name)
  53. {
  54. struct proc *p;
  55. int error;
  56. /*
  57. * First, create the new process. Share the memory, file
  58. * descriptors and don't leave the exit status around for the
  59. * parent to wait for.
  60. */
  61. error = fork1(&proc0, FORK_SHAREVM|FORK_SHAREFILES|FORK_NOZOMBIE|
  62. FORK_SYSTEM|FORK_SIGHAND, NULL, 0, func, arg, NULL, &p);
  63. if (error)
  64. return (error);
  65. /* Name it as specified. */
  66. strlcpy(p->p_comm, name, sizeof p->p_comm);
  67. /* All done! */
  68. if (newpp != NULL)
  69. *newpp = p;
  70. return (0);
  71. }
  72. /*
  73. * Cause a kernel thread to exit. Assumes the exiting thread is the
  74. * current context.
  75. */
  76. void
  77. kthread_exit(int ecode)
  78. {
  79. /*
  80. * XXX What do we do with the exit code? Should we even bother
  81. * XXX with it? The parent (proc0) isn't going to do much with
  82. * XXX it.
  83. */
  84. if (ecode != 0)
  85. printf("WARNING: thread `%s' (%d) exits with status %d\n",
  86. curproc->p_comm, curproc->p_pid, ecode);
  87. exit1(curproc, W_EXITCODE(ecode, 0), EXIT_NORMAL);
  88. /*
  89. * XXX Fool the compiler. Making exit1() __dead is a can
  90. * XXX of worms right now.
  91. */
  92. for (;;);
  93. }
  94. struct kthread_q {
  95. SIMPLEQ_ENTRY(kthread_q) kq_q;
  96. void (*kq_func)(void *);
  97. void *kq_arg;
  98. };
  99. SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
  100. /*
  101. * Defer the creation of a kernel thread. Once the standard kernel threads
  102. * and processes have been created, this queue will be run to callback to
  103. * the caller to create threads for e.g. file systems and device drivers.
  104. */
  105. void
  106. kthread_create_deferred(void (*func)(void *), void *arg)
  107. {
  108. struct kthread_q *kq;
  109. if (kthread_create_now) {
  110. (*func)(arg);
  111. return;
  112. }
  113. kq = malloc(sizeof *kq, M_TEMP, M_NOWAIT|M_ZERO);
  114. if (kq == NULL)
  115. panic("unable to allocate kthread_q");
  116. kq->kq_func = func;
  117. kq->kq_arg = arg;
  118. SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
  119. }
  120. void
  121. kthread_run_deferred_queue(void)
  122. {
  123. struct kthread_q *kq;
  124. /* No longer need to defer kthread creation. */
  125. kthread_create_now = 1;
  126. while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
  127. SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q);
  128. (*kq->kq_func)(kq->kq_arg);
  129. free(kq, M_TEMP, sizeof(*kq));
  130. }
  131. }