Skip to content

Commit c27cbca

Browse files
committed
ATRONIX: Add a /only refinement to wait
Such that it only checks events from the ports passed in as the argument. This could be used to ease sychroneous communication and used for implementing a pause when the port list is empty (in case of no GUI events).
1 parent bc59ae1 commit c27cbca

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/boot/natives.r

+1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ wait: native [
682682
{Waits for a duration, port, or both.}
683683
value [number! time! port! block! none!]
684684
/all {Returns all in a block}
685+
/only {only check for ports given in the block to this function}
685686
]
686687

687688
wake-up: native [

src/core/c-port.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138

139139
/***********************************************************************
140140
**
141-
*/ REBINT Awake_System(REBSER *ports)
141+
*/ REBINT Awake_System(REBSER *ports, REBINT only)
142142
/*
143143
** Returns:
144144
** -1 for errors
@@ -152,6 +152,7 @@
152152
REBVAL *waked;
153153
REBVAL *awake;
154154
REBVAL tmp;
155+
REBVAL ref_only;
155156
REBVAL *v;
156157

157158
// Get the system port object:
@@ -177,8 +178,10 @@
177178
if (ports) Set_Block(&tmp, ports);
178179
else SET_NONE(&tmp);
179180

181+
if (only) SET_TRUE(&ref_only);
182+
else SET_NONE(&ref_only);
180183
// Call the system awake function:
181-
v = Apply_Func(0, awake, port, &tmp, 0); // ds is return value
184+
v = Apply_Func(0, awake, port, &tmp, &ref_only, 0); // ds is return value
182185

183186
// Awake function returns 1 for end of WAIT:
184187
return (IS_LOGIC(v) && VAL_LOGIC(v)) ? 1 : 0;
@@ -187,7 +190,7 @@
187190

188191
/***********************************************************************
189192
**
190-
*/ REBINT Wait_Ports(REBSER *ports, REBCNT timeout)
193+
*/ REBINT Wait_Ports(REBSER *ports, REBCNT timeout, REBINT only)
191194
/*
192195
** Inputs:
193196
** Ports: a block of ports or zero (on stack to avoid GC).
@@ -211,7 +214,7 @@
211214
}
212215

213216
// Process any waiting events:
214-
if ((result = Awake_System(ports)) > 0) return TRUE;
217+
if ((result = Awake_System(ports, only)) > 0) return TRUE;
215218

216219
// If activity, use low wait time, otherwise increase it:
217220
if (result == 0) wt = 1;

src/core/n-io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static REBSER *Read_All_File(char *fname)
371371
if (ports) Set_Block(D_RET, ports);
372372

373373
// Process port events [stack-move]:
374-
if (!Wait_Ports(ports, timeout)) {
374+
if (!Wait_Ports(ports, timeout, D_REF(3))) {
375375
Sieve_Ports(NULL); /* just reset the waked list */
376376
return R_NONE;
377377
}

src/mezz/sys-ports.r

+24-8
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,35 @@ init-schemes: func [
174174
awake: func [
175175
sport "System port (State block holds events)"
176176
ports "Port list (Copy of block passed to WAIT)"
177-
/local event port waked
177+
/only
178+
/local event event-list n-event port waked
178179
][
179180
waked: sport/data ; The wake list (pending awakes)
180181

182+
if only [
183+
unless block? ports [return none] ;short cut for a pause
184+
]
185+
181186
; Process all events (even if no awake ports).
182-
; Do only 8 events at a time (to prevent polling lockout).
183-
loop 8 [
184-
unless event: take sport/state [break]
187+
n-event: 0
188+
event-list: sport/state
189+
while [not empty? event-list][
190+
if n-event > 8 [break] ; Do only 8 events at a time (to prevent polling lockout).
191+
event: first event-list
185192
port: event/port
186-
if wake-up port event [
187-
; Add port to wake list:
188-
;print ["==System-waked:" port/spec/ref]
189-
unless find waked port [append waked port]
193+
either any [
194+
none? only
195+
find ports port
196+
][
197+
remove event-list ;avoid event overflow caused by wake-up recursively calling into wait
198+
if wake-up port event [
199+
; Add port to wake list:
200+
;print ["==System-waked:" port/spec/ref]
201+
unless find waked port [append waked port]
202+
]
203+
++ n-event
204+
][
205+
event-list: next event-list
190206
]
191207
]
192208

0 commit comments

Comments
 (0)