Skip to content

Commit 893d157

Browse files
committed
simplify cv_reset()
1 parent ea2cb73 commit 893d157

File tree

7 files changed

+29
-40
lines changed

7 files changed

+29
-40
lines changed

R/sync.R

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
#' @section Condition:
4949
#'
5050
#' The condition internal to this 'conditionVariable' maintains a state
51-
#' (counter). Each signal increments the counter by 1. Each time
51+
#' (value). Each signal increments the value by 1. Each time
5252
#' \code{wait} or \code{until} returns (apart from due to timeout), the
53-
#' counter is decremented by 1.
53+
#' value is decremented by 1.
5454
#'
5555
#' The internal condition may be inspected at any time using \code{cv_value}
5656
#' and reset to zero using \code{cv_reset}. This affords a high degree of
@@ -118,19 +118,15 @@ cv_value <- function(cv) .Call(rnng_cv_value, cv)
118118

119119
#' Condition Variables - Reset
120120
#'
121-
#' \code{cv_reset} resets the internal value and/or flag of a condition variable.
122-
#'
123-
#' @param condition [default TRUE] logical value whether to reset the
124-
#' condition (counter) to zero.
125-
#' @param flag [default TRUE] logical value whether to reset the flag.
121+
#' \code{cv_reset} resets the internal value and flag of a condition variable.
126122
#'
127123
#' @examples
128124
#' cv_reset(cv)
129125
#'
130126
#' @rdname cv
131127
#' @export
132128
#'
133-
cv_reset <- function(cv, condition = TRUE, flag = TRUE) invisible(.Call(rnng_cv_reset, cv, condition, flag))
129+
cv_reset <- function(cv) invisible(.Call(rnng_cv_reset, cv))
134130

135131
#' Pipe Notify
136132
#'

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ As the R interpreter runs on a single thread, synchronisation primitives such as
326326

327327
The events that can be signalled include asynchronous receive completions, and pipe events - these are when connections are established or when they are dropped.
328328

329-
Condition variables can be used simply to record such events, or more powerfully, to wait upon these events. The condition variables implemented in {nanonext} include a both a condition (counter) and flag (binary). Each signal increments the counter, and each return of `wait()` or `until()` decrements the counter. A non-zero condition allows waiting threads to continue.
329+
Condition variables can be used simply to record such events, or more powerfully, to wait upon these events. The condition variables implemented in {nanonext} include a both a condition (value) and flag (binary). Each signal increments the value, and each return of `wait()` or `until()` decrements the value. A non-zero condition allows waiting threads to continue.
330330

331331
In any situation where polling for an event presents a solution, waiting upon a condition to be signalled can be more efficient, both in terms of consuming no resources while waiting, and also being synchronised with the event (having no latency).
332332

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ aio
367367
#> < recvAio >
368368
#> - $data for message data
369369
aio$data |> str()
370-
#> num [1:100000000] 0.4905 -0.6613 0.4074 -0.7236 -0.0115 ...
370+
#> num [1:100000000] -1.959 0.507 0.83 -0.458 0.844 ...
371371
```
372372

373373
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -408,10 +408,10 @@ established or when they are dropped.
408408

409409
Condition variables can be used simply to record such events, or more
410410
powerfully, to wait upon these events. The condition variables
411-
implemented in {nanonext} include a both a condition (counter) and flag
412-
(binary). Each signal increments the counter, and each return of
413-
`wait()` or `until()` decrements the counter. A non-zero condition
414-
allows waiting threads to continue.
411+
implemented in {nanonext} include a both a condition (value) and flag
412+
(binary). Each signal increments the value, and each return of `wait()`
413+
or `until()` decrements the value. A non-zero condition allows waiting
414+
threads to continue.
415415

416416
In any situation where polling for an event presents a solution, waiting
417417
upon a condition to be signalled can be more efficient, both in terms of
@@ -651,11 +651,11 @@ ncurl("https://httpbin.org/headers")
651651
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
652652
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
653653
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
654-
#> [76] 2d 36 34 31 62 36 64 30 30 2d 31 38 35 65 66 31 64 65 37 31 34 31 34 65 33
655-
#> [101] 34 35 63 64 63 35 34 35 37 22 0a 20 20 7d 0a 7d 0a
654+
#> [76] 2d 36 34 31 64 34 64 36 63 2d 34 65 62 61 38 36 65 66 37 62 34 35 64 37 65
655+
#> [101] 36 36 30 30 65 33 62 31 30 22 0a 20 20 7d 0a 7d 0a
656656
#>
657657
#> $data
658-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641b6d00-185ef1de71414e345cdc5457\"\n }\n}\n"
658+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641d4d6c-4eba86ef7b45d7e6600e3b10\"\n }\n}\n"
659659
```
660660

661661
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -676,13 +676,13 @@ res
676676

677677
call_aio(res)$headers
678678
#> $Date
679-
#> [1] "Wed, 22 Mar 2023 21:02:56 GMT"
679+
#> [1] "Fri, 24 Mar 2023 07:12:44 GMT"
680680
#>
681681
#> $Server
682682
#> [1] "gunicorn/19.9.0"
683683

