1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //=================================================================
30 //=================================================================
31 //=================================================================
32 //#####DESCRIPTIONBEGIN####
37 // Description: Threading-related utiltities
40 //####DESCRIPTIONEND####
42 #ifndef _ECOSTHREADUTILS_H
43 #define _ECOSTHREADUTILS_H
45 #include "Collections.h"
47 #ifndef _WIN32 // UNIX
53 //=================================================================
54 // This class handles threads in a host-independent manner.
55 // It also contains a few thread-related functions such as Sleep
56 //=================================================================
58 class CeCosThreadUtils {
62 typedef DWORD THREAD_ID;
65 typedef int THREAD_ID;
67 typedef pthread_t THREAD_ID;
71 static THREAD_ID GetThreadId(); // Get my current thread ID, mostly for debugging
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.
81 static bool InCriticalSection(); // This thread owns the critical section
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.
88 static CRITICAL_SECTION cs; // The one and only critical section
89 static bool bCSInitialized;
92 static pthread_mutex_t cs; // The one and only critical section
97 #define ENTERCRITICAL {CeCosThreadUtils::CS c
98 #define LEAVECRITICAL }
100 static int AtomicIncrement (int &n); // return old value
101 static int AtomicDecrement (int &n); // return old value
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);
108 ///////////////////////////////////////////////////////////////////////////
109 // Define the characteristics of a callback procedure:
111 // A callback procedure, used both for thread entry points and thread completion callbacks
112 typedef void (CALLBACK CallbackProc)(void *);
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); }
121 static void Sleep (int nMsec);
125 // Run a thread: arbitrary callbcak
126 static bool RunThread(CallbackProc *pThreadFunc, void *pParam, CallbackProc *pCompletionFunc, void *pCompletionParam, LPCTSTR pszName);
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.
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),
144 // THREADFUNC is the result type of the thread function
146 typedef unsigned long THREADFUNC;
147 static int CALLBACK FilterFunction(LPEXCEPTION_POINTERS p);
149 typedef void * THREADFUNC;
151 static THREADFUNC CALLBACK SThreadFunc (void *pParam);