Skip to content

Commit

Permalink
Thread: merge from trunk noit, default to version 3, synchronized_val…
Browse files Browse the repository at this point in the history
…ue move semantics

[SVN r81763]
  • Loading branch information
viboes committed Dec 7, 2012
1 parent ad3247d commit 233484f
Show file tree
Hide file tree
Showing 18 changed files with 595 additions and 320 deletions.
2 changes: 2 additions & 0 deletions build/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ project boost/thread
<toolset>gcc:<cxxflags>-Wno-long-long
<define>BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
<define>BOOST_SYSTEM_NO_DEPRECATED
#<define>BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS

<library>/boost/system//boost_system
#-pedantic -ansi -std=gnu++0x -Wextra -fpermissive
<warnings>all
Expand Down
28 changes: 11 additions & 17 deletions example/synchronized_person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//#define BOOST_THREAD_VERSION 4

// There is yet a limitation when BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET is defined
#define BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
#define BOOST_THREAD_VERSION 4

#include <iostream>
#include <string>
#include <boost/thread/synchronized_value.hpp>

#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES && ! defined BOOST_NO_CXX11_AUTO


//class SafePerson {
//public:
// std::string GetName() const {
Expand Down Expand Up @@ -161,12 +155,20 @@ class HelperPerson {
member(age)
{ }
std::string GetName() const {
auto&& memberSync = member.synchronize();
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
auto memberSync = member.synchronize();
#else
boost::synchronized_value<Member>::const_strict_synchronizer memberSync = member.synchronize();
#endif
Invariant(memberSync);
return memberSync->name;
}
void SetName(const std::string& newName) {
auto&& memberSync = member.synchronize();
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
auto memberSync = member.synchronize();
#else
boost::synchronized_value<Member>::strict_synchronizer memberSync = member.synchronize();
#endif
Invariant(memberSync);
memberSync->name = newName;
}
Expand Down Expand Up @@ -236,11 +238,3 @@ int main()
}
return 0;
}

#else

int main()
{
return 0;
}
#endif
42 changes: 16 additions & 26 deletions example/synchronized_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//#define BOOST_THREAD_VERSION 4

// There is yet a limitation when BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET is defined
#define BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
#define BOOST_THREAD_VERSION 4

#include <iostream>
#include <string>
#include <boost/thread/synchronized_value.hpp>

#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES

void addTrailingSlashIfMissing(boost::synchronized_value<std::string> & path)
{
boost::synchronized_value<std::string>::strict_synchronizer u=path.synchronize();
Expand Down Expand Up @@ -57,6 +52,21 @@ int main()
std::cout<<"v1="<<*u<<std::endl;
g(u);
}
boost::synchronized_value<int> v2(2);
std::cout<<"v2="<<*v2<<std::endl;
v2 = 3;
std::cout<<"v2="<<*v2<<std::endl;

boost::synchronized_value<int> v3(v2);
std::cout<<"v3="<<*v3<<std::endl;
v3 = v1;
std::cout<<"v3="<<*v3<<std::endl;

std::cout<<"v2="<<*v3<<std::endl;
std::cout<<"v3="<<*v3<<std::endl;
swap(v3,v2);
v1.swap(v2);
std::cout<<"v3="<<*v3<<std::endl;
}
{
boost::synchronized_value<std::string> s;
Expand All @@ -65,31 +75,11 @@ int main()
}
{
boost::synchronized_value<std::string> s;
#if 1
s->append("foo/");
#else
s.synchronize()->append("foo");
#endif
addTrailingSlashIfMissing(s);
std::cout<<"s="<<std::string(*s)<<std::endl;
}
{
boost::synchronized_value<std::string> s;
#if 1
s->append("foo");
#else
s.synchronize()->append("foo");
#endif
addTrailingSlashIfMissing(s);
std::cout<<"s="<<std::string(*s)<<std::endl;
}
return 0;
}

#else

