1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #ifndef _omi_selector_h
26 #define _omi_selector_h
27
28 #include "config.h"
29 #include <common.h>
30 #include "sock.h"
31 #include <base/list.h>
32 #include <base/messages.h>
33
34 BEGIN_EXTERNC
35
|
36 krisbash 1.3 #define SELECTOR_ADD 0x1
37 #define SELECTOR_READ 0x2
38 #define SELECTOR_WRITE 0x4
39 #define SELECTOR_EXCEPTION 0x8
40 #define SELECTOR_TIMEOUT 0x10
41 #define SELECTOR_REMOVE 0x20
42 #define SELECTOR_DESTROY 0x40
|
43 mike 1.1
44 typedef struct _Selector Selector;
45 typedef struct _Handler Handler;
46
47 struct _Handler
48 {
49 Handler* next;
50 Handler* prev;
51 Sock sock;
52 MI_Uint32 mask;
|
53 krisbash 1.3 /* time when selector gets 'timeout' event - typically PAL_Time() + ttl-in-usec;
|
54 mike 1.1 '0' means 'ignore me' */
55 MI_Uint64 fireTimeoutAt;
56 MI_Boolean (*callback)(Selector*, Handler*, MI_Uint32 mask, MI_Uint64 currentTimeUsec);
57 void* data;
58 };
59
60 struct _Selector
61 {
62 struct _SelectorRep* rep;
63 };
64
65 typedef void (*Selector_NotificationCallback)(void* self, Message* message);
66
67 MI_Result Selector_Init(
68 Selector* self);
69
70 void Selector_Destroy(
71 Selector* self);
72
73 MI_Result Selector_AddHandler(
74 Selector* self,
75 mike 1.1 Handler* handler);
76
77 MI_Result Selector_RemoveHandler(
78 Selector* self,
79 Handler* handler);
80
81 /* Used for quick shutdown procedure */
82 MI_Result Selector_RemoveAllHandlers(
83 Selector* self);
84
85 MI_Result Selector_ContainsHandler(
86 Selector* self,
87 Handler* handler);
88
89 /* Runs socket processing loop;
90 Parameters:
91 self - selector
92 timeoutUsec - time to run
93
94 Retunrs:
95 OK - was stopped by StopRunning call
96 mike 1.1 FAILED - system call 'select' failed or no more sockets to monitor
97 TIMEOUT - timeout reached
98 */
99 MI_Result Selector_Run(
100 Selector* self,
|
101 krisbash 1.3 MI_Uint64 timeoutUsec,
102 MI_Boolean noReadsMode );
103
104 int Selector_IsSelectorThread(Selector* self, ThreadID *id);
|
105 mike 1.1
106 /* Informs selector's Run method that it should exit normaly.
107 Funciton is safe for calling from signal hanlder */
108 MI_Result Selector_StopRunning(
109 Selector* self);
|
110 krisbash 1.3 MI_Result Selector_StopRunningNoReadsMode(
111 Selector* self);
|
112 mike 1.1
113 /* Wakes up selector's thread
114 Typical usage is to recalculate timeouts on handlers
115 when selector's Run is running in different thread */
116 MI_Result Selector_Wakeup(
|
117 krisbash 1.3 _In_ Selector* self,
118 MI_Boolean retryDispatching );
|
119 mike 1.1
120 /*
121 * This function guaranties that callback is called in 'Run'/'IO' thread context,
122 * so no locking is required for accessing sokcet objects, updating buffers etc
123 */
124 MI_Result Selector_CallInIOThread(
125 Selector* self,
126 Selector_NotificationCallback callback,
127 void* callback_self,
128 Message* message);
129
130 /*
131 Sets flag 'allowEmptySelector' to allow
132 'run' call on empty selector
133 */
134 void Selector_SetAllowEmptyFlag(
135 Selector* self,
136 MI_Boolean allowEmptySelector);
137
138 END_EXTERNC
139
140 mike 1.1 #endif /* _omi_selector_h */
|