2002-04-21 22:06:12 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2002-05-05 18:34:06 +00:00
|
|
|
|
#include <stdlib.h>
|
2002-06-29 21:30:42 +00:00
|
|
|
|
#include <string.h>
|
2005-03-01 14:33:45 +00:00
|
|
|
|
#include "config.h"
|
2002-04-21 22:06:12 +00:00
|
|
|
|
#include "kernel.h"
|
2002-04-25 00:15:04 +00:00
|
|
|
|
#include "thread.h"
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#include "cpu.h"
|
2002-05-05 18:34:06 +00:00
|
|
|
|
#include "system.h"
|
|
|
|
|
#include "panic.h"
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
2006-11-22 00:41:30 +00:00
|
|
|
|
#if !defined(CPU_PP) || !defined(BOOTLOADER)
|
2007-03-04 20:06:41 +00:00
|
|
|
|
long current_tick NOCACHEDATA_ATTR = 0;
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
2002-12-18 14:57:45 +00:00
|
|
|
|
static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
|
2002-06-04 12:25:53 +00:00
|
|
|
|
|
2002-06-29 21:19:55 +00:00
|
|
|
|
/* This array holds all queues that are initiated. It is used for broadcast. */
|
2007-03-26 16:55:17 +00:00
|
|
|
|
static struct event_queue *all_queues[32] NOCACHEBSS_ATTR;
|
|
|
|
|
static int num_queues NOCACHEBSS_ATTR;
|
2002-06-29 21:19:55 +00:00
|
|
|
|
|
2005-11-12 14:48:52 +00:00
|
|
|
|
void queue_wait(struct event_queue *q, struct event *ev) ICODE_ATTR;
|
2002-08-01 08:14:01 +00:00
|
|
|
|
|
2002-05-05 18:34:06 +00:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Standard kernel stuff
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void kernel_init(void)
|
|
|
|
|
{
|
2002-06-07 14:56:10 +00:00
|
|
|
|
/* Init the threading API */
|
|
|
|
|
init_threads();
|
|
|
|
|
|
2007-03-04 20:06:41 +00:00
|
|
|
|
if(CURRENT_CORE == CPU)
|
|
|
|
|
{
|
|
|
|
|
memset(tick_funcs, 0, sizeof(tick_funcs));
|
2002-06-29 21:19:55 +00:00
|
|
|
|
|
2007-03-04 20:06:41 +00:00
|
|
|
|
num_queues = 0;
|
|
|
|
|
memset(all_queues, 0, sizeof(all_queues));
|
|
|
|
|
}
|
2006-01-05 17:02:48 +00:00
|
|
|
|
|
2002-05-07 23:01:42 +00:00
|
|
|
|
tick_start(1000/HZ);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
|
void sleep(int ticks)
|
|
|
|
|
{
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#if CONFIG_CPU == S3C2440 && defined(BOOTLOADER)
|
2006-12-29 02:49:12 +00:00
|
|
|
|
volatile int counter;
|
2006-08-12 08:27:48 +00:00
|
|
|
|
TCON &= ~(1 << 20); // stop timer 4
|
|
|
|
|
// TODO: this constant depends on dividers settings inherited from
|
|
|
|
|
// firmware. Set them explicitly somwhere.
|
|
|
|
|
TCNTB4 = 12193 * ticks / HZ;
|
|
|
|
|
TCON |= 1 << 21; // set manual bit
|
|
|
|
|
TCON &= ~(1 << 21); // reset manual bit
|
|
|
|
|
TCON &= ~(1 << 22); //autoreload Off
|
|
|
|
|
TCON |= (1 << 20); // start timer 4
|
|
|
|
|
do {
|
|
|
|
|
counter = TCNTO4;
|
|
|
|
|
} while(counter > 0);
|
|
|
|
|
|
2007-03-12 22:12:20 +00:00
|
|
|
|
#elif defined(CPU_PP) && defined(BOOTLOADER)
|
|
|
|
|
unsigned stop = USEC_TIMER + ticks * (1000000/HZ);
|
|
|
|
|
while (TIME_BEFORE(USEC_TIMER, stop))
|
|
|
|
|
switch_thread(true,NULL);
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#else
|
2006-09-16 16:18:11 +00:00
|
|
|
|
sleep_thread(ticks);
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void yield(void)
|
|
|
|
|
{
|
2006-12-29 02:49:12 +00:00
|
|
|
|
#if ((CONFIG_CPU == S3C2440 || defined(ELIO_TPJ1022)) && defined(BOOTLOADER))
|
2006-08-31 19:19:35 +00:00
|
|
|
|
/* Some targets don't like yielding in the bootloader */
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#else
|
2006-09-16 16:18:11 +00:00
|
|
|
|
switch_thread(true, NULL);
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Queue handling stuff
|
|
|
|
|
****************************************************************************/
|
2006-12-16 18:35:12 +00:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
/* Moves waiting thread's descriptor to the current sender when a
|
|
|
|
|
message is dequeued */
|
|
|
|
|
static void queue_fetch_sender(struct queue_sender_list *send,
|
|
|
|
|
unsigned int i)
|
|
|
|
|
{
|
2007-03-23 01:00:13 +00:00
|
|
|
|
/* Disable interrupts to protect against collision in this slot */
|
2006-12-16 18:35:12 +00:00
|
|
|
|
int old_level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2007-03-21 22:58:53 +00:00
|
|
|
|
struct thread_entry **spp = &send->senders[i];
|
2006-12-16 18:35:12 +00:00
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (*spp)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
send->curr_sender = *spp;
|
|
|
|
|
*spp = NULL;
|
|
|
|
|
}
|
2007-03-21 22:58:53 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
set_irq_level(old_level);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Puts the specified return value in the waiting thread's return value
|
2007-03-23 01:00:13 +00:00
|
|
|
|
* and wakes the thread.
|
|
|
|
|
* 1) A sender should be confirmed to exist before calling which makes it
|
|
|
|
|
* more efficent to reject the majority of cases that don't need this
|
|
|
|
|
called.
|
|
|
|
|
* 2) Requires interrupts disabled since queue overflows can cause posts
|
|
|
|
|
* from interrupt handlers to wake threads. Not doing so could cause
|
|
|
|
|
* an attempt at multiple wakes or other problems.
|
|
|
|
|
*/
|
2007-03-21 22:58:53 +00:00
|
|
|
|
static void queue_release_sender(struct thread_entry **sender,
|
2006-12-19 16:50:07 +00:00
|
|
|
|
intptr_t retval)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
(*sender)->retval = retval;
|
2007-03-26 03:24:36 +00:00
|
|
|
|
wakeup_thread_irq_safe(sender);
|
2007-03-23 01:00:13 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/* This should _never_ happen - there must never be multiple
|
|
|
|
|
threads in this list and it is a corrupt state */
|
|
|
|
|
if (*sender != NULL)
|
|
|
|
|
panicf("Queue: send slot ovf");
|
|
|
|
|
#endif
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Releases any waiting threads that are queued with queue_send -
|
2007-03-23 01:00:13 +00:00
|
|
|
|
* reply with 0.
|
|
|
|
|
* Disable IRQs before calling since it uses queue_release_sender.
|
|
|
|
|
*/
|
2006-12-16 18:35:12 +00:00
|
|
|
|
static void queue_release_all_senders(struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
if(q->send)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
|
|
|
|
for(i = q->read; i != q->write; i++)
|
|
|
|
|
{
|
2007-03-21 22:58:53 +00:00
|
|
|
|
struct thread_entry **spp =
|
2006-12-16 18:35:12 +00:00
|
|
|
|
&q->send->senders[i & QUEUE_LENGTH_MASK];
|
2007-03-21 22:58:53 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
if(*spp)
|
|
|
|
|
{
|
2006-12-19 16:50:07 +00:00
|
|
|
|
queue_release_sender(spp, 0);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Enables queue_send on the specified queue - caller allocates the extra
|
|
|
|
|
data structure */
|
|
|
|
|
void queue_enable_queue_send(struct event_queue *q,
|
|
|
|
|
struct queue_sender_list *send)
|
|
|
|
|
{
|
|
|
|
|
q->send = send;
|
2007-03-26 16:55:17 +00:00
|
|
|
|
memset(send, 0, sizeof(struct queue_sender_list));
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
#endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
|
void queue_init(struct event_queue *q, bool register_queue)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
q->read = 0;
|
|
|
|
|
q->write = 0;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
q->thread = NULL;
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
q->irq_safe = false;
|
|
|
|
|
#endif
|
2006-12-16 18:35:12 +00:00
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
q->send = NULL; /* No message sending by default */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if(register_queue)
|
2006-09-16 16:18:11 +00:00
|
|
|
|
{
|
|
|
|
|
/* Add it to the all_queues array */
|
|
|
|
|
all_queues[num_queues++] = q;
|
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
/**
|
|
|
|
|
* If IRQ mode is enabled, some core-wise locking mechanisms are disabled
|
|
|
|
|
* causing accessing queue to be no longer thread safe from the other core.
|
|
|
|
|
* However, that locking mechanism would also kill IRQ handlers.
|
|
|
|
|
*
|
|
|
|
|
* @param q struct of an event_queue
|
|
|
|
|
* @param state enable/disable IRQ mode
|
|
|
|
|
* @default state disabled
|
|
|
|
|
*/
|
|
|
|
|
void queue_set_irq_safe(struct event_queue *q, bool state)
|
|
|
|
|
{
|
|
|
|
|
q->irq_safe = state;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-23 10:53:47 +00:00
|
|
|
|
void queue_delete(struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2006-01-23 10:59:07 +00:00
|
|
|
|
bool found = false;
|
2006-01-23 10:53:47 +00:00
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
2006-09-16 16:18:11 +00:00
|
|
|
|
wakeup_thread(&q->thread);
|
|
|
|
|
|
2006-01-23 10:53:47 +00:00
|
|
|
|
/* Find the queue to be deleted */
|
|
|
|
|
for(i = 0;i < num_queues;i++)
|
|
|
|
|
{
|
|
|
|
|
if(all_queues[i] == q)
|
2006-01-23 10:59:07 +00:00
|
|
|
|
{
|
|
|
|
|
found = true;
|
2006-01-23 10:53:47 +00:00
|
|
|
|
break;
|
2006-01-23 10:59:07 +00:00
|
|
|
|
}
|
2006-01-23 10:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-23 10:59:07 +00:00
|
|
|
|
if(found)
|
2006-01-23 10:53:47 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
/* Release waiting threads and reply to any dequeued message
|
|
|
|
|
waiting for one. */
|
2007-03-23 01:00:13 +00:00
|
|
|
|
int level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
queue_release_all_senders(q);
|
|
|
|
|
queue_reply(q, NULL);
|
2007-03-23 01:00:13 +00:00
|
|
|
|
set_irq_level(level);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
#endif
|
2006-01-23 10:59:07 +00:00
|
|
|
|
/* Move the following queues up in the list */
|
|
|
|
|
for(;i < num_queues-1;i++)
|
|
|
|
|
{
|
|
|
|
|
all_queues[i] = all_queues[i+1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num_queues--;
|
2006-01-23 10:53:47 +00:00
|
|
|
|
}
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
|
|
|
|
unlock_cores();
|
2006-01-23 10:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-16 20:57:32 +00:00
|
|
|
|
void queue_wait(struct event_queue *q, struct event *ev)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
unsigned int rd;
|
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (q->read == q->write)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
2006-11-11 05:33:24 +00:00
|
|
|
|
block_thread(&q->thread);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
2007-03-21 22:58:53 +00:00
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
rd = q->read++ & QUEUE_LENGTH_MASK;
|
|
|
|
|
*ev = q->events[rd];
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
if(q->send && q->send->senders[rd])
|
|
|
|
|
{
|
|
|
|
|
/* Get data for a waiting thread if one */
|
|
|
|
|
queue_fetch_sender(q->send, rd);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
|
|
|
|
unlock_cores();
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-11 08:56:23 +00:00
|
|
|
|
void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks)
|
|
|
|
|
{
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (q->read == q->write && ticks > 0)
|
2002-10-11 08:56:23 +00:00
|
|
|
|
{
|
2006-11-11 05:33:24 +00:00
|
|
|
|
block_thread_w_tmo(&q->thread, ticks);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
2002-10-11 08:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (q->read != q->write)
|
2002-10-11 08:56:23 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
unsigned int rd = q->read++ & QUEUE_LENGTH_MASK;
|
|
|
|
|
*ev = q->events[rd];
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
if(q->send && q->send->senders[rd])
|
|
|
|
|
{
|
|
|
|
|
/* Get data for a waiting thread if one */
|
|
|
|
|
queue_fetch_sender(q->send, rd);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2002-10-11 08:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ev->id = SYS_TIMEOUT;
|
|
|
|
|
}
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
|
|
|
|
unlock_cores();
|
2002-10-11 08:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 16:50:07 +00:00
|
|
|
|
void queue_post(struct event_queue *q, long id, intptr_t data)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
unsigned int wr;
|
|
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
lock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
wr = q->write++ & QUEUE_LENGTH_MASK;
|
2002-05-08 22:07:41 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
q->events[wr].id = id;
|
|
|
|
|
q->events[wr].data = data;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
if(q->send)
|
|
|
|
|
{
|
2007-03-21 22:58:53 +00:00
|
|
|
|
struct thread_entry **spp = &q->send->senders[wr];
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (*spp)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
/* overflow protect - unblock any thread waiting at this index */
|
2006-12-19 16:50:07 +00:00
|
|
|
|
queue_release_sender(spp, 0);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-03-26 03:24:36 +00:00
|
|
|
|
wakeup_thread_irq_safe(&q->thread);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
unlock_cores();
|
|
|
|
|
#endif
|
2006-12-16 18:35:12 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
2007-03-26 03:24:36 +00:00
|
|
|
|
/* No wakeup_thread_irq_safe here because IRQ handlers are not allowed
|
|
|
|
|
use of this function - we only aim to protect the queue integrity by
|
|
|
|
|
turning them off. */
|
2006-12-19 16:50:07 +00:00
|
|
|
|
intptr_t queue_send(struct event_queue *q, long id, intptr_t data)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
unsigned int wr;
|
2007-04-14 09:47:47 +00:00
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
|
|
|
|
|
|
|
|
|
wr = q->write++ & QUEUE_LENGTH_MASK;
|
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
q->events[wr].id = id;
|
2002-04-29 14:25:44 +00:00
|
|
|
|
q->events[wr].data = data;
|
2007-03-21 22:58:53 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
if(q->send)
|
|
|
|
|
{
|
2007-03-21 22:58:53 +00:00
|
|
|
|
struct thread_entry **spp = &q->send->senders[wr];
|
2006-12-16 18:35:12 +00:00
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (*spp)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
/* overflow protect - unblock any thread waiting at this index */
|
2006-12-19 16:50:07 +00:00
|
|
|
|
queue_release_sender(spp, 0);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wakeup_thread(&q->thread);
|
2007-03-21 22:58:53 +00:00
|
|
|
|
set_irq_level_and_block_thread(spp, oldlevel);
|
|
|
|
|
return thread_get_current()->retval;
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Function as queue_post if sending is not enabled */
|
2006-09-16 16:18:11 +00:00
|
|
|
|
wakeup_thread(&q->thread);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
unlock_cores();
|
2002-05-08 22:07:41 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
2007-03-21 22:58:53 +00:00
|
|
|
|
|
2006-12-19 16:50:07 +00:00
|
|
|
|
return 0;
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
#if 0 /* not used now but probably will be later */
|
|
|
|
|
/* Query if the last message dequeued was added by queue_send or not */
|
|
|
|
|
bool queue_in_queue_send(struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
return q->send && q->send->curr_sender;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Replies with retval to any dequeued message sent with queue_send */
|
2006-12-19 16:50:07 +00:00
|
|
|
|
void queue_reply(struct event_queue *q, intptr_t retval)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
2006-12-16 18:35:12 +00:00
|
|
|
|
if(q->send && q->send->curr_sender)
|
|
|
|
|
{
|
2007-03-23 01:00:13 +00:00
|
|
|
|
int level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
|
|
|
|
if(q->send->curr_sender)
|
|
|
|
|
{
|
|
|
|
|
queue_release_sender(&q->send->curr_sender, retval);
|
|
|
|
|
}
|
|
|
|
|
set_irq_level(level);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
2007-03-26 16:55:17 +00:00
|
|
|
|
unlock_cores();
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
#endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
|
|
|
|
|
|
2004-08-16 23:37:23 +00:00
|
|
|
|
bool queue_empty(const struct event_queue* q)
|
2002-05-23 09:22:07 +00:00
|
|
|
|
{
|
2007-03-26 16:55:17 +00:00
|
|
|
|
bool is_empty;
|
|
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
lock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
is_empty = ( q->read == q->write );
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
unlock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return is_empty;
|
2002-05-23 09:22:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-09-01 06:24:05 +00:00
|
|
|
|
void queue_clear(struct event_queue* q)
|
2004-09-01 06:20:21 +00:00
|
|
|
|
{
|
2004-09-01 06:24:05 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
lock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
/* Release all thread waiting in the queue for a reply -
|
|
|
|
|
dequeued sent message will be handled by owning thread */
|
|
|
|
|
queue_release_all_senders(q);
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-09-01 06:20:21 +00:00
|
|
|
|
q->read = 0;
|
|
|
|
|
q->write = 0;
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
unlock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-09-01 06:24:05 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
2004-09-01 06:20:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-10-19 11:43:13 +00:00
|
|
|
|
void queue_remove_from_head(struct event_queue *q, long id)
|
|
|
|
|
{
|
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2007-03-21 22:58:53 +00:00
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
lock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-12-16 18:35:12 +00:00
|
|
|
|
while(q->read != q->write)
|
2006-10-19 11:43:13 +00:00
|
|
|
|
{
|
2006-12-16 18:35:12 +00:00
|
|
|
|
unsigned int rd = q->read & QUEUE_LENGTH_MASK;
|
|
|
|
|
|
|
|
|
|
if(q->events[rd].id != id)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
|
|
|
|
|
if(q->send)
|
|
|
|
|
{
|
2007-03-21 22:58:53 +00:00
|
|
|
|
struct thread_entry **spp = &q->send->senders[rd];
|
2006-12-16 18:35:12 +00:00
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
if (*spp)
|
2006-12-16 18:35:12 +00:00
|
|
|
|
{
|
|
|
|
|
/* Release any thread waiting on this message */
|
2006-12-19 16:50:07 +00:00
|
|
|
|
queue_release_sender(spp, 0);
|
2006-12-16 18:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2006-10-19 11:43:13 +00:00
|
|
|
|
q->read++;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
unlock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-10-19 11:43:13 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-11 10:52:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* The number of events waiting in the queue.
|
|
|
|
|
*
|
|
|
|
|
* @param struct of event_queue
|
|
|
|
|
* @return number of events in the queue
|
|
|
|
|
*/
|
|
|
|
|
int queue_count(const struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2007-04-14 09:47:47 +00:00
|
|
|
|
int result;
|
2007-03-11 10:52:36 +00:00
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
lock_cores();
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-04-14 09:47:47 +00:00
|
|
|
|
result = q->write - q->read;
|
|
|
|
|
|
2007-03-26 16:55:17 +00:00
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
|
if (!q->irq_safe)
|
|
|
|
|
unlock_cores();
|
|
|
|
|
#endif
|
2007-04-14 09:47:47 +00:00
|
|
|
|
|
2007-03-11 10:52:36 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 16:50:07 +00:00
|
|
|
|
int queue_broadcast(long id, intptr_t data)
|
2002-06-29 21:19:55 +00:00
|
|
|
|
{
|
2007-03-21 22:58:53 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for(i = 0;i < num_queues;i++)
|
|
|
|
|
{
|
|
|
|
|
queue_post(all_queues[i], id, data);
|
|
|
|
|
}
|
2002-06-29 21:19:55 +00:00
|
|
|
|
|
2007-03-21 22:58:53 +00:00
|
|
|
|
return num_queues;
|
2002-06-29 21:19:55 +00:00
|
|
|
|
}
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Timer tick
|
|
|
|
|
****************************************************************************/
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#if CONFIG_CPU == SH7034
|
2005-03-01 14:33:45 +00:00
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
2005-10-03 09:24:36 +00:00
|
|
|
|
unsigned long count;
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
count = CPU_FREQ * interval_in_ms / 1000 / 8;
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
if(count > 0x10000)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We are using timer 0 */
|
|
|
|
|
|
|
|
|
|
TSTR &= ~0x01; /* Stop the timer */
|
|
|
|
|
TSNC &= ~0x01; /* No synchronization */
|
|
|
|
|
TMDR &= ~0x01; /* Operate normally */
|
|
|
|
|
|
|
|
|
|
TCNT0 = 0; /* Start counting at 0 */
|
2005-10-03 09:24:36 +00:00
|
|
|
|
GRA0 = (unsigned short)(count - 1);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
TCR0 = 0x23; /* Clear at GRA match, sysclock/8 */
|
|
|
|
|
|
|
|
|
|
/* Enable interrupt on level 1 */
|
|
|
|
|
IPRC = (IPRC & ~0x00f0) | 0x0010;
|
|
|
|
|
|
|
|
|
|
TSR0 &= ~0x01;
|
|
|
|
|
TIER0 = 0xf9; /* Enable GRA match interrupt */
|
|
|
|
|
|
|
|
|
|
TSTR |= 0x01; /* Start timer 1 */
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
void IMIA0(void) __attribute__ ((interrupt_handler));
|
2002-05-05 18:34:06 +00:00
|
|
|
|
void IMIA0(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Run through the list of tick tasks */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_tick++;
|
|
|
|
|
|
|
|
|
|
TSR0 &= ~0x01;
|
|
|
|
|
}
|
2005-07-18 12:40:29 +00:00
|
|
|
|
#elif defined(CPU_COLDFIRE)
|
2005-03-01 14:33:45 +00:00
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
2004-10-27 07:07:54 +00:00
|
|
|
|
{
|
2005-10-03 09:24:36 +00:00
|
|
|
|
unsigned long count;
|
|
|
|
|
int prescale;
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
count = CPU_FREQ/2 * interval_in_ms / 1000 / 16;
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
if(count > 0x10000)
|
2004-10-27 07:07:54 +00:00
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-10-03 09:24:36 +00:00
|
|
|
|
|
|
|
|
|
prescale = cpu_frequency / CPU_FREQ;
|
|
|
|
|
/* Note: The prescaler is later adjusted on-the-fly on CPU frequency
|
|
|
|
|
changes within timer.c */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
/* We are using timer 0 */
|
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
TRR0 = (unsigned short)(count - 1); /* The reference count */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
TCN0 = 0; /* reset the timer */
|
2005-10-03 09:24:36 +00:00
|
|
|
|
TMR0 = 0x001d | ((unsigned short)(prescale - 1) << 8);
|
|
|
|
|
/* restart, CLK/16, enabled, prescaler */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
|
2005-11-05 03:28:20 +00:00
|
|
|
|
ICR1 = 0x8c; /* Interrupt on level 3.0 */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
IMR &= ~0x200;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TIMER0(void) __attribute__ ((interrupt_handler));
|
|
|
|
|
void TIMER0(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Run through the list of tick tasks */
|
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_tick++;
|
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
}
|
2005-01-09 23:24:02 +00:00
|
|
|
|
|
2006-08-02 09:46:51 +00:00
|
|
|
|
#elif defined(CPU_PP)
|
2005-11-07 23:07:19 +00:00
|
|
|
|
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#ifndef BOOTLOADER
|
2005-12-12 13:53:22 +00:00
|
|
|
|
void TIMER1(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2006-01-24 22:31:57 +00:00
|
|
|
|
TIMER1_VAL; /* Read value to ack IRQ */
|
2007-03-04 20:06:41 +00:00
|
|
|
|
/* Run through the list of tick tasks (using main core) */
|
|
|
|
|
if (CURRENT_CORE == CPU)
|
2005-12-12 13:53:22 +00:00
|
|
|
|
{
|
2007-03-04 20:06:41 +00:00
|
|
|
|
for (i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2005-12-12 13:53:22 +00:00
|
|
|
|
{
|
2007-03-04 20:06:41 +00:00
|
|
|
|
if (tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
2005-12-12 13:53:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-04 20:06:41 +00:00
|
|
|
|
current_tick++;
|
|
|
|
|
}
|
2005-12-12 13:53:22 +00:00
|
|
|
|
}
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#endif
|
2005-12-12 13:53:22 +00:00
|
|
|
|
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#ifndef BOOTLOADER
|
2007-03-04 20:06:41 +00:00
|
|
|
|
if(CURRENT_CORE == CPU)
|
|
|
|
|
{
|
|
|
|
|
TIMER1_CFG = 0x0;
|
|
|
|
|
TIMER1_VAL;
|
|
|
|
|
/* enable timer */
|
|
|
|
|
TIMER1_CFG = 0xc0000000 | (interval_in_ms*1000 - 1);
|
|
|
|
|
/* unmask interrupt source */
|
|
|
|
|
CPU_INT_EN = TIMER1_MASK;
|
|
|
|
|
} else {
|
|
|
|
|
COP_INT_EN = TIMER1_MASK;
|
|
|
|
|
}
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#else
|
|
|
|
|
/* We don't enable interrupts in the bootloader */
|
|
|
|
|
(void)interval_in_ms;
|
|
|
|
|
#endif
|
2005-11-07 23:07:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-12 00:35:50 +00:00
|
|
|
|
#elif CONFIG_CPU == PNX0101
|
|
|
|
|
|
|
|
|
|
void timer_handler(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Run through the list of tick tasks */
|
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_tick++;
|
|
|
|
|
|
2007-03-24 19:26:13 +00:00
|
|
|
|
TIMER0.clr = 0;
|
2006-01-12 00:35:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
2007-03-24 19:26:13 +00:00
|
|
|
|
TIMER0.ctrl &= ~0x80; /* Disable the counter */
|
|
|
|
|
TIMER0.ctrl |= 0x40; /* Reload after counting down to zero */
|
|
|
|
|
TIMER0.load = 3000000 * interval_in_ms / 1000;
|
|
|
|
|
TIMER0.ctrl &= ~0xc; /* No prescaler */
|
|
|
|
|
TIMER0.clr = 1; /* Clear the interrupt request */
|
|
|
|
|
|
|
|
|
|
irq_set_int_handler(IRQ_TIMER0, timer_handler);
|
|
|
|
|
irq_enable_int(IRQ_TIMER0);
|
|
|
|
|
|
|
|
|
|
TIMER0.ctrl |= 0x80; /* Enable the counter */
|
2006-01-12 00:35:50 +00:00
|
|
|
|
}
|
2006-08-12 08:01:54 +00:00
|
|
|
|
#elif CONFIG_CPU == S3C2440
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
2006-12-29 02:49:12 +00:00
|
|
|
|
TCON &= ~(1 << 20); // stop timer 4
|
|
|
|
|
// TODO: this constant depends on dividers settings inherited from
|
|
|
|
|
// firmware. Set them explicitly somwhere.
|
|
|
|
|
TCNTB4 = 12193 * interval_in_ms / 1000;
|
|
|
|
|
TCON |= 1 << 21; // set manual bit
|
|
|
|
|
TCON &= ~(1 << 21); // reset manual bit
|
|
|
|
|
TCON |= 1 << 22; //interval mode
|
|
|
|
|
TCON |= (1 << 20); // start timer 4
|
2006-08-12 08:01:54 +00:00
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
INTMOD &= ~(1 << 14); // timer 4 to IRQ mode
|
|
|
|
|
INTMSK &= ~(1 << 14); // timer 4 unmask interrupts
|
|
|
|
|
}
|
2006-01-12 00:35:50 +00:00
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
void timer4(void) {
|
2007-03-21 22:58:53 +00:00
|
|
|
|
int i;
|
2006-12-29 02:49:12 +00:00
|
|
|
|
/* Run through the list of tick tasks */
|
|
|
|
|
for(i = 0; i < MAX_NUM_TICK_TASKS; i++)
|
2006-08-12 08:01:54 +00:00
|
|
|
|
{
|
2006-12-29 02:49:12 +00:00
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
2006-08-12 08:01:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
current_tick++;
|
2006-08-12 08:01:54 +00:00
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
|
/* following needs to be fixed. */
|
|
|
|
|
/*wake_up_thread();*/
|
2006-08-12 08:01:54 +00:00
|
|
|
|
}
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#endif
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
int tick_add_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2004-03-02 11:32:59 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/* Add a task if there is room */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i] == NULL)
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i] = f;
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set_irq_level(oldlevel);
|
2002-06-29 21:30:42 +00:00
|
|
|
|
panicf("Error! tick_add_task(): out of tasks");
|
2002-05-05 18:34:06 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int tick_remove_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2004-03-02 11:32:59 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/* Remove a task if it is there */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i] == f)
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i] = NULL;
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2002-05-16 20:57:32 +00:00
|
|
|
|
|
2005-02-22 12:19:12 +00:00
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
|
/*
|
|
|
|
|
* Simulator versions in uisimulator/SIMVER/
|
|
|
|
|
*/
|
|
|
|
|
|
2002-05-16 20:57:32 +00:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Simple mutex functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void mutex_init(struct mutex *m)
|
|
|
|
|
{
|
2002-06-04 12:47:39 +00:00
|
|
|
|
m->locked = false;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
m->thread = NULL;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-04 20:06:41 +00:00
|
|
|
|
void mutex_lock(struct mutex *m)
|
|
|
|
|
{
|
2007-03-09 08:03:18 +00:00
|
|
|
|
if (test_and_set(&m->locked, 1))
|
2007-03-04 20:06:41 +00:00
|
|
|
|
{
|
|
|
|
|
/* Wait until the lock is open... */
|
|
|
|
|
block_thread(&m->thread);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-09 08:03:18 +00:00
|
|
|
|
void mutex_unlock(struct mutex *m)
|
2002-05-16 20:57:32 +00:00
|
|
|
|
{
|
2007-03-26 16:55:17 +00:00
|
|
|
|
lock_cores();
|
|
|
|
|
|
2007-03-09 08:03:18 +00:00
|
|
|
|
if (m->thread == NULL)
|
|
|
|
|
m->locked = 0;
|
|
|
|
|
else
|
|
|
|
|
wakeup_thread(&m->thread);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
|
|
|
|
|
unlock_cores();
|
2007-03-09 08:03:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void spinlock_lock(struct mutex *m)
|
|
|
|
|
{
|
|
|
|
|
while (test_and_set(&m->locked, 1))
|
2006-09-16 16:18:11 +00:00
|
|
|
|
{
|
2007-03-09 08:03:18 +00:00
|
|
|
|
/* wait until the lock is open... */
|
|
|
|
|
switch_thread(true, NULL);
|
2006-09-16 16:18:11 +00:00
|
|
|
|
}
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-09 08:03:18 +00:00
|
|
|
|
void spinlock_unlock(struct mutex *m)
|
2002-05-16 20:57:32 +00:00
|
|
|
|
{
|
2007-03-09 08:03:18 +00:00
|
|
|
|
m->locked = 0;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
2005-02-22 12:19:12 +00:00
|
|
|
|
|
2007-03-09 08:03:18 +00:00
|
|
|
|
#endif /* ndef SIMULATOR */
|