Skip to content

Commit

Permalink
Fixes, commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiller committed Nov 8, 2017
1 parent 15626bb commit 1ef989c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/core/lib/iomgr/lockfree_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,29 @@ extern grpc_tracer_flag grpc_polling_trace;
namespace grpc_core {

LockfreeEvent::LockfreeEvent() {
/* Perform an atomic store to start the state machine.
Note carefully that LockfreeEvent *MAY* be used whilst in a destroyed
state, while a file descriptor is on a freelist. In such a state it may
be SetReady'd, and so we need to perform an atomic operation here to
ensure no races */
gpr_atm_no_barrier_store(&state_, kClosureNotReady);
}

LockfreeEvent::~LockfreeEvent() {
gpr_atm curr = gpr_atm_no_barrier_load(&state_);
if (curr & kShutdownBit) {
GRPC_ERROR_UNREF((grpc_error*)(curr & ~kShutdownBit));
} else {
GPR_ASSERT(curr == kClosureNotReady || curr == kClosureReady);
}
gpr_atm curr;
do {
curr = gpr_atm_no_barrier_load(&state_);
if (curr & kShutdownBit) {
GRPC_ERROR_UNREF((grpc_error*)(curr & ~kShutdownBit));
} else {
GPR_ASSERT(curr == kClosureNotReady || curr == kClosureReady);
}
/* we CAS in a shutdown, no error value here. If this event is interacted
with post-deletion (see the note in the constructor) we want the bit
pattern to prevent error retention in a deleted object */
} while (!gpr_atm_no_barrier_cas(&state_, curr,
kShutdownBit /* shutdown, no error */));
}

void LockfreeEvent::NotifyOn(grpc_exec_ctx* exec_ctx, grpc_closure* closure) {
Expand Down

0 comments on commit 1ef989c

Please sign in to comment.