blob: 947d82a55039f29ae9c442f3fb17fde5624883d3 [file] [log] [blame]
Jens Arnold74b731e2005-03-18 23:51:52 +00001/***************************************************************************
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 Arnoldb51f7df2005-11-21 23:55:39 +000020#include <stddef.h>
Jens Arnold74b731e2005-03-18 23:51:52 +000021#include "kernel.h"
22#include "thread.h"
Jens Arnoldb51f7df2005-11-21 23:55:39 +000023#include "debug.h"
24
25static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
Jens Arnold74b731e2005-03-18 23:51:52 +000026
27int set_irq_level (int level)
28{
29 static int _lv = 0;
30 return (_lv = level);
31}
32
33void queue_init(struct event_queue *q)
34{
35 q->read = 0;
36 q->write = 0;
37}
38
Linus Nielsen Feltzing765e0f82006-01-23 10:53:47 +000039void queue_delete(struct event_queue *q)
40{
41 (void)q;
42}
43
Jens Arnold74b731e2005-03-18 23:51:52 +000044void 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
54void 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
73void 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
86bool queue_empty(const struct event_queue* q)
87{
88 return ( q->read == q->write );
89}
90
91void 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
98void switch_thread (void)
99{
100 yield ();
101}
102
Jens Arnoldb51f7df2005-11-21 23:55:39 +0000103void 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
117int 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
134int 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 Arnold74b731e2005-03-18 23:51:52 +0000151void mutex_init(struct mutex *m)
152{
153 (void)m;
154}
155
156void mutex_lock(struct mutex *m)
157{
158 (void)m;
159}
160
161void mutex_unlock(struct mutex *m)
162{
163 (void)m;
164}
165