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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 108 sybreon
/* $Id: thread.hh,v 1.2 2008-04-11 11:34:30 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
  Checks to see if currently executing Thread 1
39
  @return true if is Thread 1
40
  */
41
 
42
  inline bool isThread1()
43
  {
44
    int rmsr = aemb::getMSR();
45
    return ((rmsr & aemb::MSR_HTE) and (rmsr & aemb::MSR_PHA));
46
  }
47
 
48
  /**
49
  Checks to see if currently executing Thread 0
50
  @return true if is Thread 0
51
  */
52
 
53
  inline bool isThread0()
54
  {
55
    int rmsr = aemb::getMSR();
56
    return ((rmsr & aemb::MSR_HTE) and (not (rmsr & aemb::MSR_PHA)));
57
  }
58
 
59
  /**
60
  Checks to see if it is multi-threaded or not
61
  @return true if thread capable
62
  */
63
  inline bool isThreaded()
64
  {
65
    int rmsr = aemb::getMSR();
66
    return (rmsr & aemb::MSR_HTE);
67
  }
68
 
69
  // TODO: Extend this library to include threading mechanisms such as
70
  // semaphores, mutexes and such.
71
 
72
 
73
  /**
74
  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
  */
82
 
83 108 sybreon
  class semaphore {
84 107 sybreon
    volatile int _sem; ///< Semaphore in Memory
85
  public:
86
    /**
87
    Preload the semaphore
88
    @param pval preload value
89
    */
90 108 sybreon
    semaphore(int pval) { _sem = pval; }
91 107 sybreon
 
92
    /**
93
    Increment the semaphore
94
    */
95
    inline void signal() { _sem++; }
96
 
97
    /**
98
    Decrement the semaphore and block if < 0
99
    */
100
    inline void wait() { _sem--; while (_sem < 0); } // block while
101
                                                     // semaphore is
102
                                                     // negative
103
  };
104
 
105
}
106
 
107
#endif
108
 
109
/*
110
  $Log: not supported by cvs2svn $
111 108 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
112
  Added new C++ files
113
 
114 107 sybreon
*/

powered by: WebSVN 2.1.0

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