FOSSology  4.4.0
Open Source License Compliance by Open Source Software
event.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 /* local includes */
12 #include <event.h>
13 #include <logging.h>
14 #include <scheduler.h>
15 
16 /* std libaray includes */
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 
21 /* ************************************************************************** */
22 /* **** Local(private) fields *********************************************** */
23 /* ************************************************************************** */
24 
30 
35 int el_created = 0;
36 
49 {
50 
51  /* if the event loop has already been created, return it */
52  if(el_created)
53  {
54  return &vl_singleton;
55  }
56 
57  vl_singleton.queue = g_async_queue_new_full((GDestroyNotify)event_destroy);
60  el_created = 1;
61 
62  return &vl_singleton;
63 }
64 
76 {
77  g_async_queue_push(event_loop->queue, e);
78  return 1;
79 }
80 
91 {
92 #if !GLIB_CHECK_VERSION(2, 32, 0)
93  GTimeVal timeout;
94 #endif
95  event_t* ret;
96  int sec = 0;
97  int usec = 0;
98 
100  {
101  return NULL;
102  }
103 
104  /* wait for 1 second */
105  sec = 1;
106  usec = 0;
107 #if GLIB_CHECK_VERSION(2, 32, 0)
108  if((ret = g_async_queue_timeout_pop(event_loop->queue,
109  sec * 1000000 + usec)) == NULL)
110  return ret;
111 #else
112  timeout.tv_sec = sec;
113  timeout.tv_usec = usec;
114  g_get_current_time(&timeout);
115  g_time_val_add(&timeout, 1000000);
116  if((ret = g_async_queue_timed_pop(event_loop->queue, &timeout)) == NULL)
117  return ret;
118 #endif
119 
120  if(ret->func == NULL)
121  {
122  event_destroy(ret);
123  ret = NULL;
124  }
125 
126  return ret;
127 }
128 
129 /* ************************************************************************** */
130 /* **** Constructor Destructor ********************************************** */
131 /* ************************************************************************** */
132 
147 event_t* event_init(void(*func)(scheduler_t*, void*), void* arg, char* name, char* source_name, uint16_t source_line)
148 {
149  event_t* e = g_new(event_t, 1);
150 
151  e->func = func;
152  e->argument = arg;
153  e->name = name;
154  e->source_name = source_name;
155  e->source_line = source_line;
156 
157  return e;
158 }
159 
166 {
167  e->func = NULL;
168  e->argument = NULL;
169  e->name = NULL;
170 
171  g_free(e);
172 }
173 
180 {
181  g_async_queue_unref(event_loop_get()->queue);
182  el_created = 0;
183 }
184 
185 /* ************************************************************************** */
186 /* **** EventLoop Functions ************************************************* */
187 /* ************************************************************************** */
188 
200 void event_signal_ext(void* func, void* args, char* name, char* s_name, uint16_t s_line)
201 {
202  V_EVENT("EVENT: creating event: [%p, %p, %s, %s, %d]", func, args, name, s_name, s_line);
203  event_loop_put(event_loop_get(), event_init((event_function)func, args, name, s_name, s_line));
204 }
205 
222  void(*update_call)(scheduler_t*),
223  void(*signal_call)(scheduler_t*))
224 {
225  event_t* e;
227 
228  /* start by checking to make sure this is the only thread in this loop */
229  g_async_queue_lock(event_loop->queue);
230  if(event_loop->occupied)
231  {
232  g_async_queue_unlock(event_loop->queue);
233  return 0x1;
234  }
235  event_loop->occupied = 1;
236  event_loop->terminated = 0;
237  g_async_queue_unlock(event_loop->queue);
238 
239  main_thread = g_thread_self();
240 
241  /* from here on out, this is the only thread in this event loop */
242  /* the loop to execute events is very simple, grab event, run event */
243  while(!event_loop->terminated)
244  {
246 
247  if(signal_call)
248  signal_call(scheduler);
249  if(e == NULL)
250  continue;
251 
252  if(TVERB_EVENT && strcmp(e->name, "log_event") != 0)
253  log_printf("EVENT: calling %s, source[%s.%d] \n", e->name, e->source_name, e->source_line);
254  e->func(scheduler, e->argument);
255 
256  if(TVERB_EVENT && strcmp(e->name, "log_event") != 0)
257  log_printf("EVENT: finished %s, source[%s.%d] \n", e->name, e->source_name, e->source_line);
258 
259  event_destroy(e);
260 
261  if(update_call)
262  update_call(scheduler);
263  }
264 
265  return 0x0;
266 }
267 
276 {
278 
279  event_loop->terminated = 1;
280  event_loop->occupied = 0;
281  event_signal(NULL, NULL);
282 }
event_loop_t * event_loop_get()
Definition: event.c:48
int event_loop_put(event_loop_t *event_loop, event_t *e)
Definition: event.c:75
void event_destroy(event_t *e)
Free any memory associated with an event.
Definition: event.c:165
int el_created
Definition: event.c:35
void event_loop_terminate()
Stops the event loop from executing.
Definition: event.c:275
void event_signal_ext(void *func, void *args, char *name, char *s_name, uint16_t s_line)
Definition: event.c:200
event_t * event_loop_take(event_loop_t *event_loop)
Definition: event.c:90
event_loop_t vl_singleton
Definition: event.c:29
int event_loop_enter(scheduler_t *scheduler, void(*update_call)(scheduler_t *), void(*signal_call)(scheduler_t *))
Enters the event loop.
Definition: event.c:221
event_t * event_init(void(*func)(scheduler_t *, void *), void *arg, char *name, char *source_name, uint16_t source_line)
Definition: event.c:147
void event_loop_destroy()
Frees any memory associated with the event queue.
Definition: event.c:179
Event handling operations.
Log related operations.
GThread * main_thread
Pointer to the main thread.
Definition: scheduler.c:60
Header file for the scheduler.
GAsyncQueue * queue
The queue that is the core of the event loop.
Definition: event.h:34
int terminated
Flag that signals the end of the event loop.
Definition: event.h:35
int occupied
Does this loop already have a worker thread.
Definition: event.h:36
Definition: event.h:24
char * name
Name of the event, used for debugging.
Definition: event.h:27
char * source_name
Name of the source file creating the event.
Definition: event.h:28
void(* func)(scheduler_t *, void *)
The function that will be executed for this event.
Definition: event.h:25
void * argument
The arguments for the function.
Definition: event.h:26
uint16_t source_line
Line in the source file creating the event.
Definition: event.h:29
char * s_name
Sample source file name.
Definition: testEvent.c:27
uint16_t s_line
Sample source line number.
Definition: testEvent.c:28