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

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [cc/] [aemb/] [thread.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: thread.hh,v 1.8 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 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
  inline void signalMutex()
76
  {
77
    int tmp;
78
    asm volatile ("msrclr %0, 0x0010":"=r"(tmp));
79
  }
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
  inline void waitMutex()
88
  {
89
    int rmsr;
90
    do
91
      {
92
        asm volatile ("msrset %0, 0x0010":"=r"(rmsr));
93
      }
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 128 sybreon
  typedef volatile int semaphore;
107
 
108
  /**
109
     Increment the semaphore
110
  */
111
  inline void signal(semaphore _sem) { _sem++; }
112 107 sybreon
 
113 128 sybreon
  /**
114
     Decrement the semaphore and block if < 0
115
  */
116
  inline void wait(semaphore _sem) { _sem--; while (_sem < 0); } // block while
117 107 sybreon
                                                     // semaphore is
118
                                                     // negative
119 117 sybreon
 
120 128 sybreon
  semaphore __mutex_rendezvous0 = 0; ///< internal rendezvous mutex
121
  semaphore __mutex_rendezvous1 = 1; ///< internal rendezvous mutex
122 117 sybreon
 
123
  /**
124
     Implements a simple rendezvous mechanism
125
   */
126
 
127
  void rendezvous()
128
  {
129
    if (isThread1())
130
      {
131 128 sybreon
        wait(__mutex_rendezvous0);
132
        signal(__mutex_rendezvous1);
133 117 sybreon
      }
134
    else
135
      {
136 128 sybreon
        signal(__mutex_rendezvous0);
137
        wait(__mutex_rendezvous1);
138 117 sybreon
      }
139
  }
140 121 sybreon
 
141 139 sybreon
#ifdef __cplusplus
142 107 sybreon
}
143 139 sybreon
#endif
144 107 sybreon
 
145
#endif
146
 
147
/*
148
  $Log: not supported by cvs2svn $
149 139 sybreon
  Revision 1.7  2008/04/26 18:05:22  sybreon
150
  Minor cosmetic changes.
151
 
152 137 sybreon
  Revision 1.6  2008/04/23 14:19:39  sybreon
153
  Fixed minor bugs.
154
  Initial use of hardware mutex.
155
 
156 128 sybreon
  Revision 1.5  2008/04/20 16:35:53  sybreon
157
  Added C/C++ compatible #ifdef statements
158
 
159 121 sybreon
  Revision 1.4  2008/04/12 14:07:26  sybreon
160
  added a rendezvous function
161
 
162 117 sybreon
  Revision 1.3  2008/04/11 15:53:24  sybreon
163
  changed MSR bits
164
 
165 114 sybreon
  Revision 1.2  2008/04/11 11:34:30  sybreon
166
  changed semaphore case
167
 
168 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
169
  Added new C++ files
170
 
171 107 sybreon
*/

powered by: WebSVN 2.1.0

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