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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 137 sybreon
/* $Id: thread.hh,v 1.7 2008-04-26 18:05:22 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 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
namespace aemb {
36
 
37
  /**
38 117 sybreon
     Checks to see if currently executing Thread 1
39
     @return true if is Thread 1
40 107 sybreon
  */
41
 
42
  inline bool isThread1()
43
  {
44 121 sybreon
    int rmsr = getMSR();
45
    return ((rmsr & MSR_HTX) && (rmsr & MSR_PHA));
46 107 sybreon
  }
47
 
48
  /**
49 117 sybreon
     Checks to see if currently executing Thread 0
50
     @return true if is Thread 0
51 107 sybreon
  */
52
 
53
  inline bool isThread0()
54
  {
55 121 sybreon
    int rmsr = getMSR();
56
    return ((rmsr & MSR_HTX) && (!(rmsr & MSR_PHA)));
57 107 sybreon
  }
58
 
59
  /**
60 121 sybreon
     Checks to see if it is multi-threaded or not.
61 117 sybreon
     @return true if thread capable
62 107 sybreon
  */
63
  inline bool isThreaded()
64
  {
65 121 sybreon
    int rmsr = getMSR();
66
    return (rmsr & MSR_HTX);
67 107 sybreon
  }
68 121 sybreon
 
69
  /**
70
     Hardware Mutex Signal.
71
     Unlock the hardware mutex, which is unlocked on reset.
72
   */
73
  inline void signalMutex()
74
  {
75
    int tmp;
76
    asm volatile ("msrclr %0, 0x0010":"=r"(tmp));
77
  }
78
 
79
  /**
80
     Hardware Mutex Wait.
81 128 sybreon
 
82
     Waits until the hardware mutex is unlocked. This should be used
83
     as part of a larger software mutex mechanism.
84 121 sybreon
   */
85
  inline void waitMutex()
86
  {
87
    int rmsr;
88
    do
89
      {
90
        asm volatile ("msrset %0, 0x0010":"=r"(rmsr));
91
      }
92
    while (rmsr & MSR_MTX);
93
  }
94
 
95 107 sybreon
  // TODO: Extend this library to include threading mechanisms such as
96
  // semaphores, mutexes and such.
97 121 sybreon
 
98 107 sybreon
  /**
99 128 sybreon
     Semaphore struct.
100 117 sybreon
     Presently implemented as software solution but a hardware one may be
101 121 sybreon
     required as the threads are hardware.
102 107 sybreon
  */
103
 
104 128 sybreon
  typedef volatile int semaphore;
105
 
106
  /**
107
     Increment the semaphore
108
  */
109
  inline void signal(semaphore _sem) { _sem++; }
110 107 sybreon
 
111 128 sybreon
  /**
112
     Decrement the semaphore and block if < 0
113
  */
114
  inline void wait(semaphore _sem) { _sem--; while (_sem < 0); } // block while
115 107 sybreon
                                                     // semaphore is
116
                                                     // negative
117 117 sybreon
 
118 128 sybreon
  semaphore __mutex_rendezvous0 = 0; ///< internal rendezvous mutex
119
  semaphore __mutex_rendezvous1 = 1; ///< internal rendezvous mutex
120 117 sybreon
 
121
  /**
122
     Implements a simple rendezvous mechanism
123
   */
124
 
125
  void rendezvous()
126
  {
127
    if (isThread1())
128
      {
129 128 sybreon
        wait(__mutex_rendezvous0);
130
        signal(__mutex_rendezvous1);
131 117 sybreon
      }
132
    else
133
      {
134 128 sybreon
        signal(__mutex_rendezvous0);
135
        wait(__mutex_rendezvous1);
136 117 sybreon
      }
137
  }
138 121 sybreon
 
139 107 sybreon
}
140
 
141
#endif
142
 
143
/*
144
  $Log: not supported by cvs2svn $
145 137 sybreon
  Revision 1.6  2008/04/23 14:19:39  sybreon
146
  Fixed minor bugs.
147
  Initial use of hardware mutex.
148
 
149 128 sybreon
  Revision 1.5  2008/04/20 16:35:53  sybreon
150
  Added C/C++ compatible #ifdef statements
151
 
152 121 sybreon
  Revision 1.4  2008/04/12 14:07:26  sybreon
153
  added a rendezvous function
154
 
155 117 sybreon
  Revision 1.3  2008/04/11 15:53:24  sybreon
156
  changed MSR bits
157
 
158 114 sybreon
  Revision 1.2  2008/04/11 11:34:30  sybreon
159
  changed semaphore case
160
 
161 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
162
  Added new C++ files
163
 
164 107 sybreon
*/

powered by: WebSVN 2.1.0

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