int main()
{
return 0;
}
#endif
2 changes: 2 additions & 0 deletions example/thread_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ int main()
threads.create_thread(&increment_count);
threads.join_all();
}
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
{
boost::thread_group threads;
for (int i = 0; i < 3; ++i)
threads.create_thread(&increment_count);
threads.interrupt_all();
threads.join_all();
}
#endif
{
boost::thread_group threads;
boost::thread* th = new boost::thread(&increment_count);
Expand Down
4 changes: 2 additions & 2 deletions include/boost/thread/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
#define BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR
#endif

// Default version is 2
// Default version is 3
#if !defined BOOST_THREAD_VERSION
#define BOOST_THREAD_VERSION 2
#define BOOST_THREAD_VERSION 3
#else
#if BOOST_THREAD_VERSION!=2 && BOOST_THREAD_VERSION!=3 && BOOST_THREAD_VERSION!=4
#error "BOOST_THREAD_VERSION must be 2, 3 or 4"
Expand Down
18 changes: 10 additions & 8 deletions include/boost/thread/future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,30 @@ namespace boost
bool is_deferred_;
launch policy_;
bool is_constructed;
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
bool thread_was_interrupted;
#endif
boost::mutex mutex;
boost::condition_variable waiters;
typedef std::list<boost::condition_variable_any*> waiter_list;
waiter_list external_waiters;
boost::function<void()> callback;
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
bool thread_was_interrupted;
//#endif
#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
shared_ptr<future_continuation_base> continuation_ptr;
#else
shared_ptr<void> continuation_ptr;
#endif
future_object_base():
done(false),
is_deferred_(false),
policy_(launch::any),
is_constructed(false)
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
, thread_was_interrupted(false)
#endif
#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
, thread_was_interrupted(false)
//#endif
//#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
, continuation_ptr()
#endif
//#endif
{}
virtual ~future_object_base()
{}
Expand Down
8 changes: 8 additions & 0 deletions include/boost/thread/pthread/condition_variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace boost
thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
#else
boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
#endif
guard.activate(m);
do {
Expand Down Expand Up @@ -91,6 +93,8 @@ namespace boost
{
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
#else
boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
#endif
guard.activate(m);
cond_res=pthread_cond_timedwait(&cond,&internal_mutex,&timeout);
Expand Down Expand Up @@ -156,6 +160,8 @@ namespace boost
thread_cv_detail::lock_on_exit<lock_type> guard;
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
#else
boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
#endif
guard.activate(m);
res=pthread_cond_wait(&cond,&internal_mutex);
Expand Down Expand Up @@ -328,6 +334,8 @@ namespace boost
thread_cv_detail::lock_on_exit<lock_type> guard;
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
#else
boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
#endif
guard.activate(m);
res=pthread_cond_timedwait(&cond,&internal_mutex,&timeout);
Expand Down
11 changes: 9 additions & 2 deletions include/boost/thread/pthread/condition_variable_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
#include <boost/thread/lock_types.hpp>
#include <boost/thread/thread_time.hpp>
#include <boost/thread/pthread/timespec.hpp>
#if defined BOOST_THREAD_USES_DATETIME
#include <boost/thread/xtime.hpp>
#endif
#ifdef BOOST_THREAD_USES_CHRONO
#include <boost/chrono/system_clocks.hpp>
#include <boost/chrono/ceil.hpp>
#endif
#include <boost/thread/detail/delete.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>

#include <boost/config/abi_prefix.hpp>

namespace boost
Expand Down Expand Up @@ -64,12 +67,15 @@ namespace boost
}
~condition_variable()
{
BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
int ret;
do {
ret = pthread_mutex_destroy(&internal_mutex);
} while (ret == EINTR);
BOOST_ASSERT(!ret);
do {
ret = pthread_cond_destroy(&cond);
} while (ret == EINTR);
BOOST_VERIFY(!ret);
BOOST_ASSERT(!ret);
}

void wait(unique_lock<mutex>& m);
Expand Down Expand Up @@ -241,6 +247,7 @@ namespace boost
};

BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);

}


Expand Down
22 changes: 13 additions & 9 deletions include/boost/thread/pthread/thread_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ namespace boost
bool joined;
boost::detail::thread_exit_callback_node* thread_exit_callbacks;
std::map<void const*,boost::detail::tss_data_node> tss_data;
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
bool interrupt_enabled;
bool interrupt_requested;
#endif

pthread_mutex_t* cond_mutex;
pthread_cond_t* current_cond;
typedef std::vector<std::pair<condition_variable*, mutex*>
Expand All @@ -127,16 +124,23 @@ namespace boost
typedef std::vector<shared_ptr<future_object_base> > async_states_t;
async_states_t async_states_;

//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
// These data must be at the end so that the access to the other fields doesn't change
// when BOOST_THREAD_PROVIDES_INTERRUPTIONS is defined.
// Another option is to have them always
bool interrupt_enabled;
bool interrupt_requested;
//#endif
thread_data_base():
done(false),join_started(false),joined(false),
thread_exit_callbacks(0),
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
interrupt_enabled(true),
interrupt_requested(false),
#endif
current_cond(0),
notify(),
async_states_()
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
, interrupt_enabled(true)
, interrupt_requested(false)
//#endif
{}
virtual ~thread_data_base();

Expand Down Expand Up @@ -209,8 +213,8 @@ namespace boost
}
}
};
}
#endif
}

namespace this_thread
{
Expand Down
4 changes: 2 additions & 2 deletions include/boost/thread/strict_lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <boost/thread/detail/delete.hpp>
#include <boost/thread/lock_options.hpp>
#include <boost/thread/is_locked_by_this_thread.hpp>
//#include <boost/thread/is_locked_by_this_thread.hpp>
#include <boost/thread/lock_traits.hpp>
#include <boost/thread/lockable_traits.hpp>
#include <boost/thread/lockable_concepts.hpp>
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace boost
public:

/**
* @return whether if this lock is locking that mutex.
* @return whether this lock is locking that mutex.
*/
bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT
{
Expand Down
Loading

0 comments on commit 233484f

Please sign in to comment.