[a9153ec] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup rtems_bsd_rtems |
---|
| 5 | * |
---|
| 6 | * @brief TODO. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
[8420b94] | 10 | * Copyright (c) 2009, 2010 embedded brains GmbH. |
---|
| 11 | * All rights reserved. |
---|
[a9153ec] | 12 | * |
---|
| 13 | * embedded brains GmbH |
---|
| 14 | * Obere Lagerstr. 30 |
---|
| 15 | * 82178 Puchheim |
---|
| 16 | * Germany |
---|
| 17 | * <rtems@embedded-brains.de> |
---|
| 18 | * |
---|
[8420b94] | 19 | * Redistribution and use in source and binary forms, with or without |
---|
| 20 | * modification, are permitted provided that the following conditions |
---|
| 21 | * are met: |
---|
| 22 | * 1. Redistributions of source code must retain the above copyright |
---|
| 23 | * notice, this list of conditions and the following disclaimer. |
---|
| 24 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 25 | * notice, this list of conditions and the following disclaimer in the |
---|
| 26 | * documentation and/or other materials provided with the distribution. |
---|
| 27 | * |
---|
| 28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 38 | * SUCH DAMAGE. |
---|
[a9153ec] | 39 | */ |
---|
| 40 | |
---|
[6ad03bf] | 41 | #include <freebsd/machine/rtems-bsd-config.h> |
---|
[a9153ec] | 42 | |
---|
[6ad03bf] | 43 | #include <freebsd/sys/param.h> |
---|
| 44 | #include <freebsd/sys/types.h> |
---|
| 45 | #include <freebsd/sys/systm.h> |
---|
| 46 | #include <freebsd/sys/callout.h> |
---|
| 47 | #include <freebsd/sys/lock.h> |
---|
| 48 | #include <freebsd/sys/mutex.h> |
---|
[a9153ec] | 49 | |
---|
| 50 | RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_callout_chain); |
---|
| 51 | |
---|
| 52 | static void |
---|
| 53 | rtems_bsd_callout_dispatch(rtems_id id, void *arg) |
---|
| 54 | { |
---|
| 55 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 56 | struct callout *c = arg; |
---|
| 57 | |
---|
| 58 | if (c->c_lock != NULL) { |
---|
| 59 | sc = rtems_semaphore_obtain(c->c_lock->lo_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
| 60 | BSD_ASSERT_SC(sc); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if (c->c_func != NULL) { |
---|
| 64 | (*c->c_func)(c->c_arg); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if (c->c_lock != NULL && (c->c_flags & CALLOUT_RETURNUNLOCKED) == 0) { |
---|
| 68 | sc = rtems_semaphore_release(c->c_lock->lo_id); |
---|
| 69 | BSD_ASSERT_SC(sc); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void |
---|
| 74 | callout_init(struct callout *c, int mpsafe) |
---|
| 75 | { |
---|
| 76 | _callout_init_lock(c, mpsafe ? NULL : &Giant.lock_object, mpsafe ? CALLOUT_RETURNUNLOCKED : 0); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void |
---|
| 80 | _callout_init_lock(struct callout *c, struct lock_object *lock, int flags) |
---|
| 81 | { |
---|
| 82 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 83 | rtems_id id = RTEMS_ID_NONE; |
---|
| 84 | |
---|
| 85 | sc = rtems_timer_create(rtems_build_name('_', 'T', 'M', 'R'), &id); |
---|
| 86 | BSD_ASSERT_SC(sc); |
---|
| 87 | |
---|
| 88 | c->c_id = id; |
---|
| 89 | c->c_lock = lock; |
---|
| 90 | c->c_flags = flags; |
---|
| 91 | c->c_func = NULL; |
---|
| 92 | c->c_arg = NULL; |
---|
| 93 | |
---|
| 94 | rtems_chain_append(&rtems_bsd_callout_chain, &c->c_node); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int |
---|
| 98 | callout_reset(struct callout *c, int to_ticks, void (*ftn)(void *), void *arg) |
---|
| 99 | { |
---|
| 100 | /* FIXME: Integer conversions */ |
---|
| 101 | |
---|
| 102 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 103 | |
---|
| 104 | if (to_ticks <= 0) { |
---|
| 105 | to_ticks = 1; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | c->c_func = ftn; |
---|
| 109 | c->c_arg = arg; |
---|
| 110 | |
---|
| 111 | sc = rtems_timer_server_fire_after(c->c_id, (rtems_interval) to_ticks, rtems_bsd_callout_dispatch, c); |
---|
| 112 | BSD_ASSERT_SC(sc); |
---|
| 113 | |
---|
| 114 | return 0; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int |
---|
| 118 | callout_schedule(struct callout *c, int to_ticks) |
---|
| 119 | { |
---|
| 120 | return callout_reset(c, to_ticks, c->c_func, c->c_arg); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | int |
---|
| 124 | _callout_stop_safe(struct callout *c, int safe) |
---|
| 125 | { |
---|
| 126 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 127 | |
---|
| 128 | if (!safe) { |
---|
| 129 | sc = rtems_timer_cancel(c->c_id); |
---|
| 130 | BSD_ASSERT_SC(sc); |
---|
| 131 | } else { |
---|
| 132 | sc = rtems_timer_delete(c->c_id); |
---|
| 133 | BSD_ASSERT_SC(sc); |
---|
| 134 | |
---|
| 135 | c->c_id = RTEMS_ID_NONE; |
---|
| 136 | rtems_chain_extract(&c->c_node); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | return 0; |
---|
| 140 | } |
---|