├── .gitignore
├── LICENSE.txt
├── Makefile
├── README.md
├── co.mk
├── co_closure.h
├── co_epoll.cpp
├── co_epoll.h
├── co_hook_sys_call.cpp
├── co_routine.cpp
├── co_routine.h
├── co_routine_inner.h
├── co_routine_specific.h
├── coctx.cpp
├── coctx.h
├── coctx_swap.S
├── example_closure.cpp
├── example_cond.cpp
├── example_copystack.cpp
├── example_echocli.cpp
├── example_echosvr.cpp
├── example_poll.cpp
├── example_setenv.cpp
├── example_specific.cpp
└── example_thread.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | BUILD
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tencent-wechat/libco/12878a93c9b345a3c175418affb214e6d5034d7e/LICENSE.txt
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | #
2 | # Tencent is pleased to support the open source community by making Libco available.
3 | #
4 | # Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | # software 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 |
20 | COMM_MAKE = 1
21 | COMM_ECHO = 1
22 | version=0.5
23 | v=debug
24 | include co.mk
25 |
26 | ########## options ##########
27 | CFLAGS += -g -fno-strict-aliasing -O2 -Wall -export-dynamic \
28 | -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64
29 |
30 | LINKS += -g -L./lib -lcolib -lpthread -ldl
31 |
32 | COLIB_OBJS=co_epoll.o co_routine.o co_hook_sys_call.o coctx_swap.o coctx.o
33 | #co_swapcontext.o
34 |
35 | PROGS = colib example_poll example_echosvr example_echocli example_thread example_cond example_specific example_copystack example_closure
36 |
37 | all:$(PROGS)
38 |
39 | colib:libcolib.a libcolib.so
40 |
41 | libcolib.a: $(COLIB_OBJS)
42 | $(ARSTATICLIB)
43 | libcolib.so: $(COLIB_OBJS)
44 | $(BUILDSHARELIB)
45 |
46 | example_echosvr:example_echosvr.o
47 | $(BUILDEXE)
48 | example_echocli:example_echocli.o
49 | $(BUILDEXE)
50 | example_thread:example_thread.o
51 | $(BUILDEXE)
52 | example_poll:example_poll.o
53 | $(BUILDEXE)
54 | example_exit:example_exit.o
55 | $(BUILDEXE)
56 | example_cond:example_cond.o
57 | $(BUILDEXE)
58 | example_specific:example_specific.o
59 | $(BUILDEXE)
60 | example_copystack:example_copystack.o
61 | $(BUILDEXE)
62 | example_setenv:example_setenv.o
63 | $(BUILDEXE)
64 | example_closure:example_closure.o
65 | $(BUILDEXE)
66 |
67 | dist: clean libco-$(version).src.tar.gz
68 |
69 | libco-$(version).src.tar.gz:
70 | @find . -type f | grep -v CVS | grep -v .svn | sed s:^./:libco-$(version)/: > MANIFEST
71 | @(cd ..; ln -s libco_pub libco-$(version))
72 | (cd ..; tar cvf - `cat libco_pub/MANIFEST` | gzip > libco_pub/libco-$(version).src.tar.gz)
73 | @(cd ..; rm libco-$(version))
74 |
75 | clean:
76 | $(CLEAN) *.o $(PROGS)
77 | rm -fr MANIFEST lib solib libco-$(version).src.tar.gz libco-$(version)
78 |
79 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 本库已转至 http://github.com/Tencent/libco
2 |
--------------------------------------------------------------------------------
/co.mk:
--------------------------------------------------------------------------------
1 | #
2 | # Tencent is pleased to support the open source community by making Libco available.
3 | #
4 | # Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | # software 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 |
20 |
21 | ##### Makefile Rules ##########
22 | MAIL_ROOT=.
23 | SRCROOT=.
24 |
25 | ##define the compliers
26 | CPP = g++
27 | CC = gcc
28 | AR = ar -rc
29 | RANLIB = ranlib
30 |
31 | CPPSHARE = $(CPP) -fPIC -shared -O2 -pipe -L$(SRCROOT)/solib/ -o
32 | CSHARE = $(CC) -fPIC -shared -O2 -pipe -L$(SRCROOT)/solib/ -o
33 |
34 | ifeq ($v,release)
35 | CFLAGS= -O2 $(INCLS) -fPIC -DLINUX -pipe -Wno-deprecated -c
36 | else
37 | CFLAGS= -g $(INCLS) -fPIC -DLINUX -pipe -c -fno-inline
38 | endif
39 |
40 | ifneq ($v,release)
41 | BFLAGS= -g
42 | endif
43 |
44 | STATICLIBPATH=$(SRCROOT)/lib
45 | DYNAMICLIBPATH=$(SRCROOT)/solib
46 |
47 | INCLS += -I$(SRCROOT)
48 |
49 | ## default links
50 | ifeq ($(LINKS_DYNAMIC), 1)
51 | LINKS += -L$(DYNAMICLIBPATH) -L$(STATICLIBPATH)
52 | else
53 | LINKS += -L$(STATICLIBPATH)
54 | endif
55 |
56 | CPPSRCS = $(wildcard *.cpp)
57 | CSRCS = $(wildcard *.c)
58 | CPPOBJS = $(patsubst %.cpp,%.o,$(CPPSRCS))
59 | COBJS = $(patsubst %.c,%.o,$(CSRCS))
60 |
61 | SRCS = $(CPPSRCS) $(CSRCS)
62 | OBJS = $(CPPOBJS) $(COBJS)
63 |
64 | CPPCOMPI=$(CPP) $(CFLAGS) -Wno-deprecated
65 | CCCOMPI=$(CC) $(CFLAGS)
66 |
67 | BUILDEXE = $(CPP) $(BFLAGS) -o $@ $^ $(LINKS)
68 | CLEAN = rm -f *.o
69 |
70 | CPPCOMPILE = $(CPPCOMPI) $< $(FLAGS) $(INCLS) $(MTOOL_INCL) -o $@
71 | CCCOMPILE = $(CCCOMPI) $< $(FLAGS) $(INCLS) $(MTOOL_INCL) -o $@
72 |
73 | ARSTATICLIB = $(AR) $@.tmp $^ $(AR_FLAGS); \
74 | if [ $$? -ne 0 ]; then exit 1; fi; \
75 | test -d $(STATICLIBPATH) || mkdir -p $(STATICLIBPATH); \
76 | mv -f $@.tmp $(STATICLIBPATH)/$@;
77 |
78 | BUILDSHARELIB = $(CPPSHARE) $@.tmp $^ $(BS_FLAGS); \
79 | if [ $$? -ne 0 ]; then exit 1; fi; \
80 | test -d $(DYNAMICLIBPATH) || mkdir -p $(DYNAMICLIBPATH); \
81 | mv -f $@.tmp $(DYNAMICLIBPATH)/$@;
82 |
83 | .cpp.o:
84 | $(CPPCOMPILE)
85 | .c.o:
86 | $(CCCOMPILE)
87 |
--------------------------------------------------------------------------------
/co_closure.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Tencent is pleased to support the open source community by making Libco available.
3 |
4 | * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | * software 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 __CO_CLOSURE_H__
20 | #define __CO_CLOSURE_H__
21 | struct stCoClosure_t
22 | {
23 | public:
24 | virtual void exec() = 0;
25 | };
26 |
27 | //1.base
28 | //-- 1.1 comac_argc
29 |
30 | #define comac_get_args_cnt( ... ) comac_arg_n( __VA_ARGS__ )
31 | #define comac_arg_n( _1,_2,_3,_4,_5,_6,_7,N,...) N
32 | #define comac_args_seqs() 7,6,5,4,3,2,1,0
33 | #define comac_join_1( x,y ) x##y
34 |
35 | #define comac_argc( ... ) comac_get_args_cnt( __VA_ARGS__,comac_args_seqs() )
36 | #define comac_join( x,y) comac_join_1( x,y )
37 |
38 | //-- 1.2 repeat
39 | #define repeat_0( fun,a,... )
40 | #define repeat_1( fun,a,... ) fun( 1,a,__VA_ARGS__ ) repeat_0( fun,__VA_ARGS__ )
41 | #define repeat_2( fun,a,... ) fun( 2,a,__VA_ARGS__ ) repeat_1( fun,__VA_ARGS__ )
42 | #define repeat_3( fun,a,... ) fun( 3,a,__VA_ARGS__ ) repeat_2( fun,__VA_ARGS__ )
43 | #define repeat_4( fun,a,... ) fun( 4,a,__VA_ARGS__ ) repeat_3( fun,__VA_ARGS__ )
44 | #define repeat_5( fun,a,... ) fun( 5,a,__VA_ARGS__ ) repeat_4( fun,__VA_ARGS__ )
45 | #define repeat_6( fun,a,... ) fun( 6,a,__VA_ARGS__ ) repeat_5( fun,__VA_ARGS__ )
46 |
47 | #define repeat( n,fun,... ) comac_join( repeat_,n )( fun,__VA_ARGS__)
48 |
49 | //2.implement
50 | #define decl_typeof( i,a,... ) typedef typeof( a ) typeof_##a;
51 | #define impl_typeof( i,a,... ) typeof_##a & a;
52 | #define impl_typeof_cpy( i,a,... ) typeof_##a a;
53 | #define con_param_typeof( i,a,... ) typeof_##a & a##r,
54 | #define param_init_typeof( i,a,... ) a(a##r),
55 |
56 |
57 | //2.1 reference
58 |
59 | #define co_ref( name,... )\
60 | repeat( comac_argc(__VA_ARGS__) ,decl_typeof,__VA_ARGS__ )\
61 | class type_##name\
62 | {\
63 | public:\
64 | repeat( comac_argc(__VA_ARGS__) ,impl_typeof,__VA_ARGS__ )\
65 | int _member_cnt;\
66 | type_##name( \
67 | repeat( comac_argc(__VA_ARGS__),con_param_typeof,__VA_ARGS__ ) ... ): \
68 | repeat( comac_argc(__VA_ARGS__),param_init_typeof,__VA_ARGS__ ) _member_cnt(comac_argc(__VA_ARGS__)) \
69 | {}\
70 | } name( __VA_ARGS__ ) ;
71 |
72 |
73 | //2.2 function
74 |
75 | #define co_func(name,...)\
76 | repeat( comac_argc(__VA_ARGS__) ,decl_typeof,__VA_ARGS__ )\
77 | class name:public stCoClosure_t\
78 | {\
79 | public:\
80 | repeat( comac_argc(__VA_ARGS__) ,impl_typeof_cpy,__VA_ARGS__ )\
81 | int _member_cnt;\
82 | public:\
83 | name( repeat( comac_argc(__VA_ARGS__),con_param_typeof,__VA_ARGS__ ) ... ): \
84 | repeat( comac_argc(__VA_ARGS__),param_init_typeof,__VA_ARGS__ ) _member_cnt(comac_argc(__VA_ARGS__))\
85 | {}\
86 | void exec()
87 |
88 | #define co_func_end }
89 |
90 |
91 | #endif
92 |
93 |
--------------------------------------------------------------------------------
/co_epoll.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Tencent is pleased to support the open source community by making Libco available.
3 |
4 | * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | * software 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 | #include "co_epoll.h"
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #ifndef __APPLE__
27 |
28 | int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout )
29 | {
30 | return epoll_wait( epfd,events->events,maxevents,timeout );
31 | }
32 | int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ev )
33 | {
34 | return epoll_ctl( epfd,op,fd,ev );
35 | }
36 | int co_epoll_create( int size )
37 | {
38 | return epoll_create( size );
39 | }
40 |
41 | struct co_epoll_res *co_epoll_res_alloc( int n )
42 | {
43 | struct co_epoll_res * ptr =
44 | (struct co_epoll_res *)malloc( sizeof( struct co_epoll_res ) );
45 |
46 | ptr->size = n;
47 | ptr->events = (struct epoll_event*)calloc( 1,n * sizeof( struct epoll_event ) );
48 |
49 | return ptr;
50 |
51 | }
52 | void co_epoll_res_free( struct co_epoll_res * ptr )
53 | {
54 | if( !ptr ) return;
55 | if( ptr->events ) free( ptr->events );
56 | free( ptr );
57 | }
58 |
59 | #else
60 | class clsFdMap // million of fd , 1024 * 1024
61 | {
62 | private:
63 | static const int row_size = 1024;
64 | static const int col_size = 1024;
65 |
66 | void **m_pp[ 1024 ];
67 | public:
68 | clsFdMap()
69 | {
70 | memset( m_pp,0,sizeof(m_pp) );
71 | }
72 | ~clsFdMap()
73 | {
74 | for(int i=0;i= sizeof(m_pp)/sizeof(m_pp[0]) )
92 | {
93 | assert( __LINE__ == 0 );
94 | return -__LINE__;
95 | }
96 | if( !m_pp[ idx ] )
97 | {
98 | m_pp[ idx ] = (void**)calloc( 1,sizeof(void*) * col_size );
99 | }
100 | m_pp[ idx ][ fd % col_size ] = (void*)ptr;
101 | return 0;
102 | }
103 | inline void *get( int fd )
104 | {
105 | int idx = fd / row_size;
106 | if( idx < 0 || idx >= sizeof(m_pp)/sizeof(m_pp[0]) )
107 | {
108 | return NULL;
109 | }
110 | void **lp = m_pp[ idx ];
111 | if( !lp ) return NULL;
112 |
113 | return lp[ fd % col_size ];
114 | }
115 | };
116 |
117 | __thread clsFdMap *s_fd_map = NULL;
118 |
119 | static inline clsFdMap *get_fd_map()
120 | {
121 | if( !s_fd_map )
122 | {
123 | s_fd_map = new clsFdMap();
124 | }
125 | return s_fd_map;
126 | }
127 |
128 | struct kevent_pair_t
129 | {
130 | int fire_idx;
131 | int events;
132 | uint64_t u64;
133 | };
134 | int co_epoll_create( int size )
135 | {
136 | return kqueue();
137 | }
138 | int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout )
139 | {
140 | struct timespec t = { 0 };
141 | if( timeout > 0 )
142 | {
143 | t.tv_sec = timeout;
144 | }
145 | int ret = kevent( epfd,
146 | NULL, 0, //register null
147 | events->eventlist, maxevents,//just retrival
148 | ( -1 == timeout ) ? NULL : &t );
149 | int j = 0;
150 | for(int i=0;ieventlist[i];
153 | struct kevent_pair_t *ptr = (struct kevent_pair_t*)kev.udata;
154 | struct epoll_event *ev = events->events + i;
155 | if( 0 == ptr->fire_idx )
156 | {
157 | ptr->fire_idx = i + 1;
158 | memset( ev,0,sizeof(*ev) );
159 | ++j;
160 | }
161 | else
162 | {
163 | ev = events->events + ptr->fire_idx - 1;
164 | }
165 | if( EVFILT_READ == kev.filter )
166 | {
167 | ev->events |= EPOLLIN;
168 | }
169 | else if( EVFILT_WRITE == kev.filter )
170 | {
171 | ev->events |= EPOLLOUT;
172 | }
173 | ev->data.u64 = ptr->u64;
174 | }
175 | for(int i=0;ieventlist[i].udata) )->fire_idx = 0;
178 | }
179 | return j;
180 | }
181 | int co_epoll_del( int epfd,int fd )
182 | {
183 |
184 | struct timespec t = { 0 };
185 | struct kevent_pair_t *ptr = ( struct kevent_pair_t* )get_fd_map()->get( fd );
186 | if( !ptr ) return 0;
187 | if( EPOLLIN & ptr->events )
188 | {
189 | struct kevent kev = { 0 };
190 | kev.ident = fd;
191 | kev.filter = EVFILT_READ;
192 | kev.flags = EV_DELETE;
193 | kevent( epfd,&kev,1, NULL,0,&t );
194 | }
195 | if( EPOLLOUT & ptr->events )
196 | {
197 | struct kevent kev = { 0 };
198 | kev.ident = fd;
199 | kev.filter = EVFILT_WRITE;
200 | kev.flags = EV_DELETE;
201 | kevent( epfd,&kev,1, NULL,0,&t );
202 | }
203 | get_fd_map()->clear( fd );
204 | free( ptr );
205 | return 0;
206 | }
207 | int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * ev )
208 | {
209 | if( EPOLL_CTL_DEL == op )
210 | {
211 | return co_epoll_del( epfd,fd );
212 | }
213 |
214 | const int flags = ( EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP );
215 | if( ev->events & ~flags )
216 | {
217 | return -1;
218 | }
219 |
220 | if( EPOLL_CTL_ADD == op && get_fd_map()->get( fd ) )
221 | {
222 | errno = EEXIST;
223 | return -1;
224 | }
225 | else if( EPOLL_CTL_MOD == op && !get_fd_map()->get( fd ) )
226 | {
227 | errno = ENOENT;
228 | return -1;
229 | }
230 |
231 | struct kevent_pair_t *ptr = (struct kevent_pair_t*)get_fd_map()->get( fd );
232 | if( !ptr )
233 | {
234 | ptr = (kevent_pair_t*)calloc(1,sizeof(kevent_pair_t));
235 | get_fd_map()->set( fd,ptr );
236 | }
237 |
238 | int ret = 0;
239 | struct timespec t = { 0 };
240 |
241 | printf("ptr->events 0x%X\n",ptr->events);
242 |
243 | if( EPOLL_CTL_MOD == op )
244 | {
245 | //1.delete if exists
246 | if( ptr->events & EPOLLIN )
247 | {
248 | struct kevent kev = { 0 };
249 | EV_SET( &kev,fd,EVFILT_READ,EV_DELETE,0,0,NULL );
250 | kevent( epfd, &kev,1, NULL,0, &t );
251 | }
252 | //1.delete if exists
253 | if( ptr->events & EPOLLOUT )
254 | {
255 | struct kevent kev = { 0 };
256 | EV_SET( &kev,fd,EVFILT_WRITE,EV_DELETE,0,0,NULL );
257 | ret = kevent( epfd, &kev,1, NULL,0, &t );
258 | printf("delete write ret %d\n",ret );
259 | }
260 | }
261 |
262 | do
263 | {
264 | if( ev->events & EPOLLIN )
265 | {
266 |
267 | //2.add
268 | struct kevent kev = { 0 };
269 | EV_SET( &kev,fd,EVFILT_READ,EV_ADD,0,0,ptr );
270 | ret = kevent( epfd, &kev,1, NULL,0, &t );
271 | if( ret ) break;
272 | }
273 | if( ev->events & EPOLLOUT )
274 | {
275 | //2.add
276 | struct kevent kev = { 0 };
277 | EV_SET( &kev,fd,EVFILT_WRITE,EV_ADD,0,0,ptr );
278 | ret = kevent( epfd, &kev,1, NULL,0, &t );
279 | if( ret ) break;
280 | }
281 | } while( 0 );
282 |
283 | if( ret )
284 | {
285 | get_fd_map()->clear( fd );
286 | free( ptr );
287 | return ret;
288 | }
289 |
290 | ptr->events = ev->events;
291 | ptr->u64 = ev->data.u64;
292 |
293 |
294 | return ret;
295 | }
296 |
297 | struct co_epoll_res *co_epoll_res_alloc( int n )
298 | {
299 | struct co_epoll_res * ptr =
300 | (struct co_epoll_res *)malloc( sizeof( struct co_epoll_res ) );
301 |
302 | ptr->size = n;
303 | ptr->events = (struct epoll_event*)calloc( 1,n * sizeof( struct epoll_event ) );
304 | ptr->eventlist = (struct kevent*)calloc( 1,n * sizeof( struct kevent) );
305 |
306 | return ptr;
307 | }
308 |
309 | void co_epoll_res_free( struct co_epoll_res * ptr )
310 | {
311 | if( !ptr ) return;
312 | if( ptr->events ) free( ptr->events );
313 | if( ptr->eventlist ) free( ptr->eventlist );
314 | free( ptr );
315 | }
316 |
317 | #endif
318 |
319 |
320 |
--------------------------------------------------------------------------------
/co_epoll.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Tencent is pleased to support the open source community by making Libco available.
3 |
4 | * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | * software 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 __CO_EPOLL_H__
20 | #define __CO_EPOLL_H__
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 |
29 | #ifndef __APPLE__
30 |
31 | #include
32 |
33 | struct co_epoll_res
34 | {
35 | int size;
36 | struct epoll_event *events;
37 | struct kevent *eventlist;
38 | };
39 | int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout );
40 | int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * );
41 | int co_epoll_create( int size );
42 | struct co_epoll_res *co_epoll_res_alloc( int n );
43 | void co_epoll_res_free( struct co_epoll_res * );
44 |
45 | #else
46 |
47 | #include
48 | enum EPOLL_EVENTS
49 | {
50 | EPOLLIN = 0X001,
51 | EPOLLPRI = 0X002,
52 | EPOLLOUT = 0X004,
53 |
54 | EPOLLERR = 0X008,
55 | EPOLLHUP = 0X010,
56 | };
57 | #define EPOLL_CTL_ADD 1
58 | #define EPOLL_CTL_DEL 2
59 | #define EPOLL_CTL_MOD 3
60 | typedef union epoll_data
61 | {
62 | void *ptr;
63 | int fd;
64 | uint32_t u32;
65 | uint64_t u64;
66 |
67 | } epoll_data_t;
68 |
69 | struct epoll_event
70 | {
71 | uint32_t events;
72 | epoll_data_t data;
73 | };
74 |
75 | struct co_epoll_res
76 | {
77 | int size;
78 | struct epoll_event *events;
79 | struct kevent *eventlist;
80 | };
81 | int co_epoll_wait( int epfd,struct co_epoll_res *events,int maxevents,int timeout );
82 | int co_epoll_ctl( int epfd,int op,int fd,struct epoll_event * );
83 | int co_epoll_create( int size );
84 | struct co_epoll_res *co_epoll_res_alloc( int n );
85 | void co_epoll_res_free( struct co_epoll_res * );
86 |
87 | #endif
88 | #endif
89 |
90 |
91 |
--------------------------------------------------------------------------------
/co_hook_sys_call.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tencent-wechat/libco/12878a93c9b345a3c175418affb214e6d5034d7e/co_hook_sys_call.cpp
--------------------------------------------------------------------------------
/co_routine.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Tencent is pleased to support the open source community by making Libco available.
3 |
4 | * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
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,
13 | * software 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 | #include "co_routine.h"
20 | #include "co_routine_inner.h"
21 | #include "co_epoll.h"
22 |
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include