forked from baidu/sofa-pbrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_group.h
72 lines (55 loc) · 2.42 KB
/
thread_group.h
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
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: qinzuoyan01@baidu.com (Qin Zuoyan)
#ifndef _SOFA_PBRPC_THREAD_GROUP_H_
#define _SOFA_PBRPC_THREAD_GROUP_H_
#include <sofa/pbrpc/common.h>
#include <sofa/pbrpc/ext_closure.h>
namespace sofa {
namespace pbrpc {
// Defined in this file.
class ThreadGroup;
typedef sofa::pbrpc::shared_ptr<ThreadGroup> ThreadGroupPtr;
// Defined in other files.
class ThreadGroupImpl;
class ThreadGroup
{
public:
// Constructor. User should specify the "thread_num", which can not be changed
// afterwards. All the threads will start running after contruct done.
ThreadGroup(int thread_num);
// Destructor. It will join all threads, so user must ensure that all threads
// will exit eventually.
~ThreadGroup();
// Get the number of threads in this thread group.
int thread_num() const;
// Request the thread group to invoke the given handler.
// The handler may be executed inside this function if the guarantee can be met.
// The "handler" should be a self delete closure, which can be created through
// NewClosure().
void dispatch(google::protobuf::Closure* handler);
// Request the thread group to invoke the given handler and return immediately.
// It guarantees that the handler will not be called from inside this function.
// The "handler" should be a self delete closure, which can be created through
// NewClosure().
void post(google::protobuf::Closure* handler);
// Request the thread group to invoke the given handler.
// The handler may be executed inside this function if the guarantee can be met.
// The "handler" should be a self delete closure, which can be created through
// NewExtClosure().
void dispatch(ExtClosure<void()>* handler);
// Request the thread group to invoke the given handler and return immediately.
// It guarantees that the handler will not be called from inside this function.
// The "handler" should be a self delete closure, which can be created through
// NewExtClosure().
void post(ExtClosure<void()>* handler);
private:
sofa::pbrpc::shared_ptr<ThreadGroupImpl> _imp;
SOFA_PBRPC_DISALLOW_EVIL_CONSTRUCTORS(ThreadGroup);
};
} // namespace pbrpc
} // namespace sofa
#endif // _SOFA_PBRPC_THREAD_GROUP_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */