]> git.lyx.org Git - lyx.git/blob - boost/boost/re_detail/regex_synch.hpp
cvsignore ++
[lyx.git] / boost / boost / re_detail / regex_synch.hpp
1 /*
2  *
3  * Copyright (c) 1998-2000
4  * Dr John Maddock
5  *
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.
13  *
14  */
15
16  /*
17   *   LOCATION:    see http://www.boost.org for most recent version.
18   *   FILE         regex_synch.hpp
19   *   VERSION      3.03
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.
23   */
24
25 #ifndef BOOST_REGEX_SYNCH_HPP
26 #define BOOST_REGEX_SYNCH_HPP
27
28 #ifndef BOOST_REGEX_CONFIG_HPP
29 #include <boost/re_detail/regex_config.hpp>
30 #endif
31
32 #if defined(BOOST_RE_PLATFORM_W32) && defined(BOOST_RE_THREADS)
33 #include <windows.h>
34 #endif
35
36 #if !defined(BOOST_RE_PLATFORM_W32) && defined(BOOST_RE_THREADS)
37 #include <pthread.h>
38 #endif
39
40
41 namespace boost{
42    namespace re_detail{
43
44 #ifdef __BORLANDC__
45    #if __BORLANDC__ == 0x530
46     #pragma option push -a4 -b -Ve
47    #elif __BORLANDC__ > 0x530
48     #pragma option push -a8 -b -Ve
49    #endif
50 #endif
51
52 void BOOST_RE_CALL re_init_threads();
53 void BOOST_RE_CALL re_free_threads();
54
55 #ifdef BOOST_RE_THREADS
56
57 #ifndef BOOST_RE_PLATFORM_W32
58
59 typedef pthread_mutex_t CRITICAL_SECTION;
60
61 inline void BOOST_RE_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
62 {
63    pthread_mutex_init(ps, NULL);
64 }
65
66 inline void BOOST_RE_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
67 {
68    pthread_mutex_destroy(ps);
69 }
70
71 inline void BOOST_RE_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
72 {
73    pthread_mutex_lock(ps);
74 }
75
76 inline void BOOST_RE_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
77 {
78    pthread_mutex_unlock(ps);
79 }
80
81 #endif
82
83 template <class Lock>
84 class lock_guard
85 {
86    typedef Lock lock_type;
87 public:
88    lock_guard(lock_type& m, bool aq = true)
89       : mut(m), owned(false){ acquire(aq); }
90
91    ~lock_guard()
92    { acquire(false); }
93
94    void BOOST_RE_CALL acquire(bool aq = true)
95    {
96       if(aq && !owned)
97       {
98          mut.acquire(true);
99          owned = true;
100       }
101       else if(!aq && owned)
102       {
103          mut.acquire(false);
104          owned = false;
105       }
106    }
107 private:
108    lock_type& mut;
109    bool owned;
110    // VC6 warning suppression:
111    lock_guard& operator=(const lock_guard&);
112 };
113
114
115 class critical_section
116 {
117 public:
118    critical_section()
119    { InitializeCriticalSection(&hmutex);}
120
121    critical_section(const critical_section&)
122    { InitializeCriticalSection(&hmutex);}
123
124    const critical_section& BOOST_RE_CALL operator=(const critical_section&)
125    {return *this;}
126
127    ~critical_section()
128    {DeleteCriticalSection(&hmutex);}
129
130 private:
131
132    void BOOST_RE_CALL acquire(bool aq)
133    { if(aq) EnterCriticalSection(&hmutex);
134       else LeaveCriticalSection(&hmutex);
135    }
136
137    CRITICAL_SECTION hmutex;
138
139 public:
140    typedef lock_guard<critical_section> ro_guard;
141    typedef lock_guard<critical_section> rw_guard;
142
143    friend lock_guard<critical_section>;
144 };
145
146 inline bool BOOST_RE_CALL operator==(const critical_section&, const critical_section&)
147 {
148    return false;
149 }
150
151 inline bool BOOST_RE_CALL operator<(const critical_section&, const critical_section&)
152 {
153    return true;
154 }
155
156 typedef lock_guard<critical_section> cs_guard;
157
158 BOOST_RE_IX_DECL extern critical_section* p_re_lock;
159 BOOST_RE_IX_DECL extern unsigned int re_lock_count;
160
161 #define BOOST_RE_GUARD(inst) boost::re_detail::critical_section::rw_guard g(inst);
162
163 #else  // BOOST_RE_THREADS
164
165 #define BOOST_RE_GUARD(inst)
166
167 #endif // BOOST_RE_THREADS
168
169 #ifdef __BORLANDC__
170  #if __BORLANDC__ > 0x520
171   #pragma option pop
172  #endif
173 #endif
174
175 } // namespace re_detail
176 } // namespace boost
177
178 #endif // sentry
179
180
181
182
183