summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/Serializer.h
blob: 337686cca035f25885e90e2df26d0ae7bdebfca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef SERIALIZER_H
#define SERIALIZER_H


/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */


#include "qpid/sys/Runnable.h"
#include "qpid/sys/Monitor.h"
#include "qpid/sys/Thread.h"

#include <boost/function.hpp>
#include <boost/noncopyable.hpp>

#include <deque>

namespace qpid {
namespace sys {

/**
 * Execute tasks sequentially, queuing tasks when necessary to
 * ensure only one thread at a time executes a task and tasks
 * are executed in order.
 */
class Serializer : private boost::noncopyable, private Runnable
{
  public:
    typedef boost::function<void()> Task;

    /** Start a serializer.
     *
     * @param notifyDispatch Called when work is pending and there is no
     * active dispatch thread. Must arrange for dispatch() to be called
     * in some thread other than the calling thread and return. 
     * By default the Serializer supplies its own dispatch thread.
     *
     * @param immediate Allow execute() to execute a task immediatly
     * in the current thread.
     */
    Serializer(bool immediate=true, Task notifyDispatch=Task());

    ~Serializer();
    
    /** 
     * Task may be executed immediately in the calling thread if there
     * are no other tasks pending or executing and the "immediate"
     * paramater to the constructor was true. Otherwise task will be
     * enqueued for execution by a dispatch thread.
     */
    void execute(Task& task);

    /** Execute pending tasks sequentially in calling thread.
     * Drains the task queue and returns, does not block for more tasks.
     * 
     * @exception ShutdownException if the serializer is being destroyed.
     */
    void dispatch();

  private:
    enum State {
        IDLE, ///< No threads are active.
        EXECUTING, ///< execute() is executing a single task.
        DISPATCHING, ///< dispatch() is draining the queue.
        SHUTDOWN ///< Serializer is being destroyed.
    };

    void dispatch(Task&);
    void notifyWorker();
    void run();

    Monitor lock;

    State state;
    bool immediate;
    std::deque<Task> queue;
    Thread worker;
    Task notifyDispatch;
};

}} // namespace qpid::sys





#endif  /*!SERIALIZER_H*/