1 // Module: Log4CPLUS
2 // File: thread-config.h
3 // Created: 4/2003
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_HELPERS_THREAD_CONFIG_HEADER_
17 #define LOG4CPLUS_HELPERS_THREAD_CONFIG_HEADER_
18
19
20 #ifdef LOG4CPLUS_USE_PTHREADS
21 # include <pthread.h>
22 # define LOG4CPLUS_MUTEX_PTR_DECLARE pthread_mutex_t*
23 # define LOG4CPLUS_MUTEX_CREATE ::log4cplus::thread::createNewMutex()
24 # define LOG4CPLUS_MUTEX_ASSIGN( mutex_a, mutex_b) mutex_a = mutex_b
25 # define LOG4CPLUS_MUTEX_LOCK( mutex ) pthread_mutex_lock(mutex)
26 # define LOG4CPLUS_MUTEX_UNLOCK( mutex ) pthread_mutex_unlock(mutex)
27 # define LOG4CPLUS_MUTEX_FREE(mutex) ::log4cplus::thread::deleteMutex(mutex)
28 # define LOG4CPLUS_THREAD_KEY_TYPE pthread_t
29 # define LOG4CPLUS_GET_CURRENT_THREAD_NAME ::log4cplus::thread::getCurrentThreadName()
30
31 # define LOG4CPLUS_GET_CURRENT_THREAD pthread_self()
32 # define LOG4CPLUS_THREAD_LOCAL_TYPE pthread_key_t*
33 # define LOG4CPLUS_THREAD_LOCAL_INIT ::log4cplus::thread::createPthreadKey()
34 # define LOG4CPLUS_GET_THREAD_LOCAL_VALUE( key ) pthread_getspecific(*key)
35 # define LOG4CPLUS_SET_THREAD_LOCAL_VALUE( key, value ) pthread_setspecific(*key, value)
36 # define LOG4CPLUS_THREAD_LOCAL_CLEANUP( key ) pthread_key_delete(*key)
37 namespace log4cplus {
38 namespace thread {
39 LOG4CPLUS_MUTEX_PTR_DECLARE createNewMutex();
40 void deleteMutex(LOG4CPLUS_MUTEX_PTR_DECLARE);
41 LOG4CPLUS_THREAD_LOCAL_TYPE createPthreadKey();
42 }
43 }
44
45 #elif defined(LOG4CPLUS_USE_WIN32_THREADS)
46 # include <windows.h>
47 # define LOG4CPLUS_MUTEX_PTR_DECLARE CRITICAL_SECTION*
48 # define LOG4CPLUS_MUTEX_CREATE ::log4cplus::thread::createNewMutex()
49 # define LOG4CPLUS_MUTEX_ASSIGN( mutex_a, mutex_b) mutex_a = mutex_b
50 # define LOG4CPLUS_MUTEX_LOCK( mutex ) EnterCriticalSection(mutex)
51 # define LOG4CPLUS_MUTEX_UNLOCK( mutex ) LeaveCriticalSection(mutex)
52 # define LOG4CPLUS_MUTEX_FREE(mutex) ::log4cplus::thread::deleteMutex(mutex)
53 # define LOG4CPLUS_THREAD_KEY_TYPE DWORD
54 # define LOG4CPLUS_GET_CURRENT_THREAD GetCurrentThreadId()
55 # define LOG4CPLUS_GET_CURRENT_THREAD_NAME ::log4cplus::thread::getCurrentThreadName()
56 # define LOG4CPLUS_THREAD_LOCAL_TYPE DWORD
57 # define LOG4CPLUS_THREAD_LOCAL_INIT TlsAlloc()
58 # define LOG4CPLUS_GET_THREAD_LOCAL_VALUE( key ) TlsGetValue(key)
59 # define LOG4CPLUS_SET_THREAD_LOCAL_VALUE( key, value ) \
60 TlsSetValue(key, static_cast<LPVOID>(value))
61 # define LOG4CPLUS_THREAD_LOCAL_CLEANUP( key ) TlsFree(key)
62 namespace log4cplus {
63 namespace thread {
64 LOG4CPLUS_EXPORT LOG4CPLUS_MUTEX_PTR_DECLARE createNewMutex();
65 LOG4CPLUS_EXPORT void deleteMutex(LOG4CPLUS_MUTEX_PTR_DECLARE);
66 }
67 }
68
69 #elif defined(LOG4CPLUS_SINGLE_THREADED)
70 # define LOG4CPLUS_MUTEX_PTR_DECLARE int*
71 # define LOG4CPLUS_MUTEX_CREATE NULL
72 # define LOG4CPLUS_MUTEX_ASSIGN( mutex_a, mutex_b) mutex_a = mutex_b;
73 # define LOG4CPLUS_MUTEX_LOCK( mutex )
74 # define LOG4CPLUS_MUTEX_UNLOCK( mutex )
75 # define LOG4CPLUS_MUTEX_FREE(mutex)
76 # define LOG4CPLUS_THREAD_KEY_TYPE int
77 # define LOG4CPLUS_GET_CURRENT_THREAD (int)1
78 # define LOG4CPLUS_GET_CURRENT_THREAD_NAME "single"
79 # define LOG4CPLUS_THREAD_LOCAL_TYPE void*
80 # define LOG4CPLUS_THREAD_LOCAL_INIT NULL
81 # define LOG4CPLUS_GET_THREAD_LOCAL_VALUE( key ) key
82 # define LOG4CPLUS_SET_THREAD_LOCAL_VALUE( key, value ) key = value;
83 # define LOG4CPLUS_THREAD_LOCAL_CLEANUP( key ) key = NULL
84
85 #else
86 # error "You Must define a Threading model"
87 #endif
88
89 /**
90 * @def BEGIN_SYNCHRONIZE_ON_MUTEX(mutex)
91 * Begin a block synchronized on a mutex.
92 *
93 * @see END_SYNCHRONIZE_ON_MUTEX
94 *
95 * @def END_SYNCHRONIZE_ON_MUTEX
96 * End a block synchronized on a mutex.
97 *
98 * @see BEGIN_SYNCHRONIZE_ON_MUTEX
99 */
100 #ifndef LOG4CPLUS_SINGLE_THREADED
101 # define LOG4CPLUS_BEGIN_SYNCHRONIZE_ON_MUTEX( mutex ) \
102 {::log4cplus::thread::Guard _sync_guard_object(mutex);
103 #else
104 # define LOG4CPLUS_BEGIN_SYNCHRONIZE_ON_MUTEX( mutex ) {
105 #endif
106
107 #define LOG4CPLUS_END_SYNCHRONIZE_ON_MUTEX }
108
109
110 #endif // LOG4CPLUS_HELPERS_THREAD_CONFIG_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1