summaryrefslogtreecommitdiffstats
path: root/src/windows/identity/kmq/kmqinternal.h
blob: 8ae0ab67e18a45defb0a3bdc904c1284b8c428ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (c) 2005 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* $Id$ */

#ifndef __KHIMAIRA_KMQINTERNAL_H
#define __KHIMAIRA_KMQINTERNAL_H

#define _NIMLIB_

#include<windows.h>
#include<kmq.h>
#include<khlist.h>
#include<kherror.h>
#include<khmsgtypes.h>
#include<kconfig.h>

#define NOEXPORT

#include<utils.h>
#include<strsafe.h>




/*! \brief Message reference */
typedef struct tag_kmq_message_ref {
    kmq_message * msg;          /*!< Message that we are referring
                                  to */
    kmq_callback_t recipient;   /*!< The recipient of the message */

    LDCL(struct tag_kmq_message_ref);
} kmq_message_ref;




/*! \brief Message queue

    Each thread gets its own message queue.  When a message is
    broadcast to which there is a subscriber in a particular thread, a
    reference to the message is placed in the message queue of the
    thread.  The dispatch procedure then dispatches the message as
    described in the message reference.
*/
typedef struct tag_kmq_queue {
    kmq_thread_id thread;       /*!< The thread id  */

    CRITICAL_SECTION cs;
    HANDLE wait_o;

    khm_int32 load;             /*!< Number of messages waiting to be
                                  processed on this message queue.  */
    kmq_timer last_post;        /*!< Time the last message was
                                  received */

    khm_int32 flags;            /*!< Flags.  Currently, it's just KMQ_QUEUE_FLAG_DELETED */

    /*Q*/
    QDCL(kmq_message_ref);      /*!< Queue of message references  */

    /*Lnode*/
    LDCL(struct tag_kmq_queue);
} kmq_queue;

#define KMQ_QUEUE_FLAG_DELETED   0x00000008
#define KMQ_QUEUE_FLAG_DETACHING 0x00000010

/*! \brief Message subscription

    A subscription binds a recipient with a message type.  These are
    specific to a thread. I.e. a subscription that was made in one
    thread will not receive messages in the context of another thread.
*/
typedef struct tag_kmq_msg_subscription {
    khm_int32 magic;            /*!< Magic number.  Should always be
                                  ::KMQ_MSG_SUB_MAGIC */
    khm_int32 type;             /*!< Type of message */
    khm_int32 rcpt_type;        /*!< Type of recipient.  One of
                                  ::KMQ_RCPTTYPE_CB or
                                  ::KMQ_RCPTTYPE_HWND  */
    union {
        kmq_callback_t cb;      /*!< Callback if the subscription is
                                  of callback type */
        HWND hwnd;              /*!< Window handle if the subscription
                                  is a windows message type */
    } recipient;

    kmq_queue * queue;          /*!< Associated queue */

    /*lnode*/
    LDCL(struct tag_kmq_msg_subscription);
} kmq_msg_subscription;

#define KMQ_MSG_SUB_MAGIC 0x3821b58e

/*! \brief Callback recipient type

    The recipient is a callback function */
#define KMQ_RCPTTYPE_CB     1

/*! \brief Windows recipient type

    The recipient is a window */
#define KMQ_RCPTTYPE_HWND   2




/*! \brief A message type
 */
typedef struct tag_kmq_msg_type {
    khm_int32 id;               /*!< Identifier for the message
                                  type. */
    kmq_msg_subscription * subs; /*!< The list of subscriptions */
    kmq_msg_completion_handler completion_handler; /*!< Completion
                                  handler for the message type */

    wchar_t * name;             /*!< Name of the message type for
                                  named types.  Message type names are
                                  language independant. */

    /*Lnode*/
    LDCL(struct tag_kmq_msg_type);
} kmq_msg_type;

/*! \brief The maximum number of message types
 */
#define KMQ_MSG_TYPE_MAX 255

/*! \brief Maximum number of characters in a message type name

    The count includes the terminating NULL
 */
#define KMQ_MAXCCH_TYPE_NAME 256

/*! \brief Maximum number of bytes in a message type name

    Type count includes the terminating NULL
 */
#define KMQ_MAXCB_TYPE_NAME (KMQ_MAXCCH_TYPE_NAME * sizeof(wchar_t))




#define KMQ_CONF_SPACE_NAME L"KMQ"
#define KMQ_CONF_QUEUE_DEAD_TIMEOUT_NAME L"QueueDeadTimeout"
#define KMQ_CONF_CALL_DEAD_TIMEOUT_NAME L"CallDeadTimeout"

extern CRITICAL_SECTION cs_kmq_global;
extern kmq_timer kmq_queue_dead_timeout;
extern kmq_timer kmq_call_dead_timeout;

extern kmq_queue * queues;

/* message type */
extern CRITICAL_SECTION cs_kmq_types;
extern kmq_msg_type *msg_types[KMQ_MSG_TYPE_MAX+1];

void kmqint_init_msg_types(void);
void kmqint_exit_msg_types(void);
void kmqint_free_msg_type(int t);
void kmqint_msg_type_create(int t);
void kmqint_msg_type_add_sub(int t, kmq_msg_subscription *s);
void kmqint_msg_type_del_sub(kmq_msg_subscription *s);
kmq_msg_subscription * kmqint_msg_type_del_sub_hwnd(khm_int32 t, HWND hwnd);
kmq_msg_subscription * kmqint_msg_type_del_sub_cb(khm_int32 t, kmq_callback_t cb);
khm_int32 kmqint_msg_publish(kmq_message * m, khm_boolean try_send);
khm_int32 kmqint_msg_type_set_handler(khm_int32 type, kmq_msg_completion_handler handler);
int kmqint_notify_msg_completion(kmq_message * m);

/* consumer */
extern DWORD kmq_tls_queue;

void kmqint_post_queue(kmq_queue * q, kmq_message *m);
void kmqint_post(kmq_msg_subscription * s, kmq_message * m, khm_boolean try_send);
kmq_queue * kmqint_get_thread_queue(void);
void kmqint_get_queue_message_ref(kmq_queue * q, kmq_message_ref ** r);
void kmqint_put_message_ref(kmq_message_ref * r);

/* publisher */
extern CRITICAL_SECTION cs_kmq_msg;
extern CRITICAL_SECTION cs_kmq_msg_ref;

kmq_message * kmqint_get_message(void);
void kmqint_put_message(kmq_message *m);

void kmqint_init(void);
void kmqint_exit(void);
void kmqint_attach_this_thread(void);
void kmqint_detach_this_thread(void);

khm_int32 kmqint_post_message_ex(
    khm_int32 type, 
    khm_int32 subtype, 
    khm_ui_4 uparam, 
    void * blob, 
    kmq_call * call,
    khm_boolean try_send);

int kmqint_call_completion_handler(kmq_msg_completion_handler h,
                                   kmq_message * m);

/* global */
extern kconf_schema schema_kmqconfig[];

/* Lock hiearchy :

    cs_kmq_types
    cs_kmq_msg
    cs_kmq_msg_ref
    cs_compl
    cs_kmq_global
    kmq_queue::cs

    If you have a level 'x' lock, you can obtain a level 'x+n' lock.
    You can't obtain a 'x-n' lock if you already have a level 'x' lock.
    If you don't have any locks, you can obtain any lock.
 */
#endif