1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use prelude::Protocol;
use ffi::{RawFd, AsRawFd};
use error::ErrCode;

use std::io;
use std::sync::Arc;

pub unsafe trait AsIoContext {
    fn as_ctx(&self) -> &IoContext;
}

#[derive(Clone, Debug)]
pub struct IoContext(Arc<Impl>);

impl IoContext {
    /// Returns a new `IoContext`.
    ///
    /// # Panics
    /// Panics if too many open files.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    ///
    /// IoContext::new().unwrap();
    /// ```
    pub fn new() -> io::Result<IoContext> {
        Impl::new()
    }

    /// Requests a process to invoke the given handler.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    /// use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
    ///
    /// static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// ctx.dispatch(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// ctx.dispatch(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// ctx.dispatch(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// assert_eq!(COUNT.load(Ordering::Relaxed), 0);
    ///
    /// ctx.run();
    /// assert_eq!(COUNT.load(Ordering::Relaxed), 3);
    /// ```
    pub fn dispatch<F>(&self, func: F)
        where F: FnOnce(&IoContext) + Send + 'static
    {
        self.do_dispatch(move|ctx: &IoContext, _: &mut ThreadIoContext| func(ctx))
    }

    #[doc(hidden)]
    pub fn do_dispatch<F>(&self, func: F)
        where F: FnOnce(&IoContext, &mut ThreadIoContext) + Send + 'static
    {
        Impl::do_dispatch(self, func)
    }

    /// Requests a process to invoke the given handler and return immediately.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    /// use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
    ///
    /// static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// ctx.post(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// ctx.post(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// ctx.post(|_| { COUNT.fetch_add(1, Ordering::SeqCst); });
    /// assert_eq!(COUNT.load(Ordering::Relaxed), 0);
    ///
    /// ctx.run();
    /// assert_eq!(COUNT.load(Ordering::Relaxed), 3);
    /// ```
    pub fn post<F>(&self, func: F)
        where F: FnOnce(&IoContext) + Send + 'static
    {
        self.do_post(move|ctx: &IoContext, _: &mut ThreadIoContext| func(ctx))
    }

    #[doc(hidden)]
    pub fn do_post<F>(&self, func: F)
        where F: FnOnce(&IoContext, &mut ThreadIoContext) + Send + 'static
    {
        Impl::do_post(self, func)
    }

    /// Resets a stopped `IoContext`.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// assert_eq!(ctx.stopped(), false);
    /// ctx.stop();
    /// assert_eq!(ctx.stopped(), true);
    /// ctx.restart();
    /// assert_eq!(ctx.stopped(), false);
    /// ```
    pub fn restart(&self) -> bool {
        Impl::restart(self)
    }

    /// Runs all given handlers.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// ctx.run();
    /// ```
    pub fn run(&self) -> usize {
        Impl::run(self)
    }

    pub fn run_one(&self) -> usize {
        Impl::run_one(self)
    }

    /// Sets a stop request and cancel all of the waiting event in an `IoContext`.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// ctx.stop();
    /// ```
    pub fn stop(&self) {
        Impl::stop(self)
    }

    /// Returns true if this has been stopped.
    ///
    /// # Examples
    /// ```
    /// use asyncio::IoContext;
    ///
    /// let ctx = IoContext::new().unwrap();
    /// assert_eq!(ctx.stopped(), false);
    /// ctx.stop();
    /// assert_eq!(ctx.stopped(), true);
    /// ```
    pub fn stopped(&self) -> bool {
        self.0.stopped()
    }

    pub fn work(ctx: &IoContext) -> IoContextWork {
        ctx.0.work_started();
        IoContextWork(ctx.clone())
    }
}

unsafe impl AsIoContext for IoContext {
    fn as_ctx(&self) -> &IoContext {
        self
    }
}

unsafe impl Send for IoContext { }

