> This page is for version 3.0 (Latest) (default).
> For other versions, use one of these documentation indexes:
> - 3.0 (Latest) (default): https://docs.nvidia.com/holoscan/sensor-bridge/3-0/llms.txt
> - 2.7: https://docs.nvidia.com/holoscan/sensor-bridge/2-7/llms.txt
> - Archives: https://docs.nvidia.com/holoscan/sensor-bridge/archives/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/holoscan/sensor-bridge/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/holoscan/sensor-bridge/_mcp/server.

# hololink::module::ReactorV1

> Reactor provides an event loop that runs deferred, timed and file-descriptor-driven callbacks in a single thread.

Reactor provides an event loop that runs deferred, timed and file-descriptor-driven callbacks in a single thread.

Reactor is a singleton object, owned by the host and shared to every module. All callbacks it dispatches run one sequentially in a dedicated thread, so callbacks never run concurrently with each other and need no locking against one another. Callbacks must return promptly, since each one delays all the others, and must not let an exception escape.

Every method may be called from any thread, including from within a callback. Pending callbacks and alarms are discarded when the host shuts down.

```cpp showLineNumbers={false}
#include <hololink/module/reactor.hpp>
```

**Inherits from:** `hololink::module::Service< ReactorV1 >` (public)

---

## Methods

### now \[#now]

```cpp showLineNumbers={false}
virtual struct timespec hololink::module::ReactorV1::now() const
```

The current time on the reactor's clock.

This is a monotonic clock (not wall-clock time); it is the clock add_alarm() interprets its `when` argument against.

**Returns:** The current time.

### add_callback \[#addcallback]

```cpp showLineNumbers={false}
virtual void hololink::module::ReactorV1::add_callback(
    std::shared_ptr<Callback> callback
)
```

Queues a callback on the reactor thread for execution as soon as possible.

Callbacks queued this way run in the order they were added.

**Parameters**

**`callback`** `std::shared_ptr<Callback>`

The function to run. The reactor keeps it alive until it has run.

---

### add_fd_callback \[#addfdcallback]

```cpp showLineNumbers={false}
virtual void hololink::module::ReactorV1::add_fd_callback(
    int fd,
    std::shared_ptr<FdCallback> callback,
    short events
)
```

Call a function whenever a file descriptor has events of interest.

Readiness is level-triggered, as for `poll(2)`: the callback keeps being called for as long as the condition persists, so it should consume or clear it. There can be only one callback per file descriptor; adding another for the same `fd` replaces the earlier one. Events the reactor saw for an earlier registration of `fd` are never passed to a later one, even of the same callback. The reactor does not own or close `fd`.

**Parameters**

**`fd`** `int`

The file descriptor to watch.

---

**`callback`** `std::shared_ptr<FdCallback>`

The function to call. The reactor keeps it alive until it is removed or replaced and the reactor is done with it: a callback removed while the reactor waits for events, or runs another callback, is released on the reactor thread once that round of callbacks has run.

---

**`events`** `short`

The `poll(2)` events to wait for, e.g. `POLLIN`.

---

### remove_fd_callback \[#removefdcallback]

```cpp showLineNumbers={false}
virtual void hololink::module::ReactorV1::remove_fd_callback(
    int fd
)
```

Stop watching a file descriptor.

After this returns no new calls to the file descriptor's callback will be scheduled. If called from a thread other than the reactor thread, a call that is already running may still be finishing. Does nothing if `fd` is not being watched. Remove the callback before closing `fd`: a callback whose `fd` is closed while it is registered is dropped, with a warning, when the reactor next polls it.

**Parameters**

**`fd`** `int`

The file descriptor passed to add_fd_callback().

---

### add_alarm_s \[#addalarms]

```cpp showLineNumbers={false}
virtual AlarmHandle hololink::module::ReactorV1::add_alarm_s(
    float seconds,
    std::shared_ptr<Callback> callback
)
```

Run a callback once, after a delay.

**Returns:** A handle for cancel_alarm().

**Parameters**

**`seconds`** `float`

Delay from now, in seconds.

---

**`callback`** `std::shared_ptr<Callback>`

The function to run. The reactor keeps it alive until it has run or the alarm is cancelled.

---

### add_alarm \[#addalarm]

```cpp showLineNumbers={false}
virtual AlarmHandle hololink::module::ReactorV1::add_alarm(
    const struct timespec &when,
    std::shared_ptr<Callback> callback
)
```

Run a callback once, at a given time.

