Skip to content

Commit b85a6ee

Browse files
bradjcalevy
authored andcommitted
doc: update guide to specify using yield-waitfor
1 parent b062f21 commit b85a6ee

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

doc/guide.md

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,87 @@ returncode_t libtock_sensor_read(libtock_sensor_callback_reading cb) {
277277
278278
## Synchronous APIs
279279
280-
Most system call interfaces will want to provide a synchronous API as well.
280+
Most system call interfaces will want to provide a synchronous API as well. This
281+
requires another file for syscalls.
282+
283+
All synchronous APIs MUST use the
284+
[Yield-WaitFor](https://book.tockos.org/trd/trd104-syscalls.html#413-yield-waitfor)
285+
(`yield_wait_for()`) variant of the yield syscall. This ensures predictable
286+
behavior for `libtock-sync` users because Yield-WaitFor ensures that no other
287+
application upcall will run until the synchronous API has finished.
288+
289+
### Synchronous Syscall APIs
290+
291+
| Characteristic | Value |
292+
|------------------|------------------------------------------------------|
293+
| Location | `libtock-sync/[category]/syscalls` |
294+
| Source File Name | `libtock-sync/[category]/syscalls/[name]_syscalls.c` |
295+
| Header File Name | `libtock-sync/[category]/syscalls/[name]_syscalls.h` |
296+
297+
### Header File
298+
299+
The `[name]_syscalls.h` must be wrapped in `extern "C" { ... }` if the header
300+
file is used in a C++ app.
301+
302+
#### Example:
303+
304+
```c
305+
#pragma once
306+
307+
#include <libtock/tock.h>
308+
#include <libtock/[category]/syscalls/[name]_syscalls.h>
309+
310+
#ifdef __cplusplus
311+
extern "C" {
312+
#endif
313+
314+
// Signatures go here.
315+
316+
#ifdef __cplusplus
317+
}
318+
#endif
319+
```
320+
321+
### Yield-WaitFor
322+
323+
Each supported upcall must have a matching Yield-WaitFor call. This function
324+
calls `yield_wait_for()` and interprets the `yield_waitfor_return_t` return
325+
value arguments.
326+
327+
The signature is:
328+
329+
```c
330+
returncode_t libtocksync_[name]_yield_wait_for(<arguments>);
331+
```
332+
333+
If only one upcall is supported, the function name must be
334+
`libtocksync_[name]_yield_wait_for`.
335+
336+
If more than one upcall is supported, the function names must start with
337+
`libtocksync_[name]_yield_wait_for_` followed by the same description of what
338+
the upcall is used for from the `libtock` library.
339+
340+
The arguments must be appropriately named pointers to data types returned by the
341+
upcall, except for any returned ReturnCode. The ReturnCode must be returned from
342+
the function.
343+
344+
#### Example:
345+
346+
```c
347+
returncode_t libtocksync_[name]_yield_wait_for(int* value) {
348+
yield_waitfor_return_t ret;
349+
ret = yield_wait_for(DRIVER_NUM_[NAME], 0);
350+
if (ret.data0 != RETURNCODE_SUCCESS) return ret.data0;
351+
352+
*value = (int) ret.data1;
353+
return RETURNCODE_SUCCESS;
354+
}
355+
```
356+
357+
### Synchronous APIs
358+
359+
The asynchronous operations should have matching synchronous versions using
360+
Yield-WaitFor internally.
281361

282362
| Characteristic | Value |
283363
|------------------|------------------------------------|
@@ -292,8 +372,8 @@ The libtock-sync `[name].h` header file must look like:
292372
```c
293373
#pragma once
294374

375+
#include "syscalls/temperature_syscalls.h"
295376
#include <libtock/tock.h>
296-
#include <libtock/[category]/[name].h>
297377

298378
#ifdef __cplusplus
299379
extern "C" {
@@ -337,17 +417,13 @@ operation:
337417

338418
```c
339419
returncode_t libtocksync_sensor_read(int* val) {
340-
int err;
341-
result.fired = false;
420+
returncode_t err;
342421

343-
err = libtock_sensor_read(sensor_cb);
422+
err = libtock_sensor_command_read(sensor_cb);
344423
if (err != RETURNCODE_SUCCESS) return err;
345424

346-
// Wait for the callback.
347-
yield_for(&result.fired);
348-
if (result.result != RETURNCODE_SUCCESS) return result.result;
349-
350-
*val = result.val;
351-
return RETURNCODE_SUCCESS;
425+
// Wait for the operation to finish.
426+
err = libtock_temperature_yield_wait_for(val);
427+
return err;
352428
}
353429
```

0 commit comments

Comments
 (0)