use std::ops::Deref;
use std::ptr;
use crate::context::blocked::BlockedClient;
use crate::{raw, Context, RedisResult};
pub struct ContextGuard {
ctx: Context,
}
impl Drop for ContextGuard {
fn drop(&mut self) {
unsafe { raw::RedisModule_ThreadSafeContextUnlock.unwrap()(self.ctx.ctx) };
}
}
impl Deref for ContextGuard {
type Target = Context;
fn deref(&self) -> &Self::Target {
&self.ctx
}
}
pub struct DetachedFromClient;
pub struct ThreadSafeContext<B: Send> {
pub(crate) ctx: *mut raw::RedisModuleCtx,
#[allow(dead_code)]
blocked_client: B,
}
unsafe impl<B: Send> Send for ThreadSafeContext<B> {}
unsafe impl<B: Send> Sync for ThreadSafeContext<B> {}
impl ThreadSafeContext<DetachedFromClient> {
#[must_use]
pub fn new() -> Self {
let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(ptr::null_mut()) };
Self {
ctx,
blocked_client: DetachedFromClient,
}
}
}
impl Default for ThreadSafeContext<DetachedFromClient> {
fn default() -> Self {
Self::new()
}
}
impl ThreadSafeContext<BlockedClient> {
#[must_use]
pub fn with_blocked_client(blocked_client: BlockedClient) -> Self {
let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(blocked_client.inner) };
Self {
ctx,
blocked_client,
}
}
#[allow(clippy::must_use_candidate)]
pub fn reply(&self, r: RedisResult) -> raw::Status {
let ctx = Context::new(self.ctx);
ctx.reply(r)
}
}
impl<B: Send> ThreadSafeContext<B> {
pub fn lock(&self) -> ContextGuard {
unsafe { raw::RedisModule_ThreadSafeContextLock.unwrap()(self.ctx) };
let ctx = Context::new(self.ctx);
ContextGuard { ctx }
}
}
impl<B: Send> Drop for ThreadSafeContext<B> {
fn drop(&mut self) {
unsafe { raw::RedisModule_FreeThreadSafeContext.unwrap()(self.ctx) };
}
}