1 /*
2   +----------------------------------------------------------------------+
3   | Swoole                                                               |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 2.0 of the Apache license,    |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.apache.org/licenses/LICENSE-2.0.html                      |
9   | If you did not receive a copy of the Apache2.0 license and are unable|
10   | to obtain it through the world-wide-web, please send a note to       |
11   | license@swoole.com so we can mail you a copy immediately.            |
12   +----------------------------------------------------------------------+
13   | Author: Xinyu Zhu  <xyzhu1120@gmail.com>                             |
14   |         shiguangqi <shiguangqi2008@gmail.com>                        |
15   |         Twosee  <twose@qq.com>                                       |
16   |         Tianfeng Han  <rango@swoole.com>                             |
17   +----------------------------------------------------------------------+
18  */
19 
20 #pragma once
21 
22 #include "swoole.h"
23 #include "swoole_lock.h"
24 
25 namespace swoole {
26 
27 enum ChannelFlag {
28     SW_CHAN_LOCK = 1u << 1,
29     SW_CHAN_NOTIFY = 1u << 2,
30     SW_CHAN_SHM = 1u << 3,
31 };
32 
33 struct Channel {
34     off_t head;
35     off_t tail;
36     size_t size;
37     char head_tag;
38     char tail_tag;
39     int num;
40     int max_num;
41     /**
42      * Data length, excluding structure
43      */
44     size_t bytes;
45     int flags;
46     int maxlen;
47     /**
48      * memory point
49      */
50     void *mem;
51     Lock *lock;
52     Pipe *notify_pipe;
53 
emptyChannel54     inline bool empty() {
55         return num == 0;
56     }
fullChannel57     inline bool full() {
58         return ((head == tail && tail_tag != head_tag) || (bytes + sizeof(int) * num == size));
59     }
60     int pop(void *out_buf, int buffer_length);
61     int push(const void *in_data, int data_length);
62     int out(void *out_buf, int buffer_length);
63     int in(const void *in_data, int data_length);
64     int peek(void *out, int buffer_length);
65     int wait();
66     int notify();
67     void destroy();
68     void print();
countChannel69     inline int count() {
70         return num;
71     }
get_bytesChannel72     inline int get_bytes() {
73         return bytes;
74     }
75     static Channel *make(size_t size, size_t maxlen, int flags);
76 };
77 }  // namespace swoole
78