OSDN Git Service

a1a63b27f334587f7de09d85aac4c240df0b9f61
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / mutex
1 // <mutex> -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file include/mutex
27  *  This is a Standard C++ Library header.
28  */
29
30 #ifndef _GLIBCXX_MUTEX
31 #define _GLIBCXX_MUTEX 1
32
33 #pragma GCC system_header
34
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
37 #else
38
39 #include <tuple>
40 #include <chrono>
41 #include <exception>
42 #include <type_traits>
43 #include <functional>
44 #include <system_error>
45 #include <bits/functexcept.h>
46 #include <bits/gthr.h>
47 #include <bits/move.h> // for std::swap
48
49 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
50
51 namespace std _GLIBCXX_VISIBILITY(default)
52 {
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54
55   /**
56    * @defgroup mutexes Mutexes
57    * @ingroup concurrency
58    *
59    * Classes for mutex support.
60    * @{
61    */
62
63   /// mutex
64   class mutex
65   {
66     typedef __gthread_mutex_t                   __native_type;
67     __native_type  _M_mutex;
68
69   public:
70     typedef __native_type*                      native_handle_type;
71
72 #ifdef __GTHREAD_MUTEX_INIT
73     constexpr mutex() noexcept : _M_mutex(__GTHREAD_MUTEX_INIT) { }
74 #else
75     mutex() noexcept
76     {
77       // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
78       __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
79     }
80
81     ~mutex() { __gthread_mutex_destroy(&_M_mutex); }
82 #endif
83
84     mutex(const mutex&) = delete;
85     mutex& operator=(const mutex&) = delete;
86
87     void
88     lock()
89     {
90       int __e = __gthread_mutex_lock(&_M_mutex);
91
92       // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
93       if (__e)
94         __throw_system_error(__e);
95     }
96
97     bool
98     try_lock() noexcept
99     {
100       // XXX EINVAL, EAGAIN, EBUSY
101       return !__gthread_mutex_trylock(&_M_mutex);
102     }
103
104     void
105     unlock()
106     {
107       // XXX EINVAL, EAGAIN, EPERM
108       __gthread_mutex_unlock(&_M_mutex);
109     }
110
111     native_handle_type
112     native_handle()
113     { return &_M_mutex; }
114   };
115
116 #ifndef __GTHREAD_RECURSIVE_MUTEX_INIT
117   // FIXME: gthreads doesn't define __gthread_recursive_mutex_destroy
118   // so we need to obtain a __gthread_mutex_t to destroy
119   class __destroy_recursive_mutex
120   {
121     template<typename _Mx, typename _Rm>
122       static void
123       _S_destroy_win32(_Mx* __mx, _Rm const* __rmx)
124       {
125         __mx->counter = __rmx->counter;
126         __mx->sema = __rmx->sema;
127         __gthread_mutex_destroy(__mx);
128       }
129
130   public:
131     // matches a gthr-win32.h recursive mutex
132     template<typename _Rm>
133       static typename enable_if<sizeof(&_Rm::sema), void>::type
134       _S_destroy(_Rm* __mx)
135       {
136         __gthread_mutex_t __tmp;
137         _S_destroy_win32(&__tmp, __mx);
138       }
139
140     // matches a recursive mutex with a member 'actual'
141     template<typename _Rm>
142       static typename enable_if<sizeof(&_Rm::actual), void>::type
143       _S_destroy(_Rm* __mx)
144       { __gthread_mutex_destroy(&__mx->actual); }
145
146     // matches when there's only one mutex type
147     template<typename _Rm>
148       static
149       typename enable_if<is_same<_Rm, __gthread_mutex_t>::value, void>::type
150       _S_destroy(_Rm* __mx)
151       { __gthread_mutex_destroy(__mx); }
152   };
153 #endif
154
155   /// recursive_mutex
156   class recursive_mutex
157   {
158     typedef __gthread_recursive_mutex_t         __native_type;
159     __native_type  _M_mutex;
160
161   public:
162     typedef __native_type*                      native_handle_type;
163
164 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
165     recursive_mutex() : _M_mutex(__GTHREAD_RECURSIVE_MUTEX_INIT) { }
166 #else
167     recursive_mutex()
168     {
169       // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
170       __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
171     }
172
173     ~recursive_mutex()
174     { __destroy_recursive_mutex::_S_destroy(&_M_mutex); }
175 #endif
176
177     recursive_mutex(const recursive_mutex&) = delete;
178     recursive_mutex& operator=(const recursive_mutex&) = delete;
179
180     void
181     lock()
182     {
183       int __e = __gthread_recursive_mutex_lock(&_M_mutex);
184
185       // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
186       if (__e)
187         __throw_system_error(__e);
188     }
189
190     bool
191     try_lock() noexcept
192     {
193       // XXX EINVAL, EAGAIN, EBUSY
194       return !__gthread_recursive_mutex_trylock(&_M_mutex);
195     }
196
197     void
198     unlock()
199     {
200       // XXX EINVAL, EAGAIN, EBUSY
201       __gthread_recursive_mutex_unlock(&_M_mutex);
202     }
203
204     native_handle_type
205     native_handle()
206     { return &_M_mutex; }
207   };
208
209 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
210   /// timed_mutex
211   class timed_mutex
212   {
213     typedef __gthread_mutex_t                   __native_type;
214
215 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
216     typedef chrono::steady_clock                __clock_t;
217 #else
218     typedef chrono::high_resolution_clock       __clock_t;
219 #endif
220
221     __native_type  _M_mutex;
222
223   public:
224     typedef __native_type*                      native_handle_type;
225
226 #ifdef __GTHREAD_MUTEX_INIT
227     timed_mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { }
228 #else
229     timed_mutex()
230     {
231       __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
232     }
233
234     ~timed_mutex() { __gthread_mutex_destroy(&_M_mutex); }
235 #endif
236
237     timed_mutex(const timed_mutex&) = delete;
238     timed_mutex& operator=(const timed_mutex&) = delete;
239
240     void
241     lock()
242     {
243       int __e = __gthread_mutex_lock(&_M_mutex);
244
245       // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
246       if (__e)
247         __throw_system_error(__e);
248     }
249
250     bool
251     try_lock() noexcept
252     {
253       // XXX EINVAL, EAGAIN, EBUSY
254       return !__gthread_mutex_trylock(&_M_mutex);
255     }
256
257     template <class _Rep, class _Period>
258       bool
259       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
260       { return __try_lock_for_impl(__rtime); }
261
262     template <class _Clock, class _Duration>
263       bool
264       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
265       {
266         chrono::time_point<_Clock, chrono::seconds> __s =
267           chrono::time_point_cast<chrono::seconds>(__atime);
268
269         chrono::nanoseconds __ns =
270           chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
271
272         __gthread_time_t __ts = {
273           static_cast<std::time_t>(__s.time_since_epoch().count()),
274           static_cast<long>(__ns.count())
275         };
276
277         return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
278       }
279
280     void
281     unlock()
282     {
283       // XXX EINVAL, EAGAIN, EBUSY
284       __gthread_mutex_unlock(&_M_mutex);
285     }
286
287     native_handle_type
288     native_handle()
289     { return &_M_mutex; }
290
291   private:
292     template<typename _Rep, typename _Period>
293       typename enable_if<
294         ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
295       __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
296       {
297         __clock_t::time_point __atime = __clock_t::now()
298           + chrono::duration_cast<__clock_t::duration>(__rtime);
299
300         return try_lock_until(__atime);
301       }
302
303     template <typename _Rep, typename _Period>
304       typename enable_if<
305         !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
306       __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
307       {
308         __clock_t::time_point __atime = __clock_t::now()
309           + ++chrono::duration_cast<__clock_t::duration>(__rtime);
310
311         return try_lock_until(__atime);
312       }
313   };
314
315   /// recursive_timed_mutex
316   class recursive_timed_mutex
317   {
318     typedef __gthread_recursive_mutex_t         __native_type;
319
320 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
321     typedef chrono::steady_clock                __clock_t;
322 #else
323     typedef chrono::high_resolution_clock       __clock_t;
324 #endif
325
326     __native_type  _M_mutex;
327
328   public:
329     typedef __native_type*                      native_handle_type;
330
331 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
332     recursive_timed_mutex() : _M_mutex(__GTHREAD_RECURSIVE_MUTEX_INIT) { }
333 #else
334     recursive_timed_mutex()
335     {
336       // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
337       __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
338     }
339
340     ~recursive_timed_mutex()
341     { __destroy_recursive_mutex::_S_destroy(&_M_mutex); }
342 #endif
343
344     recursive_timed_mutex(const recursive_timed_mutex&) = delete;
345     recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
346
347     void
348     lock()
349     {
350       int __e = __gthread_recursive_mutex_lock(&_M_mutex);
351
352       // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
353       if (__e)
354         __throw_system_error(__e);
355     }
356
357     bool
358     try_lock() noexcept
359     {
360       // XXX EINVAL, EAGAIN, EBUSY
361       return !__gthread_recursive_mutex_trylock(&_M_mutex);
362     }
363
364     template <class _Rep, class _Period>
365       bool
366       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
367       { return __try_lock_for_impl(__rtime); }
368
369     template <class _Clock, class _Duration>
370       bool
371       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
372       {
373         chrono::time_point<_Clock, chrono::seconds>  __s =
374           chrono::time_point_cast<chrono::seconds>(__atime);
375
376         chrono::nanoseconds __ns =
377           chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
378
379         __gthread_time_t __ts = {
380           static_cast<std::time_t>(__s.time_since_epoch().count()),
381           static_cast<long>(__ns.count())
382         };
383
384         return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
385       }
386
387     void
388     unlock()
389     {
390       // XXX EINVAL, EAGAIN, EBUSY
391       __gthread_recursive_mutex_unlock(&_M_mutex);
392     }
393
394     native_handle_type
395     native_handle()
396     { return &_M_mutex; }
397
398   private:
399     template<typename _Rep, typename _Period>
400       typename enable_if<
401         ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
402       __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
403       {
404         __clock_t::time_point __atime = __clock_t::now()
405           + chrono::duration_cast<__clock_t::duration>(__rtime);
406
407         return try_lock_until(__atime);
408       }
409
410     template <typename _Rep, typename _Period>
411       typename enable_if<
412         !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
413       __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
414       {
415         __clock_t::time_point __atime = __clock_t::now()
416           + ++chrono::duration_cast<__clock_t::duration>(__rtime);
417
418         return try_lock_until(__atime);
419       }
420   };
421 #endif
422
423   /// Do not acquire ownership of the mutex.
424   struct defer_lock_t { };
425
426   /// Try to acquire ownership of the mutex without blocking.
427   struct try_to_lock_t { };
428
429   /// Assume the calling thread has already obtained mutex ownership
430   /// and manage it.
431   struct adopt_lock_t { };
432
433   constexpr defer_lock_t        defer_lock { };
434   constexpr try_to_lock_t       try_to_lock { };
435   constexpr adopt_lock_t        adopt_lock { };
436
437   /// @brief  Scoped lock idiom.
438   // Acquire the mutex here with a constructor call, then release with
439   // the destructor call in accordance with RAII style.
440   template<typename _Mutex>
441     class lock_guard
442     {
443     public:
444       typedef _Mutex mutex_type;
445
446       explicit lock_guard(mutex_type& __m) : _M_device(__m)
447       { _M_device.lock(); }
448
449       lock_guard(mutex_type& __m, adopt_lock_t) : _M_device(__m)
450       { } // calling thread owns mutex
451
452       ~lock_guard()
453       { _M_device.unlock(); }
454
455       lock_guard(const lock_guard&) = delete;
456       lock_guard& operator=(const lock_guard&) = delete;
457
458     private:
459       mutex_type&  _M_device;
460     };
461
462   /// unique_lock
463   template<typename _Mutex>
464     class unique_lock
465     {
466     public:
467       typedef _Mutex mutex_type;
468
469       unique_lock() noexcept
470       : _M_device(0), _M_owns(false)
471       { }
472
473       explicit unique_lock(mutex_type& __m)
474       : _M_device(&__m), _M_owns(false)
475       {
476         lock();
477         _M_owns = true;
478       }
479
480       unique_lock(mutex_type& __m, defer_lock_t) noexcept
481       : _M_device(&__m), _M_owns(false)
482       { }
483
484       unique_lock(mutex_type& __m, try_to_lock_t)
485       : _M_device(&__m), _M_owns(_M_device->try_lock())
486       { }
487
488       unique_lock(mutex_type& __m, adopt_lock_t)
489       : _M_device(&__m), _M_owns(true)
490       {
491         // XXX calling thread owns mutex
492       }
493
494       template<typename _Clock, typename _Duration>
495         unique_lock(mutex_type& __m,
496                     const chrono::time_point<_Clock, _Duration>& __atime)
497         : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
498         { }
499
500       template<typename _Rep, typename _Period>
501         unique_lock(mutex_type& __m,
502                     const chrono::duration<_Rep, _Period>& __rtime)
503         : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
504         { }
505
506       ~unique_lock()
507       {
508         if (_M_owns)
509           unlock();
510       }
511
512       unique_lock(const unique_lock&) = delete;
513       unique_lock& operator=(const unique_lock&) = delete;
514
515       unique_lock(unique_lock&& __u) noexcept
516       : _M_device(__u._M_device), _M_owns(__u._M_owns)
517       {
518         __u._M_device = 0;
519         __u._M_owns = false;
520       }
521
522       unique_lock& operator=(unique_lock&& __u) noexcept
523       {
524         if(_M_owns)
525           unlock();
526
527         unique_lock(std::move(__u)).swap(*this);
528
529         __u._M_device = 0;
530         __u._M_owns = false;
531
532         return *this;
533       }
534
535       void
536       lock()
537       {
538         if (!_M_device)
539           __throw_system_error(int(errc::operation_not_permitted));
540         else if (_M_owns)
541           __throw_system_error(int(errc::resource_deadlock_would_occur));
542         else
543           {
544             _M_device->lock();
545             _M_owns = true;
546           }
547       }
548
549       bool
550       try_lock()
551       {
552         if (!_M_device)
553           __throw_system_error(int(errc::operation_not_permitted));
554         else if (_M_owns)
555           __throw_system_error(int(errc::resource_deadlock_would_occur));
556         else
557           {
558             _M_owns = _M_device->try_lock();
559             return _M_owns;
560           }
561       }
562
563       template<typename _Clock, typename _Duration>
564         bool
565         try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
566         {
567           if (!_M_device)
568             __throw_system_error(int(errc::operation_not_permitted));
569           else if (_M_owns)
570             __throw_system_error(int(errc::resource_deadlock_would_occur));
571           else
572             {
573               _M_owns = _M_device->try_lock_until(__atime);
574               return _M_owns;
575             }
576         }
577
578       template<typename _Rep, typename _Period>
579         bool
580         try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
581         {
582           if (!_M_device)
583             __throw_system_error(int(errc::operation_not_permitted));
584           else if (_M_owns)
585             __throw_system_error(int(errc::resource_deadlock_would_occur));
586           else
587             {
588               _M_owns = _M_device->try_lock_for(__rtime);
589               return _M_owns;
590             }
591          }
592
593       void
594       unlock()
595       {
596         if (!_M_owns)
597           __throw_system_error(int(errc::operation_not_permitted));
598         else if (_M_device)
599           {
600             _M_device->unlock();
601             _M_owns = false;
602           }
603       }
604
605       void
606       swap(unique_lock& __u) noexcept
607       {
608         std::swap(_M_device, __u._M_device);
609         std::swap(_M_owns, __u._M_owns);
610       }
611
612       mutex_type*
613       release() noexcept
614       {
615         mutex_type* __ret = _M_device;
616         _M_device = 0;
617         _M_owns = false;
618         return __ret;
619       }
620
621       bool
622       owns_lock() const noexcept
623       { return _M_owns; }
624
625       explicit operator bool() const noexcept
626       { return owns_lock(); }
627
628       mutex_type*
629       mutex() const noexcept
630       { return _M_device; }
631
632     private:
633       mutex_type*       _M_device;
634       bool              _M_owns; // XXX use atomic_bool
635     };
636
637   /// Partial specialization for unique_lock objects.
638   template<typename _Mutex>
639     inline void
640     swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
641     { __x.swap(__y); }
642
643   template<int _Idx>
644     struct __unlock_impl
645     {
646       template<typename... _Lock>
647         static void
648         __do_unlock(tuple<_Lock&...>& __locks)
649         {
650           std::get<_Idx>(__locks).unlock();
651           __unlock_impl<_Idx - 1>::__do_unlock(__locks);
652         }
653     };
654
655   template<>
656     struct __unlock_impl<-1>
657     {
658       template<typename... _Lock>
659         static void
660         __do_unlock(tuple<_Lock&...>&)
661         { }
662     };
663
664   template<typename _Lock>
665     unique_lock<_Lock>
666     __try_to_lock(_Lock& __l)
667     { return unique_lock<_Lock>(__l, try_to_lock); }
668
669   template<int _Idx, bool _Continue = true>
670     struct __try_lock_impl
671     {
672       template<typename... _Lock>
673         static void
674         __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
675         {
676           __idx = _Idx;
677           auto __lock = __try_to_lock(std::get<_Idx>(__locks));
678           if (__lock.owns_lock())
679             {
680               __try_lock_impl<_Idx + 1, _Idx + 2 < sizeof...(_Lock)>::
681                 __do_try_lock(__locks, __idx);
682               if (__idx == -1)
683                 __lock.release();
684             }
685         }
686     };
687
688   template<int _Idx>
689     struct __try_lock_impl<_Idx, false>
690     {
691       template<typename... _Lock>
692         static void
693         __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
694         {
695           __idx = _Idx;
696           auto __lock = __try_to_lock(std::get<_Idx>(__locks));
697           if (__lock.owns_lock())
698             {
699               __idx = -1;
700               __lock.release();
701             }
702         }
703     };
704
705   /** @brief Generic try_lock.
706    *  @param __l1 Meets Mutex requirements (try_lock() may throw).
707    *  @param __l2 Meets Mutex requirements (try_lock() may throw).
708    *  @param __l3 Meets Mutex requirements (try_lock() may throw).
709    *  @return Returns -1 if all try_lock() calls return true. Otherwise returns
710    *          a 0-based index corresponding to the argument that returned false.
711    *  @post Either all arguments are locked, or none will be.
712    *
713    *  Sequentially calls try_lock() on each argument.
714    */
715   template<typename _Lock1, typename _Lock2, typename... _Lock3>
716     int
717     try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
718     {
719       int __idx;
720       auto __locks = std::tie(__l1, __l2, __l3...);
721       __try
722       { __try_lock_impl<0>::__do_try_lock(__locks, __idx); }
723       __catch(...)
724       { }
725       return __idx;
726     }
727
728   /** @brief Generic lock.
729    *  @param __l1 Meets Mutex requirements (try_lock() may throw).
730    *  @param __l2 Meets Mutex requirements (try_lock() may throw).
731    *  @param __l3 Meets Mutex requirements (try_lock() may throw).
732    *  @throw An exception thrown by an argument's lock() or try_lock() member.
733    *  @post All arguments are locked.
734    *
735    *  All arguments are locked via a sequence of calls to lock(), try_lock()
736    *  and unlock().  If the call exits via an exception any locks that were
737    *  obtained will be released.
738    */
739   template<typename _L1, typename _L2, typename ..._L3>
740     void
741     lock(_L1& __l1, _L2& __l2, _L3&... __l3)
742     {
743       while (true)
744         {
745           unique_lock<_L1> __first(__l1);
746           int __idx;
747           auto __locks = std::tie(__l2, __l3...);
748           __try_lock_impl<0, sizeof...(_L3)>::__do_try_lock(__locks, __idx);
749           if (__idx == -1)
750             {
751               __first.release();
752               return;
753             }
754         }
755     }
756
757   /// once_flag
758   struct once_flag
759   {
760   private:
761     typedef __gthread_once_t __native_type;
762     __native_type  _M_once;
763
764   public:
765     /// Constructor
766     constexpr once_flag() noexcept : _M_once(__GTHREAD_ONCE_INIT) { }
767
768     /// Deleted copy constructor
769     once_flag(const once_flag&) = delete;
770     /// Deleted assignment operator
771     once_flag& operator=(const once_flag&) = delete;
772
773     template<typename _Callable, typename... _Args>
774       friend void
775       call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
776   };
777
778 #ifdef _GLIBCXX_HAVE_TLS
779   extern __thread void* __once_callable;
780   extern __thread void (*__once_call)();
781
782   template<typename _Callable>
783     inline void
784     __once_call_impl()
785     {
786       (*(_Callable*)__once_callable)();
787     }
788 #else
789   extern function<void()> __once_functor;
790
791   extern void
792   __set_once_functor_lock_ptr(unique_lock<mutex>*);
793
794   extern mutex&
795   __get_once_mutex();
796 #endif
797
798   extern "C" void __once_proxy();
799
800   /// call_once
801   template<typename _Callable, typename... _Args>
802     void
803     call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
804     {
805 #ifdef _GLIBCXX_HAVE_TLS
806       auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
807           std::forward<_Args>(__args)...);
808       __once_callable = &__bound_functor;
809       __once_call = &__once_call_impl<decltype(__bound_functor)>;
810 #else
811       unique_lock<mutex> __functor_lock(__get_once_mutex());
812       __once_functor = std::__bind_simple(std::forward<_Callable>(__f),
813           std::forward<_Args>(__args)...);
814       __set_once_functor_lock_ptr(&__functor_lock);
815 #endif
816
817       int __e = __gthread_once(&(__once._M_once), &__once_proxy);
818
819 #ifndef _GLIBCXX_HAVE_TLS
820       if (__functor_lock)
821         __set_once_functor_lock_ptr(0);
822 #endif
823
824       if (__e)
825         __throw_system_error(__e);
826     }
827
828   // @} group mutexes
829 _GLIBCXX_END_NAMESPACE_VERSION
830 } // namespace
831
832 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
833
834 #endif // __GXX_EXPERIMENTAL_CXX0X__
835
836 #endif // _GLIBCXX_MUTEX