1 | // (C) Copyright Mac Murrett 2001. |
---|
2 | // Use, modification and distribution are subject to the |
---|
3 | // Boost Software License, Version 1.0. (See accompanying file |
---|
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | // See http://www.boost.org for most recent version. |
---|
7 | |
---|
8 | #ifndef BOOST_DELIVERY_MAN_MJM012402_HPP |
---|
9 | #define BOOST_DELIVERY_MAN_MJM012402_HPP |
---|
10 | |
---|
11 | |
---|
12 | #include <boost/function.hpp> |
---|
13 | #include <boost/utility.hpp> |
---|
14 | |
---|
15 | #include <boost/thread/mutex.hpp> |
---|
16 | |
---|
17 | #include "package.hpp" |
---|
18 | |
---|
19 | #include <Multiprocessing.h> |
---|
20 | |
---|
21 | |
---|
22 | namespace boost { |
---|
23 | |
---|
24 | namespace threads { |
---|
25 | |
---|
26 | namespace mac { |
---|
27 | |
---|
28 | namespace detail { |
---|
29 | |
---|
30 | |
---|
31 | // class delivery_man is intended to move boost::function objects from MP tasks to |
---|
32 | // other execution contexts (such as deferred task time or system task time). |
---|
33 | |
---|
34 | class delivery_man: private noncopyable |
---|
35 | { |
---|
36 | public: |
---|
37 | delivery_man(); |
---|
38 | ~delivery_man(); |
---|
39 | |
---|
40 | public: |
---|
41 | template<class R> |
---|
42 | R deliver(function<R> &rFunctor); |
---|
43 | |
---|
44 | void accept_deliveries(); |
---|
45 | |
---|
46 | private: |
---|
47 | base_package *m_pPackage; |
---|
48 | mutex m_oMutex; |
---|
49 | MPSemaphoreID m_pSemaphore; |
---|
50 | bool m_bPackageWaiting; |
---|
51 | }; |
---|
52 | |
---|
53 | |
---|
54 | template<class R> |
---|
55 | R delivery_man::deliver(function<R> &rFunctor) |
---|
56 | { |
---|
57 | assert(at_mp()); |
---|
58 | |
---|
59 | // lock our mutex |
---|
60 | mutex::scoped_lock oLock(m_oMutex); |
---|
61 | |
---|
62 | // create a package and save it |
---|
63 | package<R> oPackage(rFunctor); |
---|
64 | m_pPackage = &oPackage; |
---|
65 | m_bPackageWaiting = true; |
---|
66 | |
---|
67 | // wait on the semaphore |
---|
68 | OSStatus lStatus = MPWaitOnSemaphore(m_pSemaphore, kDurationForever); |
---|
69 | assert(lStatus == noErr); |
---|
70 | |
---|
71 | return(oPackage.return_value()); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | } // namespace detail |
---|
76 | |
---|
77 | } // namespace mac |
---|
78 | |
---|
79 | } // namespace threads |
---|
80 | |
---|
81 | } // namespace boost |
---|
82 | |
---|
83 | |
---|
84 | #endif // BOOST_DELIVERY_MAN_MJM012402_HPP |
---|