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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 117 sybreon
/* $Id: thread.hh,v 1.4 2008-04-12 14:07:26 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
    int rmsr = aemb::getMSR();
45 114 sybreon
    return ((rmsr & aemb::MSR_HTX) && (rmsr & aemb::MSR_HTP));
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
    int rmsr = aemb::getMSR();
56 114 sybreon
    return ((rmsr & aemb::MSR_HTX) && (!(rmsr & aemb::MSR_HTP)));
57 107 sybreon
  }
58
 
59
  /**
60 117 sybreon
     Checks to see if it is multi-threaded or not
61
     @return true if thread capable
62 107 sybreon
  */
63
  inline bool isThreaded()
64
  {
65
    int rmsr = aemb::getMSR();
66 114 sybreon
    return (rmsr & aemb::MSR_HTX);
67 107 sybreon
  }
68
 
69
  // TODO: Extend this library to include threading mechanisms such as
70
  // semaphores, mutexes and such.
71
 
72
 
73
  /**
74 117 sybreon
     Semaphore class
75
 
76
     Based on: Little Book of Semaphores, The - Downey, Allen B.
77
 
78
     Presently implemented as software solution but a hardware one may be
79
     required as the threads are hardware. This can be implemented using
80
     a specialised add/sub/load register.
81 107 sybreon
  */
82
 
83 108 sybreon
  class semaphore {
84 117 sybreon
  private:
85 107 sybreon
    volatile int _sem; ///< Semaphore in Memory
86
  public:
87
    /**
88 117 sybreon
       Preload the semaphore
89
       @param pval preload value
90 107 sybreon
    */
91 108 sybreon
    semaphore(int pval) { _sem = pval; }
92 107 sybreon
 
93
    /**
94 117 sybreon
       Increment the semaphore
95 107 sybreon
    */
96
    inline void signal() { _sem++; }
97
 
98
    /**
99 117 sybreon
       Decrement the semaphore and block if < 0
100 107 sybreon
    */
101
    inline void wait() { _sem--; while (_sem < 0); } // block while
102
                                                     // semaphore is
103
                                                     // negative
104
  };
105 117 sybreon
 
106
  semaphore __mutex_rendezvous0(0); ///< internal rendezvous mutex
107
  semaphore __mutex_rendezvous1(1); ///< internal rendezvous mutex
108
 
109
  /**
110
     Implements a simple rendezvous mechanism
111
   */
112
 
113
  void rendezvous()
114
  {
115
    if (isThread1())
116
      {
117
        __mutex_rendezvous0.wait();
118
        __mutex_rendezvous1.signal();
119
      }
120
    else
121
      {
122
        __mutex_rendezvous0.signal();
123
        __mutex_rendezvous1.wait();
124
      }
125
  }
126 107 sybreon
 
127
}
128
 
129
#endif
130
 
131
/*
132
  $Log: not supported by cvs2svn $
133 117 sybreon
  Revision 1.3  2008/04/11 15:53:24  sybreon
134
  changed MSR bits
135
 
136 114 sybreon
  Revision 1.2  2008/04/11 11:34:30  sybreon
137
  changed semaphore case
138
 
139 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
140
  Added new C++ files
141
 
142 107 sybreon
*/

powered by: WebSVN 2.1.0

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