684684
res$data
685-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641b6d00-4e94393a5393f2d4210c7334\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"131.111.5.14\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
685+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641d4d6c-4ff4b2e17fd2884123ae75b1\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"131.111.5.14\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
686686
```
687687

688688
In this respect, it may be used as a performant and lightweight method
@@ -708,7 +708,7 @@ transact(sess)
708708
#>
709709
#> $headers
710710
#> $headers$date
711-
#> [1] "Wed, 22 Mar 2023 21:02:57 GMT"
711+
#> [1] "Fri, 24 Mar 2023 07:12:45 GMT"
712712
#>
713713
#>
714714
#> $raw
@@ -718,15 +718,15 @@ transact(sess)
718718
#> [76] 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22 3a 20 22 61 70 70 6c 69 63 61 74
719719
#> [101] 69 6f 6e 2f 6a 73 6f 6e 22 2c 20 0a 20 20 20 20 22 48 6f 73 74 22 3a 20 22
720720
#> [126] 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22 58 2d 41 6d 7a
721-
#> [151] 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31 2d 36 34 31 62
722-
#> [176] 36 64 30 31 2d 34 37 38 35 65 30 31 62 33 31 61 34 61 61 31 65 32 61 64 33
723-
#> [201] 39 65 64 65 22 0a 20 20 7d 2c 20 0a 20 20 22 6f 72 69 67 69 6e 22 3a 20 22
721+
#> [151] 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31 2d 36 34 31 64
722+
#> [176] 34 64 36 64 2d 37 33 32 39 61 30 35 61 36 65 34 35 38 31 33 37 36 35 36 36
723+
#> [201] 37 38 37 34 22 0a 20 20 7d 2c 20 0a 20 20 22 6f 72 69 67 69 6e 22 3a 20 22
724724
#> [226] 31 38 35 2e 32 32 35 2e 34 35 2e 34 39 22 2c 20 0a 20 20 22 75 72 6c 22 3a
725725
#> [251] 20 22 68 74 74 70 73 3a 2f 2f 68 74 74 70 62 69 6e 2e 6f 72 67 2f 67 65 74
726726
#> [276] 22 0a 7d 0a
727727
#>
728728
#> $data
729-
#> [1] "{\n \"args\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641b6d01-4785e01b31a4aa1e2ad39ede\"\n }, \n \"origin\": \"131.111.5.14\", \n \"url\": \"https://httpbin.org/get\"\n}\n"
729+
#> [1] "{\n \"args\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-641d4d6d-7329a05a6e45813765667874\"\n }, \n \"origin\": \"131.111.5.14\", \n \"url\": \"https://httpbin.org/get\"\n}\n"
730730
```
731731

732732
[« Back to ToC](#table-of-contents)

man/cv.Rd

Lines changed: 4 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aio.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,19 +1477,17 @@ SEXP rnng_cv_until(SEXP cvar, SEXP msec) {
14771477

14781478
}
14791479

1480-
SEXP rnng_cv_reset(SEXP cvar, SEXP condition, SEXP flag) {
1480+
SEXP rnng_cv_reset(SEXP cvar) {
14811481

14821482
if (R_ExternalPtrTag(cvar) != nano_CvSymbol)
14831483
Rf_error("'cv' is not a valid Condition Variable");
14841484

14851485
nano_cv *ncv = (nano_cv *) R_ExternalPtrAddr(cvar);
14861486
nng_mtx *mtx = ncv->mtx;
1487-
const int cond = LOGICAL(condition)[0];
1488-
const int flg = LOGICAL(flag)[0];
14891487

14901488
nng_mtx_lock(mtx);
1491-
ncv->flag = flg ? 0 : ncv->flag;
1492-
ncv->condition = cond ? 0 : ncv->condition;
1489+
ncv->flag = 0;
1490+
ncv->condition = 0;
14931491
nng_mtx_unlock(mtx);
14941492

14951493
return R_NilValue;

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static const R_CallMethodDef callMethods[] = {
139139
{"rnng_cv_alloc", (DL_FUNC) &rnng_cv_alloc, 0},
140140
{"rnng_cv_recv_aio", (DL_FUNC) &rnng_cv_recv_aio, 7},
141141
{"rnng_cv_request", (DL_FUNC) &rnng_cv_request, 8},
142-
{"rnng_cv_reset", (DL_FUNC) &rnng_cv_reset, 3},
142+
{"rnng_cv_reset", (DL_FUNC) &rnng_cv_reset, 1},
143143
{"rnng_cv_until", (DL_FUNC) &rnng_cv_until, 2},
144144
{"rnng_cv_value", (DL_FUNC) &rnng_cv_value, 1},
145145
{"rnng_cv_wait", (DL_FUNC) &rnng_cv_wait, 1},

src/nanonext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extern SEXP rnng_ctx_open(SEXP);
132132
extern SEXP rnng_cv_alloc(void);
133133
extern SEXP rnng_cv_recv_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
134134
extern SEXP rnng_cv_request(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
135-
extern SEXP rnng_cv_reset(SEXP, SEXP, SEXP);
135+
extern SEXP rnng_cv_reset(SEXP);
136136
extern SEXP rnng_cv_until(SEXP, SEXP);
137137
extern SEXP rnng_cv_value(SEXP);
138138
extern SEXP rnng_cv_wait(SEXP);

0 commit comments

Comments
 (0)