reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ThreadIf.hxx
Go to the documentation of this file.
1#if !defined(RESIP_THREADIF_HXX)
2#define RESIP_THREADIF_HXX
3
4#include <thread>
5
6#include "rutil/Socket.hxx"
7
8#ifdef WIN32
9# include <BaseTsd.h>
10# include <winbase.h>
11# include <map>
12#else
13# include <pthread.h>
14#endif
15
16#include "rutil/Mutex.hxx"
17#include "rutil/Condition.hxx"
18
19namespace resip
20{
21
46// Subclasses should use C++11 std::thread directly so that they will
47// be more readable.
48// FIXME: uncomment the RESIP_DEPRECATED macro after we stop using ThreadIf
49// within the stack itself.
50// FIXME: remove ThreadIf altogether after a delay of at least 6 months.
51// It remains here for transition purposes.
52//RESIP_DEPRECATED(
54{
55 public:
56 ThreadIf();
57 virtual ~ThreadIf();
58
59 // runs the code in thread() . Returns immediately
60 virtual void run();
61
62 // joins to the thread running thread()
63 void join();
64
65 // guarantees resources consumed by thread are released when thread terminates
66 // after this join can no-longer be used
67 void detach();
68
69 // request the thread running thread() to return, by setting mShutdown
70 virtual void shutdown();
71
72 //waits for waitMs, or stops waiting and returns true if shutdown was
73 //called
74 virtual bool waitForShutdown(int ms) const;
75
76 // returns true if the thread has been asked to shutdown or not running
77 bool isShutdown() const;
78
79 typedef std::thread::id Id;
80 static Id selfId();
81
82#ifdef WIN32
83 typedef DWORD TlsKey;
84#else
85 typedef pthread_key_t TlsKey;
86#endif
87 typedef void TlsDestructor(void*);
88
90 static int tlsKeyCreate(TlsKey &key, TlsDestructor *destructor);
92 static int tlsKeyDelete(TlsKey key);
94 static int tlsSetValue(TlsKey key, const void *val);
96 static void *tlsGetValue(TlsKey key);
97
98
99 /* thread is a virtual method. Users should derive and define
100 thread() such that it returns when isShutdown() is true.
101 */
102 virtual void thread() = 0;
103
104 protected:
105 std::shared_ptr<std::thread> mThreadObj;
106#ifdef WIN32
107 typedef std::map<DWORD,TlsDestructor *> TlsDestructorMap;
108 public:
110 static void tlsDestroyAll();
111 protected:
113 static TlsDestructorMap *mTlsDestructors;
115 static Mutex *mTlsDestructorsMutex;
116 friend class TlsDestructorInitializer;
117#endif
119
123
124 private:
125 // Suppress copying
126 ThreadIf(const ThreadIf &);
127 const ThreadIf & operator=(const ThreadIf &);
128};
129
130#ifdef WIN32
133public:
134 TlsDestructorInitializer();
135 ~TlsDestructorInitializer();
136protected:
137 static unsigned int mInstanceCounter;
138};
139static TlsDestructorInitializer _staticTlsInit;
140#endif
141
142}
143
144#endif
145
146/* ====================================================================
147 * The Vovida Software License, Version 1.0
148 *
149 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
150 *
151 * Redistribution and use in source and binary forms, with or without
152 * modification, are permitted provided that the following conditions
153 * are met:
154 *
155 * 1. Redistributions of source code must retain the above copyright
156 * notice, this list of conditions and the following disclaimer.
157 *
158 * 2. Redistributions in binary form must reproduce the above copyright
159 * notice, this list of conditions and the following disclaimer in
160 * the documentation and/or other materials provided with the
161 * distribution.
162 *
163 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
164 * and "Vovida Open Communication Application Library (VOCAL)" must
165 * not be used to endorse or promote products derived from this
166 * software without prior written permission. For written
167 * permission, please contact vocal@vovida.org.
168 *
169 * 4. Products derived from this software may not be called "VOCAL", nor
170 * may "VOCAL" appear in their name, without prior written
171 * permission of Vovida Networks, Inc.
172 *
173 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
174 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
176 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
177 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
178 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
179 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
180 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
181 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
182 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
184 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
185 * DAMAGE.
186 *
187 * ====================================================================
188 *
189 * This software consists of voluntary contributions made by Vovida
190 * Networks, Inc. and many individuals on behalf of Vovida Networks,
191 * Inc. For more information on Vovida Networks, Inc., please see
192 * <http://www.vovida.org/>.
193 *
194 */
Handles cross-platform sockets compatibility.
A wrapper class to create and spawn a thread.
Definition ThreadIf.hxx:54
static void * tlsGetValue(TlsKey key)
This function follows pthread_getspecific() signature.
Definition ThreadIf.cxx:179
void TlsDestructor(void *)
Definition ThreadIf.hxx:87
static int tlsKeyCreate(TlsKey &key, TlsDestructor *destructor)
This function follows pthread_key_create() signature.
Definition ThreadIf.cxx:130
std::shared_ptr< std::thread > mThreadObj
Definition ThreadIf.hxx:105
pthread_key_t TlsKey
Definition ThreadIf.hxx:85
virtual ~ThreadIf()
Definition ThreadIf.cxx:68
virtual void shutdown()
Definition ThreadIf.cxx:190
virtual void run()
Definition ThreadIf.cxx:75
static Id selfId()
Definition ThreadIf.cxx:124
static int tlsKeyDelete(TlsKey key)
This function follows pthread_key_delete() signature.
Definition ThreadIf.cxx:150
bool isShutdown() const
Definition ThreadIf.cxx:212
static int tlsSetValue(TlsKey key, const void *val)
This function follows pthread_setspecific() signature.
Definition ThreadIf.cxx:169
Condition mShutdownCondition
Definition ThreadIf.hxx:122
virtual bool waitForShutdown(int ms) const
Definition ThreadIf.cxx:201
virtual void thread()=0
std::thread::id Id
Definition ThreadIf.hxx:79
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
std::condition_variable Condition
A condition variable that can be signaled or waited on, wraps POSIX/Windows implementations depending...
Definition Condition.hxx:51
void list()
Definition userAdmin.cxx:66