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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [Utils/] [common/] [eCosThreadUtils.h] - Blame information for rev 790

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

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.            
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
//=================================================================
23
//
24
//        eCosThreadUtils.h
25
//
26
//
27
//=================================================================
28
//=================================================================
29
//#####DESCRIPTIONBEGIN####
30
//
31
// Author(s):     sdf
32
// Contributors:  sdf
33
// Date:          1999-04-01
34
// Description:   Threading-related utiltities 
35
// Usage:
36
//
37
//####DESCRIPTIONEND####
38
 
39
#ifndef _ECOSTHREADUTILS_H
40
#define _ECOSTHREADUTILS_H
41
#include "eCosStd.h"
42
#include "Collections.h"
43
 
44
#ifndef _WIN32 // UNIX
45
  #ifndef NO_THREADS
46
    #include <pthread.h>
47
  #endif 
48
#endif
49
 
50
//=================================================================
51
// This class handles threads in a host-independent manner.  
52
// It also contains a few thread-related functions such as Sleep
53
//=================================================================
54
 
55
class CeCosThreadUtils {
56
public:
57
 
58
#ifdef _WIN32
59
  typedef DWORD THREAD_ID;
60
#else // UNIX
61
  #ifndef NO_THREADS
62
    typedef int THREAD_ID;
63
  #else
64
    typedef pthread_t THREAD_ID;
65
  #endif
66
#endif
67
 
68
  static THREAD_ID GetThreadId(); // Get my current thread ID, mostly for debugging
69
 
70
  // CS supports a single system-wide critical sections (recursive mutexes).
71
  // You are expected to use macros ENTERCRITICAL and LEAVECRITICAL to use this class - these macros define
72
  // a block containing a CS object, which has the effect of creating a critical section.
73
  // Exit from the block (by whatever means, including an exception) causes the CS dtor
74
  // to be called so as to release the section.
75
 
76
  class CS{
77
  public:
78
    static bool InCriticalSection();     // This thread owns the critical section
79
    CS();
80
    virtual ~CS();
81
  protected:
82
    static int m_nCriticalSectionLock; // The number of times the recursive mutex has been locked.  Management of this allows us to avoid use of true recursive mutexes on UNIX.
83
    static THREAD_ID nCSOwner;         // The thread owning the resource.
84
#ifdef _WIN32
85
    static CRITICAL_SECTION cs;        // The one and only critical section
86
    static bool bCSInitialized;
87
#else // UNIX
88
    #ifndef NO_THREADS
89
    static pthread_mutex_t cs;         // The one and only critical section
90
    #endif
91
#endif
92
  };
93
 
94
  #define ENTERCRITICAL {CeCosThreadUtils::CS c
95
  #define LEAVECRITICAL }
96
 
97
  static int AtomicIncrement (int &n); // return old value
98
  static int AtomicDecrement (int &n); // return old value
99
 
100
 
101
  // Wait for this boolean to become true, subject to the given timeout
102
  // If the timeout happens first, the return code will be false - otherwise true
103
  static bool WaitFor (bool &b, int dTimeout=0x7fffffff);
104
 
105
  ///////////////////////////////////////////////////////////////////////////
106
  // Define the characteristics of a callback procedure:
107
 
108
  // A callback procedure, used both for thread entry points and thread completion callbacks
109
  typedef void (CALLBACK CallbackProc)(void *);
110
 
111
  // Run a thread: pThreadFunc is the entry point (passed pParam).  No notification of completion.
112
  static bool RunThread(CallbackProc *pThreadFunc, void *pParam, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,0,0,pszName); }
113
  // Run a thread, setting the bool on completion
114
  static bool RunThread(CallbackProc *pThreadFunc, void *pParam, bool *pb, LPCTSTR pszName=_T("")) { *pb=false; return RunThread(pThreadFunc,pParam,0,pb,pszName); }
115
  // Run a thread, calling the callback on completion
116
  static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,pCompletionFunc,pParam,pszName); }
117
 
118
  static void Sleep (int nMsec);
119
 
120
protected:
121
 
122
  // Run a thread: arbitrary callbcak
123
  static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, void *pCompletionParam, LPCTSTR pszName);
124
 
125
  // This is the information that is passed to the host-specific thread proc.  It is simply enough to call the thread entry point and
126
  // call the callback (or set the boolean) at the end.
127
  struct ThreadInfo {
128
    CallbackProc *pThreadFunc;      // The thread proc is this function
129
    void         *pThreadParam;     //            - called with this parameter
130
    CallbackProc *pCompletionFunc;  // At the end - call this function
131
    void         *pCompletionParam; //              with this parameter
132
    String       strName;           // For debugging
133
    ThreadInfo (CallbackProc *_pThreadFunc,void *_pThreadParam,CallbackProc *_pCompletionFunc,void  *_pCompletionParam,LPCTSTR pszName) :
134
      pThreadFunc(_pThreadFunc),
135
      pThreadParam(_pThreadParam),
136
      pCompletionFunc(_pCompletionFunc),
137
      pCompletionParam(_pCompletionParam),
138
      strName(pszName){}
139
  };
140
 
141
  // THREADFUNC is the result type of the thread function
142
  #ifdef _WIN32
143
    typedef unsigned long THREADFUNC;
144
    static int CALLBACK FilterFunction(LPEXCEPTION_POINTERS p);
145
  #else // UNIX
146
    typedef void * THREADFUNC;
147
  #endif
148
  static THREADFUNC CALLBACK SThreadFunc (void *pParam);
149
 
150
};
151
 
152
#endif

powered by: WebSVN 2.1.0

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