Alarms that are due run in order of their `when`. A `when` that has already passed runs as soon as possible.

**Returns:** A handle for cancel_alarm().

**Parameters**

**`when`** `const struct timespec &`

Time to run at, on the clock reported by now().

---

**`callback`** `std::shared_ptr<Callback>`

The function to run. The reactor keeps it alive until it has run or the alarm is cancelled.

---

### cancel_alarm \[#cancelalarm]

```cpp showLineNumbers={false}
virtual void hololink::module::ReactorV1::cancel_alarm(
    AlarmHandle handle
)
```

Cancel a scheduled alarm.

Does nothing if `handle` is null, or if the alarm has already run or been cancelled.

**Parameters**

**`handle`** `AlarmHandle`

The handle returned when the alarm was added.

---

### is_current_thread \[#iscurrentthread]

```cpp showLineNumbers={false}
virtual bool hololink::module::ReactorV1::is_current_thread() const
```

Determine if the caller is running in the reactor thread.

**Returns:** `true` when called in the reactor thread.

### module \[#module]

```cpp showLineNumbers={false}
std::shared_ptr<Module> hololink::module::ServiceBase::module() const
```

The [Module](module) that this service was published from.

The service holds no strong reference to its [Module](module), so this does not extend the [Module](module)'s lifetime.

**Returns:** The owning [Module](module), or an empty pointer if the service has not been published or its [Module](module) no longer exists.

---

## Static methods

### get_service \[#getservice]

#### Fetch the process-wide reactor from a module

```cpp showLineNumbers={false}
static std::shared_ptr<ReactorV1> hololink::module::ReactorV1::get_service(
    std::shared_ptr<Module> module,
    bool allow_null = false
)
```

Fetch the process-wide reactor from a module.

There is only one reactor, so no instance identifier is needed.

**Returns:** A shared pointer to the reactor.

**Parameters**

**`module`** `std::shared_ptr<Module>`

The module to query.

---

**`allow_null`** `bool` — default: false

If `false` (the default), throws std::runtime_error when the module doesn't provide a reactor (or `module` is null). If `true`, returns `nullptr` instead.

---

#### Overload 2

```cpp showLineNumbers={false}
static std::shared_ptr<ReactorV1> hololink::module::Service<ReactorV1>::get_service(
    std::shared_ptr<Module> module,
    const char *instance_id,
    bool allow_null = false
)
```

Type-safe way to fetch a std::shared_ptr to an instance of \<Derived> from a [Module](module).

Note that [Module](module) usually caches instances, it's likely that calls to get_service with the same locator_id will return pointers to the same implementation object.

**Returns:** A shared pointer to the service instance.

**Parameters**

**`module`** `std::shared_ptr<Module>`

Is the shared object that owns the instance we wish to fetch

---

**`instance_id`** `const char *`

Names the specific object, usually expressed as a set of "name=value;..." pairs, as defined by the implementations themselves. This name is normally fetched by calling `Derived::locator_id(enumeration_metadata)`.

---

**`allow_null`** `bool` — default: false

The default value of `false` causes this routine to throw a std::runtime_error if the service cannot be found within the module; this would be expected to terminate the calling program. If the application wants to handle the error, pass in `true` here and get_service will return `nullptr` when this module doesn't provide the requested service.

---

### for_each_type_id \[#foreachtypeid]

```cpp showLineNumbers={false}
static bool hololink::module::Service<ReactorV1>::for_each_type_id(
    Callback &&cb
)
```

Iterate over every type_id supported by this instance.

The callback signature is `bool(const char*)` — returning false halts the walk; true continues. Iteration is depth-first, so you'll see the most specific implementation types first.

---

## Types

### Typedefs

| Name          | Definition                                   | Description                                                                        |
| ------------- | -------------------------------------------- | ---------------------------------------------------------------------------------- |
| `Callback`    | `std::function< void()>`                     | A function run once on the reactor thread.                                         |
| `FdCallback`  | `std::function< void(int fd, short events)>` | A function run on the reactor thread when a watched file descriptor becomes ready. |
| `AlarmHandle` | `std::shared_ptr< AlarmEntry >`              | Handle to a pending alarm.                                                         |

---

## Inner classes

### AlarmEntry

```cpp showLineNumbers={false}
class hololink::module::ReactorV1::AlarmEntry
```

Opaque identifier of a pending alarm.

Obtained from add_alarm() or add_alarm_s(), and passed to cancel_alarm(). Applications neither create nor derive from it.