1 /* **************************************************************
2 Copyright (C) 2010, 2011, 2012 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 ************************************************************** */
17 /**
18  * \file
19  * \brief Event handling operations
20  */
21 
22 #ifndef EVENT_H_INCLUDE
23 #define EVENT_H_INCLUDE
24 
25 /* scheduler includes */
26 #include <scheduler.h>
27 
28 /* ************************************************************************** */
29 /* **** Data Types ********************************************************** */
30 /* ************************************************************************** */
31 
32 #define EVENT_LOOP_SIZE 1024
33 
34 /** interanl structure for an event */
35 typedef struct {
36   void(*func)(scheduler_t*, void*); ///< The function that will be executed for this event
37   void* argument;                   ///< The arguments for the function
38   char* name;                       ///< Name of the event, used for debugging
39   char*    source_name;             ///< Name of the source file creating the event
40   uint16_t source_line;             ///< Line in the source file creating the event
41 } event_t;
42 
43 /** internal structure for the event loop */
44 typedef struct event_loop {
45   GAsyncQueue* queue; ///< The queue that is the core of the event loop
46   int terminated;     ///< Flag that signals the end of the event loop
47   int occupied;       ///< Does this loop already have a worker thread
48 } event_loop_t;
49 
50 
51 typedef void(*event_function)(scheduler_t*, void*);
52 
53 /**
54  * structure used to pass an argument and an integer to an event.
55  */
56 typedef struct
57 {
58   void* first;
59   int second;
60 } arg_int;
61 
62 /* ************************************************************************** */
63 /* **** Constructor Destructor ********************************************** */
64 /* ************************************************************************** */
65 
66 event_t* event_init(void(*func)(scheduler_t*, void*), void* arg, char* name, char* source_name, uint16_t source_line);
67 void     event_destroy(event_t* e);
68 int event_loop_put(event_loop_t* event_loop, event_t* event);
69 event_t* event_loop_take(event_loop_t* event_loop);
70 
71 void     event_loop_destroy();
72 
73 /* ************************************************************************** */
74 /* **** EventLoop Functions ************************************************* */
75 /* ************************************************************************** */
76 
77 #define event_signal(func, args) event_signal_ext(func, args, #func, __FILE__, __LINE__)
78 
79 void event_signal_ext(void* func, void* args, char* name, char* s_name, uint16_t s_line);
80 int  event_loop_enter(scheduler_t* scheduler, void(*)(scheduler_t*), void(*)(scheduler_t*));
81 void event_loop_terminate();
82 
83 #endif /* EVENT_H_INCLUDE */
84