-
Notifications
You must be signed in to change notification settings - Fork 8
/
coroutines_sockets.h
66 lines (57 loc) · 1.94 KB
/
coroutines_sockets.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
/**
* coroutines_sockets.h - header for simple coroutines for SimpleMail.
* Copyright (C) 2015 Sebastian Bauer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file coroutines_sockets.h
*/
#ifndef SM__COROUTINES_SOCKETS_H_
#define SM__COROUTINES_SOCKETS_H
#include "coroutines.h"
/**
* Insert a preemption point but don't continue until the given socket is ready
* to be read or written.
*/
#define COROUTINE_AWAIT_SOCKET(context, sfd, write)\
context->basic_context.next_state = __LINE__;\
coroutine_await_socket(&context->basic_context, sfd, write);\
return COROUTINE_WAIT;\
case __LINE__:\
context->basic_context.socket_fd = -1; \
context->basic_context.is_now_ready = NULL;
/**
* Create a new scheduler for coroutines.
*
* @return the scheduler nor NULL for an error.
*/
coroutine_scheduler_t coroutine_scheduler_new(void);
/**
* Prepare the waiting state.
*
* @param context
* @param socket_fd
* @param write
*/
void coroutine_await_socket(struct coroutine_basic_context *context, int socket_fd, int write);
/**
* Checks whether the given blocked coroutine becomes now active due to some fd conditions.
*
* @param scheduler
* @param cor
* @return 1 if cor should become active, 0 if it should stay blocked.
*/
int coroutine_is_fd_now_ready(coroutine_scheduler_t scheduler, coroutine_t cor);
#endif