Skip to content

Commit c7c290b

Browse files
Merge branch 'iss-14' into develop
2 parents 3c9148c + 1775b1d commit c7c290b

File tree

18 files changed

+384
-57
lines changed

18 files changed

+384
-57
lines changed

cpp-pthread.xcodeproj/project.pbxproj

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
E519A0E41C9C4B4F00ED113D /* lock_guard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = lock_guard.cpp; path = src/lock_guard.cpp; sourceTree = "<group>"; };
4646
E519A0FF1C9C7CAB00ED113D /* thread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = thread.cpp; path = src/thread.cpp; sourceTree = "<group>"; };
4747
E54543351CAD21A30051A313 /* cpp11-pthread-tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "cpp11-pthread-tests"; sourceTree = BUILT_PRODUCTS_DIR; };
48-
E54543371CAD21A30051A313 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
4948
E5EC5B711C9C2C3000E21131 /* libcpp-pthread.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libcpp-pthread.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5049
E5EC5B791C9C2C6B00E21131 /* condition_variable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = condition_variable.cpp; path = src/condition_variable.cpp; sourceTree = "<group>"; };
5150
E5EC5B7B1C9C2C6B00E21131 /* mutex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = mutex.cpp; path = src/mutex.cpp; sourceTree = "<group>"; };
@@ -79,21 +78,12 @@
7978
path = tests;
8079
sourceTree = "<group>";
8180
};
82-
E54543361CAD21A30051A313 /* cpp11-pthread-tests */ = {
83-
isa = PBXGroup;
84-
children = (
85-
E54543371CAD21A30051A313 /* main.m */,
86-
);
87-
path = "cpp11-pthread-tests";
88-
sourceTree = "<group>";
89-
};
9081
E5EC5B681C9C2C3000E21131 = {
9182
isa = PBXGroup;
9283
children = (
9384
E5EC5B821C9C2C9000E21131 /* include */,
9485
E5EC5B801C9C2C7100E21131 /* src */,
9586
E519A0F31C9C64F600ED113D /* tests */,
96-
E54543361CAD21A30051A313 /* cpp11-pthread-tests */,
9787
E5EC5B721C9C2C3000E21131 /* Products */,
9888
);
9989
sourceTree = "<group>";
@@ -382,6 +372,7 @@
382372
E545433B1CAD21A30051A313 /* Release */,
383373
);
384374
defaultConfigurationIsVisible = 0;
375+
defaultConfigurationName = Release;
385376
};
386377
E5EC5B6C1C9C2C3000E21131 /* Build configuration list for PBXProject "cpp-pthread" */ = {
387378
isa = XCConfigurationList;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0730"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "E5B044091CAE7342005C89BB"
18+
BuildableName = "without-cpp11-pthread-tests"
19+
BlueprintName = "without-cpp11-pthread-tests"
20+
ReferencedContainer = "container:cpp-pthread.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
<MacroExpansion>
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "E5B044091CAE7342005C89BB"
36+
BuildableName = "without-cpp11-pthread-tests"
37+
BlueprintName = "without-cpp11-pthread-tests"
38+
ReferencedContainer = "container:cpp-pthread.xcodeproj">
39+
</BuildableReference>
40+
</MacroExpansion>
41+
<AdditionalOptions>
42+
</AdditionalOptions>
43+
</TestAction>
44+
<LaunchAction
45+
buildConfiguration = "Debug"
46+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
47+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
48+
launchStyle = "0"
49+
useCustomWorkingDirectory = "NO"
50+
ignoresPersistentStateOnLaunch = "NO"
51+
debugDocumentVersioning = "YES"
52+
debugServiceExtension = "internal"
53+
allowLocationSimulation = "YES">
54+
<BuildableProductRunnable
55+
runnableDebuggingMode = "0">
56+
<BuildableReference
57+
BuildableIdentifier = "primary"
58+
BlueprintIdentifier = "E5B044091CAE7342005C89BB"
59+
BuildableName = "without-cpp11-pthread-tests"
60+
BlueprintName = "without-cpp11-pthread-tests"
61+
ReferencedContainer = "container:cpp-pthread.xcodeproj">
62+
</BuildableReference>
63+
</BuildableProductRunnable>
64+
<AdditionalOptions>
65+
</AdditionalOptions>
66+
</LaunchAction>
67+
<ProfileAction
68+
buildConfiguration = "Release"
69+
shouldUseLaunchSchemeArgsEnv = "YES"
70+
savedToolIdentifier = ""
71+
useCustomWorkingDirectory = "NO"
72+
debugDocumentVersioning = "YES">
73+
<BuildableProductRunnable
74+
runnableDebuggingMode = "0">
75+
<BuildableReference
76+
BuildableIdentifier = "primary"
77+
BlueprintIdentifier = "E5B044091CAE7342005C89BB"
78+
BuildableName = "without-cpp11-pthread-tests"
79+
BlueprintName = "without-cpp11-pthread-tests"
80+
ReferencedContainer = "container:cpp-pthread.xcodeproj">
81+
</BuildableReference>
82+
</BuildableProductRunnable>
83+
</ProfileAction>
84+
<AnalyzeAction
85+
buildConfiguration = "Debug">
86+
</AnalyzeAction>
87+
<ArchiveAction
88+
buildConfiguration = "Release"
89+
revealArchiveInOrganizer = "YES">
90+
</ArchiveAction>
91+
</Scheme>

include/pthread/condition_variable.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <time.h>
1515
#include <sys/time.h>
1616

17+
#include "pthread/config.h"
18+
1719
#include "pthread/pthread_exception.hpp"
1820
#include "pthread/mutex.hpp"
1921
#include "pthread/lock_guard.hpp"
@@ -101,11 +103,16 @@ namespace pthread {
101103
*
102104
* Upon successful return, the mutex has been locked and is owned by the calling thread.
103105
*
106+
* If this method is called with millis < 0 then the timeout time is not recalculated. This make it possible to handle spurious
107+
* unblocking of condition variable without the need of a lambda expression. The call sequence is then: while(! check_condition() && wait_for(lck, 200) == no_tiemout );
108+
*
104109
* @param mtx ralated mutex, which must be locked by the current thread.
105110
* @param millis milliseconds to wait for this instance to signaled.
106111
* @return cv_status (either timeout or no_timeout)
107112
*/
108113
cv_status wait_for (mutex &mtx, int millis );
114+
115+
cv_status wait_for (lock_guard<pthread::mutex> &lck, int millis );
109116

110117
/** @see #wait_for (mutex &, int)
111118
*/
@@ -150,12 +157,12 @@ namespace pthread {
150157
* The pthread_cond_signal() call unblocks at least one of the threads that are blocked
151158
* on the specified condition variable cond (if any threads are blocked on cond).
152159
*/
153-
void notify_one () noexcept;
160+
void notify_one () __NOEXCEPT__;
154161

155162
/** signal all waiting threads
156163
* The pthread_cond_broadcast() call unblocks all threads currently blocked on the specified condition variable cond.
157164
*/
158-
void notify_all () noexcept;
165+
void notify_all () __NOEXCEPT__;
159166

160167
// constructor/destructor ------------------------------------------------
161168

include/pthread/config.h.in

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@
2222

2323
/* autoconf section ----------------------------------------------------------------------*/
2424

25+
#ifndef pthread_config_h
26+
#define pthread_config_h
27+
2528
#ifdef __IBMCPP__
26-
#define noexcept throw()
29+
30+
#define __NOEXCEPT__ throw()
31+
#define __OVERRIDE__
32+
33+
#else
34+
35+
#define __NOEXCEPT__ noexcept
36+
#define __OVERRIDE__ override
37+
2738
#endif
2839

2940
#undef CPP_PTHREAD_VERSION
@@ -61,10 +72,4 @@
6172
#undef inline
6273
#endif
6374

64-
const char *cpp_pthread_version_version(){
65-
#ifndef CPP_PTHREAD_VERSION
66-
return "missing CPP_PTHREAD_VERSION define. Re-run configure" ;
67-
#else
68-
return CPP_PTHREAD_VERSION ;
69-
#endif
70-
}
75+
#endif /* pthread_config_h */

include/pthread/lock_guard.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <pthread.h>
1313

14+
#include "pthread/config.h"
15+
1416
#include "pthread/mutex.hpp"
1517

1618
namespace pthread {

include/pthread/mutex.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <exception>
1414
#include <string>
1515

16+
#include "pthread/config.h"
17+
1618
#include "pthread/pthread_exception.hpp"
1719

1820
namespace pthread {

include/pthread/pthread.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <exception>
1919
#include <string>
2020

21+
#include "pthread/config.h"
22+
2123
#include "pthread/mutex.hpp"
2224
#include "pthread/lock_guard.hpp"
2325
#include "pthread/condition_variable.hpp"

include/pthread/pthread_exception.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef pthread_pthread_exception_hpp
1010
#define pthread_pthread_exception_hpp
1111

12+
#include "pthread/config.h"
13+
1214
#include <errno.h>
1315

1416
namespace pthread {
@@ -25,12 +27,12 @@ namespace pthread {
2527
* @param message error message
2628
* @param pthread_errno a pthread function return code.
2729
*/
28-
pthread_exception( const string message, const int pthread_errno = 0 ): _message{message}, _pthread_errno{pthread_errno}{};
29-
30+
pthread_exception( const string message, const int pthread_number = 0 ): _message(message), _pthread_errno(pthread_number){};
31+
3032
virtual ~pthread_exception(){};
3133

3234
/** @return the exception's error message. */
33-
virtual const char *what() const noexcept override { return _message.c_str();};
35+
virtual const char *what() const __NOEXCEPT__ __OVERRIDE__ { return _message.c_str();};
3436

3537
/** @return pthread error code that was at the orgin of the error */
3638
virtual int pthread_errno(){ return _pthread_errno ;};
@@ -49,7 +51,7 @@ namespace pthread {
4951
class timeout_exception: public pthread_exception{
5052
public:
5153
/** thrown when a time out occurs. */
52-
timeout_exception(const string message): pthread_exception{message, ETIMEDOUT}{};
54+
timeout_exception(const string message): pthread_exception(message, ETIMEDOUT){};
5355
};
5456

5557
} // namespace pthread

include/pthread/thread.hpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <string>
1414
#include <functional>
1515
#include <memory>
16+
#include <list>
17+
18+
#include "pthread/config.h"
1619

1720
#include "pthread/pthread_exception.hpp"
1821
#include "pthread/mutex.hpp"
@@ -45,7 +48,7 @@ namespace pthread {
4548
/**
4649
* This method must be overriden
4750
*/
48-
virtual void run () noexcept = 0 ;
51+
virtual void run () __NOEXCEPT__ = 0 ;
4952
};
5053

5154
/**
@@ -159,6 +162,52 @@ namespace pthread {
159162
thread_status _status;
160163
};
161164

165+
class abstract_thread: public runnable {
166+
public:
167+
virtual ~abstract_thread();
168+
169+
void start();
170+
171+
int join() { return _thread->join() ;};
172+
173+
private:
174+
pthread::thread *_thread;
175+
};
176+
177+
/** group (list) of abstract_threads.
178+
*
179+
* method in this class apply's to all threads in the group. this means that when an thread_group instance is deallocated, all threads it references are deleted.
180+
*/
181+
class thread_group{
182+
public:
183+
/** Setup a thread container/list.
184+
*
185+
* @param destructor_joins_first if true then destructor tries to join all regsitered threads before deleting thread instances.
186+
*/
187+
thread_group( bool destructor_joins_first = false ) __NOEXCEPT__;
188+
189+
/** delete all threads referenced by the thread_group.
190+
*/
191+
virtual ~thread_group();
192+
193+
/** @param add/register a thread to the group.
194+
*/
195+
void add(abstract_thread *thread);
196+
197+
/** start run all registered threads.
198+
* @see add(abstract_thread *thread)
199+
*/
200+
void start();
201+
202+
/** what for all threads to join the caller of this method.
203+
*/
204+
void join();
205+
206+
private:
207+
std::list<pthread::abstract_thread*> _threads;
208+
bool _destructor_joins_first;
209+
};
210+
162211
// exception & errors --------
163212

164213
/** thrown to indicate that something went wrong with a thread */

src/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ globber: clean
5252

5353
clean:
5454
${RM} *.o core *.a
55-
${RM} ${TARGETS}
55+
${RM} $(LIBDIR)/libcpp-pthread.a ${TARGETS}
5656
$(RM) *unc-backup*~ *unc-backup*~
5757

5858
cxref:

0 commit comments

Comments
 (0)