Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Jens Arnold |
| 11 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
Jens Arnold | b51f7df | 2005-11-21 23:55:39 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 21 | #include "kernel.h" |
| 22 | #include "thread.h" |
Jens Arnold | b51f7df | 2005-11-21 23:55:39 +0000 | [diff] [blame] | 23 | #include "debug.h" |
| 24 | |
| 25 | static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void); |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 26 | |
| 27 | int set_irq_level (int level) |
| 28 | { |
| 29 | static int _lv = 0; |
| 30 | return (_lv = level); |
| 31 | } |
| 32 | |
| 33 | void queue_init(struct event_queue *q) |
| 34 | { |
| 35 | q->read = 0; |
| 36 | q->write = 0; |
| 37 | } |
| 38 | |
Linus Nielsen Feltzing | 765e0f8 | 2006-01-23 10:53:47 +0000 | [diff] [blame] | 39 | void queue_delete(struct event_queue *q) |
| 40 | { |
| 41 | (void)q; |
| 42 | } |
| 43 | |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 44 | void queue_wait(struct event_queue *q, struct event *ev) |
| 45 | { |
| 46 | while(q->read == q->write) |
| 47 | { |
| 48 | switch_thread(); |
| 49 | } |
| 50 | |
| 51 | *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK]; |
| 52 | } |
| 53 | |
| 54 | void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks) |
| 55 | { |
| 56 | unsigned int timeout = current_tick + ticks; |
| 57 | |
| 58 | while(q->read == q->write && TIME_BEFORE( current_tick, timeout )) |
| 59 | { |
| 60 | sleep(1); |
| 61 | } |
| 62 | |
| 63 | if(q->read != q->write) |
| 64 | { |
| 65 | *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK]; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | ev->id = SYS_TIMEOUT; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void queue_post(struct event_queue *q, long id, void *data) |
| 74 | { |
| 75 | int wr; |
| 76 | int oldlevel; |
| 77 | |
| 78 | oldlevel = set_irq_level(15<<4); |
| 79 | wr = (q->write++) & QUEUE_LENGTH_MASK; |
| 80 | |
| 81 | q->events[wr].id = id; |
| 82 | q->events[wr].data = data; |
| 83 | set_irq_level(oldlevel); |
| 84 | } |
| 85 | |
| 86 | bool queue_empty(const struct event_queue* q) |
| 87 | { |
| 88 | return ( q->read == q->write ); |
| 89 | } |
| 90 | |
| 91 | void queue_clear(struct event_queue* q) |
| 92 | { |
| 93 | /* fixme: This is potentially unsafe in case we do interrupt-like processing */ |
| 94 | q->read = 0; |
| 95 | q->write = 0; |
| 96 | } |
| 97 | |
| 98 | void switch_thread (void) |
| 99 | { |
| 100 | yield (); |
| 101 | } |
| 102 | |
Jens Arnold | b51f7df | 2005-11-21 23:55:39 +0000 | [diff] [blame] | 103 | void sim_tick_tasks(void) |
| 104 | { |
| 105 | int i; |
| 106 | |
| 107 | /* Run through the list of tick tasks */ |
| 108 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) |
| 109 | { |
| 110 | if(tick_funcs[i]) |
| 111 | { |
| 112 | tick_funcs[i](); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | int tick_add_task(void (*f)(void)) |
| 118 | { |
| 119 | int i; |
| 120 | |
| 121 | /* Add a task if there is room */ |
| 122 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) |
| 123 | { |
| 124 | if(tick_funcs[i] == NULL) |
| 125 | { |
| 126 | tick_funcs[i] = f; |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | DEBUGF("Error! tick_add_task(): out of tasks"); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | int tick_remove_task(void (*f)(void)) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | /* Remove a task if it is there */ |
| 139 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) |
| 140 | { |
| 141 | if(tick_funcs[i] == f) |
| 142 | { |
| 143 | tick_funcs[i] = NULL; |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return -1; |
| 149 | } |
| 150 | |
Jens Arnold | 74b731e | 2005-03-18 23:51:52 +0000 | [diff] [blame] | 151 | void mutex_init(struct mutex *m) |
| 152 | { |
| 153 | (void)m; |
| 154 | } |
| 155 | |
| 156 | void mutex_lock(struct mutex *m) |
| 157 | { |
| 158 | (void)m; |
| 159 | } |
| 160 | |
| 161 | void mutex_unlock(struct mutex *m) |
| 162 | { |
| 163 | (void)m; |
| 164 | } |
| 165 | |