blocxx

ThreadTypes.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00039 #ifndef BLOCXX_THREAD_TYPES_HPP_
00040 #define BLOCXX_THREAD_TYPES_HPP_
00041 #include "blocxx/BLOCXX_config.h"
00042 
00043 // The classes and functions defined in this file are not meant for general
00044 // use, they are internal implementation details.  They may change at any time.
00045 
00046 #if defined(BLOCXX_USE_PTHREAD)
00047 
00048    #include <pthread.h>
00049 
00050 #ifdef BLOCXX_NCR //for the function pthread_cond_init
00051 #define PTHREAD_COND_ATTR_DEFAULT  pthread_condattr_default
00052 #else
00053 #define PTHREAD_COND_ATTR_DEFAULT  0
00054 #endif
00055 
00056    namespace BLOCXX_NAMESPACE
00057    {
00058 
00059       // Platform specific thread type
00060       typedef pthread_t       Thread_t;
00061       typedef pthread_mutex_t NativeMutex_t;
00062       struct NonRecursiveMutex_t
00063       {
00064          pthread_mutex_t mutex;
00065       };
00066 
00067       #if defined(BLOCXX_HAVE_PTHREAD_MUTEXATTR_SETTYPE)
00068       // Platform specific mutex type
00069       // we have native recursive mutexes.
00070       struct Mutex_t
00071       {
00072          pthread_mutex_t mutex;
00073       };
00074 
00075       #else
00076 
00077       // we have to emulate recursive mutexes.
00078       struct Mutex_t
00079       {
00080          pthread_mutex_t mutex;
00081          pthread_cond_t unlocked;
00082          bool valid_id;
00083          unsigned count;
00084          pthread_t thread_id;
00085       };
00086       #endif
00087 
00088       // Platform specific conditional variable type
00089       typedef pthread_cond_t        ConditionVar_t;
00090       struct NonRecursiveMutexLockState
00091       {
00092          pthread_t thread_id;
00093          NativeMutex_t* pmutex;
00094       };
00095 
00096    } // end namespace BLOCXX_NAMESPACE
00097 
00098 #elif defined(BLOCXX_WIN32)
00099 
00100 #define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
00101 #include <wtypes.h>
00102 
00103    namespace BLOCXX_NAMESPACE
00104    {
00105       // Platform specific thread type
00106       typedef DWORD Thread_t;
00107       typedef HANDLE NativeMutex_t;
00108       typedef HANDLE NonRecursiveMutex_t;
00109       typedef LPCRITICAL_SECTION Mutex_t;
00110 
00111       // Platform specific conditional variable type
00112       typedef struct
00113       {
00114          // Number of waiting threads
00115          int waitersCount;
00116          // Serialize access to waitersCount
00117          CRITICAL_SECTION waitersCountLock;
00118          // Semaphore used to queue up threads waiting for the condition to
00119          // become signaled
00120          HANDLE queue;
00121          // An auto-reset event used during broadcasting to wait for all the 
00122          // threads to wake up and be released from the queue
00123          HANDLE waitersDone;
00124          // Keeps track of whether we are broadcasting or signaling. This allows
00125          // for optimization if just signaling.
00126          bool wasBroadcast;
00127       } ConditionInfo_t;
00128 
00129       typedef ConditionInfo_t* ConditionVar_t;
00130       //typedef void*         ConditionVar_t;
00131       struct NonRecursiveMutexLockState
00132       {
00133          DWORD thread_id;
00134          NativeMutex_t* pmutex;
00135       };
00136 
00137    } // end namespace BLOCXX_NAMESPACE
00138 
00139 #endif
00140 
00141 #endif   // #ifndef BLOCXX_THREAD_TYPES_HPP_
00142