1 // Module: Log4CPLUS
2 // File: threads.h
3 // Created: 6/2001
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef _LOG4CPLUS_THREADS_HEADER_
17 #define _LOG4CPLUS_THREADS_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/tstring.h>
21 #include <log4cplus/helpers/sleep.h>
22 #include <log4cplus/helpers/pointer.h>
23 #include <memory>
24
25
26 namespace log4cplus {
27 namespace thread {
28
29 /**
30 * This is used to lock a mutex. The dtor unlocks the mutex.
31 */
32 class LOG4CPLUS_EXPORT Guard {
33 public:
34 /** "locks" <code>mutex</code>. */
35 Guard(LOG4CPLUS_MUTEX_PTR_DECLARE mutex) {
36 LOG4CPLUS_MUTEX_ASSIGN( _mutex, mutex );
37 LOG4CPLUS_MUTEX_LOCK( _mutex );
38 }
39
40 /** "unlocks" <code>mutex</code>. */
41 ~Guard() { LOG4CPLUS_MUTEX_UNLOCK( _mutex ); }
42
43 private:
44 LOG4CPLUS_MUTEX_PTR_DECLARE _mutex;
45
46 // disable copy
47 Guard(const Guard&);
48 Guard& operator=(const Guard&);
49 };
50
51 #ifndef LOG4CPLUS_SINGLE_THREADED
52 #ifdef LOG4CPLUS_USE_PTHREADS
53 void* threadStartFunc(void*);
54 #elif defined(LOG4CPLUS_USE_WIN32_THREADS)
55 DWORD WINAPI threadStartFunc(LPVOID arg);
56 #endif
57
58 LOG4CPLUS_EXPORT void yield();
59 LOG4CPLUS_EXPORT log4cplus::tstring getCurrentThreadName();
60
61 /**
62 * There are many cross-platform C++ Threading libraries. The goal of
63 * this class is not to replace (or match in functionality) those
64 * libraries. The goal of this class is to provide a simple Threading
65 * class with basic functionality.
66 */
67 class LOG4CPLUS_EXPORT AbstractThread : public log4cplus::helpers::SharedObject {
68 public:
69 AbstractThread();
70 bool isRunning() { return running; }
71 LOG4CPLUS_THREAD_KEY_TYPE getThreadId() { return threadId; }
72 virtual void start();
73
74 protected:
75 // Force objects to be constructed on the heap
76 virtual ~AbstractThread();
77 virtual void run() = 0;
78
79 private:
80 bool running;
81 LOG4CPLUS_THREAD_KEY_TYPE threadId;
82
83 // Disallow copying of instances of this class
84 AbstractThread(const AbstractThread&);
85 AbstractThread& operator=(const AbstractThread&);
86
87 // Friends
88 #ifdef LOG4CPLUS_USE_PTHREADS
89 friend void* threadStartFunc(void*);
90 #elif defined(LOG4CPLUS_USE_WIN32_THREADS)
91 friend DWORD WINAPI threadStartFunc(LPVOID arg);
92 #endif
93
94 };
95 #endif // LOG4CPLUS_SINGLE_THREADED
96
97 } // end namespace thread
98 } // end namespace log4cplus
99
100
101 #endif // _LOG4CPLUS_THREADS_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1