Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project are documented in this file.
## [Unreleased]

### Added
- **The C header of the poll-mode host.** `host/nim_ffi.h` declares what a
`-d:ffiPollMode` library hands out to a host: the `PollResult` message and its
`NIMFFI_MSG_*` kinds, the `NIMFFI_RET_*` codes and `nimffi_name_id()`. A test
keeps it equal to `ffi/ret_codes.nim` and `ffi/ffi_msg.nim`.
- Under `-d:ffiPollMode` the generated `<lib>.h` declares the poll shape (the
message, every export as `(ctx, req, len, id_out)`, the fixed exports, the
codecs) instead of the callback ABI.
Expand Down
86 changes: 86 additions & 0 deletions host/nim_ffi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* nim-ffi poll mode: what a host sees.
*
* A library built with -d:ffiPollMode exports, per library <lib>:
*
* int <lib>_create[...](const uint8_t* req, size_t len, void** ctx_out, uint64_t* id_out);
* int <lib>_destroy(void* ctx);
* int <lib>_shutdown(void);
* int <lib>_<method>(void* ctx, const uint8_t* req, size_t len, uint64_t* id_out);
* int <lib>_poll(void* ctx, int32_t timeout_ms, const PollResult** msg);
* int64_t <lib>_poll_fd(void* ctx); (an fd on POSIX, a HANDLE on Windows)
* int <lib>_reverse_reply(void* ctx, uint64_t call_id, int ret, const uint8_t* payload, size_t len);
*
* A request is a CBOR map keyed by the proc's parameter names. NIMFFI_RET_OK
* from a method promises exactly one REPLY message carrying *id_out; any other
* return means no reply will come. Everything the library has to say --
* replies, events, its own questions (REVERSE_CALL) -- comes out of
* <lib>_poll, and <lib>_poll_fd is readable while a message waits. poll and
* reverse_reply may be called from any host thread; a message is borrowed
* until the next poll on the same context.
*
* Kept in step with ffi/ffi_msg.nim (cMsgDecl) and ffi/ret_codes.nim. */
#ifndef NIM_FFI_H
#define NIM_FFI_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef NIMFFI_RET_OK
#define NIMFFI_RET_OK 0
#define NIMFFI_RET_ERR 1
#define NIMFFI_RET_MISSING_CALLBACK 2
#define NIMFFI_RET_STALE_WARN 3
#define NIMFFI_RET_TIMEOUT 4
#define NIMFFI_RET_CLOSED 5
#define NIMFFI_RET_INVALID_CTX 6
#define NIMFFI_RET_BUSY 7
#define NIMFFI_RET_QUEUE_FULL 8
#define NIMFFI_RET_TOO_LARGE 9
#define NIMFFI_RET_NOT_ACTIVE 10
#define NIMFFI_RET_WORKERS_FAILED 11
#define NIMFFI_RET_INVALID_ARGUMENT 12
#define NIMFFI_RET_OUT_OF_MEMORY 13
#endif

#ifndef NIMFFI_POLL_RESULT_DECLARED
#define NIMFFI_POLL_RESULT_DECLARED
typedef struct {
uint32_t struct_size; /* sizeof(PollResult) of the library; fields are only appended */
uint32_t kind; /* NIMFFI_MSG_* */
uint64_t seq_num; /* production order within the context */
uint64_t id; /* REPLY, STALE: the request id. Otherwise 0 */
uint64_t name_id; /* EVENT: which one. Otherwise 0 */
uint64_t duration_ms; /* STALE: ms in flight; REVERSE_CALL: ms left to answer */
int32_t ret_code;
uint32_t flags; /* reserved; zero today */
const uint8_t* payload; /* bare CBOR value; never NULL */
size_t len;
} PollResult;

#define NIMFFI_MSG_REPLY 1 /* id is the request; ret_code OK: payload is its CBOR, ERR: UTF-8 text */
#define NIMFFI_MSG_STALE 3 /* request id is still running; duration_ms is the ms in flight, and its REPLY still comes */
#define NIMFFI_MSG_EVENT 2 /* name_id names it; payload is its CBOR */
#define NIMFFI_MSG_REVERSE_CALL 4 /* the library asks and waits; id names the call and duration_ms is the time left to answer, measured as poll returns, so add it to your own monotonic clock at once */
#define NIMFFI_MSG_NOT_RESPONDING_HEARTBEAT 5 /* the FFI thread's heartbeat stalled */
#define NIMFFI_MSG_NOT_RESPONDING_QUEUE_FULL 8 /* events kept being dropped for ffiEventOverflowSustainMs; requests still run */
#define NIMFFI_MSG_RESPONDING 6 /* the heartbeat resumed, or the event drops stopped */
#define NIMFFI_MSG_CLOSED 7 /* the context is gone; every later poll fails */

#endif /* NIMFFI_POLL_RESULT_DECLARED */

/* FNV-1a 64 of a wire name: what EVENT and REVERSE_CALL carry as name_id. */
static inline uint64_t nimffi_name_id(const char* wire)
{
uint64_t h = 0xcbf29ce484222325ULL;
for (; *wire; ++wire) h = (h ^ (uint64_t)(unsigned char)*wire) * 0x100000001b3ULL;
return h;
}

#ifdef __cplusplus
}
#endif
#endif /* NIM_FFI_H */
22 changes: 22 additions & 0 deletions tests/unit/test_host_header.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## `host/nim_ffi.h` is a hand copy of the codes and message kinds a host reads;
## it must say what ffi/ret_codes.nim and ffi/ffi_msg.nim say.

import std/[strutils, strformat]
import unittest2
import ffi/[ret_codes, ffi_msg]

const header = staticRead("../../host/nim_ffi.h")

suite "host/nim_ffi.h matches the Nim sources":
test "every return code is defined with its value":
for code in RetCodes:
check header.contains(&"#define NIMFFI_RET_{code.name} {code.value}\n")

test "every message kind is defined with its value":
for kind in MsgKinds:
check header.contains(&"#define NIMFFI_MSG_{kind.name} {kind.value} ")

test "the message struct and kinds are the ones the library's own header declares":
# The struct layout and every kind's doc, byte for byte: a field added on
# the Nim side without the header following would shift every read.
check header.contains(cMsgDecl())
Loading