1 #ifndef CYGONCE_KERNEL_KAPI_H
2 #define CYGONCE_KERNEL_KAPI_H
4 /*==========================================================================
8 // Native API for Kernel
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 2002 Bart Veer
15 // Copyright (C) 2002 Nick Garnett
16 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
18 // eCos is free software; you can redistribute it and/or modify it under
19 // the terms of the GNU General Public License as published by the Free
20 // Software Foundation; either version 2 or (at your option) any later version.
22 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
23 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 // You should have received a copy of the GNU General Public License along
28 // with eCos; if not, write to the Free Software Foundation, Inc.,
29 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
31 // As a special exception, if other files instantiate templates or use macros
32 // or inline functions from this file, or you compile this file and link it
33 // with other works to produce a work based on this file, this file does not
34 // by itself cause the resulting work to be covered by the GNU General Public
35 // License. However the source code for this file must still be made available
36 // in accordance with section (3) of the GNU General Public License.
38 // This exception does not invalidate any other reasons why a work based on
39 // this file might be covered by the GNU General Public License.
41 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
42 // at http://sources.redhat.com/ecos/ecos-license/
43 // -------------------------------------------
44 //####ECOSGPLCOPYRIGHTEND####
45 //==========================================================================
46 //#####DESCRIPTIONBEGIN####
48 // Author(s): nickg, dsm
49 // Contributors: nickg
51 // Purpose: Native API for Kernel
52 // Description: This file describes the native API for using the kernel.
53 // It is essentially a set of C wrappers for the C++ class
55 // Usage: #include <cyg/kernel/kapi.h>
57 //####DESCRIPTIONEND####
59 //========================================================================*/
61 #include <pkgconf/system.h>
62 #include <pkgconf/kernel.h>
64 #ifdef CYGFUN_KERNEL_API_C
65 #include <cyg/infra/cyg_type.h>
67 /*---------------------------------------------------------------------------*/
73 /*---------------------------------------------------------------------------*/
74 /* The following are derived types, they may have different */
75 /* definitions from these depending on configuration. */
77 typedef CYG_ADDRWORD cyg_addrword_t; /* May hold pointer or word */
78 typedef cyg_addrword_t cyg_handle_t; /* Object handle */
79 typedef cyg_uint32 cyg_priority_t; /* type for priorities */
80 typedef cyg_int32 cyg_code_t; /* type for various codes */
81 typedef cyg_uint32 cyg_vector_t; /* Interrupt vector id */
82 typedef cyg_uint32 cyg_cpu_t; /* CPU id type */
84 typedef cyg_uint64 cyg_tick_count_t;
86 typedef int cyg_bool_t;
88 /* Exception handler function definition */
89 typedef void cyg_exception_handler_t(
91 cyg_code_t exception_number,
95 /*---------------------------------------------------------------------------*/
97 typedef struct cyg_thread cyg_thread;
100 typedef struct cyg_interrupt cyg_interrupt;
103 typedef struct cyg_counter cyg_counter;
106 typedef struct cyg_clock cyg_clock;
109 typedef struct cyg_alarm cyg_alarm;
112 typedef struct cyg_mbox cyg_mbox;
115 typedef struct cyg_sem_t cyg_sem_t;
118 typedef struct cyg_flag_t cyg_flag_t;
121 typedef struct cyg_mutex_t cyg_mutex_t;
124 typedef struct cyg_cond_t cyg_cond_t;
126 struct cyg_spinlock_t;
127 typedef struct cyg_spinlock_t cyg_spinlock_t;
129 /*---------------------------------------------------------------------------*/
130 /* Scheduler operations */
132 /* Starts scheduler with created threads. Never returns. */
133 void cyg_scheduler_start(void) __THROW CYGBLD_ATTRIB_NORET;
135 /* Lock and unlock the scheduler. When the scheduler is */
136 /* locked thread preemption is disabled. */
137 void cyg_scheduler_lock(void) __THROW;
139 void cyg_scheduler_unlock(void) __THROW;
141 /* Just like 'cyg_scheduler_lock()', but never take the lock higher than 1 */
142 /* Thus this call is safe even if the scheduler is already locked and a */
143 /* subsequent call to 'cyg_scheduler_unlock()' will completely unlock. */
144 void cyg_scheduler_safe_lock(void) __THROW;
146 /* Read the scheduler lock value. */
147 cyg_ucount32 cyg_scheduler_read_lock(void) __THROW;
149 /*---------------------------------------------------------------------------*/
150 /* Thread operations */
152 typedef void cyg_thread_entry_t(cyg_addrword_t);
154 void cyg_thread_create(
155 cyg_addrword_t sched_info, /* scheduling info (eg pri) */
156 cyg_thread_entry_t *entry, /* entry point function */
157 cyg_addrword_t entry_data, /* entry data */
158 char *name, /* optional thread name */
159 void *stack_base, /* stack base, NULL = alloc */
160 cyg_ucount32 stack_size, /* stack size, 0 = default */
161 cyg_handle_t *handle, /* returned thread handle */
162 cyg_thread *thread /* put thread here */
165 void cyg_thread_exit(void) __THROW;
167 /* It may be necessary to arrange for the victim to run for it to disappear */
168 cyg_bool_t cyg_thread_delete(cyg_handle_t thread) __THROW; /* false if NOT deleted */
170 void cyg_thread_suspend(cyg_handle_t thread) __THROW;
172 void cyg_thread_resume(cyg_handle_t thread) __THROW;
174 void cyg_thread_kill(cyg_handle_t thread) __THROW;
176 void cyg_thread_release(cyg_handle_t thread) __THROW;
178 void cyg_thread_yield(void) __THROW;
180 cyg_handle_t cyg_thread_self(void) __THROW;
182 cyg_handle_t cyg_thread_idle_thread(void) __THROW;
184 /* Priority manipulation */
186 void cyg_thread_set_priority(cyg_handle_t thread, cyg_priority_t priority ) __THROW;
188 cyg_priority_t cyg_thread_get_priority(cyg_handle_t thread) __THROW;
189 cyg_priority_t cyg_thread_get_current_priority(cyg_handle_t thread) __THROW;
191 /* Deadline scheduling control (optional) */
193 void cyg_thread_deadline_wait(
194 cyg_tick_count_t start_time, /* abs earliest start time */
195 cyg_tick_count_t run_time, /* worst case execution time */
196 cyg_tick_count_t deadline /* absolute deadline */
199 void cyg_thread_delay(cyg_tick_count_t delay) __THROW;
201 /* Stack information */
202 cyg_addrword_t cyg_thread_get_stack_base(cyg_handle_t thread) __THROW;
204 cyg_uint32 cyg_thread_get_stack_size(cyg_handle_t thread) __THROW;
206 #ifdef CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
207 cyg_uint32 cyg_thread_measure_stack_usage(cyg_handle_t thread) __THROW;
210 /*---------------------------------------------------------------------------*/
211 /* Thread enumeration and information */
219 cyg_priority_t set_pri;
220 cyg_priority_t cur_pri;
221 cyg_addrword_t stack_base;
222 cyg_uint32 stack_size;
223 cyg_uint32 stack_used;
226 cyg_bool_t cyg_thread_get_next( cyg_handle_t *thread, cyg_uint16 *id ) __THROW;
228 cyg_bool_t cyg_thread_get_info( cyg_handle_t thread,
230 cyg_thread_info *info ) __THROW;
232 cyg_uint16 cyg_thread_get_id( cyg_handle_t thread ) __THROW;
234 cyg_handle_t cyg_thread_find( cyg_uint16 id ) __THROW;
236 /*---------------------------------------------------------------------------*/
237 /* Per-thread Data */
239 #ifdef CYGVAR_KERNEL_THREADS_DATA
241 cyg_ucount32 cyg_thread_new_data_index(void) __THROW;
243 void cyg_thread_free_data_index(cyg_ucount32 index) __THROW;
245 CYG_ADDRWORD cyg_thread_get_data(cyg_ucount32 index) __THROW;
247 CYG_ADDRWORD *cyg_thread_get_data_ptr(cyg_ucount32 index) __THROW;
249 void cyg_thread_set_data(cyg_ucount32 index, CYG_ADDRWORD data) __THROW;
253 /*---------------------------------------------------------------------------*/
254 /* Thread destructors */
256 #ifdef CYGPKG_KERNEL_THREADS_DESTRUCTORS
258 typedef void (*cyg_thread_destructor_fn)(cyg_addrword_t);
260 cyg_bool_t cyg_thread_add_destructor( cyg_thread_destructor_fn fn,
261 cyg_addrword_t data ) __THROW;
262 cyg_bool_t cyg_thread_rem_destructor( cyg_thread_destructor_fn fn,
263 cyg_addrword_t data ) __THROW;
266 /*---------------------------------------------------------------------------*/
267 /* Exception handling. */
269 /* Replace current exception handler, this may apply to either the */
270 /* current thread, or to a global exception handler. The exception */
271 /* number may be ignored, or used to specify a particular handler. */
273 void cyg_exception_set_handler(
274 cyg_code_t exception_number,
275 cyg_exception_handler_t *new_handler,
276 cyg_addrword_t new_data,
277 cyg_exception_handler_t **old_handler,
278 cyg_addrword_t *old_data
281 /* Clear exception hander to default value */
282 void cyg_exception_clear_handler(
283 cyg_code_t exception_number
286 /* Invoke exception handler */
287 void cyg_exception_call_handler(
289 cyg_code_t exception_number,
290 cyg_addrword_t exception_info
294 /*---------------------------------------------------------------------------*/
295 /* Interrupt handling */
296 typedef void cyg_VSR_t(void);
297 typedef cyg_uint32 cyg_ISR_t(cyg_vector_t vector, cyg_addrword_t data);
298 typedef void cyg_DSR_t( cyg_vector_t vector,
300 cyg_addrword_t data);
305 CYG_ISR_HANDLED = 1, /* Interrupt was handled */
306 CYG_ISR_CALL_DSR = 2 /* Schedule DSR */
309 void cyg_interrupt_create(
310 cyg_vector_t vector, /* Vector to attach to */
311 cyg_priority_t priority, /* Queue priority */
312 cyg_addrword_t data, /* Data pointer */
313 cyg_ISR_t *isr, /* Interrupt Service Routine */
314 cyg_DSR_t *dsr, /* Deferred Service Routine */
315 cyg_handle_t *handle, /* returned handle */
316 cyg_interrupt *intr /* put interrupt here */
319 void cyg_interrupt_delete( cyg_handle_t interrupt ) __THROW;
321 void cyg_interrupt_attach( cyg_handle_t interrupt ) __THROW;
323 void cyg_interrupt_detach( cyg_handle_t interrupt ) __THROW;
325 /* VSR manipulation */
327 void cyg_interrupt_get_vsr(
328 cyg_vector_t vector, /* vector to get */
329 cyg_VSR_t **vsr /* vsr got */
332 void cyg_interrupt_set_vsr(
333 cyg_vector_t vector, /* vector to set */
334 cyg_VSR_t *vsr /* vsr to set */
337 /* CPU level interrupt mask */
338 void cyg_interrupt_disable(void) __THROW;
340 void cyg_interrupt_enable(void) __THROW;
342 /* Interrupt controller access */
343 void cyg_interrupt_mask(cyg_vector_t vector) __THROW;
344 void cyg_interrupt_mask_intunsafe(cyg_vector_t vector) __THROW;
346 void cyg_interrupt_unmask(cyg_vector_t vector) __THROW;
347 void cyg_interrupt_unmask_intunsafe(cyg_vector_t vector) __THROW;
349 void cyg_interrupt_acknowledge(cyg_vector_t vector) __THROW;
351 void cyg_interrupt_configure(
352 cyg_vector_t vector, /* vector to configure */
353 cyg_bool_t level, /* level or edge triggered */
354 cyg_bool_t up /* rising/faling edge, high/low level*/
357 void cyg_interrupt_set_cpu(
358 cyg_vector_t vector, /* vector to control */
359 cyg_cpu_t cpu /* CPU to set */
362 cyg_cpu_t cyg_interrupt_get_cpu(
363 cyg_vector_t vector /* vector to control */
366 /*---------------------------------------------------------------------------*/
367 /* Counters, Clocks and Alarms */
369 void cyg_counter_create(
370 cyg_handle_t *handle, /* returned counter handle */
371 cyg_counter *counter /* put counter here */
374 void cyg_counter_delete(cyg_handle_t counter) __THROW;
376 /* Return current value of counter */
377 cyg_tick_count_t cyg_counter_current_value(cyg_handle_t counter) __THROW;
379 /* Set new current value */
380 void cyg_counter_set_value(
381 cyg_handle_t counter,
382 cyg_tick_count_t new_value
385 /* Advance counter by one tick */
386 void cyg_counter_tick(cyg_handle_t counter) __THROW;
388 /* Advance counter by multiple ticks */
389 void cyg_counter_multi_tick(cyg_handle_t counter, cyg_tick_count_t _ticks) __THROW;
392 #define CYG_RESOLUTION_T_MEMBERS \
393 cyg_uint32 dividend; \
398 CYG_RESOLUTION_T_MEMBERS
401 /* Create a clock object */
402 void cyg_clock_create(
403 cyg_resolution_t resolution, /* Initial resolution */
404 cyg_handle_t *handle, /* Returned clock handle */
405 cyg_clock *clock /* put clock here */
408 void cyg_clock_delete(cyg_handle_t clock) __THROW;
410 /* convert a clock handle to a counter handle so we can use the */
411 /* counter API on it. */
412 void cyg_clock_to_counter(
414 cyg_handle_t *counter
417 void cyg_clock_set_resolution(
419 cyg_resolution_t resolution /* New resolution */
422 cyg_resolution_t cyg_clock_get_resolution(cyg_handle_t clock) __THROW;
424 /* handle of real time clock */
425 cyg_handle_t cyg_real_time_clock(void) __THROW;
427 /* returns value of real time clock's counter.
429 (cyg_clock_to_counter(cyg_real_time_clock(), &h),
430 cyg_counter_current_value(h)) */
431 cyg_tick_count_t cyg_current_time(void) __THROW;
433 /* Alarm handler function */
434 typedef void cyg_alarm_t(cyg_handle_t alarm, cyg_addrword_t data);
436 void cyg_alarm_create(
437 cyg_handle_t counter, /* Attached to this counter */
438 cyg_alarm_t *alarmfn, /* Call-back function */
439 cyg_addrword_t data, /* Call-back data */
440 cyg_handle_t *handle, /* Returned alarm object */
441 cyg_alarm *alarm /* put alarm here */
444 /* Disable alarm, detach from counter and invalidate handles */
445 void cyg_alarm_delete( cyg_handle_t alarm) __THROW;
447 void cyg_alarm_initialize(
449 cyg_tick_count_t trigger, /* Absolute trigger time */
450 cyg_tick_count_t interval /* Relative retrigger interval */
453 void cyg_alarm_get_times(
455 cyg_tick_count_t *trigger, /* Next trigger time */
456 cyg_tick_count_t *interval /* Current interval */
459 void cyg_alarm_enable( cyg_handle_t alarm ) __THROW;
461 void cyg_alarm_disable( cyg_handle_t alarm ) __THROW;
463 /*---------------------------------------------------------------------------*/
465 void cyg_mbox_create(
466 cyg_handle_t *handle,
470 void cyg_mbox_delete(cyg_handle_t mbox) __THROW;
472 void *cyg_mbox_get(cyg_handle_t mbox) __THROW;
474 #ifdef CYGFUN_KERNEL_THREADS_TIMER
475 void *cyg_mbox_timed_get(
477 cyg_tick_count_t abstime
481 void *cyg_mbox_tryget(cyg_handle_t mbox) __THROW;
483 void *cyg_mbox_peek_item(cyg_handle_t mbox) __THROW;
485 #ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
486 cyg_bool_t cyg_mbox_put(cyg_handle_t mbox, void *item) __THROW;
487 #ifdef CYGFUN_KERNEL_THREADS_TIMER
488 cyg_bool_t cyg_mbox_timed_put(
491 cyg_tick_count_t abstime
496 cyg_bool_t cyg_mbox_tryput(cyg_handle_t mbox, void *item) __THROW;
498 cyg_count32 cyg_mbox_peek(cyg_handle_t mbox) __THROW;
500 cyg_bool_t cyg_mbox_waiting_to_get(cyg_handle_t mbox) __THROW;
502 cyg_bool_t cyg_mbox_waiting_to_put(cyg_handle_t mbox) __THROW;
505 /*-----------------------------------------------------------------------*/
508 /* These definitions are found in the "memalloc" package as this is */
509 /* where the implementation lives. */
511 #ifdef CYGPKG_MEMALLOC
512 # include <cyg/memalloc/kapi.h>
515 /*---------------------------------------------------------------------------*/
518 void cyg_semaphore_init(
519 cyg_sem_t *sem, /* Semaphore to init */
520 cyg_count32 val /* Initial semaphore value */
523 void cyg_semaphore_destroy( cyg_sem_t *sem ) __THROW;
525 cyg_bool_t cyg_semaphore_wait( cyg_sem_t *sem ) __THROW;
527 #ifdef CYGFUN_KERNEL_THREADS_TIMER
528 cyg_bool_t cyg_semaphore_timed_wait(
530 cyg_tick_count_t abstime
534 cyg_bool_t cyg_semaphore_trywait( cyg_sem_t *sem ) __THROW;
536 void cyg_semaphore_post( cyg_sem_t *sem ) __THROW;
538 void cyg_semaphore_peek( cyg_sem_t *sem, cyg_count32 *val ) __THROW;
540 /*---------------------------------------------------------------------------*/
543 typedef cyg_uint32 cyg_flag_value_t;
544 typedef cyg_uint8 cyg_flag_mode_t;
545 #define CYG_FLAG_WAITMODE_AND ((cyg_flag_mode_t)0) /* all bits must be set */
546 #define CYG_FLAG_WAITMODE_OR ((cyg_flag_mode_t)2) /* any bit must be set */
547 #define CYG_FLAG_WAITMODE_CLR ((cyg_flag_mode_t)1) /* clear when satisfied */
550 cyg_flag_t *flag /* Flag to init */
553 void cyg_flag_destroy( cyg_flag_t *flag ) __THROW;
555 /* bitwise-or in the bits in value; awaken any waiting tasks whose
556 condition is now satisfied */
557 void cyg_flag_setbits( cyg_flag_t *flag, cyg_flag_value_t value) __THROW;
559 /* bitwise-and with the the bits in value; this clears the bits which
560 are not set in value. No waiting task can be awoken. */
561 void cyg_flag_maskbits( cyg_flag_t *flag, cyg_flag_value_t value) __THROW;
563 /* wait for the flag value to match the pattern, according to the mode.
564 If mode includes CLR, set the flag value to zero when
565 our pattern is matched. The return value is that which matched
566 the request, or zero for an error/timeout return.
567 Value must not itself be zero. */
568 cyg_flag_value_t cyg_flag_wait( cyg_flag_t *flag,
569 cyg_flag_value_t pattern,
570 cyg_flag_mode_t mode ) __THROW;
572 #ifdef CYGFUN_KERNEL_THREADS_TIMER
573 cyg_flag_value_t cyg_flag_timed_wait( cyg_flag_t *flag,
574 cyg_flag_value_t pattern,
575 cyg_flag_mode_t mode,
576 cyg_tick_count_t abstime ) __THROW;
580 cyg_flag_value_t cyg_flag_poll( cyg_flag_t *flag,
581 cyg_flag_value_t pattern,
582 cyg_flag_mode_t mode ) __THROW;
584 cyg_flag_value_t cyg_flag_peek( cyg_flag_t *flag ) __THROW;
586 cyg_bool_t cyg_flag_waiting( cyg_flag_t *flag ) __THROW;
588 /*---------------------------------------------------------------------------*/
591 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
592 enum cyg_mutex_protocol
594 CYG_MUTEX_NONE = 0, // no inversion protocol
595 CYG_MUTEX_INHERIT, // priority inheritance protocol
596 CYG_MUTEX_CEILING // priority ceiling protocol
601 cyg_mutex_t *mutex /* Mutex to init */
604 void cyg_mutex_destroy( cyg_mutex_t *mutex ) __THROW;
606 cyg_bool_t cyg_mutex_lock( cyg_mutex_t *mutex ) __THROW;
608 cyg_bool_t cyg_mutex_trylock( cyg_mutex_t *mutex ) __THROW;
610 void cyg_mutex_unlock( cyg_mutex_t *mutex ) __THROW;
612 void cyg_mutex_release( cyg_mutex_t *mutex ) __THROW;
614 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
615 void cyg_mutex_set_ceiling( cyg_mutex_t *mutex, cyg_priority_t priority ) __THROW;
618 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
619 void cyg_mutex_set_protocol ( cyg_mutex_t *mutex, enum cyg_mutex_protocol protocol ) __THROW;
622 /*---------------------------------------------------------------------------*/
623 /* Condition Variables */
626 cyg_cond_t *cond, /* condition variable to init */
627 cyg_mutex_t *mutex /* associated mutex */
630 void cyg_cond_destroy( cyg_cond_t *cond ) __THROW;
632 cyg_bool_t cyg_cond_wait( cyg_cond_t *cond ) __THROW;
634 void cyg_cond_signal( cyg_cond_t *cond ) __THROW;
636 void cyg_cond_broadcast( cyg_cond_t *cond ) __THROW;
638 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
639 cyg_bool_t cyg_cond_timed_wait(
641 cyg_tick_count_t abstime
645 /*---------------------------------------------------------------------------*/
648 void cyg_spinlock_init(
649 cyg_spinlock_t *lock, /* spinlock to initialize */
650 cyg_bool_t locked /* init locked or unlocked */
653 void cyg_spinlock_destroy( cyg_spinlock_t *lock ) __THROW;
655 void cyg_spinlock_spin( cyg_spinlock_t *lock ) __THROW;
657 void cyg_spinlock_clear( cyg_spinlock_t *lock ) __THROW;
659 cyg_bool_t cyg_spinlock_try( cyg_spinlock_t *lock ) __THROW;
661 cyg_bool_t cyg_spinlock_test( cyg_spinlock_t *lock ) __THROW;
663 void cyg_spinlock_spin_intsave( cyg_spinlock_t *lock,
664 cyg_addrword_t *istate ) __THROW;
666 void cyg_spinlock_clear_intsave( cyg_spinlock_t *lock,
667 cyg_addrword_t istate ) __THROW;
669 /*---------------------------------------------------------------------------*/
674 /*---------------------------------------------------------------------------*/
676 #include <cyg/kernel/kapidata.h>
678 /*---------------------------------------------------------------------------*/
680 #endif /* CYGFUN_KERNEL_API_C */
681 #endif /* CYGONCE_KERNEL_KAPI_H */