@@ -5,8 +5,9 @@ static _ctor1_ void modules_init(void);
55static _dtor0_ void modules_destroy (void );
66static void evaluate_new_state (m_context * c );
77static module_ret_code loop_start (m_context * c , const int max_events );
8- static int recv_events (m_context * c , int timeout );
98static int loop_stop (m_context * c );
9+ static inline int loop_quit (m_context * c , const uint8_t quit_code );
10+ static int recv_events (m_context * c , int timeout );
1011
1112map_t * ctx ;
1213memalloc_hook memhook ;
@@ -31,14 +32,36 @@ static module_ret_code loop_start(m_context *c, const int max_events) {
3132 c -> looping = true;
3233 c -> quit_code = 0 ;
3334 c -> max_events = max_events ;
34-
35- /* Tell every module that loop is started */
35+
36+ /* Eventually start any IDLE module */
37+ evaluate_new_state (c );
38+
39+ /* Tell every RUNNING module that loop is started */
3640 tell_system_pubsub_msg (c , LOOP_STARTED , NULL );
3741 return MOD_OK ;
3842 }
3943 return MOD_ERR ;
4044}
4145
46+ static int loop_stop (m_context * c ) {
47+ /* Tell every module that loop is stopped */
48+ tell_system_pubsub_msg (c , LOOP_STOPPED , NULL );
49+
50+ /* Flush pubsub msg to avoid memleaks */
51+ map_iterate (c -> modules , flush_pubsub_msg , NULL );
52+
53+ poll_destroy_pevents (& c -> pevents , & c -> max_events );
54+ c -> looping = false;
55+ c -> quit = false;
56+ return c -> quit_code ;
57+ }
58+
59+ static inline int loop_quit (m_context * c , const uint8_t quit_code ) {
60+ c -> quit = true;
61+ c -> quit_code = quit_code ;
62+ return MOD_OK ;
63+ }
64+
4265static int recv_events (m_context * c , int timeout ) {
4366 int nfds = poll_wait (c -> fd , c -> max_events , c -> pevents , timeout );
4467 for (int i = 0 ; i < nfds ; i ++ ) {
@@ -84,28 +107,14 @@ static int recv_events(m_context *c, int timeout) {
84107 } else if (nfds < 0 ) {
85108 if (errno != EINTR && errno != EAGAIN ) {
86109 fprintf (stderr , "Module loop error: %s.\n" , strerror (errno ));
87- c -> quit = true;
88- c -> quit_code = errno ;
110+ loop_quit (c , errno );
89111 } else {
90112 nfds = 0 ; // return < 0 only for real errors
91113 }
92114 }
93115 return nfds ;
94116}
95117
96- static int loop_stop (m_context * c ) {
97- /* Tell every module that loop is stopped */
98- tell_system_pubsub_msg (c , LOOP_STOPPED , NULL );
99-
100- /* Flush pubsub msg to avoid memleaks */
101- map_iterate (c -> modules , flush_pubsub_msg , NULL );
102-
103- poll_destroy_pevents (& c -> pevents , & c -> max_events );
104- c -> looping = false;
105- c -> quit = false;
106- return c -> quit_code ;
107- }
108-
109118/** Public API **/
110119
111120module_ret_code modules_set_memalloc_hook (const memalloc_hook * hook ) {
@@ -135,7 +144,6 @@ module_ret_code modules_ctx_set_logger(const char *ctx_name, const log_cb logger
135144module_ret_code modules_ctx_loop_events (const char * ctx_name , const int max_events ) {
136145 MOD_PARAM_ASSERT (max_events > 0 );
137146 FIND_CTX (ctx_name );
138- MOD_ASSERT (c -> num_fds > 0 , "No fds to loop on." , MOD_ERR );
139147 MOD_ASSERT (!c -> looping , "Context already looping." , MOD_ERR );
140148
141149 if (loop_start (c , max_events ) == MOD_OK ) {
@@ -151,9 +159,7 @@ module_ret_code modules_ctx_quit(const char *ctx_name, const uint8_t quit_code)
151159 FIND_CTX (ctx_name );
152160 MOD_ASSERT (c -> looping , "Context not looping." , MOD_ERR );
153161
154- c -> quit = true;
155- c -> quit_code = quit_code ;
156- return MOD_OK ;
162+ return loop_quit (c , quit_code );
157163}
158164
159165module_ret_code modules_ctx_get_fd (const char * ctx_name , int * fd ) {
0 commit comments