blocxx
ThreadTypes.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_THREAD_TYPES_HPP_
40 #define BLOCXX_THREAD_TYPES_HPP_
41 #include "blocxx/BLOCXX_config.h"
42 
43 // The classes and functions defined in this file are not meant for general
44 // use, they are internal implementation details. They may change at any time.
45 
46 #if defined(BLOCXX_USE_PTHREAD)
47 
48  #include <pthread.h>
49 
50 #ifdef BLOCXX_NCR //for the function pthread_cond_init
51 #define PTHREAD_COND_ATTR_DEFAULT pthread_condattr_default
52 #else
53 #define PTHREAD_COND_ATTR_DEFAULT 0
54 #endif
55 
56  namespace BLOCXX_NAMESPACE
57  {
58 
59  // Platform specific thread type
60  typedef pthread_t Thread_t;
61  typedef pthread_mutex_t NativeMutex_t;
62  struct NonRecursiveMutex_t
63  {
64  pthread_mutex_t mutex;
65  };
66 
67  #if defined(BLOCXX_HAVE_PTHREAD_MUTEXATTR_SETTYPE)
68  // Platform specific mutex type
69  // we have native recursive mutexes.
70  struct Mutex_t
71  {
72  pthread_mutex_t mutex;
73  };
74 
75  #else
76 
77  // we have to emulate recursive mutexes.
78  struct Mutex_t
79  {
80  pthread_mutex_t mutex;
81  pthread_cond_t unlocked;
82  bool valid_id;
83  unsigned count;
84  pthread_t thread_id;
85  };
86  #endif
87 
88  // Platform specific conditional variable type
89  typedef pthread_cond_t ConditionVar_t;
90  struct NonRecursiveMutexLockState
91  {
92  pthread_t thread_id;
93  NativeMutex_t* pmutex;
94  };
95 
96  } // end namespace BLOCXX_NAMESPACE
97 
98 #elif defined(BLOCXX_WIN32)
99 
100 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
101 #include <wtypes.h>
102 
103  namespace BLOCXX_NAMESPACE
104  {
105  // Platform specific thread type
106  typedef DWORD Thread_t;
107  typedef HANDLE NativeMutex_t;
108  typedef HANDLE NonRecursiveMutex_t;
109  typedef LPCRITICAL_SECTION Mutex_t;
110 
111  // Platform specific conditional variable type
112  typedef struct
113  {
114  // Number of waiting threads
115  int waitersCount;
116  // Serialize access to waitersCount
117  CRITICAL_SECTION waitersCountLock;
118  // Semaphore used to queue up threads waiting for the condition to
119  // become signaled
120  HANDLE queue;
121  // An auto-reset event used during broadcasting to wait for all the
122  // threads to wake up and be released from the queue
123  HANDLE waitersDone;
124  // Keeps track of whether we are broadcasting or signaling. This allows
125  // for optimization if just signaling.
126  bool wasBroadcast;
127  } ConditionInfo_t;
128 
129  typedef ConditionInfo_t* ConditionVar_t;
130  //typedef void* ConditionVar_t;
131  struct NonRecursiveMutexLockState
132  {
133  DWORD thread_id;
134  NativeMutex_t* pmutex;
135  };
136 
137  } // end namespace BLOCXX_NAMESPACE
138 
139 #endif
140 
141 #endif // #ifndef BLOCXX_THREAD_TYPES_HPP_
142