3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
18 * FILE regex_synch.hpp
19 * VERSION see <boost/version.hpp>
20 * DESCRIPTION: Thread synchronisation for regex code.
21 * Note this is an internal header file included
22 * by regex.hpp, do not include on its own.
25 #ifndef BOOST_REGEX_SYNCH_HPP
26 #define BOOST_REGEX_SYNCH_HPP
28 #ifndef BOOST_REGEX_CONFIG_HPP
29 #include <boost/regex/config.hpp>
32 #if defined(BOOST_HAS_THREADS)
33 # if defined(BOOST_HAS_WINTHREADS)
35 # elif defined(BOOST_HAS_BETHREADS)
38 # elif defined(BOOST_HAS_PTHREADS)
41 # error "Unknown threading API"
50 #pragma option push -a4 -b -Ve -pc
53 void BOOST_REGEX_CALL re_init_threads();
54 void BOOST_REGEX_CALL re_free_threads();
56 #ifdef BOOST_HAS_THREADS
58 # ifdef BOOST_HAS_BETHREADS
60 typedef sem_id CRITICAL_SECTION;
62 inline void BOOST_REGEX_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
64 *ps = create_sem(1, "regex++");
68 inline void BOOST_REGEX_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
70 int t = delete_sem(*ps);
71 assert(t == B_NO_ERROR);
74 inline void BOOST_REGEX_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
76 status_t t = acquire_sem(*ps);
77 assert(t == B_NO_ERROR);
80 inline void BOOST_REGEX_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
82 status_t t = release_sem(*ps);
83 assert(t == B_NO_ERROR);
86 # elif defined(BOOST_HAS_PTHREADS)
88 typedef pthread_mutex_t CRITICAL_SECTION;
90 inline void BOOST_REGEX_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
92 pthread_mutex_init(ps, 0);
95 inline void BOOST_REGEX_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
97 pthread_mutex_destroy(ps);
100 inline void BOOST_REGEX_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
102 pthread_mutex_lock(ps);
105 inline void BOOST_REGEX_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
107 pthread_mutex_unlock(ps);
110 # elif !defined(BOOST_HAS_WINTHREADS)
111 # error "Unknown threading API"
114 template <class Lock>
117 typedef Lock lock_type;
119 lock_guard(lock_type& m, bool aq = true)
120 : mut(m), owned(false){ acquire(aq); }
125 void BOOST_REGEX_CALL acquire(bool aq = true)
132 else if(!aq && owned)
141 // VC6 warning suppression:
142 lock_guard& operator=(const lock_guard&);
146 class critical_section
150 { InitializeCriticalSection(&hmutex);}
152 critical_section(const critical_section&)
153 { InitializeCriticalSection(&hmutex);}
155 const critical_section& BOOST_REGEX_CALL operator=(const critical_section&)
159 {DeleteCriticalSection(&hmutex);}
163 void BOOST_REGEX_CALL acquire(bool aq)
164 { if(aq) EnterCriticalSection(&hmutex);
165 else LeaveCriticalSection(&hmutex);
168 CRITICAL_SECTION hmutex;
171 typedef lock_guard<critical_section> ro_guard;
172 typedef lock_guard<critical_section> rw_guard;
174 friend class lock_guard<critical_section>;
177 inline bool BOOST_REGEX_CALL operator==(const critical_section&, const critical_section&)
182 inline bool BOOST_REGEX_CALL operator<(const critical_section&, const critical_section&)
187 typedef lock_guard<critical_section> cs_guard;
189 BOOST_REGEX_DECL extern critical_section* p_re_lock;
190 BOOST_REGEX_DECL extern unsigned int re_lock_count;
192 #define BOOST_REGEX_GUARD(inst) boost::re_detail::critical_section::rw_guard g(inst);
194 #else // BOOST_HAS_THREADS
196 #define BOOST_REGEX_GUARD(inst)
198 #endif // BOOST_HAS_THREADS
204 } // namespace re_detail