Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9dff3c6

Browse files
committed
Merge pull request #33 from markdroth/error
Fixed endpoint tests. Also more boolification.
2 parents 692bb33 + 808ac38 commit 9dff3c6

File tree

3 files changed

+62
-60
lines changed

3 files changed

+62
-60
lines changed

src/core/lib/iomgr/ev_poll_and_epoll_posix.c

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <assert.h>
5252
#include <errno.h>
5353
#include <poll.h>
54+
#include <stdbool.h>
5455
#include <string.h>
5556
#include <sys/socket.h>
5657
#include <unistd.h>
@@ -88,9 +89,9 @@ struct grpc_fd {
8889
gpr_atm refst;
8990

9091
gpr_mu mu;
91-
int shutdown;
92-
int closed;
93-
int released;
92+
bool shutdown;
93+
bool closed;
94+
bool released;
9495

9596
/* The watcher list.
9697
@@ -186,8 +187,8 @@ typedef struct grpc_cached_wakeup_fd {
186187

187188
struct grpc_pollset_worker {
188189
grpc_cached_wakeup_fd *wakeup_fd;
189-
int reevaluate_polling_on_wakeup;
190-
int kicked_specifically;
190+
bool reevaluate_polling_on_wakeup;
191+
bool kicked_specifically;
191192
struct grpc_pollset_worker *next;
192193
struct grpc_pollset_worker *prev;
193194
};
@@ -201,9 +202,9 @@ struct grpc_pollset {
201202
gpr_mu mu;
202203
grpc_pollset_worker root_worker;
203204
int in_flight_cbs;
204-
int shutting_down;
205-
int called_shutdown;
206-
int kicked_without_pollers;
205+
bool shutting_down;
206+
bool called_shutdown;
207+
bool kicked_without_pollers;
207208
grpc_closure *shutdown_done;
208209
grpc_closure_list idle_jobs;
209210
union {
@@ -332,7 +333,7 @@ static grpc_fd *alloc_fd(int fd) {
332333

333334
gpr_mu_lock(&r->mu);
334335
gpr_atm_rel_store(&r->refst, 1);
335-
r->shutdown = 0;
336+
r->shutdown = false;
336337
r->read_closure = CLOSURE_NOT_READY;
337338
r->write_closure = CLOSURE_NOT_READY;
338339
r->fd = fd;
@@ -341,8 +342,8 @@ static grpc_fd *alloc_fd(int fd) {
341342
r->freelist_next = NULL;
342343
r->read_watcher = r->write_watcher = NULL;
343344
r->on_done_closure = NULL;
344-
r->closed = 0;
345-
r->released = 0;
345+
r->closed = false;
346+
r->released = false;
346347
gpr_mu_unlock(&r->mu);
347348
return r;
348349
}
@@ -455,7 +456,7 @@ static int has_watchers(grpc_fd *fd) {
455456
}
456457

457458
static void close_fd_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
458-
fd->closed = 1;
459+
fd->closed = true;
459460
if (!fd->released) {
460461
close(fd->fd);
461462
} else {
@@ -538,28 +539,28 @@ static void notify_on_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
538539
}
539540
}
540541

541-
/* returns 1 if state becomes not ready */
542-
static int set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
543-
grpc_closure **st) {
542+
/* returns true if state becomes not ready */
543+
static bool set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
544+
grpc_closure **st) {
544545
if (*st == CLOSURE_READY) {
545546
/* duplicate ready ==> ignore */
546-
return 0;
547+
return false;
547548
} else if (*st == CLOSURE_NOT_READY) {
548549
/* not ready, and not waiting ==> flag ready */
549550
*st = CLOSURE_READY;
550-
return 0;
551+
return false;
551552
} else {
552553
/* waiting ==> queue closure */
553554
grpc_exec_ctx_push(exec_ctx, *st, fd_shutdown_error(fd->shutdown), NULL);
554555
*st = CLOSURE_NOT_READY;
555-
return 1;
556+
return true;
556557
}
557558
}
558559

559560
static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
560561
gpr_mu_lock(&fd->mu);
561562
GPR_ASSERT(!fd->shutdown);
562-
fd->shutdown = 1;
563+
fd->shutdown = true;
563564
set_ready_locked(exec_ctx, fd, &fd->read_closure);
564565
set_ready_locked(exec_ctx, fd, &fd->write_closure);
565566
gpr_mu_unlock(&fd->mu);
@@ -632,8 +633,8 @@ static uint32_t fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
632633

