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