Skip to content

Replace Spinlock with parking_lot::Mutex #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protobuf = { version = "2.0", optional = true }
prost = { version = "0.6", optional = true }
bytes = { version = "0.5", optional = true }
log = "0.4"
parking_lot = "0.10"

[workspace]
members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"]
Expand Down
17 changes: 9 additions & 8 deletions src/call/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use futures::ready;
use futures::sink::Sink;
use futures::stream::Stream;
use futures::task::{Context, Poll};
use parking_lot::Mutex;
use std::future::Future;

use super::{ShareCall, ShareCallHolder, SinkBase, WriteFlags};
Expand All @@ -18,7 +19,7 @@ use crate::channel::Channel;
use crate::codec::{DeserializeFn, SerializeFn};
use crate::error::{Error, Result};
use crate::metadata::Metadata;
use crate::task::{BatchFuture, BatchType, SpinLock};
use crate::task::{BatchFuture, BatchType};

/// Update the flag bit in res.
#[inline]
Expand Down Expand Up @@ -144,7 +145,7 @@ impl Call {
)
});

let share_call = Arc::new(SpinLock::new(ShareCall::new(call, cq_f)));
let share_call = Arc::new(Mutex::new(ShareCall::new(call, cq_f)));
let sink = ClientCStreamSender::new(share_call.clone(), method.req_ser());
let recv = ClientCStreamReceiver {
call: share_call,
Expand Down Expand Up @@ -209,7 +210,7 @@ impl Call {
grpc_sys::grpcwrap_call_recv_initial_metadata(call.call, ctx, tag)
});

