stl_threads.h

Go to the documentation of this file.
00001 // Threading support -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  * Copyright (c) 1997-1999
00032  * Silicon Graphics Computer Systems, Inc.
00033  *
00034  * Permission to use, copy, modify, distribute and sell this software
00035  * and its documentation for any purpose is hereby granted without fee,
00036  * provided that the above copyright notice appear in all copies and
00037  * that both that copyright notice and this permission notice appear
00038  * in supporting documentation.  Silicon Graphics makes no
00039  * representations about the suitability of this software for any
00040  * purpose.  It is provided "as is" without express or implied warranty.
00041  */
00042 
00048 #ifndef _STL_THREADS_H
00049 #define _STL_THREADS_H 1
00050 
00051 #include <cstddef>
00052 
00053 // The only supported threading model is GCC's own gthr.h abstraction
00054 // layer.
00055 #include "bits/gthr.h"
00056 
00057 namespace __gnu_internal
00058 {
00059 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00060   extern __gthread_mutex_t _GLIBCXX_mutex;
00061   extern __gthread_mutex_t *_GLIBCXX_mutex_address;
00062   extern __gthread_once_t _GLIBCXX_once;
00063   extern void _GLIBCXX_mutex_init(void);
00064   extern void _GLIBCXX_mutex_address_init(void);
00065 #endif
00066 } // namespace __gnu_internal
00067 
00068 namespace __gnu_cxx
00069 {
00070   // Locking class.  Note that this class *does not have a
00071   // constructor*.  It must be initialized either statically, with
00072   // __STL_MUTEX_INITIALIZER, or dynamically, by explicitly calling
00073   // the _M_initialize member function.  (This is similar to the ways
00074   // that a pthreads mutex can be initialized.)  There are explicit
00075   // member functions for acquiring and releasing the lock.
00076 
00077   // There is no constructor because static initialization is
00078   // essential for some uses, and only a class aggregate (see section
00079   // 8.5.1 of the C++ standard) can be initialized that way.  That
00080   // means we must have no constructors, no base classes, no virtual
00081   // functions, and no private or protected members.
00082   struct _STL_mutex_lock
00083   {
00084     // The class must be statically initialized with __STL_MUTEX_INITIALIZER.
00085 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00086     volatile int _M_init_flag;
00087     __gthread_once_t _M_once;
00088 #endif
00089     __gthread_mutex_t _M_lock;
00090 
00091     void
00092     _M_initialize()
00093     {
00094 #ifdef __GTHREAD_MUTEX_INIT
00095       // There should be no code in this path given the usage rules above.
00096 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00097       if (_M_init_flag) return;
00098       if (__gthread_once(&__gnu_internal::_GLIBCXX_once,
00099              __gnu_internal::_GLIBCXX_mutex_init) != 0
00100       && __gthread_active_p())
00101     abort ();
00102       __gthread_mutex_lock(&__gnu_internal::_GLIBCXX_mutex);
00103       if (!_M_init_flag)
00104     {
00105       // Even though we have a global lock, we use __gthread_once to be
00106       // absolutely certain the _M_lock mutex is only initialized once on
00107       // multiprocessor systems.
00108       __gnu_internal::_GLIBCXX_mutex_address = &_M_lock;
00109       if (__gthread_once(&_M_once,
00110                  __gnu_internal::_GLIBCXX_mutex_address_init) != 0
00111         && __gthread_active_p())
00112         abort();
00113       _M_init_flag = 1;
00114     }
00115       __gthread_mutex_unlock(&__gnu_internal::_GLIBCXX_mutex);
00116 #endif
00117     }
00118 
00119     void
00120     _M_acquire_lock()
00121     {
00122 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00123       if (!_M_init_flag) _M_initialize();
00124 #endif
00125       __gthread_mutex_lock(&_M_lock);
00126     }
00127 
00128     void
00129     _M_release_lock()
00130     {
00131 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00132       if (!_M_init_flag) _M_initialize();
00133 #endif
00134       __gthread_mutex_unlock(&_M_lock);
00135     }
00136   };
00137 
00138 #ifdef __GTHREAD_MUTEX_INIT
00139 #define __STL_MUTEX_INITIALIZER = { __GTHREAD_MUTEX_INIT }
00140 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00141 #ifdef __GTHREAD_MUTEX_INIT_DEFAULT
00142 #define __STL_MUTEX_INITIALIZER \
00143   = { 0, __GTHREAD_ONCE_INIT, __GTHREAD_MUTEX_INIT_DEFAULT }
00144 #else
00145 #define __STL_MUTEX_INITIALIZER = { 0, __GTHREAD_ONCE_INIT }
00146 #endif
00147 #endif
00148 } // namespace __gnu_cxx
00149 
00150 #endif

Generated on Tue Jan 30 17:31:55 2007 for GNU C++ STL by doxygen 1.3.6