]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/detail/regex_synch.hpp
Jrgen's "Tooltips for the reference dialog" patch. I've tried to incorporate
[lyx.git] / boost / boost / regex / detail / regex_synch.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
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      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.
23   */
24
25 #ifndef BOOST_REGEX_SYNCH_HPP
26 #define BOOST_REGEX_SYNCH_HPP
27
28 #ifndef BOOST_REGEX_CONFIG_HPP
29 #include <boost/regex/config.hpp>
30 #endif
31
32 #if defined(BOOST_HAS_THREADS)
33 #  if defined(BOOST_HAS_WINTHREADS)
34 #     include <windows.h>
35 #  elif defined(BOOST_HAS_BETHREADS)
36 #     include <OS.h>
37 #     include <cassert>
38 #  elif defined(BOOST_HAS_PTHREADS)
39 #     include <pthread.h>
40 #  else
41 #     error "Unknown threading API"
42 #  endif
43 #endif
44
45
46 namespace boost{
47    namespace re_detail{
48
49 #ifdef __BORLANDC__
50    #pragma option push -a4 -b -Ve -pc
51 #endif
52
53 void BOOST_REGEX_CALL re_init_threads();
54 void BOOST_REGEX_CALL re_free_threads();
55
56 #ifdef BOOST_HAS_THREADS
57
58 #  ifdef BOOST_HAS_BETHREADS
59
60 typedef sem_id CRITICAL_SECTION;
61
62 inline void BOOST_REGEX_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
63 {
64     *ps = create_sem(1, "regex++");
65     assert(*ps > 0);
66 }
67
68 inline void BOOST_REGEX_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
69 {
70     int t = delete_sem(*ps);
71     assert(t == B_NO_ERROR);
72 }
73
74 inline void BOOST_REGEX_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
75 {
76    status_t t = acquire_sem(*ps);
77    assert(t == B_NO_ERROR);
78 }
79
80 inline void BOOST_REGEX_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
81 {
82     status_t t = release_sem(*ps);
83     assert(t == B_NO_ERROR);
84 }
85
86 #  elif defined(BOOST_HAS_PTHREADS)
87
88 typedef pthread_mutex_t CRITICAL_SECTION;
89
90 inline void BOOST_REGEX_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
91 {
92    pthread_mutex_init(ps, 0);
93 }
94
95 inline void BOOST_REGEX_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
96 {
97    pthread_mutex_destroy(ps);
98 }
99
100 inline void BOOST_REGEX_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
101 {
102    pthread_mutex_lock(ps);
103 }
104
105 inline void BOOST_REGEX_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
106 {
107    pthread_mutex_unlock(ps);
108 }
109
110 #  elif !defined(BOOST_HAS_WINTHREADS)
111 #    error "Unknown threading API"
112 #  endif
113
114 template <class Lock>
115 class lock_guard
116 {
117    typedef Lock lock_type;
118 public:
119    lock_guard(lock_type& m, bool aq = true)
120       : mut(m), owned(false){ acquire(aq); }
121
122    ~lock_guard()
123    { acquire(false); }
124
125    void BOOST_REGEX_CALL acquire(bool aq = true)
126    {
127       if(aq && !owned)
128       {
129          mut.acquire(true);
130          owned = true;
131       }
132       else if(!aq && owned)
133       {
134          mut.acquire(false);
135          owned = false;
136       }
137    }
138 private:
139    lock_type& mut;
140    bool owned;
141    // VC6 warning suppression:
142    lock_guard& operator=(const lock_guard&);
143 };
144
145
146 class critical_section
147 {
148 public:
149    critical_section()
150    { InitializeCriticalSection(&hmutex);}
151
152    critical_section(const critical_section&)
153    { InitializeCriticalSection(&hmutex);}
154
155    const critical_section& BOOST_REGEX_CALL operator=(const critical_section&)
156    {return *this;}
157
158    ~critical_section()
159    {DeleteCriticalSection(&hmutex);}
160
161 private:
162
163    void BOOST_REGEX_CALL acquire(bool aq)
164    { if(aq) EnterCriticalSection(&hmutex);
165       else LeaveCriticalSection(&hmutex);
166    }
167
168    CRITICAL_SECTION hmutex;
169
170 public:
171    typedef lock_guard<critical_section> ro_guard;
172    typedef lock_guard<critical_section> rw_guard;
173
174    friend class lock_guard<critical_section>;
175 };
176
177 inline bool BOOST_REGEX_CALL operator==(const critical_section&, const critical_section&)
178 {
179    return false;
180 }
181
182 inline bool BOOST_REGEX_CALL operator<(const critical_section&, const critical_section&)
183 {
184    return true;
185 }
186
187 typedef lock_guard<critical_section> cs_guard;
188
189 BOOST_REGEX_DECL extern critical_section* p_re_lock;
190 BOOST_REGEX_DECL extern unsigned int re_lock_count;
191
192 #define BOOST_REGEX_GUARD(inst) boost::re_detail::critical_section::rw_guard g(inst);
193
194 #else  // BOOST_HAS_THREADS
195
196 #define BOOST_REGEX_GUARD(inst)
197
198 #endif // BOOST_HAS_THREADS
199
200 #ifdef __BORLANDC__
201   #pragma option pop
202 #endif
203
204 } // namespace re_detail
205 } // namespace boost
206
207 #endif // sentry
208
209
210
211
212
213