OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [include/] [std/] [condition_variable] - Blame information for rev 742

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
//  -*- C++ -*-
2
 
3
// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4
//
5
// This file is part of the GNU ISO C++ Library.  This library is free
6
// software; you can redistribute it and/or modify it under the
7
// terms of the GNU General Public License as published by the
8
// Free Software Foundation; either version 3, or (at your option)
9
// any later version.
10
 
11
// This library is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU General Public License for more details.
15
 
16
// Under Section 7 of GPL version 3, you are granted additional
17
// permissions described in the GCC Runtime Library Exception, version
18
// 3.1, as published by the Free Software Foundation.
19
 
20
// You should have received a copy of the GNU General Public License and
21
// a copy of the GCC Runtime Library Exception along with this program;
22
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23
// .
24
 
25
/** @file include/condition_variable
26
 *  This is a Standard C++ Library header.
27
 */
28
 
29
#ifndef _GLIBCXX_CONDITION_VARIABLE
30
#define _GLIBCXX_CONDITION_VARIABLE 1
31
 
32
#pragma GCC system_header
33
 
34
#ifndef __GXX_EXPERIMENTAL_CXX0X__
35
# include 
36
#else
37
 
38
#include 
39
#include  // unique_lock
40
 
41
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
42
 
43
namespace std _GLIBCXX_VISIBILITY(default)
44
{
45
_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
 
47
  /**
48
   * @defgroup condition_variables Condition Variables
49
   * @ingroup concurrency
50
   *
51
   * Classes for condition_variable support.
52
   * @{
53
   */
54
 
55
  /// cv_status
56
  enum class cv_status { no_timeout, timeout };
57
 
58
  /// condition_variable
59
  class condition_variable
60
  {
61
    typedef chrono::system_clock        __clock_t;
62
    typedef __gthread_cond_t            __native_type;
63
 
64
#ifdef __GTHREAD_COND_INIT
65
    __native_type                       _M_cond = __GTHREAD_COND_INIT;
66
#else
67
    __native_type                       _M_cond;
68
#endif
69
 
70
  public:
71
    typedef __native_type*              native_handle_type;
72
 
73
    condition_variable() noexcept;
74
    ~condition_variable() noexcept;
75
 
76
    condition_variable(const condition_variable&) = delete;
77
    condition_variable& operator=(const condition_variable&) = delete;
78
 
79
    void
80
    notify_one() noexcept;
81
 
82
    void
83
    notify_all() noexcept;
84
 
85
    void
86
    wait(unique_lock& __lock);
87
 
88
    template
89
      void
90
      wait(unique_lock& __lock, _Predicate __p)
91
      {
92
        while (!__p())
93
          wait(__lock);
94
      }
95
 
96
    template
97
      cv_status
98
      wait_until(unique_lock& __lock,
99
                 const chrono::time_point<__clock_t, _Duration>& __atime)
100
      { return __wait_until_impl(__lock, __atime); }
101
 
102
    template
103
      cv_status
104
      wait_until(unique_lock& __lock,
105
                 const chrono::time_point<_Clock, _Duration>& __atime)
106
      {
107
        // DR 887 - Sync unknown clock to known clock.
108
        const typename _Clock::time_point __c_entry = _Clock::now();
109
        const __clock_t::time_point __s_entry = __clock_t::now();
110
        const chrono::nanoseconds __delta = __atime - __c_entry;
111
        const __clock_t::time_point __s_atime = __s_entry + __delta;
112
 
113
        return __wait_until_impl(__lock, __s_atime);
114
      }
115
 
116
    template
117
      bool
118
      wait_until(unique_lock& __lock,
119
                 const chrono::time_point<_Clock, _Duration>& __atime,
120
                 _Predicate __p)
121
      {
122
        while (!__p())
123
          if (wait_until(__lock, __atime) == cv_status::timeout)
124
            return __p();
125
        return true;
126
      }
127
 
128
    template
129
      cv_status
130
      wait_for(unique_lock& __lock,
131
               const chrono::duration<_Rep, _Period>& __rtime)
132
      { return wait_until(__lock, __clock_t::now() + __rtime); }
133
 
134
    template
135
      bool
136
      wait_for(unique_lock& __lock,
137
               const chrono::duration<_Rep, _Period>& __rtime,
138
               _Predicate __p)
139
      { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
140
 
141
    native_handle_type
142
    native_handle()
143
    { return &_M_cond; }
144
 
145
  private:
146
    template
147
      cv_status
148
      __wait_until_impl(unique_lock& __lock,
149
                        const chrono::time_point<_Clock, _Duration>& __atime)
150
      {
151
        chrono::time_point<__clock_t, chrono::seconds> __s =
152
          chrono::time_point_cast(__atime);
153
 
154
        chrono::nanoseconds __ns =
155
          chrono::duration_cast(__atime - __s);
156
 
157
        __gthread_time_t __ts =
158
          {
159
            static_cast(__s.time_since_epoch().count()),
160
            static_cast(__ns.count())
161
          };
162
 
163
        __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
164
                                 &__ts);
165
 
166
        return (_Clock::now() < __atime
167
                ? cv_status::no_timeout : cv_status::timeout);
168
      }
169
  };
170
 
171
  /// condition_variable_any
172
  // Like above, but mutex is not required to have try_lock.
173
  class condition_variable_any
174
  {
175
    typedef chrono::system_clock        __clock_t;
176
    condition_variable                  _M_cond;
177
    mutex                               _M_mutex;
178
 
179
  public:
180
 
181
    condition_variable_any() noexcept;
182
    ~condition_variable_any() noexcept;
183
 
184
    condition_variable_any(const condition_variable_any&) = delete;
185
    condition_variable_any& operator=(const condition_variable_any&) = delete;
186
 
187
    void
188
    notify_one() noexcept
189
    {
190
      lock_guard __lock(_M_mutex);
191
      _M_cond.notify_one();
192
    }
193
 
194
    void
195
    notify_all() noexcept
196
    {
197
      lock_guard __lock(_M_mutex);
198
      _M_cond.notify_all();
199
    }
200
 
201
    template
202
      void
203
      wait(_Lock& __lock)
204
      {
205
        // scoped unlock - unlocks in ctor, re-locks in dtor
206
        struct _Unlock {
207
          explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
208
          ~_Unlock() noexcept(false)
209
          {
210
            if (uncaught_exception())
211
              __try { _M_lock.lock(); } __catch(...) { }
212
            else
213
              _M_lock.lock();
214
          }
215
          _Lock& _M_lock;
216
        };
217
 
218
        unique_lock __my_lock(_M_mutex);
219
        _Unlock __unlock(__lock);
220
        // _M_mutex must be unlocked before re-locking __lock so move
221
        // ownership of _M_mutex lock to an object with shorter lifetime.
222
        unique_lock __my_lock2(std::move(__my_lock));
223
        _M_cond.wait(__my_lock2);
224
      }
225
 
226
 
227
    template
228
      void
229
      wait(_Lock& __lock, _Predicate __p)
230
      {
231
        while (!__p())
232
          wait(__lock);
233
      }
234
 
235
    template
236
      cv_status
237
      wait_until(_Lock& __lock,
238
                 const chrono::time_point<_Clock, _Duration>& __atime)
239
      {
240
        unique_lock __my_lock(_M_mutex);
241
        __lock.unlock();
242
        cv_status __status = _M_cond.wait_until(__my_lock, __atime);
243
        __lock.lock();
244
        return __status;
245
      }
246
 
247
    template
248
             typename _Duration, typename _Predicate>
249
      bool
250
      wait_until(_Lock& __lock,
251
                 const chrono::time_point<_Clock, _Duration>& __atime,
252
                 _Predicate __p)
253
      {
254
        while (!__p())
255
          if (wait_until(__lock, __atime) == cv_status::timeout)
256
            return __p();
257
        return true;
258
      }
259
 
260
    template
261
      cv_status
262
      wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
263
      { return wait_until(__lock, __clock_t::now() + __rtime); }
264
 
265
    template
266
             typename _Period, typename _Predicate>
267
      bool
268
      wait_for(_Lock& __lock,
269
               const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
270
      { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
271
  };
272
 
273
  // @} group condition_variables
274
_GLIBCXX_END_NAMESPACE_VERSION
275
} // namespace
276
 
277
#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
278
 
279
#endif // __GXX_EXPERIMENTAL_CXX0X__
280
 
281
#endif // _GLIBCXX_CONDITION_VARIABLE

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.