Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Fork of mbed-rtos by mbed official

Committer:
emilmont
Date:
Fri Nov 23 09:57:31 2012 +0000
Revision:
6:350b53afb889
Child:
11:db1fc233faa9
Merge RTOS C++ API and RTX under the same library; Update RTX to version 4.60; Add proper Thread destructor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 6:350b53afb889 1 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 2 * RL-ARM - RTX
emilmont 6:350b53afb889 3 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 4 * Name: RT_MEMBOX.C
emilmont 6:350b53afb889 5 * Purpose: Interface functions for fixed memory block management system
emilmont 6:350b53afb889 6 * Rev.: V4.60
emilmont 6:350b53afb889 7 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 8 *
emilmont 6:350b53afb889 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
emilmont 6:350b53afb889 10 * All rights reserved.
emilmont 6:350b53afb889 11 * Redistribution and use in source and binary forms, with or without
emilmont 6:350b53afb889 12 * modification, are permitted provided that the following conditions are met:
emilmont 6:350b53afb889 13 * - Redistributions of source code must retain the above copyright
emilmont 6:350b53afb889 14 * notice, this list of conditions and the following disclaimer.
emilmont 6:350b53afb889 15 * - Redistributions in binary form must reproduce the above copyright
emilmont 6:350b53afb889 16 * notice, this list of conditions and the following disclaimer in the
emilmont 6:350b53afb889 17 * documentation and/or other materials provided with the distribution.
emilmont 6:350b53afb889 18 * - Neither the name of ARM nor the names of its contributors may be used
emilmont 6:350b53afb889 19 * to endorse or promote products derived from this software without
emilmont 6:350b53afb889 20 * specific prior written permission.
emilmont 6:350b53afb889 21 *
emilmont 6:350b53afb889 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
emilmont 6:350b53afb889 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
emilmont 6:350b53afb889 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
emilmont 6:350b53afb889 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
emilmont 6:350b53afb889 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
emilmont 6:350b53afb889 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
emilmont 6:350b53afb889 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
emilmont 6:350b53afb889 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
emilmont 6:350b53afb889 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
emilmont 6:350b53afb889 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
emilmont 6:350b53afb889 32 * POSSIBILITY OF SUCH DAMAGE.
emilmont 6:350b53afb889 33 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 34
emilmont 6:350b53afb889 35 #include "rt_TypeDef.h"
emilmont 6:350b53afb889 36 #include "RTX_Config.h"
emilmont 6:350b53afb889 37 #include "rt_System.h"
emilmont 6:350b53afb889 38 #include "rt_MemBox.h"
emilmont 6:350b53afb889 39 #include "rt_HAL_CM.h"
emilmont 6:350b53afb889 40
emilmont 6:350b53afb889 41 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 42 * Global Functions
emilmont 6:350b53afb889 43 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 44
emilmont 6:350b53afb889 45
emilmont 6:350b53afb889 46 /*--------------------------- _init_box -------------------------------------*/
emilmont 6:350b53afb889 47
emilmont 6:350b53afb889 48 int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
emilmont 6:350b53afb889 49 /* Initialize memory block system, returns 0 if OK, 1 if fails. */
emilmont 6:350b53afb889 50 void *end;
emilmont 6:350b53afb889 51 void *blk;
emilmont 6:350b53afb889 52 void *next;
emilmont 6:350b53afb889 53 U32 sizeof_bm;
emilmont 6:350b53afb889 54
emilmont 6:350b53afb889 55 /* Create memory structure. */
emilmont 6:350b53afb889 56 if (blk_size & BOX_ALIGN_8) {
emilmont 6:350b53afb889 57 /* Memory blocks 8-byte aligned. */
emilmont 6:350b53afb889 58 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
emilmont 6:350b53afb889 59 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
emilmont 6:350b53afb889 60 }
emilmont 6:350b53afb889 61 else {
emilmont 6:350b53afb889 62 /* Memory blocks 4-byte aligned. */
emilmont 6:350b53afb889 63 blk_size = (blk_size + 3) & ~3;
emilmont 6:350b53afb889 64 sizeof_bm = sizeof (struct OS_BM);
emilmont 6:350b53afb889 65 }
emilmont 6:350b53afb889 66 if (blk_size == 0) {
emilmont 6:350b53afb889 67 return (1);
emilmont 6:350b53afb889 68 }
emilmont 6:350b53afb889 69 if ((blk_size + sizeof_bm) > box_size) {
emilmont 6:350b53afb889 70 return (1);
emilmont 6:350b53afb889 71 }
emilmont 6:350b53afb889 72 /* Create a Memory structure. */
emilmont 6:350b53afb889 73 blk = ((U8 *) box_mem) + sizeof_bm;
emilmont 6:350b53afb889 74 ((P_BM) box_mem)->free = blk;
emilmont 6:350b53afb889 75 end = ((U8 *) box_mem) + box_size;
emilmont 6:350b53afb889 76 ((P_BM) box_mem)->end = end;
emilmont 6:350b53afb889 77 ((P_BM) box_mem)->blk_size = blk_size;
emilmont 6:350b53afb889 78
emilmont 6:350b53afb889 79 /* Link all free blocks using offsets. */
emilmont 6:350b53afb889 80 end = ((U8 *) end) - blk_size;
emilmont 6:350b53afb889 81 while (1) {
emilmont 6:350b53afb889 82 next = ((U8 *) blk) + blk_size;
emilmont 6:350b53afb889 83 if (next > end) break;
emilmont 6:350b53afb889 84 *((void **)blk) = next;
emilmont 6:350b53afb889 85 blk = next;
emilmont 6:350b53afb889 86 }
emilmont 6:350b53afb889 87 /* end marker */
emilmont 6:350b53afb889 88 *((void **)blk) = 0;
emilmont 6:350b53afb889 89 return (0);
emilmont 6:350b53afb889 90 }
emilmont 6:350b53afb889 91
emilmont 6:350b53afb889 92 /*--------------------------- rt_alloc_box ----------------------------------*/
emilmont 6:350b53afb889 93
emilmont 6:350b53afb889 94 void *rt_alloc_box (void *box_mem) {
emilmont 6:350b53afb889 95 /* Allocate a memory block and return start address. */
emilmont 6:350b53afb889 96 void **free;
emilmont 6:350b53afb889 97 #ifndef __USE_EXCLUSIVE_ACCESS
emilmont 6:350b53afb889 98 int irq_dis;
emilmont 6:350b53afb889 99
emilmont 6:350b53afb889 100 irq_dis = __disable_irq ();
emilmont 6:350b53afb889 101 free = ((P_BM) box_mem)->free;
emilmont 6:350b53afb889 102 if (free) {
emilmont 6:350b53afb889 103 ((P_BM) box_mem)->free = *free;
emilmont 6:350b53afb889 104 }
emilmont 6:350b53afb889 105 if (!irq_dis) __enable_irq ();
emilmont 6:350b53afb889 106 #else
emilmont 6:350b53afb889 107 do {
emilmont 6:350b53afb889 108 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
emilmont 6:350b53afb889 109 __clrex();
emilmont 6:350b53afb889 110 break;
emilmont 6:350b53afb889 111 }
emilmont 6:350b53afb889 112 } while (__strex((U32)*free, &((P_BM) box_mem)->free));
emilmont 6:350b53afb889 113 #endif
emilmont 6:350b53afb889 114 return (free);
emilmont 6:350b53afb889 115 }
emilmont 6:350b53afb889 116
emilmont 6:350b53afb889 117
emilmont 6:350b53afb889 118 /*--------------------------- _calloc_box -----------------------------------*/
emilmont 6:350b53afb889 119
emilmont 6:350b53afb889 120 void *_calloc_box (void *box_mem) {
emilmont 6:350b53afb889 121 /* Allocate a 0-initialized memory block and return start address. */
emilmont 6:350b53afb889 122 void *free;
emilmont 6:350b53afb889 123 U32 *p;
emilmont 6:350b53afb889 124 U32 i;
emilmont 6:350b53afb889 125
emilmont 6:350b53afb889 126 free = _alloc_box (box_mem);
emilmont 6:350b53afb889 127 if (free) {
emilmont 6:350b53afb889 128 p = free;
emilmont 6:350b53afb889 129 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
emilmont 6:350b53afb889 130 *p = 0;
emilmont 6:350b53afb889 131 p++;
emilmont 6:350b53afb889 132 }
emilmont 6:350b53afb889 133 }
emilmont 6:350b53afb889 134 return (free);
emilmont 6:350b53afb889 135 }
emilmont 6:350b53afb889 136
emilmont 6:350b53afb889 137
emilmont 6:350b53afb889 138 /*--------------------------- rt_free_box -----------------------------------*/
emilmont 6:350b53afb889 139
emilmont 6:350b53afb889 140 int rt_free_box (void *box_mem, void *box) {
emilmont 6:350b53afb889 141 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
emilmont 6:350b53afb889 142 #ifndef __USE_EXCLUSIVE_ACCESS
emilmont 6:350b53afb889 143 int irq_dis;
emilmont 6:350b53afb889 144 #endif
emilmont 6:350b53afb889 145
emilmont 6:350b53afb889 146 if (box < box_mem || box >= ((P_BM) box_mem)->end) {
emilmont 6:350b53afb889 147 return (1);
emilmont 6:350b53afb889 148 }
emilmont 6:350b53afb889 149
emilmont 6:350b53afb889 150 #ifndef __USE_EXCLUSIVE_ACCESS
emilmont 6:350b53afb889 151 irq_dis = __disable_irq ();
emilmont 6:350b53afb889 152 *((void **)box) = ((P_BM) box_mem)->free;
emilmont 6:350b53afb889 153 ((P_BM) box_mem)->free = box;
emilmont 6:350b53afb889 154 if (!irq_dis) __enable_irq ();
emilmont 6:350b53afb889 155 #else
emilmont 6:350b53afb889 156 do {
emilmont 6:350b53afb889 157 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
emilmont 6:350b53afb889 158 } while (__strex ((U32)box, &((P_BM) box_mem)->free));
emilmont 6:350b53afb889 159 #endif
emilmont 6:350b53afb889 160 return (0);
emilmont 6:350b53afb889 161 }
emilmont 6:350b53afb889 162
emilmont 6:350b53afb889 163 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 164 * end of file
emilmont 6:350b53afb889 165 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 166