root/trunk/freebob/src/threads.h

Revision 37, 4.3 kB (checked in by wagi, 19 years ago)

Added syncCall implementation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* threads.h
2  * Copyright (C) 2004 by Daniel Wagner
3  *
4  * This file is part of FreeBob.
5  *
6  * FreeBob 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 of the License, or
9  * (at your option) any later version.
10  * FreeBob is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBob; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #ifndef THREADS_H
22 #define THREADS_H
23
24 #include <semaphore.h>
25
26 #include "workerthread.h"
27
28 class Functor
29 {
30 public:
31     virtual void operator() () = 0;
32 };
33
34 ////////////////////////////////////////////////////////////////////////
35
36 template< typename CalleePtr, typename MemFunPtr >
37 class MemberFunctor0
38     : public Functor
39 {
40 public:
41     MemberFunctor0( const CalleePtr& pCallee,
42                     MemFunPtr pMemFun,
43                     bool bDelete = true )
44         : m_pCallee( pCallee )
45         , m_pMemFun( pMemFun )
46         , m_pSem( 0 )
47         , m_bDelete( bDelete )
48         {}
49
50     MemberFunctor0( const CalleePtr& pCallee,
51                     MemFunPtr pMemFun,
52                     sem_t* pSem,
53                     bool bDelete = true )
54         : m_pCallee( pCallee )
55         , m_pMemFun( pMemFun )
56         , m_pSem( pSem )
57         , m_bDelete( bDelete )
58         {}
59
60     virtual ~MemberFunctor0()
61         {}
62
63     virtual void operator() ()
64         {
65             ( ( *m_pCallee ).*m_pMemFun )();
66             if ( m_pSem ) {
67                 sem_post( m_pSem);
68             }
69             if (m_bDelete) {
70                 delete this;
71             }
72         }
73
74 private:
75     CalleePtr  m_pCallee;
76     MemFunPtr  m_pMemFun;
77     sem_t* m_pSem;
78     bool       m_bDelete;
79 };
80
81 template< typename CalleePtr, typename MemFunPtr, typename Parm0 >
82 class MemberFunctor1
83     : public Functor
84 {
85 public:
86     MemberFunctor1( const CalleePtr& pCallee,
87                     MemFunPtr pMemFun,
88                     Parm0 parm0,
89                     bool bDelete = true)
90         : m_pCallee( pCallee )
91         , m_pMemFun( pMemFun )
92         , m_parm0( parm0 )
93         , m_pSem( 0 )
94         , m_bDelete( bDelete ) 
95         {}
96
97     MemberFunctor1( const CalleePtr& pCallee,
98                     MemFunPtr pMemFun,
99                     Parm0 parm0,
100                     sem_t* pSem,
101                     bool bDelete = true )
102         : m_pCallee( pCallee )
103         , m_pMemFun( pMemFun )
104         , m_parm0( parm0 )
105         , m_pSem( 0 )
106         , m_bDelete( bDelete )
107         {}
108     virtual ~MemberFunctor1()
109         {}
110
111     virtual void operator() ()
112         {
113             ( ( *m_pCallee ).*m_pMemFun )( m_parm0 );
114             if (bDelete) {
115                 delete this;
116             }
117         }
118
119 private:
120     CalleePtr  m_pCallee;
121     MemFunPtr  m_pMemFun;
122     Parm0      m_parm0;
123     sem_t* m_pSem;
124     bool       m_bDelete;
125 };
126
127 ////////////////////////////////////////////////////////////////////////
128
129 // 0 params
130 template< typename CalleePtr, typename Callee,  typename Ret >
131 inline Functor* deferCall( const CalleePtr& pCallee,
132                            Ret( Callee::*pFunction )( ) )
133 {
134     return new MemberFunctor0< CalleePtr, Ret ( Callee::* )( ) >
135         ( pCallee,  pFunction );
136 }
137
138 // 1 params
139 template< typename CalleePtr, typename Callee,  typename Ret, typename Parm0 >
140 inline Functor* deferCall( const CalleePtr& pCallee,
141                            Ret( Callee::*pFunction )( ),
142                            Parm0 parm0 )
143 {
144     return new MemberFunctor1< CalleePtr, Ret ( Callee::* )( ), Parm0 >
145         ( pCallee,  pFunction, parm0 );
146 }
147
148 ////////////////////////////////////////////////////////////////////////
149
150 template< typename CalleePtr, typename Callee,  typename Ret >
151 inline void asyncCall( const CalleePtr& pCallee,
152                        Ret( Callee::*pFunction )( ) )
153 {
154     WorkerThread::instance()->addFunctor(new MemberFunctor0< CalleePtr, Ret ( Callee::* )( ) > ( pCallee,  pFunction ));
155 }
156
157 ////////////////////////////////////////////////////////////////////////
158
159 template< typename CalleePtr, typename Callee,  typename Ret >
160 inline void syncCall( const CalleePtr& pCallee,
161                       Ret( Callee::*pFunction )( ) )
162 {
163     sem_t sem;
164     sem_init( &sem, 1, 0 );
165
166     WorkerThread::instance()->addFunctor(new MemberFunctor0< CalleePtr, Ret ( Callee::* )( ) > ( pCallee,  pFunction, &sem ));
167
168     sem_wait( &sem );
169     sem_destroy( &sem );
170 }
171
172 #endif
173
174
Note: See TracBrowser for help on using the browser.