OpenCores
URL https://opencores.org/ocsvn/aemb/aemb/trunk

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [cc/] [aemb/] [hook.hh] - Blame information for rev 139

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 139 sybreon
/* $Id: hook.hh,v 1.7 2008-04-26 19:31:35 sybreon Exp $
2 107 sybreon
**
3
** AEMB2 HI-PERFORMANCE CPU
4
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap 
5
**
6
** This file is part of AEMB.
7
**
8
** AEMB is free software: you can redistribute it and/or modify it
9
** under the terms of the GNU Lesser General Public License as
10
** published by the Free Software Foundation, either version 3 of the
11
** License, or (at your option) any later version.
12
**
13
** AEMB is distributed in the hope that it will be useful, but WITHOUT
14
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
16
** Public License for more details.
17
**
18
** You should have received a copy of the GNU Lesser General Public
19
** License along with AEMB. If not, see .
20
*/
21
 
22
/**
23
   Basic begin/end hooks
24
   @file hook.hh
25
 
26 116 sybreon
   These routines hook themselves onto parts of the main programme to
27
   enable the hardware threads to work properly.
28 107 sybreon
 */
29
 
30
#include "aemb/stack.hh"
31
#include "aemb/heap.hh"
32
#include "aemb/thread.hh"
33
 
34
#ifndef AEMB_HOOK_HH
35
#define AEMB_HOOK_HH
36
 
37 139 sybreon
#ifdef __cplusplus
38 107 sybreon
namespace aemb {
39 121 sybreon
  extern "C" {
40 139 sybreon
#endif
41
 
42 121 sybreon
    void _program_init();
43
    void _program_clean();
44 135 sybreon
 
45
    // newlib locks
46
    void __malloc_lock(struct _reent *reent);
47
    void __malloc_unlock(struct _reent *reent);
48
    //void __env_lock(struct _reent *reent);
49
    //void __env_unlock(struct _reent *reent);
50
 
51 139 sybreon
#ifdef __cplusplus
52 135 sybreon
  }
53 139 sybreon
#endif
54 135 sybreon
 
55 107 sybreon
  /**
56 116 sybreon
     Finalisation hook
57
 
58
     This function executes during the shutdown phase after the
59
     finalisation routine is called. It will merge the changes made
60
     during initialisation.
61 121 sybreon
  */
62 135 sybreon
 
63 111 sybreon
  void _program_clean()
64 107 sybreon
  {
65 121 sybreon
    waitMutex(); // enter critical section
66
 
67 116 sybreon
    // unify the stack backwards for thread 1
68 135 sybreon
    if (isThread0())
69 121 sybreon
      {
70 135 sybreon
        // TODO: Unify the stack
71 121 sybreon
        setStack(getStack() + (getStackSize() >> 1));
72 128 sybreon
      }
73 121 sybreon
 
74
    signalMutex(); // exit critical section
75 107 sybreon
  }
76
 
77
  /**
78 116 sybreon
     Initialisation hook
79 107 sybreon
 
80 116 sybreon
     This function executes during the startup phase before the
81
     initialisation routine is called. It splits the stack between the
82 135 sybreon
     threads. For now, it will lock up T0 for compatibility purposes.
83 121 sybreon
  */
84 135 sybreon
 
85 111 sybreon
  void _program_init()
86 107 sybreon
  {
87 121 sybreon
    waitMutex(); // enter critical section
88
 
89 116 sybreon
    // split and shift the stack for thread 1
90 135 sybreon
    if (isThread0()) // main thread
91 116 sybreon
      {
92 135 sybreon
        // NOTE: Dupe the stack otherwise it will FAIL!
93
        dupStack((int *)setStack(getStack() - (getStackSize() >> 1)),
94
                  (int *)getStack(),
95
                  (int *)getStackTop);
96
        signalMutex(); // exit critical section
97 139 sybreon
        while (1) asm volatile ("nop"); // lock thread
98 116 sybreon
      }
99 121 sybreon
 
100
    signalMutex(); // exit critical section
101 107 sybreon
  }
102
 
103 128 sybreon
  semaphore __malloc_mutex = 1;
104
 
105 116 sybreon
  /**
106
     Heap Lock
107
 
108
     This function is called during malloc() to lock out the shared
109
     heap to avoid data corruption.
110
   */
111 135 sybreon
 
112
  void __malloc_lock(struct _reent *reent)
113 116 sybreon
  {
114 128 sybreon
    waitMutex();
115 116 sybreon
  }
116
 
117
  /**
118
     Heap Unlock
119
 
120
     This function is called during malloc() to indicate that the
121
     shared heap is now available for another thread.
122
  */
123 135 sybreon
 
124
  void __malloc_unlock(struct _reent *reent)
125 116 sybreon
  {
126 121 sybreon
    signalMutex();
127 116 sybreon
  }
128 121 sybreon
 
129 139 sybreon
#ifdef __cplusplus
130 116 sybreon
}
131 139 sybreon
#endif
132 116 sybreon
 
133 107 sybreon
#endif
134
 
135 116 sybreon
#ifndef __OPTIMIZE__
136
// The main programme needs to be compiled with optimisations turned
137 135 sybreon
// on (at least -O1). If not, the MUTEX value will be written to the
138
// same RAM location, giving both threads the same value.
139 116 sybreon
OPTIMISATION_REQUIRED XXX
140
#endif
141
 
142 107 sybreon
/*
143
  $Log: not supported by cvs2svn $
144 139 sybreon
  Revision 1.6  2008/04/26 18:04:31  sybreon
145
  Updated software to freeze T0 and run T1.
146
 
147 135 sybreon
  Revision 1.5  2008/04/23 14:19:39  sybreon
148
  Fixed minor bugs.
149
  Initial use of hardware mutex.
150
 
151 128 sybreon
  Revision 1.4  2008/04/20 16:35:53  sybreon
152
  Added C/C++ compatible #ifdef statements
153
 
154 121 sybreon
  Revision 1.3  2008/04/12 13:46:02  sybreon
155
  Added malloc() lock and unlock routines
156
 
157 116 sybreon
  Revision 1.2  2008/04/11 15:20:31  sybreon
158
  added static assert hack
159
 
160 111 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
161
  Added new C++ files
162
 
163 107 sybreon
*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.