root/lang/c/mpio/trunk/mp/iothreads/reader.pre.h @ 21242

Revision 21242, 3.8 kB (checked in by frsyuki, 5 years ago)

lang/c/mpio: add mp::iothreads::handler:send_datav

Line 
1//
2// mp::iothreads::reader
3//
4// Copyright (C) 2008 FURUHASHI Sadayuki
5//
6//    Licensed under the Apache License, Version 2.0 (the "License");
7//    you may not use this file except in compliance with the License.
8//    You may obtain a copy of the License at
9//
10//        http://www.apache.org/licenses/LICENSE-2.0
11//
12//    Unless required by applicable law or agreed to in writing, software
13//    distributed under the License is distributed on an "AS IS" BASIS,
14//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15//    See the License for the specific language governing permissions and
16//    limitations under the License.
17//
18
19#ifndef MP_IOTHREADS_READER_H__
20#define MP_IOTHREADS_READER_H__
21
22#include "mp/functional.h"
23#include <memory>
24
25namespace mp {
26namespace iothreads {
27
28
29class handler {
30public:
31        handler(int fd) : m_fd(fd) {}
32        virtual ~handler() { }
33public:
34        virtual void read_event() = 0;
35        virtual void connected() {}
36public:
37        int fd() const
38                { return m_fd; }
39        void send_data(const void* buf, size_t len)
40                { return iothreads::send_data(m_fd, buf, len); }
41        void send_datav(const struct iovec* vb, const reqvec* vr, size_t count)
42                { return iothreads::send_datav(m_fd, vb, vr, count); }
43private:
44        int m_fd;
45private:
46        handler();
47        handler(const handler&);
48};
49
50
51class reader {
52public:
53        static void initialize(unsigned int num_threads);
54        static void destroy();
55
56public:
57        template <typename Handler>
58        static void add(int fd);
59
60MP_ARGS_BEGIN
61        template <typename Handler, MP_ARGS_TEMPLATE>
62        static void add(int fd, MP_ARGS_PARAMS);
63MP_ARGS_END
64
65        static void close(int fd);
66
67        typedef function<void (handler&)> message_t;
68        static void send_message(int fd, message_t* msg);
69
70private:
71        class impl;
72        static std::auto_ptr<impl> s_instance;
73        static impl& instance();
74
75private:
76        reader();
77        ~reader();
78        reader(const reader&);
79};
80
81
82class reader::impl {
83public:
84        impl(unsigned int num_threads);
85        ~impl();
86
87public:
88        template <typename Handler>
89        void add(int fd);
90
91MP_ARGS_BEGIN
92        template <typename Handler, MP_ARGS_TEMPLATE>
93        void add(int fd, MP_ARGS_PARAMS);
94MP_ARGS_END
95
96        void close(int fd);
97
98        void send_message(int fd, message_t* msg);
99
100private:
101        class worker;
102        std::vector<worker*> m_workers;
103
104private:
105        void add_impl(handler* h);
106        worker& worker_of(int fd);
107
108private:
109        impl();
110        impl(const impl&);
111};
112
113
114template <typename Handler>
115inline void reader::add(int fd)
116{
117        instance().add<Handler>(fd);
118}
119
120MP_ARGS_BEGIN
121template <typename Handler, MP_ARGS_TEMPLATE>
122inline void reader::add(int fd, MP_ARGS_PARAMS)
123{
124        instance().add<Handler, MP_ARGS_TYPES>(fd, MP_ARGS_FUNC);
125}
126MP_ARGS_END
127
128inline void reader::close(int fd)
129{
130        instance().close(fd);
131}
132
133inline void reader::send_message(int fd, message_t* msg)
134{
135        instance().send_message(fd, msg);
136}
137
138
139template <typename Handler>
140inline void reader::impl::add(int fd)
141{
142        handler* h = NULL;
143        try { h = new Handler(fd); }
144        catch (...) { ::close(fd); }
145        try { add_impl(h); }
146        catch (...) { ::close(fd); delete h; throw; }
147}
148
149MP_ARGS_BEGIN
150template <typename Handler, MP_ARGS_TEMPLATE>
151inline void reader::impl::add(int fd, MP_ARGS_PARAMS)
152{
153        handler* h = NULL;
154        try { h = new Handler(fd, MP_ARGS_FUNC); }
155        catch (...) { ::close(fd); }
156        try { add_impl(h); }
157        catch (...) { ::close(fd); delete h; throw; }
158}
159MP_ARGS_END
160
161
162template <typename Handler>
163inline void add(int fd)
164{
165        reader::add<Handler>(fd);
166}
167
168MP_ARGS_BEGIN
169template <typename Handler, MP_ARGS_TEMPLATE>
170inline void add(int fd, MP_ARGS_PARAMS)
171{
172        reader::add<Handler>(fd, MP_ARGS_FUNC);
173}
174MP_ARGS_END
175
176inline void close(int fd)
177{
178        reader::close(fd);
179}
180
181MP_ARGS_BEGIN
182template <MP_ARGS_TEMPLATE>
183inline void send_message(int fd, MP_ARGS_PARAMS)
184{
185        reader::send_message(fd,
186                        new reader::message_t(bind(MP_ARGS_FUNC)));
187}
188MP_ARGS_END
189
190
191}  // namespace iothreads
192}  // namespace mp
193
194#endif /* mp/iothreads/reader.h */
195
Note: See TracBrowser for help on using the browser.