Skip to content

Commit 080c476

Browse files
committed
Make atomic operation scope wider for the socket selector manipulation
Increasing timeout duration of tests
1 parent 4a1a5d1 commit 080c476

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

ext/socket/socket-selector.incl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ static int selector_sockets(SgSocketSelector *selector)
44
/* filter closed socket */
55
SgObject h = SG_NIL, t = SG_NIL, cp;
66
int i = 0;
7-
Sg_LockMutex(&selector->lock);
87
SG_FOR_EACH(cp, selector->sockets) {
98
SgObject slot = SG_CAR(cp);
109
if (Sg_SocketOpenP(SG_SOCKET(SG_CAR(slot)))) {
@@ -13,7 +12,6 @@ static int selector_sockets(SgSocketSelector *selector)
1312
}
1413
}
1514
selector->sockets = h;
16-
Sg_UnlockMutex(&selector->lock);
1715
return i;
1816
}
1917

@@ -31,14 +29,12 @@ static void strip_sockets(SgSocketSelector *selector, SgObject slots)
3129
if (!SG_NULLP(slots)) {
3230
/* remove the returned sockets from the targets */
3331
SgObject h = SG_NIL, t = SG_NIL, cp;
34-
Sg_LockMutex(&selector->lock);
3532
SG_FOR_EACH(cp, selector->sockets) {
3633
if (SG_FALSEP(Sg_Memq(SG_CAR(cp), slots))) {
3734
SG_APPEND1(h, t, SG_CAR(cp));
3835
}
3936
}
4037
selector->sockets = h;
41-
Sg_UnlockMutex(&selector->lock);
4238
}
4339
}
4440

@@ -106,13 +102,17 @@ SgObject Sg_SocketSelectorClear(SgSocketSelector *selector)
106102
if (Sg_SocketSelectorWaitingP(selector)) {
107103
Sg_Error(UC("There's a thread already waiting for %A"), selector);
108104
}
105+
Sg_LockMutex(&selector->lock);
106+
109107
selector_sockets(selector);
110108
sockets = selector->sockets;
111109
SG_FOR_EACH(cp, sockets) {
112110
remove_socket(selector, SG_SOCKET(SG_CAAR(cp)));
113111
}
114112
cp = selector->sockets;
115113
selector->sockets = SG_NIL;
114+
115+
Sg_UnlockMutex(&selector->lock);
116116
return cp;
117117
}
118118

@@ -142,8 +142,8 @@ SgObject Sg_SocketSelectorAdd(SgSocketSelector *selector,
142142
t->nsec = sp->tv_nsec + usec * 1000;
143143
to = SG_OBJ(t);
144144
}
145-
slot = Sg_Cons(socket, Sg_Cons(to, data));
146145
Sg_LockMutex(&selector->lock);
146+
slot = Sg_Cons(socket, Sg_Cons(to, data));
147147
add_socket(selector, slot);
148148
selector->sockets = Sg_Cons(slot, selector->sockets);
149149
selector_sockets(selector);
@@ -268,13 +268,14 @@ SgObject Sg_SocketSelectorWait(SgSocketSelector *selector, SgObject timeout)
268268
}
269269

270270
selector->waiting = TRUE;
271-
Sg_UnlockMutex(&selector->lock);
272271

273272
retry:
273+
Sg_UnlockMutex(&selector->lock);
274274
r = selector_wait(selector, n, sp);
275+
276+
Sg_LockMutex(&selector->lock);
275277
selector->waiting = FALSE;
276278
if (SG_NULLP(r) && selector->retry) {
277-
Sg_LockMutex(&selector->lock);
278279
/*
279280
socket is added during waiting. here we do
280281
- update timeout
@@ -304,7 +305,6 @@ SgObject Sg_SocketSelectorWait(SgSocketSelector *selector, SgObject timeout)
304305
}
305306

306307
selector->waiting = TRUE;
307-
Sg_UnlockMutex(&selector->lock);
308308

309309
goto retry;
310310
} else {
@@ -324,6 +324,7 @@ SgObject Sg_SocketSelectorWait(SgSocketSelector *selector, SgObject timeout)
324324
strip_sockets(selector, strip);
325325
}
326326

327+
Sg_UnlockMutex(&selector->lock);
327328
return Sg_Values2(r, timedout);
328329
}
329330

ext/socket/test.scm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418
(define delay 0.01) ;; 10ms
419419
(define server (make-server-socket "0"))
420420
(define selector (make-socket-selector))
421+
(define timeouts (make-atomic-fixnum 0))
421422
(define (echo-back s) (socket-send s (socket-recv s 255)))
422423
(define end? #f)
423424
(define t (thread-start!
@@ -438,6 +439,7 @@
438439
(let-values (((socks timed-out) (socket-selector-wait! selector)))
439440
(for-each echo-back (map car socks))
440441
(for-each (lambda (s)
442+
(socket-send s #vu8(0))
441443
(atomic-fixnum-inc! result-to)
442444
(socket-shutdown s SHUT_RDWR)
443445
(socket-close s))
@@ -451,9 +453,9 @@
451453
(lambda ()
452454
(let ((s (make-client-socket "localhost" (server-service server)))
453455
(msg (string->utf8 (string-append "hello " (number->string i)))))
454-
(socket-set-read-timeout! s 100) ;; 100ms
456+
(socket-set-read-timeout! s 500) ;; 500ms
455457
(thread-sleep! delay)
456-
(guard (e (else #t))
458+
(guard (e (else (atomic-fixnum-inc! timeouts) #t))
457459
(socket-send s msg)
458460
(when (bytevector=? (socket-recv s 255) msg)
459461
(atomic-fixnum-inc! result)))
@@ -477,6 +479,9 @@
477479
(socket-shutdown server SHUT_RDWR)
478480
(socket-close server)
479481

482+
(test-equal (format "timeouts (count = ~a, timeout = ~a)" count timeout)
483+
0 (atomic-fixnum-load timeouts))
484+
480485
(values (atomic-fixnum-load result) (atomic-fixnum-load result-to)))
481486

482487
(let-values (((r rt) (selector-test 500)))

test/tests/net/socket.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
(make-thread
238238
(lambda ()
239239
(let ((s (make-client-socket "localhost" server-port
240-
(socket-options (read-timeout 100))))
240+
(socket-options (read-timeout 500))))
241241
(msg (string->utf8
242242
(string-append "Hello world " (number->string i)))))
243243
(guard (e (else #;(print e) s))

0 commit comments

Comments
 (0)