/// The class to delaying until the stop of `IoContext` is dropped.
///
/// # Examples
/// When dropped the `IoContextWork`, to stop the `IoContext`:
///
/// ```
/// use asyncio::IoContext;
/// use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
///
/// static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
///
/// let ctx = &IoContext::new().unwrap();
/// let mut work = Some(IoContext::work(ctx));
///
/// fn count_if_not_stopped(ctx: &IoContext) {
///   if !ctx.stopped() {
///     COUNT.fetch_add(1, Ordering::Relaxed);
///   }
/// }
/// ctx.post(count_if_not_stopped);
/// ctx.post(move |_| work = None);  // call IoContext::stop()
/// ctx.post(count_if_not_stopped);
/// ctx.run();
///
/// assert_eq!(COUNT.load(Ordering::Relaxed), 1);
/// ```
///
/// # Examples
/// A multithreading example code:
///
/// ```rust,no_run
/// use asyncio::IoContext;
/// use std::thread;
/// use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
///
/// static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
///
/// let ctx = &IoContext::new().unwrap();
/// let _work = IoContext::work(ctx);
///
/// let mut thrds = Vec::new();
/// for _ in 0..10 {
///   let ctx = ctx.clone();
///   thrds.push(thread::spawn(move|| ctx.run()));
/// }
///
/// for _ in 0..100 {
///   ctx.post(move|ctx| {
///     if COUNT.fetch_add(1, Ordering::SeqCst) == 99 {
///       ctx.stop();
///     }
///   });
/// }
///
/// ctx.run();
/// for thrd in thrds {
///   thrd.join().unwrap();
/// }
///
/// assert_eq!(COUNT.load(Ordering::Relaxed), 100);
/// ```
pub struct IoContextWork(IoContext);

impl Drop for IoContextWork {
    fn drop(&mut self) {
        (self.0).0.work_finished();
        self.0.stop();
    }
}

unsafe impl AsIoContext for IoContextWork {
    fn as_ctx(&self) -> &IoContext {
        &self.0
    }
}

pub trait Socket<P: Protocol> : AsIoContext + AsRawFd + Send + 'static {
    unsafe fn from_raw_fd(&IoContext, pro: P, fd: RawFd) -> Self;
    fn protocol(&self) -> P;
}

pub trait Upcast<T: ?Sized>  {
    fn upcast(self: Box<Self>) -> Box<T>;
}

pub trait FnOp {
    fn call_op(self: Box<Self>, ctx: &IoContext, this: &mut ThreadIoContext, ec: ErrCode);
}

type Operation = Box<FnOp + Send>;

mod task_ctx;
pub use self::task_ctx::{TaskIoContext as Impl, ThreadIoContext, workplace};

mod init;
pub use self::init::Init;

mod callstack;
pub use self::callstack::ThreadCallStack;

mod reactor;
pub use self::reactor::*;

mod interrupter;
pub use self::interrupter::*;

mod scheduler;
pub use self::scheduler::*;

#[test]
fn test_new() {
    IoContext::new().unwrap();
}

#[test]
fn test_run() {
    let ctx = &IoContext::new().unwrap();
    ctx.run();
    assert!(ctx.stopped());
}

#[test]
fn test_run_one() {
    let ctx = &IoContext::new().unwrap();
    ctx.run_one();
    assert!(ctx.stopped());
}

#[test]
fn test_work() {
    let ctx = &IoContext::new().unwrap();
    {
        let _work = IoContext::work(ctx);
    }
    assert!(ctx.stopped());
}

#[test]
fn test_multithread_working() {
    use std::thread;
    use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};

    static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

    let ctx = &IoContext::new().unwrap();
    let _work = IoContext::work(ctx);

    let mut thrds = Vec::new();
    for _ in 0..10 {
        let ctx = ctx.clone();
        thrds.push(thread::spawn(move|| ctx.run()))
    }

    for _ in 0..100 {
        ctx.post(move |ctx| {
            if COUNT.fetch_add(1, Ordering::SeqCst) == 99 {
                ctx.stop();
            }
        })
    }

    ctx.run();
    for thrd in thrds {
        thrd.join().unwrap();
    }

    assert_eq!(COUNT.load(Ordering::Relaxed), 100);
}