2 // lots of code here is (c) Bartosz Milewski, 1996, www.relisoft.com
3 // The rest is (C) 2003-2004 Royce Mitchell III
4 // and released under the LGPL & BSD licenses
14 # define WIN32_LEAN_AND_MEAN
17 # define THREADAPI WINAPI
22 # include <sys/types.h> //Semaphore
23 # include <sys/ipc.h> //Semaphore
24 # include <sys/sem.h> //Semaphore
27 # error unrecognized target
31 ////////////////////////////////////////////////////////////////////////////////
34 class Thread
: public Uncopyable
37 Thread ( long (THREADAPI
* pFun
) (void* arg
), void* pArg
);
42 // platform-specific stuff:
46 DWORD _tid
; // thread id
48 pthread_t _threadId
; // id of the thread
50 # error unrecognized target
52 //DECLARE_PTR(Thread,(long (THREADAPI * pFun) (void* arg), void* pArg),(pFun,pArg));
53 }; //DECLARE_SPTR(Thread,(long (THREADAPI * pFun) (void* arg), void* pArg),(pFun,pArg));
55 ////////////////////////////////////////////////////////////////////////////////
58 class ActiveObject
: public Uncopyable
62 virtual ~ActiveObject();
67 virtual void InitThread() = 0;
68 virtual void Run() = 0;
69 virtual void FlushThread() = 0;
73 static long THREADAPI
ThreadEntry ( void *pArg
);
76 //DECLARE_PTRV(ActiveObject);
77 }; //DECLARE_SPTRV(ActiveObject);
79 // Last thing in the constructor of a class derived from
80 // ActiveObject you must call
82 // Inside the loop the Run method you must keep checking _isDying
85 // FlushThread must reset all the events on which the thread might be waiting.
88 // MyAsyncOutputter - class that outputs strings to a file asynchronously
89 class MyAsyncOutputter
: public ActiveObject
92 MyAsyncOutputter ( const string
& filename
) : _filename(filename
), _currentBuf(0)
94 Start(); // start thread
98 _f
.open ( _filename
, "wb" );
100 void Output ( const string
& s
)
103 // acquire lock long enough to add the string to the active buffer
104 Mutex::Lock
lock ( _mutex
);
105 _buf
[_currentBuf
].push_back ( s
);
107 _event
.Release(); // don't need the lock fire the event
113 // wait for signal from Output() or FlushThread()
116 // acquire lock long enough to switch active buffers
117 Mutex::Lock
lock ( _mutex
);
118 _currentBuf
= 1-_currentBuf
;
119 ASSERT ( !_buf
[_currentBuf
].size() );
121 // get a reference to the old buffer
122 vector
<string
>& buf
= _buf
[1-_currentBuf
];
123 // write each string out to file and then empty the buffer
124 for ( int i
= 0; i
< buf
.size(); i
++ )
125 _f
.write ( buf
[i
].c_str(), buf
[i
].size() );
131 // _isDying is already set: signal thread so it can see that too
138 vector
<string
> _buf
[2];
144 ////////////////////////////////////////////////////////////////////////////////
147 class Mutex
: public Uncopyable
157 // sub-class used to lock the Mutex
158 class Lock
: public Uncopyable
168 friend class Mutex::Lock
;
171 // sub-class used to attempt to lock the mutex. Use operator bool()
172 // to test if the lock was successful
173 class TryLock
: public Uncopyable
176 TryLock ( Mutex
& m
);
178 operator bool () { return _bLocked
; }
185 friend class Mutex::TryLock
;
188 // platform-specific stuff:
192 pthread_mutex_t _mutex
;
193 public: operator pthread_mutex_t
* () { return &_mutex
; }
195 # error unrecognized target
199 ////////////////////////////////////////////////////////////////////////////////
202 class Event
: public Uncopyable
207 void Release(); // put into signaled state
221 //pthread_cond_t _cond;
224 # error unrecognized target
226 //DECLARE_PTR(Event,(),());
227 }; //DECLARE_SPTR(Event,(),());
229 #endif//__RELIWIN32_H