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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 121 sybreon
/* $Id: thread.hh,v 1.5 2008-04-20 16:35:53 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 121 sybreon
#ifdef __cplusplus
36 107 sybreon
namespace aemb {
37 121 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
  inline bool isThread1()
45
  {
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
  inline bool isThread0()
56
  {
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
  inline bool isThreaded()
66
  {
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
     Waits until the hardware mutex is unlocked.
84
   */
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 121 sybreon
     Semaphore class.
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 108 sybreon
  class semaphore {
105 117 sybreon
  private:
106 107 sybreon
    volatile int _sem; ///< Semaphore in Memory
107
  public:
108
    /**
109 117 sybreon
       Preload the semaphore
110
       @param pval preload value
111 107 sybreon
    */
112 108 sybreon
    semaphore(int pval) { _sem = pval; }
113 107 sybreon
 
114
    /**
115 117 sybreon
       Increment the semaphore
116 107 sybreon
    */
117
    inline void signal() { _sem++; }
118
 
119
    /**
120 117 sybreon
       Decrement the semaphore and block if < 0
121 107 sybreon
    */
122
    inline void wait() { _sem--; while (_sem < 0); } // block while
123
                                                     // semaphore is
124
                                                     // negative
125
  };
126 117 sybreon
 
127
  semaphore __mutex_rendezvous0(0); ///< internal rendezvous mutex
128
  semaphore __mutex_rendezvous1(1); ///< internal rendezvous mutex
129
 
130
  /**
131
     Implements a simple rendezvous mechanism
132
   */
133
 
134
  void rendezvous()
135
  {
136
    if (isThread1())
137
      {
138
        __mutex_rendezvous0.wait();
139
        __mutex_rendezvous1.signal();
140
      }
141
    else
142
      {
143
        __mutex_rendezvous0.signal();
144
        __mutex_rendezvous1.wait();
145
      }
146
  }
147 121 sybreon
 
148
#ifdef __cplusplus
149 107 sybreon
}
150 121 sybreon
#endif
151 107 sybreon
 
152
#endif
153
 
154
/*
155
  $Log: not supported by cvs2svn $
156 121 sybreon
  Revision 1.4  2008/04/12 14:07:26  sybreon
157
  added a rendezvous function
158
 
159 117 sybreon
  Revision 1.3  2008/04/11 15:53:24  sybreon
160
  changed MSR bits
161
 
162 114 sybreon
  Revision 1.2  2008/04/11 11:34:30  sybreon
163
  changed semaphore case
164
 
165 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
166
  Added new C++ files
167
 
168 107 sybreon
*/

powered by: WebSVN 2.1.0

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