633634
static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
634635
int got_read, int got_write) {
635-
int was_polling = 0;
636-
int kick = 0;
636+
bool was_polling = false;
637+
bool kick = false;
637638
grpc_fd *fd = watcher->fd;
638639

639640
if (fd == NULL) {
@@ -644,17 +645,17 @@ static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
644645

645646
if (watcher == fd->read_watcher) {
646647
/* remove read watcher, kick if we still need a read */
647-
was_polling = 1;
648+
was_polling = true;
648649
if (!got_read) {
649-
kick = 1;
650+
kick = true;
650651
}
651652
fd->read_watcher = NULL;
652653
}
653654
if (watcher == fd->write_watcher) {
654655
/* remove write watcher, kick if we still need a write */
655-
was_polling = 1;
656+
was_polling = true;
656657
if (!got_write) {
657-
kick = 1;
658+
kick = true;
658659
}
659660
fd->write_watcher = NULL;
660661
}
@@ -665,12 +666,12 @@ static void fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
665666
}
666667
if (got_read) {
667668
if (set_ready_locked(exec_ctx, fd, &fd->read_closure)) {
668-
kick = 1;
669+
kick = true;
669670
}
670671
}
671672
if (got_write) {
672673
if (set_ready_locked(exec_ctx, fd, &fd->write_closure)) {
673-
kick = 1;
674+
kick = true;
674675
}
675676
}
676677
if (kick) {
@@ -753,23 +754,23 @@ static grpc_error *pollset_kick_ext(grpc_pollset *p,
753754
kick_append_error(
754755
&error, grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
755756
}
756-
p->kicked_without_pollers = 1;
757+
p->kicked_without_pollers = true;
757758
GPR_TIMER_END("pollset_kick_ext.broadcast", 0);
758759
} else if (gpr_tls_get(&g_current_thread_worker) !=
759760
(intptr_t)specific_worker) {
760761
GPR_TIMER_MARK("different_thread_worker", 0);
761762
if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
762-
specific_worker->reevaluate_polling_on_wakeup = 1;
763+
specific_worker->reevaluate_polling_on_wakeup = true;
763764
}
764-
specific_worker->kicked_specifically = 1;
765+
specific_worker->kicked_specifically = true;
765766
kick_append_error(&error,
766767
grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
767768
} else if ((flags & GRPC_POLLSET_CAN_KICK_SELF) != 0) {
768769
GPR_TIMER_MARK("kick_yoself", 0);
769770
if ((flags & GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) != 0) {
770-
specific_worker->reevaluate_polling_on_wakeup = 1;
771+
specific_worker->reevaluate_polling_on_wakeup = true;
771772
}
772-
specific_worker->kicked_specifically = 1;
773+
specific_worker->kicked_specifically = true;
773774
kick_append_error(&error,
774775
grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd->fd));
775776
}
@@ -797,7 +798,7 @@ static grpc_error *pollset_kick_ext(grpc_pollset *p,
797798
}
798799
} else {
799800
GPR_TIMER_MARK("kicked_no_pollers", 0);
800-
p->kicked_without_pollers = 1;
801+
p->kicked_without_pollers = true;
801802
}
802803
}
803804

@@ -839,12 +840,11 @@ static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
839840
*mu = &pollset->mu;
840841
pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
841842
pollset->in_flight_cbs = 0;
842-
pollset->shutting_down = 0;
843-
pollset->called_shutdown = 0;
844-
pollset->kicked_without_pollers = 0;
843+
pollset->shutting_down = false;
844+
pollset->called_shutdown = false;
845+
pollset->kicked_without_pollers = false;
845846
pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL;
846847
pollset->local_wakeup_cache = NULL;
847-
pollset->kicked_without_pollers = 0;
848848
become_basic_pollset(pollset, NULL);
849849
}
850850

@@ -868,9 +868,9 @@ static void pollset_reset(grpc_pollset *pollset) {
868868
GPR_ASSERT(!pollset_has_workers(pollset));
869869
GPR_ASSERT(pollset->idle_jobs.head == pollset->idle_jobs.tail);
870870
pollset->vtable->destroy(pollset);
871-
pollset->shutting_down = 0;
872-
pollset->called_shutdown = 0;
873-
pollset->kicked_without_pollers = 0;
871+
pollset->shutting_down = false;
872+
pollset->called_shutdown = false;
873+
pollset->kicked_without_pollers = false;
874874
become_basic_pollset(pollset, NULL);
875875
}
876876

