comparison host/tools/Utils/common/eCosThreadUtils.h @ 76:435cced73e2f ecos-v1_3_1-release

eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
author jlarmour
date Tue, 28 Mar 2000 14:10:45 +0000
parents
children 6736c52df507
comparison
equal deleted inserted replaced
75:41bf073c0c32 76:435cced73e2f
1 //####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 #define ENTERCRITICAL {CeCosThreadUtils::CS c
47 #define LEAVECRITICAL }
48
49 #ifndef _WIN32 // UNIX
50 #ifndef NO_THREADS
51 #include <pthread.h>
52 #endif
53 #endif
54
55 class CeCosThreadUtils {
56 public:
57 #ifdef _WIN32
58 static int CALLBACK FilterFunction(LPEXCEPTION_POINTERS p);
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 static THREAD_ID GetThreadId();
68
69 class CS{
70 // p and q routines (process-local recursive mutexes)
71 #ifdef _WIN32
72 static CRITICAL_SECTION cs;
73 static bool bCSInitialized;
74 #else // UNIX
75 #ifndef NO_THREADS
76 // Static recursive mutex for unix critical section
77 static pthread_mutex_t cs;
78 #endif
79 #endif
80
81 public:
82
83 static int m_nCriticalSectionLock;
84 static THREAD_ID nCSOwner;
85
86 static int AtomicIncrement (int &n); // return old value
87 static int AtomicDecrement (int &n); // return old value
88 CS();
89 virtual ~CS();
90 };
91
92 // Wait for this boolean to become true, subject to the given timeout
93 static bool WaitFor (bool &b, int dTimeout=0x7fffffff);
94
95 ///////////////////////////////////////////////////////////////////////////
96 // Define the characteristics of a callback procedure:
97
98 // A callback procedure:
99 typedef void (CALLBACK CallbackProc)(void *);
100
101 // Run a thread: pThreadFunc is the entry point (passed pParam). No notification of completion.
102 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,0,0,pszName); }
103 // Run a thread, setting the bool on completion
104 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, bool *pb, LPCTSTR pszName=_T("")) { *pb=false; return RunThread(pThreadFunc,pParam,0,pb,pszName); }
105 // Run a thread, calling the callback on completion
106 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, LPCTSTR pszName=_T("")) { return RunThread(pThreadFunc,pParam,pCompletionFunc,pParam,pszName); }
107
108 protected:
109
110 // If m_pProc is non-zero then it is called
111 // If m_pProc is 0 and m_pParam is non-0 then the callback sets the boolean whose address is held in m_pParam
112 // If m_pProc is 0 and m_pParam is 0 then the callback is "blocking"
113 //bool IsBlocking() const { return 0==m_pProc && 0==m_pParam; }
114
115 // Run a thread: arbitrary callbcak
116 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, void *pCompletionParam, LPCTSTR pszName);
117
118 struct ThreadInfo {
119 CallbackProc *pThreadFunc;
120 void *pThreadParam;
121 CallbackProc *pCompletionFunc; // Call this function
122 void *pCompletionParam; // With this parameter
123 String strName;
124 ThreadInfo (CallbackProc *_pThreadFunc,void *_pThreadParam,CallbackProc *_pCompletionFunc,void *_pCompletionParam,LPCTSTR pszName) :
125 pThreadFunc(_pThreadFunc),
126 pThreadParam(_pThreadParam),
127 pCompletionFunc(_pCompletionFunc),
128 pCompletionParam(_pCompletionParam),
129 strName(pszName){}
130 };
131
132 // Result type of the thread function
133 #ifdef _WIN32
134 typedef unsigned long THREADFUNC;
135 #else // UNIX
136 typedef void * THREADFUNC;
137 #endif
138 static THREADFUNC CALLBACK SThreadFunc (void *pParam);
139
140 };
141
142 #endif