blob: 28af1f426ceca0066552056553b37195615e0803 [file] [log] [blame]
Sharvil Nanavati19084c62014-06-23 16:30:46 -07001/******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19#pragma once
20
21#include <stdbool.h>
22#include <stdint.h>
23
Pavlin Radoslavovc196f212015-09-23 20:39:53 -070024#include "osi/include/osi.h"
Sharvil Nanavati19084c62014-06-23 16:30:46 -070025
26// This module implements the Reactor pattern.
27// See http://en.wikipedia.org/wiki/Reactor_pattern for details.
28
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070029#ifdef __cplusplus
30extern "C" {
31#endif
32
Sharvil Nanavati19084c62014-06-23 16:30:46 -070033typedef struct reactor_t reactor_t;
Sharvil Nanavati19084c62014-06-23 16:30:46 -070034typedef struct reactor_object_t reactor_object_t;
35
Sharvil Nanavati19084c62014-06-23 16:30:46 -070036// Enumerates the reasons a reactor has stopped.
37typedef enum {
Myles Watsonb55040c2016-10-19 13:15:34 -070038 REACTOR_STATUS_STOP, // |reactor_stop| was called.
39 REACTOR_STATUS_ERROR, // there was an error during the operation.
40 REACTOR_STATUS_DONE, // the reactor completed its work (for the _run_once*
41 // variants).
Sharvil Nanavati19084c62014-06-23 16:30:46 -070042} reactor_status_t;
43
Sharvil Nanavati19084c62014-06-23 16:30:46 -070044// Creates a new reactor object. Returns NULL on failure. The returned object
45// must be freed by calling |reactor_free|.
Myles Watsonb55040c2016-10-19 13:15:34 -070046reactor_t* reactor_new(void);
Sharvil Nanavati19084c62014-06-23 16:30:46 -070047
48// Frees a reactor object created with |reactor_new|. |reactor| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070049void reactor_free(reactor_t* reactor);
Sharvil Nanavati19084c62014-06-23 16:30:46 -070050
Myles Watsonb55040c2016-10-19 13:15:34 -070051// Starts the reactor. This function blocks the caller until |reactor_stop| is
Myles Watson9ca07092016-11-28 16:41:53 -080052// called from another thread or in a callback. |reactor| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070053reactor_status_t reactor_start(reactor_t* reactor);
Sharvil Nanavati19084c62014-06-23 16:30:46 -070054
Myles Watsonb55040c2016-10-19 13:15:34 -070055// Runs one iteration of the reactor. This function blocks until at least one
Myles Watson9ca07092016-11-28 16:41:53 -080056// registered object becomes ready. |reactor| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070057reactor_status_t reactor_run_once(reactor_t* reactor);
Sharvil Nanavati19084c62014-06-23 16:30:46 -070058
Myles Watsonb55040c2016-10-19 13:15:34 -070059// Immediately unblocks the reactor. This function is safe to call from any
60// thread.
Sharvil Nanavati19084c62014-06-23 16:30:46 -070061// |reactor| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070062void reactor_stop(reactor_t* reactor);
Sharvil Nanavati19084c62014-06-23 16:30:46 -070063
Myles Watsonb55040c2016-10-19 13:15:34 -070064// Registers a file descriptor with the reactor. The file descriptor, |fd|, must
Myles Watson9ca07092016-11-28 16:41:53 -080065// be valid when this function is called and its ownership is not transferred
66// to the reactor. The |context| variable is a user-defined opaque handle that
67// is passed back to the |read_ready| and |write_ready| functions. It is not
68// copied or even dereferenced by the reactor so it may contain any value
69// including NULL. The |read_ready| and |write_ready| arguments are optional and
70// may be NULL. This function returns an opaque object that represents the file
71// descriptor's registration with the reactor. When the caller is no longer
72// interested in events on the |fd|, it must free the returned object by calling
Myles Watsonb55040c2016-10-19 13:15:34 -070073// |reactor_unregister|.
74reactor_object_t* reactor_register(reactor_t* reactor, int fd, void* context,
75 void (*read_ready)(void* context),
76 void (*write_ready)(void* context));
Sharvil Nanavati19084c62014-06-23 16:30:46 -070077
Myles Watsonb55040c2016-10-19 13:15:34 -070078// Changes the subscription mode for the file descriptor represented by
Myles Watson9ca07092016-11-28 16:41:53 -080079// |object|. If the caller has already registered a file descriptor with a
80// reactor, has a valid |object|, and decides to change the |read_ready| and/or
81// |write_ready| callback routines, they can call this routine. Returns true if
82// the subscription was changed, false otherwise.
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070083// |object| may not be NULL, |read_ready| and |write_ready| may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070084bool reactor_change_registration(reactor_object_t* object,
85 void (*read_ready)(void* context),
86 void (*write_ready)(void* context));
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070087
Myles Watsonb55040c2016-10-19 13:15:34 -070088// Unregisters a previously registered file descriptor with its reactor. |obj|
Myles Watson9ca07092016-11-28 16:41:53 -080089// may not be NULL. |obj| is invalid after calling this function so the caller
90// must drop all references to it.
Myles Watsonb55040c2016-10-19 13:15:34 -070091void reactor_unregister(reactor_object_t* obj);
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070092
93#ifdef __cplusplus
94}
95#endif