wd_create.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /****************************************************************************
  2. * sched/wdog/wd_create.c
  3. *
  4. * Copyright (C) 2007-2009, 2014, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdbool.h>
  40. #include <queue.h>
  41. #include <nuttx/irq.h>
  42. #include <nuttx/wdog.h>
  43. #include <nuttx/wdog.h>
  44. #include <nuttx/kmalloc.h>
  45. #include "wdog/wdog.h"
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: wd_create
  51. *
  52. * Description:
  53. * The wd_create function will create a watchdog timer by allocating one
  54. * from the list of free watchdog timers.
  55. *
  56. * Input Parameters:
  57. * None
  58. *
  59. * Returned Value:
  60. * Pointer to watchdog (i.e., the watchdog ID), or NULL if insufficient
  61. * watchdogs are available.
  62. *
  63. ****************************************************************************/
  64. WDOG_ID wd_create (void)
  65. {
  66. FAR struct wdog_s *wdog;
  67. irqstate_t flags;
  68. /* These actions must be atomic with respect to other tasks and also with
  69. * respect to interrupt handlers that may be allocating or freeing watchdog
  70. * timers.
  71. */
  72. flags = enter_critical_section();
  73. /* If we are in an interrupt handler -OR- if the number of pre-allocated
  74. * timer structures exceeds the reserve, then take the next timer from
  75. * the head of the free list.
  76. */
  77. if (g_wdnfree > CONFIG_WDOG_INTRESERVE || up_interrupt_context())
  78. {
  79. /* Remove the watchdog timer from the free list */
  80. wdog = (FAR struct wdog_s *)sq_remfirst(&g_wdfreelist);
  81. /* Did we get one? */
  82. if (wdog != NULL)
  83. {
  84. /* Yes.. decrement the count of free, pre-allocated timers (all
  85. * with interrupts disabled).
  86. */
  87. DEBUGASSERT(g_wdnfree > 0);
  88. g_wdnfree--;
  89. /* Clear the forward link and all flags */
  90. wdog->next = NULL;
  91. wdog->flags = 0;
  92. }
  93. else
  94. {
  95. /* We didn't get one... The count should then be exactly zero */
  96. DEBUGASSERT(g_wdnfree == 0);
  97. }
  98. leave_critical_section(flags);
  99. }
  100. /* We are in a normal tasking context AND there are not enough unreserved,
  101. * pre-allocated watchdog timers. We need to allocate one from the kernel
  102. * heap.
  103. */
  104. else
  105. {
  106. /* We do not require that interrupts be disabled to do this. */
  107. leave_critical_section(flags);
  108. wdog = (FAR struct wdog_s *)kmm_malloc(sizeof(struct wdog_s));
  109. /* Did we get one? */
  110. if (wdog)
  111. {
  112. /* Yes.. Clear the forward link and set the allocated flag */
  113. wdog->next = NULL;
  114. wdog->flags = WDOGF_ALLOCED;
  115. }
  116. }
  117. return (WDOG_ID)wdog;
  118. }