1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "system-sdl.h"
34 /* Make this nonzero to enable more elaborate checks on objects */
35 #if defined(DEBUG) || defined(SIMULATOR)
36 #define KERNEL_OBJECT_CHECKS 1 /* Always 1 for DEBUG and sim*/
38 #define KERNEL_OBJECT_CHECKS 0
41 #if KERNEL_OBJECT_CHECKS
43 #define KERNEL_ASSERT(exp, msg...) \
44 ({ if (!({ exp; })) { DEBUGF(msg); exit(-1); } })
46 #define KERNEL_ASSERT(exp, msg...) \
47 ({ if (!({ exp; })) panicf(msg); })
50 #define KERNEL_ASSERT(exp, msg...) ({})
53 #if !defined(CPU_PP) || !defined(BOOTLOADER)
54 volatile long current_tick SHAREDDATA_ATTR
= 0;
57 void (*tick_funcs
[MAX_NUM_TICK_TASKS
])(void);
59 extern struct core_entry cores
[NUM_CORES
];
61 /* This array holds all queues that are initiated. It is used for broadcast. */
65 struct event_queue
*queues
[MAX_NUM_QUEUES
];
66 IF_COP( struct corelock cl
; )
67 } all_queues SHAREDBSS_ATTR
;
69 /****************************************************************************
70 * Standard kernel stuff
71 ****************************************************************************/
72 void kernel_init(void)
74 /* Init the threading API */
77 /* Other processors will not reach this point in a multicore build.
78 * In a single-core build with multiple cores they fall-through and
79 * sleep in cop_main without returning. */
80 if (CURRENT_CORE
== CPU
)
82 memset(tick_funcs
, 0, sizeof(tick_funcs
));
83 memset(&all_queues
, 0, sizeof(all_queues
));
84 corelock_init(&all_queues
.cl
);
92 /****************************************************************************
94 ****************************************************************************/
95 #if CONFIG_CPU == SH7034
96 void tick_start(unsigned int interval_in_ms
)
100 count
= CPU_FREQ
* interval_in_ms
/ 1000 / 8;
104 panicf("Error! The tick interval is too long (%d ms)\n",
109 /* We are using timer 0 */
111 TSTR
&= ~0x01; /* Stop the timer */
112 TSNC
&= ~0x01; /* No synchronization */
113 TMDR
&= ~0x01; /* Operate normally */
115 TCNT0
= 0; /* Start counting at 0 */
116 GRA0
= (unsigned short)(count
- 1);
117 TCR0
= 0x23; /* Clear at GRA match, sysclock/8 */
119 /* Enable interrupt on level 1 */
120 IPRC
= (IPRC
& ~0x00f0) | 0x0010;
123 TIER0
= 0xf9; /* Enable GRA match interrupt */
125 TSTR
|= 0x01; /* Start timer 1 */
128 void IMIA0(void) __attribute__ ((interrupt_handler
));
133 /* Run through the list of tick tasks */
134 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
146 #elif defined(CPU_COLDFIRE)
147 void tick_start(unsigned int interval_in_ms
)
152 count
= CPU_FREQ
/2 * interval_in_ms
/ 1000 / 16;
156 panicf("Error! The tick interval is too long (%d ms)\n",
161 prescale
= cpu_frequency
/ CPU_FREQ
;
162 /* Note: The prescaler is later adjusted on-the-fly on CPU frequency
163 changes within timer.c */
165 /* We are using timer 0 */
167 TRR0
= (unsigned short)(count
- 1); /* The reference count */
168 TCN0
= 0; /* reset the timer */
169 TMR0
= 0x001d | ((unsigned short)(prescale
- 1) << 8);
170 /* restart, CLK/16, enabled, prescaler */
172 TER0
= 0xff; /* Clear all events */
174 ICR1
= 0x8c; /* Interrupt on level 3.0 */
178 void TIMER0(void) __attribute__ ((interrupt_handler
));
183 /* Run through the list of tick tasks */
184 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
194 TER0
= 0xff; /* Clear all events */
197 #elif defined(CPU_PP)
204 /* Run through the list of tick tasks (using main core) */
205 TIMER1_VAL
; /* Read value to ack IRQ */
207 /* Run through the list of tick tasks using main CPU core -
208 wake up the COP through its control interface to provide pulse */
209 for (i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
220 #endif /* NUM_CORES */
226 /* Must be last function called init kernel/thread initialization */
227 void tick_start(unsigned int interval_in_ms
)
233 TIMER1_CFG
= 0xc0000000 | (interval_in_ms
*1000 - 1);
234 /* unmask interrupt source */
235 CPU_INT_EN
= TIMER1_MASK
;
237 /* We don't enable interrupts in the bootloader */
238 (void)interval_in_ms
;
242 #elif CONFIG_CPU == PNX0101
244 void timer_handler(void)
248 /* Run through the list of tick tasks */
249 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
260 void tick_start(unsigned int interval_in_ms
)
262 TIMER0
.ctrl
&= ~0x80; /* Disable the counter */
263 TIMER0
.ctrl
|= 0x40; /* Reload after counting down to zero */
264 TIMER0
.load
= 3000000 * interval_in_ms
/ 1000;
265 TIMER0
.ctrl
&= ~0xc; /* No prescaler */
266 TIMER0
.clr
= 1; /* Clear the interrupt request */
268 irq_set_int_handler(IRQ_TIMER0
, timer_handler
);
269 irq_enable_int(IRQ_TIMER0
);
271 TIMER0
.ctrl
|= 0x80; /* Enable the counter */
275 int tick_add_task(void (*f
)(void))
278 int oldlevel
= disable_irq_save();
280 /* Add a task if there is room */
281 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
283 if(tick_funcs
[i
] == NULL
)
286 restore_irq(oldlevel
);
290 restore_irq(oldlevel
);
291 panicf("Error! tick_add_task(): out of tasks");
295 int tick_remove_task(void (*f
)(void))
298 int oldlevel
= disable_irq_save();
300 /* Remove a task if it is there */
301 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
303 if(tick_funcs
[i
] == f
)
305 tick_funcs
[i
] = NULL
;
306 restore_irq(oldlevel
);
311 restore_irq(oldlevel
);
315 /****************************************************************************
316 * Tick-based interval timers/one-shots - be mindful this is not really
317 * intended for continuous timers but for events that need to run for a short
318 * time and be cancelled without further software intervention.
319 ****************************************************************************/
320 #ifdef INCLUDE_TIMEOUT_API
321 static struct timeout
*tmo_list
= NULL
; /* list of active timeout events */
323 /* timeout tick task - calls event handlers when they expire
324 * Event handlers may alter ticks, callback and data during operation.
326 static void timeout_tick(void)
328 unsigned long tick
= current_tick
;
329 struct timeout
*curr
, *next
;
331 for (curr
= tmo_list
; curr
!= NULL
; curr
= next
)
333 next
= (struct timeout
*)curr
->next
;
335 if (TIME_BEFORE(tick
, curr
->expires
))
338 /* this event has expired - call callback */
339 if (curr
->callback(curr
))
340 *(long *)&curr
->expires
= tick
+ curr
->ticks
; /* reload */
342 timeout_cancel(curr
); /* cancel */
346 /* Cancels a timeout callback - can be called from the ISR */
347 void timeout_cancel(struct timeout
*tmo
)
349 int oldlevel
= disable_irq_save();
351 if (tmo_list
!= NULL
)
353 struct timeout
*curr
= tmo_list
;
354 struct timeout
*prev
= NULL
;
356 while (curr
!= tmo
&& curr
!= NULL
)
359 curr
= (struct timeout
*)curr
->next
;
366 tmo_list
= (struct timeout
*)curr
->next
;
368 *(const struct timeout
**)&prev
->next
= curr
->next
;
370 if (tmo_list
== NULL
)
371 tick_remove_task(timeout_tick
); /* last one - remove task */
373 /* not in list or tmo == NULL */
376 restore_irq(oldlevel
);
379 /* Adds a timeout callback - calling with an active timeout resets the
380 interval - can be called from the ISR */
381 void timeout_register(struct timeout
*tmo
, timeout_cb_type callback
,
382 int ticks
, intptr_t data
)
385 struct timeout
*curr
;
390 oldlevel
= disable_irq_save();
392 /* see if this one is already registered */
394 while (curr
!= tmo
&& curr
!= NULL
)
395 curr
= (struct timeout
*)curr
->next
;
399 /* not found - add it */
400 if (tmo_list
== NULL
)
401 tick_add_task(timeout_tick
); /* first one - add task */
403 *(struct timeout
**)&tmo
->next
= tmo_list
;
407 tmo
->callback
= callback
;
410 *(long *)&tmo
->expires
= current_tick
+ ticks
;
412 restore_irq(oldlevel
);
415 #endif /* INCLUDE_TIMEOUT_API */
417 /****************************************************************************
419 ****************************************************************************/
420 void sleep(int ticks
)
422 #if CONFIG_CPU == S3C2440 && defined(BOOTLOADER)
423 volatile int counter
;
424 TCON
&= ~(1 << 20); // stop timer 4
425 // TODO: this constant depends on dividers settings inherited from
426 // firmware. Set them explicitly somwhere.
427 TCNTB4
= 12193 * ticks
/ HZ
;
428 TCON
|= 1 << 21; // set manual bit
429 TCON
&= ~(1 << 21); // reset manual bit
430 TCON
&= ~(1 << 22); //autoreload Off
431 TCON
|= (1 << 20); // start timer 4
434 } while(counter
> 0);
436 #elif defined(CPU_PP) && defined(BOOTLOADER)
437 unsigned stop
= USEC_TIMER
+ ticks
* (1000000/HZ
);
438 while (TIME_BEFORE(USEC_TIMER
, stop
))
440 #elif defined(CREATIVE_ZVx) && defined(BOOTLOADER)
442 long sleep_ticks
= current_tick
+ ticks
+ 1;
443 while (sleep_ticks
> current_tick
)
454 #if ((CONFIG_CPU == S3C2440 || defined(ELIO_TPJ1022)) && defined(BOOTLOADER))
455 /* Some targets don't like yielding in the bootloader */
461 /****************************************************************************
462 * Queue handling stuff
463 ****************************************************************************/
465 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
466 /****************************************************************************
467 * Sender thread queue structure that aids implementation of priority
468 * inheritance on queues because the send list structure is the same as
469 * for all other kernel objects:
472 * E0 added with queue_send and removed by thread via queue_wait(_w_tmo)
473 * E3 was posted with queue_post
474 * 4 events remain enqueued (E1-E4)
477 * q->events[]: | XX | E1 | E2 | E3 | E4 | XX |
478 * q->send->senders[]: | NULL | T1 | T2 | NULL | T3 | NULL |
480 * q->send->list: >->|T0|<->|T1|<->|T2|<-------->|T3|<-<
481 * q->send->curr_sender: /\
483 * Thread has E0 in its own struct queue_event.
485 ****************************************************************************/
487 /* Puts the specified return value in the waiting thread's return value
488 * and wakes the thread.
490 * A sender should be confirmed to exist before calling which makes it
491 * more efficent to reject the majority of cases that don't need this
494 static void queue_release_sender(struct thread_entry
**sender
,
497 struct thread_entry
*thread
= *sender
;
499 *sender
= NULL
; /* Clear slot. */
500 thread
->wakeup_ext_cb
= NULL
; /* Clear callback. */
501 thread
->retval
= retval
; /* Assign thread-local return value. */
502 *thread
->bqp
= thread
; /* Move blocking queue head to thread since
503 wakeup_thread wakes the first thread in
505 wakeup_thread(thread
->bqp
);
508 /* Releases any waiting threads that are queued with queue_send -
511 static void queue_release_all_senders(struct event_queue
*q
)
516 for(i
= q
->read
; i
!= q
->write
; i
++)
518 struct thread_entry
**spp
=
519 &q
->send
->senders
[i
& QUEUE_LENGTH_MASK
];
523 queue_release_sender(spp
, 0);
529 /* Callback to do extra forced removal steps from sender list in addition
530 * to the normal blocking queue removal and priority dis-inherit */
531 static void queue_remove_sender_thread_cb(struct thread_entry
*thread
)
533 *((struct thread_entry
**)thread
->retval
) = NULL
;
534 thread
->wakeup_ext_cb
= NULL
;
538 /* Enables queue_send on the specified queue - caller allocates the extra
539 * data structure. Only queues which are taken to be owned by a thread should
540 * enable this however an official owner is not compulsory but must be
541 * specified for priority inheritance to operate.
543 * Use of queue_wait(_w_tmo) by multiple threads on a queue using synchronous
544 * messages results in an undefined order of message replies.
546 void queue_enable_queue_send(struct event_queue
*q
,
547 struct queue_sender_list
*send
,
548 struct thread_entry
*owner
)
550 int oldlevel
= disable_irq_save();
551 corelock_lock(&q
->cl
);
553 if(send
!= NULL
&& q
->send
== NULL
)
555 memset(send
, 0, sizeof(*send
));
556 #ifdef HAVE_PRIORITY_SCHEDULING
557 send
->blocker
.wakeup_protocol
= wakeup_priority_protocol_release
;
558 send
->blocker
.priority
= PRIORITY_IDLE
;
559 send
->blocker
.thread
= owner
;
561 q
->blocker_p
= &send
->blocker
;
566 corelock_unlock(&q
->cl
);
567 restore_irq(oldlevel
);
572 /* Unblock a blocked thread at a given event index */
573 static inline void queue_do_unblock_sender(struct queue_sender_list
*send
,
578 struct thread_entry
**spp
= &send
->senders
[i
];
582 queue_release_sender(spp
, 0);
587 /* Perform the auto-reply sequence */
588 static inline void queue_do_auto_reply(struct queue_sender_list
*send
)
590 if(send
&& send
->curr_sender
)
593 queue_release_sender(&send
->curr_sender
, 0);
597 /* Moves waiting thread's refrence from the senders array to the
598 * current_sender which represents the thread waiting for a reponse to the
599 * last message removed from the queue. This also protects the thread from
600 * being bumped due to overflow which would not be a valid action since its
601 * message _is_ being processed at this point. */
602 static inline void queue_do_fetch_sender(struct queue_sender_list
*send
,
607 struct thread_entry
**spp
= &send
->senders
[rd
];
611 /* Move thread reference from array to the next thread
612 that queue_reply will release */
613 send
->curr_sender
= *spp
;
614 (*spp
)->retval
= (intptr_t)spp
;
617 /* else message was posted asynchronously with queue_post */
621 /* Empty macros for when synchoronous sending is not made */
622 #define queue_release_all_senders(q)
623 #define queue_do_unblock_sender(send, i)
624 #define queue_do_auto_reply(send)
625 #define queue_do_fetch_sender(send, rd)
626 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
628 /* Queue must not be available for use during this call */
629 void queue_init(struct event_queue
*q
, bool register_queue
)
631 int oldlevel
= disable_irq_save();
635 corelock_lock(&all_queues
.cl
);
638 corelock_init(&q
->cl
);
642 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
643 q
->send
= NULL
; /* No message sending by default */
644 IF_PRIO( q
->blocker_p
= NULL
; )
649 if(all_queues
.count
>= MAX_NUM_QUEUES
)
651 panicf("queue_init->out of queues");
653 /* Add it to the all_queues array */
654 all_queues
.queues
[all_queues
.count
++] = q
;
655 corelock_unlock(&all_queues
.cl
);
658 restore_irq(oldlevel
);
661 /* Queue must not be available for use during this call */
662 void queue_delete(struct event_queue
*q
)
667 oldlevel
= disable_irq_save();
668 corelock_lock(&all_queues
.cl
);
669 corelock_lock(&q
->cl
);
671 /* Find the queue to be deleted */
672 for(i
= 0;i
< all_queues
.count
;i
++)
674 if(all_queues
.queues
[i
] == q
)
676 /* Move the following queues up in the list */
679 for(;i
< all_queues
.count
;i
++)
681 all_queues
.queues
[i
] = all_queues
.queues
[i
+1];
688 corelock_unlock(&all_queues
.cl
);
690 /* Release thread(s) waiting on queue head */
691 thread_queue_wake(&q
->queue
);
693 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
696 /* Release threads waiting for replies */
697 queue_release_all_senders(q
);
699 /* Reply to any dequeued message waiting for one */
700 queue_do_auto_reply(q
->send
);
703 IF_PRIO( q
->blocker_p
= NULL
; )
710 corelock_unlock(&q
->cl
);
711 restore_irq(oldlevel
);
714 /* NOTE: multiple threads waiting on a queue head cannot have a well-
715 defined release order if timeouts are used. If multiple threads must
716 access the queue head, use a dispatcher or queue_wait only. */
717 void queue_wait(struct event_queue
*q
, struct queue_event
*ev
)
722 #ifdef HAVE_PRIORITY_SCHEDULING
723 KERNEL_ASSERT(QUEUE_GET_THREAD(q
) == NULL
||
724 QUEUE_GET_THREAD(q
) == thread_get_current(),
725 "queue_wait->wrong thread\n");
728 oldlevel
= disable_irq_save();
729 corelock_lock(&q
->cl
);
732 queue_do_auto_reply(q
->send
);
734 if (q
->read
== q
->write
)
736 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
740 IF_COP( current
->obj_cl
= &q
->cl
; )
741 current
->bqp
= &q
->queue
;
743 block_thread(current
);
745 corelock_unlock(&q
->cl
);
748 oldlevel
= disable_irq_save();
749 corelock_lock(&q
->cl
);
751 /* A message that woke us could now be gone */
752 while (q
->read
== q
->write
);
755 rd
= q
->read
++ & QUEUE_LENGTH_MASK
;
758 /* Get data for a waiting thread if one */
759 queue_do_fetch_sender(q
->send
, rd
);
761 corelock_unlock(&q
->cl
);
762 restore_irq(oldlevel
);
765 void queue_wait_w_tmo(struct event_queue
*q
, struct queue_event
*ev
, int ticks
)
769 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
770 KERNEL_ASSERT(QUEUE_GET_THREAD(q
) == NULL
||
771 QUEUE_GET_THREAD(q
) == thread_get_current(),
772 "queue_wait_w_tmo->wrong thread\n");
775 oldlevel
= disable_irq_save();
776 corelock_lock(&q
->cl
);
779 queue_do_auto_reply(q
->send
);
781 if (q
->read
== q
->write
&& ticks
> 0)
783 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
785 IF_COP( current
->obj_cl
= &q
->cl
; )
786 current
->bqp
= &q
->queue
;
788 block_thread_w_tmo(current
, ticks
);
789 corelock_unlock(&q
->cl
);
793 oldlevel
= disable_irq_save();
794 corelock_lock(&q
->cl
);
797 /* no worry about a removed message here - status is checked inside
798 locks - perhaps verify if timeout or false alarm */
799 if (q
->read
!= q
->write
)
801 unsigned int rd
= q
->read
++ & QUEUE_LENGTH_MASK
;
803 /* Get data for a waiting thread if one */
804 queue_do_fetch_sender(q
->send
, rd
);
808 ev
->id
= SYS_TIMEOUT
;
811 corelock_unlock(&q
->cl
);
812 restore_irq(oldlevel
);
815 void queue_post(struct event_queue
*q
, long id
, intptr_t data
)
820 oldlevel
= disable_irq_save();
821 corelock_lock(&q
->cl
);
823 wr
= q
->write
++ & QUEUE_LENGTH_MASK
;
825 q
->events
[wr
].id
= id
;
826 q
->events
[wr
].data
= data
;
828 /* overflow protect - unblock any thread waiting at this index */
829 queue_do_unblock_sender(q
->send
, wr
);
831 /* Wakeup a waiting thread if any */
832 wakeup_thread(&q
->queue
);
834 corelock_unlock(&q
->cl
);
835 restore_irq(oldlevel
);
838 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
839 /* IRQ handlers are not allowed use of this function - we only aim to
840 protect the queue integrity by turning them off. */
841 intptr_t queue_send(struct event_queue
*q
, long id
, intptr_t data
)
846 oldlevel
= disable_irq_save();
847 corelock_lock(&q
->cl
);
849 wr
= q
->write
++ & QUEUE_LENGTH_MASK
;
851 q
->events
[wr
].id
= id
;
852 q
->events
[wr
].data
= data
;
856 struct queue_sender_list
*send
= q
->send
;
857 struct thread_entry
**spp
= &send
->senders
[wr
];
858 struct thread_entry
*current
= cores
[CURRENT_CORE
].running
;
862 /* overflow protect - unblock any thread waiting at this index */
863 queue_release_sender(spp
, 0);
866 /* Wakeup a waiting thread if any */
867 wakeup_thread(&q
->queue
);
869 /* Save thread in slot, add to list and wait for reply */
871 IF_COP( current
->obj_cl
= &q
->cl
; )
872 IF_PRIO( current
->blocker
= q
->blocker_p
; )
873 current
->wakeup_ext_cb
= queue_remove_sender_thread_cb
;
874 current
->retval
= (intptr_t)spp
;
875 current
->bqp
= &send
->list
;
877 block_thread(current
);
879 corelock_unlock(&q
->cl
);
882 return current
->retval
;
885 /* Function as queue_post if sending is not enabled */
886 wakeup_thread(&q
->queue
);
888 corelock_unlock(&q
->cl
);
889 restore_irq(oldlevel
);
894 #if 0 /* not used now but probably will be later */
895 /* Query if the last message dequeued was added by queue_send or not */
896 bool queue_in_queue_send(struct event_queue
*q
)
901 int oldlevel
= disable_irq_save();
902 corelock_lock(&q
->cl
);
905 in_send
= q
->send
&& q
->send
->curr_sender
;
908 corelock_unlock(&q
->cl
);
909 restore_irq(oldlevel
);
916 /* Replies with retval to the last dequeued message sent with queue_send */
917 void queue_reply(struct event_queue
*q
, intptr_t retval
)
919 if(q
->send
&& q
->send
->curr_sender
)
921 int oldlevel
= disable_irq_save();
922 corelock_lock(&q
->cl
);
923 /* Double-check locking */
924 IF_COP( if(q
->send
&& q
->send
->curr_sender
) )
926 queue_release_sender(&q
->send
->curr_sender
, retval
);
929 corelock_unlock(&q
->cl
);
930 restore_irq(oldlevel
);
934 bool queue_peek(struct event_queue
*q
, struct queue_event
*ev
)
936 if(q
->read
== q
->write
)
939 bool have_msg
= false;
941 int oldlevel
= disable_irq_save();
942 corelock_lock(&q
->cl
);
944 if(q
->read
!= q
->write
)
946 *ev
= q
->events
[q
->read
& QUEUE_LENGTH_MASK
];
950 corelock_unlock(&q
->cl
);
951 restore_irq(oldlevel
);
955 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
957 /* Poll queue to see if a message exists - careful in using the result if
958 * queue_remove_from_head is called when messages are posted - possibly use
959 * queue_wait_w_tmo(&q, 0) in that case or else a removed message that
960 * unsignals the queue may cause an unwanted block */
961 bool queue_empty(const struct event_queue
* q
)
963 return ( q
->read
== q
->write
);
966 void queue_clear(struct event_queue
* q
)
970 oldlevel
= disable_irq_save();
971 corelock_lock(&q
->cl
);
973 /* Release all threads waiting in the queue for a reply -
974 dequeued sent message will be handled by owning thread */
975 queue_release_all_senders(q
);
980 corelock_unlock(&q
->cl
);
981 restore_irq(oldlevel
);
984 void queue_remove_from_head(struct event_queue
*q
, long id
)
988 oldlevel
= disable_irq_save();
989 corelock_lock(&q
->cl
);
991 while(q
->read
!= q
->write
)
993 unsigned int rd
= q
->read
& QUEUE_LENGTH_MASK
;
995 if(q
->events
[rd
].id
!= id
)
1000 /* Release any thread waiting on this message */
1001 queue_do_unblock_sender(q
->send
, rd
);
1006 corelock_unlock(&q
->cl
);
1007 restore_irq(oldlevel
);
1011 * The number of events waiting in the queue.
1013 * @param struct of event_queue
1014 * @return number of events in the queue
1016 int queue_count(const struct event_queue
*q
)
1018 return q
->write
- q
->read
;
1021 int queue_broadcast(long id
, intptr_t data
)
1026 int oldlevel
= disable_irq_save();
1027 corelock_lock(&all_queues
.cl
);
1030 for(i
= 0;i
< all_queues
.count
;i
++)
1032 queue_post(all_queues
.queues
[i
], id
, data
);
1036 corelock_unlock(&all_queues
.cl
);
1037 restore_irq(oldlevel
);
1043 /****************************************************************************
1044 * Simple mutex functions ;)
1045 ****************************************************************************/
1047 /* Initialize a mutex object - call before any use and do not call again once
1048 * the object is available to other threads */
1049 void mutex_init(struct mutex
*m
)
1051 corelock_init(&m
->cl
);
1055 MUTEX_SET_THREAD(m
, NULL
);
1056 #ifdef HAVE_PRIORITY_SCHEDULING
1057 m
->blocker
.priority
= PRIORITY_IDLE
;
1058 m
->blocker
.wakeup_protocol
= wakeup_priority_protocol_transfer
;
1059 m
->no_preempt
= false;
1063 /* Gain ownership of a mutex object or block until it becomes free */
1064 void mutex_lock(struct mutex
*m
)
1066 const unsigned int core
= CURRENT_CORE
;
1067 struct thread_entry
*current
= cores
[core
].running
;
1069 if(current
== MUTEX_GET_THREAD(m
))
1071 /* current thread already owns this mutex */
1076 /* lock out other cores */
1077 corelock_lock(&m
->cl
);
1082 MUTEX_SET_THREAD(m
, current
);
1084 corelock_unlock(&m
->cl
);
1088 /* block until the lock is open... */
1089 IF_COP( current
->obj_cl
= &m
->cl
; )
1090 IF_PRIO( current
->blocker
= &m
->blocker
; )
1091 current
->bqp
= &m
->queue
;
1094 block_thread(current
);
1096 corelock_unlock(&m
->cl
);
1098 /* ...and turn control over to next thread */
1102 /* Release ownership of a mutex object - only owning thread must call this */
1103 void mutex_unlock(struct mutex
*m
)
1105 /* unlocker not being the owner is an unlocking violation */
1106 KERNEL_ASSERT(MUTEX_GET_THREAD(m
) == thread_get_current(),
1107 "mutex_unlock->wrong thread (%s != %s)\n",
1108 MUTEX_GET_THREAD(m
)->name
,
1109 thread_get_current()->name
);
1113 /* this thread still owns lock */
1118 /* lock out other cores */
1119 corelock_lock(&m
->cl
);
1121 /* transfer to next queued thread if any */
1122 if(m
->queue
== NULL
)
1124 /* no threads waiting - open the lock */
1125 MUTEX_SET_THREAD(m
, NULL
);
1127 corelock_unlock(&m
->cl
);
1132 const int oldlevel
= disable_irq_save();
1133 /* Tranfer of owning thread is handled in the wakeup protocol
1134 * if priorities are enabled otherwise just set it from the
1136 IFN_PRIO( MUTEX_SET_THREAD(m
, m
->queue
); )
1137 IF_PRIO( unsigned int result
= ) wakeup_thread(&m
->queue
);
1138 restore_irq(oldlevel
);
1140 corelock_unlock(&m
->cl
);
1142 #ifdef HAVE_PRIORITY_SCHEDULING
1143 if((result
& THREAD_SWITCH
) && !m
->no_preempt
)
1149 /****************************************************************************
1150 * Simpl-er mutex functions ;)
1151 ****************************************************************************/
1153 void spinlock_init(struct spinlock
*l
)
1155 corelock_init(&l
->cl
);
1160 void spinlock_lock(struct spinlock
*l
)
1162 const unsigned int core
= CURRENT_CORE
;
1163 struct thread_entry
*current
= cores
[core
].running
;
1165 if(l
->thread
== current
)
1167 /* current core already owns it */
1172 /* lock against other processor cores */
1173 corelock_lock(&l
->cl
);
1175 /* take ownership */
1176 l
->thread
= current
;
1179 void spinlock_unlock(struct spinlock
*l
)
1181 /* unlocker not being the owner is an unlocking violation */
1182 KERNEL_ASSERT(l
->thread
== thread_get_current(),
1183 "spinlock_unlock->wrong thread\n");
1187 /* this core still owns lock */
1196 corelock_unlock(&l
->cl
);
1198 #endif /* NUM_CORES > 1 */
1200 /****************************************************************************
1201 * Simple semaphore functions ;)
1202 ****************************************************************************/
1203 #ifdef HAVE_SEMAPHORE_OBJECTS
1204 void semaphore_init(struct semaphore
*s
, int max
, int start
)
1206 KERNEL_ASSERT(max
> 0 && start
>= 0 && start
<= max
,
1207 "semaphore_init->inv arg\n");
1211 corelock_init(&s
->cl
);
1214 void semaphore_wait(struct semaphore
*s
)
1216 struct thread_entry
*current
;
1218 corelock_lock(&s
->cl
);
1222 /* wait satisfied */
1223 corelock_unlock(&s
->cl
);
1227 /* too many waits - block until dequeued... */
1228 current
= cores
[CURRENT_CORE
].running
;
1230 IF_COP( current
->obj_cl
= &s
->cl
; )
1231 current
->bqp
= &s
->queue
;
1234 block_thread(current
);
1236 corelock_unlock(&s
->cl
);
1238 /* ...and turn control over to next thread */
1242 void semaphore_release(struct semaphore
*s
)
1244 IF_PRIO( unsigned int result
= THREAD_NONE
; )
1246 corelock_lock(&s
->cl
);
1248 if(s
->count
< s
->max
&& ++s
->count
<= 0)
1250 /* there should be threads in this queue */
1251 KERNEL_ASSERT(s
->queue
!= NULL
, "semaphore->wakeup\n");
1252 /* a thread was queued - wake it up */
1253 int oldlevel
= disable_irq_save();
1254 IF_PRIO( result
= ) wakeup_thread(&s
->queue
);
1255 restore_irq(oldlevel
);
1258 corelock_unlock(&s
->cl
);
1260 #ifdef HAVE_PRIORITY_SCHEDULING
1261 if(result
& THREAD_SWITCH
)
1265 #endif /* HAVE_SEMAPHORE_OBJECTS */
1267 /****************************************************************************
1268 * Simple event functions ;)
1269 ****************************************************************************/
1270 #ifdef HAVE_EVENT_OBJECTS
1271 void event_init(struct event
*e
, unsigned int flags
)
1273 e
->queues
[STATE_NONSIGNALED
] = NULL
;
1274 e
->queues
[STATE_SIGNALED
] = NULL
;
1275 e
->state
= flags
& STATE_SIGNALED
;
1276 e
->automatic
= (flags
& EVENT_AUTOMATIC
) ? 1 : 0;
1277 corelock_init(&e
->cl
);
1280 void event_wait(struct event
*e
, unsigned int for_state
)
1282 struct thread_entry
*current
;
1284 corelock_lock(&e
->cl
);
1286 if(e
->automatic
!= 0)
1288 /* wait for false always satisfied by definition
1289 or if it just changed to false */
1290 if(e
->state
== STATE_SIGNALED
|| for_state
== STATE_NONSIGNALED
)
1292 /* automatic - unsignal */
1293 e
->state
= STATE_NONSIGNALED
;
1294 corelock_unlock(&e
->cl
);
1297 /* block until state matches */
1299 else if(for_state
== e
->state
)
1301 /* the state being waited for is the current state */
1302 corelock_unlock(&e
->cl
);
1306 /* block until state matches what callers requests */
1307 current
= cores
[CURRENT_CORE
].running
;
1309 IF_COP( current
->obj_cl
= &e
->cl
; )
1310 current
->bqp
= &e
->queues
[for_state
];
1313 block_thread(current
);
1315 corelock_unlock(&e
->cl
);
1317 /* turn control over to next thread */
1321 void event_set_state(struct event
*e
, unsigned int state
)
1323 unsigned int result
;
1326 corelock_lock(&e
->cl
);
1328 if(e
->state
== state
)
1331 corelock_unlock(&e
->cl
);
1335 IF_PRIO( result
= THREAD_OK
; )
1337 oldlevel
= disable_irq_save();
1339 if(state
== STATE_SIGNALED
)
1341 if(e
->automatic
!= 0)
1343 /* no thread should have ever blocked for nonsignaled */
1344 KERNEL_ASSERT(e
->queues
[STATE_NONSIGNALED
] == NULL
,
1345 "set_event_state->queue[NS]:S\n");
1346 /* pass to next thread and keep unsignaled - "pulse" */
1347 result
= wakeup_thread(&e
->queues
[STATE_SIGNALED
]);
1348 e
->state
= (result
& THREAD_OK
) ? STATE_NONSIGNALED
: STATE_SIGNALED
;
1352 /* release all threads waiting for signaled */
1353 e
->state
= STATE_SIGNALED
;
1355 thread_queue_wake(&e
->queues
[STATE_SIGNALED
]);
1360 /* release all threads waiting for nonsignaled */
1362 /* no thread should have ever blocked if automatic */
1363 KERNEL_ASSERT(e
->queues
[STATE_NONSIGNALED
] == NULL
||
1364 e
->automatic
== 0, "set_event_state->queue[NS]:NS\n");
1366 e
->state
= STATE_NONSIGNALED
;
1368 thread_queue_wake(&e
->queues
[STATE_NONSIGNALED
]);
1371 restore_irq(oldlevel
);
1373 corelock_unlock(&e
->cl
);
1375 #ifdef HAVE_PRIORITY_SCHEDULING
1376 if(result
& THREAD_SWITCH
)
1380 #endif /* HAVE_EVENT_OBJECTS */
1383 #ifdef HAVE_WAKEUP_OBJECTS
1384 /****************************************************************************
1385 * Lightweight IRQ-compatible wakeup object
1388 /* Initialize the wakeup object */
1389 void wakeup_init(struct wakeup
*w
)
1393 IF_COP( corelock_init(&w
->cl
); )
1396 /* Wait for a signal blocking indefinitely or for a specified period */
1397 int wakeup_wait(struct wakeup
*w
, int timeout
)
1399 int ret
= OBJ_WAIT_SUCCEEDED
; /* Presume success */
1400 int oldlevel
= disable_irq_save();
1402 corelock_lock(&w
->cl
);
1404 if(w
->signalled
== 0 && timeout
!= TIMEOUT_NOBLOCK
)
1406 struct thread_entry
* current
= cores
[CURRENT_CORE
].running
;
1408 IF_COP( current
->obj_cl
= &w
->cl
; )
1409 current
->bqp
= &w
->queue
;
1411 if (timeout
!= TIMEOUT_BLOCK
)
1412 block_thread_w_tmo(current
, timeout
);
1414 block_thread(current
);
1416 corelock_unlock(&w
->cl
);
1419 oldlevel
= disable_irq_save();
1420 corelock_lock(&w
->cl
);
1423 if(w
->signalled
== 0)
1425 /* Timed-out or failed */
1426 ret
= (timeout
!= TIMEOUT_BLOCK
) ?
1427 OBJ_WAIT_TIMEDOUT
: OBJ_WAIT_FAILED
;
1430 w
->signalled
= 0; /* Reset */
1432 corelock_unlock(&w
->cl
);
1433 restore_irq(oldlevel
);
1438 /* Signal the thread waiting or leave the signal if the thread hasn't
1441 * returns THREAD_NONE or THREAD_OK
1443 int wakeup_signal(struct wakeup
*w
)
1445 int oldlevel
= disable_irq_save();
1448 corelock_lock(&w
->cl
);
1451 ret
= wakeup_thread(&w
->queue
);
1453 corelock_unlock(&w
->cl
);
1454 restore_irq(oldlevel
);
1458 #endif /* HAVE_WAKEUP_OBJECTS */