root/juggler/tags/1.0.5/Threads/vjThreadFunctor.h

Revision 7539, 4.2 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33
34 #ifndef _VJ_THREAD_FUNCTOR_H_
35 #define _VJ_THREAD_FUNCTOR_H_
36
37 #include <vjConfig.h>
38 #include <stdlib.h>
39
40 //---------------------------------------------------------------
41 //: Converts a function into a functor that can be passed to a
42 //  extern C type function to be called by a thread creation
43 //  routine.
44 //!PUBLIC_API:
45 //---------------------------------------------------------------
46 class  vjBaseThreadFunctor
47 {
48 public:
49     virtual void operator()() = 0;    // Pure virtual
50     virtual void operator()(void*) = 0;
51     virtual void setArg(void*) = 0;
52 };
53
54 //---------------------------------------------------------------
55 //: Member functor class.
56 //!PUBLIC_API:
57 //---------------------------------------------------------------
58 template<class T>
59 class vjThreadMemberFunctor : public  vjBaseThreadFunctor
60 {
61 public:
62     typedef void (T::* FunPtr)(void*);
63
64     vjThreadMemberFunctor(T* theObject, FunPtr func, void* arg = NULL)
65     {
66         object = theObject;
67         function = func;
68         argument = arg;
69     }
70
71     void
72     operator() (void* arg) {
73         (object->*function)(arg);
74     }
75
76     void
77     operator() () {
78         (object->*function)(argument);
79     }
80
81     void
82     setArg (void* arg) {
83         argument = arg;
84     }
85
86 private:
87     T*      object;
88     FunPtr  function;
89     void*   argument;
90 };
91
92
93 //---------------------------------------------------------------
94 //: Nonmember functor class.  Converts a NonMember function into
95 //+ a functor.
96 //
97 //!PUBLIC_API:
98 //---------------------------------------------------------------
99 class vjThreadNonMemberFunctor : public  vjBaseThreadFunctor
100 {
101 public:
102     typedef void(* NonMemFunPtr)(void*);
103
104     //---------------------------------------------------------------
105     //: Constructor.
106     //---------------------------------------------------------------
107     vjThreadNonMemberFunctor (NonMemFunPtr f, void* a = NULL) : func(f), argument(a)
108     {;}
109
110     virtual void operator() (void* arg) {
111         (*func)(arg);
112     }
113
114     virtual void operator() () {
115         (*func)(argument);
116     }
117
118     void setArg (void* arg) {
119         argument = arg;
120     }
121
122     // private:
123     // = Arguments to thread startup.
124     NonMemFunPtr func;  // Thread startup function (C++ linkage).
125     void* argument;             // Argument to thread startup function.
126 };
127
128 //---------------------------------------------
129 // This is the actual function that is called.
130 // It must be extern "C"
131 //---------------------------------------------
132 #if defined(VJ_IRIX_SPROC)      /* ---- SGI IPC Barrier ------ */
133     extern "C" void ThreadFunctorFunction(void* args);
134 #elif defined(VJ_OS_Win32)
135     unsigned int __stdcall ThreadFunctorFunction(void* args);
136 #elif defined(VJ_USE_PTHREADS)
137 #   ifdef _PTHREADS_DRAFT_4
138         extern "C" void
139 #   else
140         extern "C" void*
141 #   endif
142         ThreadFunctorFunction (void* args);
143 #else
144     extern "C" void ThreadFunctorFunction(void* args);
145 #endif  /* VJ_IRIX_SPROC */
146
147 #endif  /* _THREAD_FUNCTOR_H_ */
Note: See TracBrowser for help on using the browser.