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

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [cc/] [aemb/] [thread.hh] - Blame information for rev 145

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

Line No. Rev Author Line
1 145 sybreon
/* $Id: thread.hh,v 1.9 2008-04-27 16:33:42 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 145 sybreon
** under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, either version 3 of the License, or
11
** (at your option) any later version.
12 107 sybreon
**
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 145 sybreon
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
** License for more details.
17 107 sybreon
**
18 145 sybreon
** You should have received a copy of the GNU General Public License
19
** along with AEMB.  If not, see .
20 107 sybreon
*/
21
 
22
/**
23
   Basic thread functions
24
   @file thread.hh
25
 
26
   These functions deal with the various hardware threads. It also
27
   provides simple mechanisms for toggling semaphores.
28
 */
29
 
30
#include "aemb/msr.hh"
31
 
32
#ifndef AEMB_THREAD_HH
33
#define AEMB_THREAD_HH
34
 
35 139 sybreon
#ifdef __cplusplus
36 107 sybreon
namespace aemb {
37 139 sybreon
#endif
38 107 sybreon
 
39
  /**
40 117 sybreon
     Checks to see if currently executing Thread 1
41
     @return true if is Thread 1
42 107 sybreon
  */
43
 
44 139 sybreon
  inline int isThread1()
45 107 sybreon
  {
46 121 sybreon
    int rmsr = getMSR();
47
    return ((rmsr & MSR_HTX) && (rmsr & MSR_PHA));
48 107 sybreon
  }
49
 
50
  /**
51 117 sybreon
     Checks to see if currently executing Thread 0
52
     @return true if is Thread 0
53 107 sybreon
  */
54
 
55 139 sybreon
  inline int isThread0()
56 107 sybreon
  {
57 121 sybreon
    int rmsr = getMSR();
58
    return ((rmsr & MSR_HTX) && (!(rmsr & MSR_PHA)));
59 107 sybreon
  }
60
 
61
  /**
62 121 sybreon
     Checks to see if it is multi-threaded or not.
63 117 sybreon
     @return true if thread capable
64 107 sybreon
  */
65 139 sybreon
  inline int isThreaded()
66 107 sybreon
  {
67 121 sybreon
    int rmsr = getMSR();
68
    return (rmsr & MSR_HTX);
69 107 sybreon
  }
70 121 sybreon
 
71
  /**
72
     Hardware Mutex Signal.
73
     Unlock the hardware mutex, which is unlocked on reset.
74
   */
75 145 sybreon
  inline void _mtx_free()
76 121 sybreon
  {
77
    int tmp;
78 145 sybreon
    asm volatile ("msrclr %0, %1":"=r"(tmp):"K"(MSR_MTX));
79 121 sybreon
  }
80
 
81
  /**
82
     Hardware Mutex Wait.
83 128 sybreon
 
84
     Waits until the hardware mutex is unlocked. This should be used
85
     as part of a larger software mutex mechanism.
86 121 sybreon
   */
87 145 sybreon
  inline void _mtx_lock()
88 121 sybreon
  {
89
    int rmsr;
90
    do
91
      {
92 145 sybreon
        asm volatile ("msrset %0, %1":"=r"(rmsr):"K"(MSR_MTX));
93 121 sybreon
      }
94
    while (rmsr & MSR_MTX);
95
  }
96
 
97 107 sybreon
  // TODO: Extend this library to include threading mechanisms such as
98
  // semaphores, mutexes and such.
99 121 sybreon
 
100 107 sybreon
  /**
101 128 sybreon
     Semaphore struct.
102 117 sybreon
     Presently implemented as software solution but a hardware one may be
103 121 sybreon
     required as the threads are hardware.
104 107 sybreon
  */
105
 
106 145 sybreon
  typedef int semaphore;
107 128 sybreon
 
108
  /**
109 145 sybreon
     Software Semaphore Signal.
110
 
111
     Increment the semaphore and run. This is a software mechanism.
112 128 sybreon
  */
113 145 sybreon
  inline void signal(volatile semaphore _sem)
114
  {
115
    _mtx_lock();
116
    _sem++;
117
    _mtx_free();
118
  }
119 107 sybreon
 
120 128 sybreon
  /**
121 145 sybreon
     Software Semaphore Wait.
122
 
123
     Decrement the semaphore and block if < 0. This is a software
124
     mechanism.
125 128 sybreon
  */
126 145 sybreon
  inline void wait(volatile semaphore _sem)
127
  {
128
    _mtx_lock();
129
    _sem--;
130
    _mtx_free();
131
    while (_sem < 0);
132
  }
133 117 sybreon
 
134 128 sybreon
  semaphore __mutex_rendezvous0 = 0; ///< internal rendezvous mutex
135
  semaphore __mutex_rendezvous1 = 1; ///< internal rendezvous mutex
136 117 sybreon
 
137
  /**
138
     Implements a simple rendezvous mechanism
139
   */
140
 
141 145 sybreon
  inline void rendezvous()
142 117 sybreon
  {
143
    if (isThread1())
144
      {
145 128 sybreon
        wait(__mutex_rendezvous0);
146
        signal(__mutex_rendezvous1);
147 117 sybreon
      }
148
    else
149
      {
150 128 sybreon
        signal(__mutex_rendezvous0);
151
        wait(__mutex_rendezvous1);
152 117 sybreon
      }
153
  }
154 121 sybreon
 
155 139 sybreon
#ifdef __cplusplus
156 107 sybreon
}
157 139 sybreon
#endif
158 107 sybreon
 
159
#endif
160
 
161
/*
162
  $Log: not supported by cvs2svn $
163 145 sybreon
  Revision 1.8  2008/04/26 19:31:35  sybreon
164
  Made headers C compatible.
165
 
166 139 sybreon
  Revision 1.7  2008/04/26 18:05:22  sybreon
167
  Minor cosmetic changes.
168
 
169 137 sybreon
  Revision 1.6  2008/04/23 14:19:39  sybreon
170
  Fixed minor bugs.
171
  Initial use of hardware mutex.
172
 
173 128 sybreon
  Revision 1.5  2008/04/20 16:35:53  sybreon
174
  Added C/C++ compatible #ifdef statements
175
 
176 121 sybreon
  Revision 1.4  2008/04/12 14:07:26  sybreon
177
  added a rendezvous function
178
 
179 117 sybreon
  Revision 1.3  2008/04/11 15:53:24  sybreon
180
  changed MSR bits
181
 
182 114 sybreon
  Revision 1.2  2008/04/11 11:34:30  sybreon
183
  changed semaphore case
184
 
185 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
186
  Added new C++ files
187
 
188 107 sybreon
*/

powered by: WebSVN 2.1.0

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