@@ -909,7 +909,7 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
909909
GPR_TIMER_BEGIN("pollset_work", 0);
910910
/* this must happen before we (potentially) drop pollset->mu */
911911
worker.next = worker.prev = NULL;
912-
worker.reevaluate_polling_on_wakeup = 0;
912+
worker.reevaluate_polling_on_wakeup = false;
913913
if (pollset->local_wakeup_cache != NULL) {
914914
worker.wakeup_fd = pollset->local_wakeup_cache;
915915
pollset->local_wakeup_cache = worker.wakeup_fd->next;
@@ -920,7 +920,7 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
920920
return error;
921921
}
922922
}
923-
worker.kicked_specifically = 0;
923+
worker.kicked_specifically = false;
924924
/* If there's work waiting for the pollset to be idle, and the
925925
pollset is idle, then do that work */
926926
if (!pollset_has_workers(pollset) &&
@@ -962,7 +962,7 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
962962
gpr_tls_set(&g_current_thread_poller, 0);
963963
} else {
964964
GPR_TIMER_MARK("pollset_work.kicked_without_pollers", 0);
965-
pollset->kicked_without_pollers = 0;
965+
pollset->kicked_without_pollers = false;
966966
}
967967
/* Finished execution - start cleaning up.
968968
Note that we may arrive here from outside the enclosing while() loop.
@@ -978,8 +978,8 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
978978
GRPC_POLLSET_REEVALUATE_POLLING_ON_WAKEUP) then we land here and force
979979
a loop */
980980
if (worker.reevaluate_polling_on_wakeup && error == GRPC_ERROR_NONE) {
981-
worker.reevaluate_polling_on_wakeup = 0;
982-
pollset->kicked_without_pollers = 0;
981+
worker.reevaluate_polling_on_wakeup = false;
982+
pollset->kicked_without_pollers = false;
983983
if (queued_work || worker.kicked_specifically) {
984984
/* If there's queued work on the list, then set the deadline to be
985985
immediate so we get back out of the polling loop quickly */
@@ -1000,7 +1000,7 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
10001000
if (pollset_has_workers(pollset)) {
10011001
pollset_kick(pollset, NULL);
10021002
} else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0) {
1003-
pollset->called_shutdown = 1;
1003+
pollset->called_shutdown = true;
10041004
gpr_mu_unlock(&pollset->mu);
10051005
finish_shutdown(exec_ctx, pollset);
10061006
grpc_exec_ctx_flush(exec_ctx);
@@ -1024,15 +1024,15 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
10241024
static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
10251025
grpc_closure *closure) {
10261026
GPR_ASSERT(!pollset->shutting_down);
1027-
pollset->shutting_down = 1;
1027+
pollset->shutting_down = true;
10281028
pollset->shutdown_done = closure;
10291029
pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
10301030
if (!pollset_has_workers(pollset)) {
10311031
grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs, NULL);
10321032
}
10331033
if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 &&
10341034
!pollset_has_workers(pollset)) {
1035-
pollset->called_shutdown = 1;
1035+
pollset->called_shutdown = true;
10361036
finish_shutdown(exec_ctx, pollset);
10371037
}
10381038
}
@@ -1096,7 +1096,7 @@ static void basic_do_promote(grpc_exec_ctx *exec_ctx, void *args,
10961096
if (pollset->shutting_down) {
10971097
/* We don't care about this pollset anymore. */
10981098
if (pollset->in_flight_cbs == 0 && !pollset->called_shutdown) {
1099-
pollset->called_shutdown = 1;
1099+
pollset->called_shutdown = true;
11001100
finish_shutdown(exec_ctx, pollset);
11011101
}
11021102
} else if (fd_is_orphaned(fd)) {
@@ -1622,7 +1622,7 @@ static void perform_delayed_add(grpc_exec_ctx *exec_ctx, void *arg,
16221622
if (da->pollset->shutting_down) {
16231623
/* We don't care about this pollset anymore. */
16241624
if (da->pollset->in_flight_cbs == 0 && !da->pollset->called_shutdown) {
1625-
da->pollset->called_shutdown = 1;
1625+
da->pollset->called_shutdown = true;
16261626
grpc_exec_ctx_push(exec_ctx, da->pollset->shutdown_done, GRPC_ERROR_NONE,
16271627
NULL);
16281628
}

src/core/lib/iomgr/tcp_posix.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "src/core/lib/iomgr/tcp_posix.h"
3939

4040
#include <errno.h>
41+
#include <stdbool.h>
4142
#include <stdlib.h>
4243
#include <string.h>
4344
#include <sys/socket.h>
@@ -74,7 +75,7 @@ typedef struct {
7475
grpc_endpoint base;
7576
grpc_fd *em_fd;
7677
int fd;
77-
int finished_edge;
78+
bool finished_edge;
7879
msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */
7980
size_t slice_size;
8081
gpr_refcount refcount;
@@ -273,7 +274,7 @@ static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
273274
gpr_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer);
274275
TCP_REF(tcp, "read");
275276
if (tcp->finished_edge) {
276-
tcp->finished_edge = 0;
277+
tcp->finished_edge = false;
277278
grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
278279
} else {
279280
grpc_exec_ctx_push(exec_ctx, &tcp->read_closure, GRPC_ERROR_NONE, NULL);
@@ -370,7 +371,7 @@ static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
370371
if (error != GRPC_ERROR_NONE) {
371372
cb = tcp->write_cb;
372373
tcp->write_cb = NULL;
373-
cb->cb(exec_ctx, cb->cb_arg, 0);
374+
cb->cb(exec_ctx, cb->cb_arg, GRPC_ERROR_REF(error));
374375
TCP_UNREF(exec_ctx, tcp, "write");
375376
return;
376377
}
@@ -381,7 +382,7 @@ static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
381382
cb = tcp->write_cb;
382383
tcp->write_cb = NULL;
383384
GPR_TIMER_BEGIN("tcp_handle_write.cb", 0);
384-
cb->cb(exec_ctx, cb->cb_arg, error);
385+
cb->cb(exec_ctx, cb->cb_arg, GRPC_ERROR_REF(error));
385386
GPR_TIMER_END("tcp_handle_write.cb", 0);
386387
TCP_UNREF(exec_ctx, tcp, "write");
387388
GRPC_ERROR_UNREF(error);
@@ -461,7 +462,7 @@ grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size,
461462
tcp->incoming_buffer = NULL;
462463
tcp->slice_size = slice_size;
463464
tcp->iov_size = 1;
464-
tcp->finished_edge = 1;
465+
tcp->finished_edge = true;
465466
/* paired with unref in grpc_tcp_destroy */
466467
gpr_ref_init(&tcp->refcount, 1);
467468
tcp->em_fd = em_fd;

test/core/iomgr/endpoint_tests.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "test/core/iomgr/endpoint_tests.h"
3535

36+
#include <stdbool.h>
3637
#include <sys/types.h>
3738

3839
#include <grpc/support/alloc.h>
@@ -182,7 +183,7 @@ static void read_and_write_test_write_handler(grpc_exec_ctx *exec_ctx,
182183
*/
183184
static void read_and_write_test(grpc_endpoint_test_config config,
184185
size_t num_bytes, size_t write_size,
185-
size_t slice_size, int shutdown) {
186+
size_t slice_size, bool shutdown) {
186187
struct read_and_write_test_state state;
187188
gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(20);
188189
grpc_endpoint_test_fixture f =
@@ -258,11 +259,11 @@ void grpc_endpoint_tests(grpc_endpoint_test_config config,
258259
size_t i;
259260
g_pollset = pollset;
260261
g_mu = mu;
261-
read_and_write_test(config, 10000000, 100000, 8192, 0);
262-
read_and_write_test(config, 1000000, 100000, 1, 0);
263-
read_and_write_test(config, 100000000, 100000, 1, 1);
262+
read_and_write_test(config, 10000000, 100000, 8192, false);
263+
read_and_write_test(config, 1000000, 100000, 1, false);
264+
read_and_write_test(config, 100000000, 100000, 1, true);
264265
for (i = 1; i < 1000; i = GPR_MAX(i + 1, i * 5 / 4)) {
265-
read_and_write_test(config, 40320, i, i, 0);
266+
read_and_write_test(config, 40320, i, i, false);
266267
}
267268
g_pollset = NULL;
268269
}

0 commit comments

Comments
 (0)