1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 2006-2020. 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, 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  * %CopyrightEnd%
19  */
20 
21 /**
22  * @description Check I/O, a cross platform IO polling framework for ERTS
23  *
24  * @author Rickard Green
25  * @author Lukas Larsson
26  */
27 
28 #ifndef ERL_CHECK_IO_H__
29 #define ERL_CHECK_IO_H__
30 
31 #include "sys.h"
32 #include "erl_sys_driver.h"
33 
34 /** @brief a structure that is used by each polling thread */
35 struct erts_poll_thread;
36 
37 /**
38  * Get the memory size of the check io framework
39  */
40 Uint erts_check_io_size(void);
41 /**
42  * Returns an Eterm with information about all the pollsets active at the
43  * moment.
44  *
45  * @param proc the Process* to allocate the result on. It is passed as
46  *  void * because of header include problems.
47  */
48 Eterm erts_check_io_info(void *proc);
49 /**
50  * Should be called when a port IO task has been executed in order to re-enable
51  * or clear the information about the fd.
52  *
53  * @param type The type of event that has been completed.
54  * @param handle The port task handle of the event.
55  * @param reset A function pointer to be called when the port task handle
56  *  should be reset.
57  */
58 void erts_io_notify_port_task_executed(ErtsPortTaskType type,
59                                        ErtsPortTaskHandle *handle,
60                                        void (*reset)(ErtsPortTaskHandle *));
61 /**
62  * Returns the maximum number of fds that the check io framework can handle.
63  */
64 int erts_check_io_max_files(void);
65 /**
66  * Called by any thread that should check for new IO events. This function will
67  * not return unless erts_check_io_interrupt(pt, 1) is called by another thread.
68  *
69  * @param pt the poll thread structure to use.
70  * @param timeout_time timeout
71  * @param poll_only_thread non zero when poll is the only thing the
72  *                         calling thread does
73  */
74 void erts_check_io(struct erts_poll_thread *pt, ErtsMonotonicTime timeout_time,
75                    int poll_only_thread);
76 /**
77  * Initialize the check io framework. This function will parse the arguments
78  * and delete any entries that it is interested in.
79  *
80  * @param argc the number of arguments
81  * @param argv an array with the arguments
82  */
83 void erts_init_check_io(int *argc, char **argv);
84 /**
85  * Interrupt the poll thread so that it can execute other code.
86  *
87  * Should be called with set = 0 by the waiting thread before calling
88  * erts_check_io.
89  *
90  * @param pt the poll thread to wake
91  * @param set whether to set or clear the interrupt flag
92  */
93 void erts_check_io_interrupt(struct erts_poll_thread *pt, int set);
94 /**
95  * Create a new poll thread structure that is associated with the number no.
96  * It is the callers responsibility that no is unique.
97  *
98  * @param no the id of the pollset thread, -2 = aux thread, -1 = scheduler
99  * @param tpd the thread progress data of the pollset thread
100  */
101 struct erts_poll_thread* erts_create_pollset_thread(int no, ErtsThrPrgrData *tpd);
102 #ifdef ERTS_ENABLE_LOCK_COUNT
103 /**
104  * Toggle lock counting on all check io locks
105  */
106 void erts_lcnt_update_cio_locks(int enable);
107 #endif
108 
109 typedef struct {
110     ErtsPortTaskHandle task;
111     ErtsSysFdType fd;
112 } ErtsIoTask;
113 
114 
115 #endif /*  ERL_CHECK_IO_H__ */
116 
117 #if !defined(ERL_CHECK_IO_C__) && !defined(ERTS_ALLOC_C__)
118 #define ERL_CHECK_IO_INTERNAL__
119 #endif
120 
121 #define ERTS_CHECK_IO_DRV_EV_STATE_LOCK_CNT 128
122 
123 /* Controls how many pollsets to allocate. Fd's are hashed into
124    each pollset based on the FD. When doing non-concurrent updates
125    there will be one pollset per thread.
126 */
127 extern int erts_no_pollsets;
128 extern int erts_no_poll_threads;
129 
130 
131 #ifndef ERL_CHECK_IO_INTERNAL__
132 #define ERL_CHECK_IO_INTERNAL__
133 #include "erl_poll.h"
134 #include "erl_port_task.h"
135 
136 typedef struct {
137     Eterm inport;
138     Eterm outport;
139     ErtsIoTask iniotask;
140     ErtsIoTask outiotask;
141 } ErtsDrvSelectDataState;
142 
143 struct erts_nif_select_event {
144     Eterm pid;
145     ErtsMessage *mp;
146 };
147 
148 typedef struct {
149     struct erts_nif_select_event in;
150     struct erts_nif_select_event out;
151 } ErtsNifSelectDataState;
152 
153 #endif /* #ifndef ERL_CHECK_IO_INTERNAL__ */
154