let share_call = Arc::new(SpinLock::new(ShareCall::new(call, cq_f)));
let share_call = Arc::new(Mutex::new(ShareCall::new(call, cq_f)));
let sink = ClientDuplexSender::new(share_call.clone(), method.req_ser());
let recv = ClientDuplexReceiver::new(share_call, method.resp_de());
Ok((sink, recv))
Expand Down Expand Up @@ -266,7 +267,7 @@ impl<T> Future for ClientUnaryReceiver<T> {
/// [`Cancelled`]: ./enum.RpcStatusCode.html#variant.Cancelled
#[must_use = "if unused the ClientCStreamReceiver may immediately cancel the RPC"]
pub struct ClientCStreamReceiver<T> {
call: Arc<SpinLock<ShareCall>>,
call: Arc<Mutex<ShareCall>>,
resp_de: DeserializeFn<T>,
finished: bool,
}
Expand Down Expand Up @@ -314,14 +315,14 @@ impl<T> Future for ClientCStreamReceiver<T> {
/// [`close`]: #method.close
#[must_use = "if unused the StreamingCallSink may immediately cancel the RPC"]
pub struct StreamingCallSink<Req> {
call: Arc<SpinLock<ShareCall>>,
call: Arc<Mutex<ShareCall>>,
sink_base: SinkBase,
close_f: Option<BatchFuture>,
req_ser: SerializeFn<Req>,
}

impl<Req> StreamingCallSink<Req> {
fn new(call: Arc<SpinLock<ShareCall>>, req_ser: SerializeFn<Req>) -> StreamingCallSink<Req> {
fn new(call: Arc<Mutex<ShareCall>>, req_ser: SerializeFn<Req>) -> StreamingCallSink<Req> {
StreamingCallSink {
call,
sink_base: SinkBase::new(false),
Expand Down Expand Up @@ -520,11 +521,11 @@ impl<Resp> Stream for ClientSStreamReceiver<Resp> {
/// [`Cancelled`]: ./enum.RpcStatusCode.html#variant.Cancelled
#[must_use = "if unused the ClientDuplexReceiver may immediately cancel the RPC"]
pub struct ClientDuplexReceiver<Resp> {
imp: ResponseStreamImpl<Arc<SpinLock<ShareCall>>, Resp>,
imp: ResponseStreamImpl<Arc<Mutex<ShareCall>>, Resp>,
}

impl<Resp> ClientDuplexReceiver<Resp> {
fn new(call: Arc<SpinLock<ShareCall>>, de: DeserializeFn<Resp>) -> ClientDuplexReceiver<Resp> {
fn new(call: Arc<Mutex<ShareCall>>, de: DeserializeFn<Resp>) -> ClientDuplexReceiver<Resp> {
ClientDuplexReceiver {
imp: ResponseStreamImpl::new(call, de),
}
Expand Down
5 changes: 3 additions & 2 deletions src/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use futures::future::Future;
use futures::ready;
use futures::task::{Context, Poll};
use libc::c_void;
use parking_lot::Mutex;

use crate::buf::{GrpcByteBuffer, GrpcByteBufferReader};
use crate::codec::{DeserializeFn, Marshaller, SerializeFn};
use crate::error::{Error, Result};
use crate::grpc_sys::grpc_status_code::*;
use crate::task::{self, BatchFuture, BatchType, CallTag, SpinLock};
use crate::task::{self, BatchFuture, BatchType, CallTag};

// By default buffers in `SinkBase` will be shrink to 4K size.
const BUF_SHRINK_SIZE: usize = 4 * 1024;
Expand Down Expand Up @@ -505,7 +506,7 @@ impl ShareCallHolder for ShareCall {
}
}

impl ShareCallHolder for Arc<SpinLock<ShareCall>> {
impl ShareCallHolder for Arc<Mutex<ShareCall>> {
fn call<R, F: FnOnce(&mut ShareCall) -> R>(&mut self, f: F) -> R {
let mut call = self.lock();
f(&mut call)
Expand Down
15 changes: 8 additions & 7 deletions src/call/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use futures::ready;
use futures::sink::Sink;
use futures::stream::Stream;
use futures::task::{Context, Poll};
use parking_lot::Mutex;

use super::{RpcStatus, ShareCall, ShareCallHolder, WriteFlags};
use crate::auth_context::AuthContext;
Expand All @@ -24,7 +25,7 @@ use crate::cq::CompletionQueue;
use crate::error::{Error, Result};
use crate::metadata::Metadata;
use crate::server::{BoxHandler, RequestCallContext};
use crate::task::{BatchFuture, CallTag, Executor, Kicker, SpinLock};
use crate::task::{BatchFuture, CallTag, Executor, Kicker};

pub struct Deadline {
spec: gpr_timespec,
Expand Down Expand Up @@ -239,13 +240,13 @@ impl UnaryRequestContext {
/// finish before dropping.
#[must_use = "if unused the RequestStream may immediately cancel the RPC"]
pub struct RequestStream<T> {
call: Arc<SpinLock<ShareCall>>,
call: Arc<Mutex<ShareCall>>,
base: StreamingBase,
de: DeserializeFn<T>,
}

impl<T> RequestStream<T> {
fn new(call: Arc<SpinLock<ShareCall>>, de: DeserializeFn<T>) -> RequestStream<T> {
fn new(call: Arc<Mutex<ShareCall>>, de: DeserializeFn<T>) -> RequestStream<T> {
RequestStream {
call,
base: StreamingBase::new(None),
Expand Down Expand Up @@ -393,7 +394,7 @@ impl_unary_sink!(
#[must_use = "if unused the sink may immediately cancel the RPC"]
ClientStreamingSink,
ClientStreamingSinkResult,
Arc<SpinLock<ShareCall>>
Arc<Mutex<ShareCall>>
);

// A macro helper to implement server side streaming sink.
Expand Down Expand Up @@ -570,7 +571,7 @@ impl_stream_sink!(
#[must_use = "if unused the sink may immediately cancel the RPC"]
DuplexSink,
DuplexSinkFailure,
Arc<SpinLock<ShareCall>>
Arc<Mutex<ShareCall>>
);

/// A context for rpc handling.
Expand Down Expand Up @@ -688,7 +689,7 @@ pub fn execute_client_streaming<P, Q, F>(
{
let mut call = ctx.call();
let close_f = accept_call!(call);
let call = Arc::new(SpinLock::new(ShareCall::new(call, close_f)));
let call = Arc::new(Mutex::new(ShareCall::new(call, close_f)));

let req_s = RequestStream::new(call.clone(), de);
let sink = ClientStreamingSink::new(call, ser);
Expand Down Expand Up @@ -735,7 +736,7 @@ pub fn execute_duplex_streaming<P, Q, F>(
{
let mut call = ctx.call();
let close_f = accept_call!(call);
let call = Arc::new(SpinLock::new(ShareCall::new(call, close_f)));
let call = Arc::new(Mutex::new(ShareCall::new(call, close_f)));

let req_s = RequestStream::new(call.clone(), de);
let sink = DuplexSink::new(call, ser);
Expand Down
87 changes: 0 additions & 87 deletions src/task/lock.rs

This file was deleted.

7 changes: 3 additions & 4 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod callback;
mod executor;
mod lock;
mod promise;

use std::fmt::{self, Debug, Formatter};
Expand All @@ -11,6 +10,7 @@ use std::sync::Arc;

use futures::future::Future;
use futures::task::{Context, Poll, Waker};
use parking_lot::Mutex;

use self::callback::{Abort, Request as RequestCallback, UnaryRequest as UnaryRequestCallback};
use self::executor::SpawnTask;
Expand All @@ -22,7 +22,6 @@ use crate::error::{Error, Result};
use crate::server::RequestCallContext;

pub(crate) use self::executor::{Executor, Kicker, UnfinishedWork};
pub use self::lock::SpinLock;
pub use self::promise::BatchType;

/// A handle that is used to notify future that the task finishes.
Expand All @@ -49,10 +48,10 @@ impl<T> NotifyHandle<T> {
}
}

type Inner<T> = SpinLock<NotifyHandle<T>>;
type Inner<T> = Mutex<NotifyHandle<T>>;

fn new_inner<T>() -> Arc<Inner<T>> {
Arc::new(SpinLock::new(NotifyHandle::new()))
Arc::new(Mutex::new(NotifyHandle::new()))
}

/// Get the future status without the need to poll.
Expand Down