1 /*
2 * Pipe management
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13 #include <unistd.h>
14 #include <fcntl.h>
15
16 #include <common/config.h>
17 #include <common/hathreads.h>
18 #include <common/memory.h>
19
20 #include <types/global.h>
21 #include <types/pipe.h>
22
23 struct pool_head *pool_head_pipe = NULL;
24 struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
25
26 __decl_hathreads(HA_SPINLOCK_T pipes_lock); /* lock used to protect pipes list */
27
28 int pipes_used = 0; /* # of pipes in use (2 fds each) */
29 int pipes_free = 0; /* # of pipes unused */
30
31 /* allocate memory for the pipes */
init_pipe()32 static void init_pipe()
33 {
34 pool_head_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
35 pipes_used = 0;
36 pipes_free = 0;
37 HA_SPIN_INIT(&pipes_lock);
38 }
39
40 /* return a pre-allocated empty pipe. Try to allocate one if there isn't any
41 * left. NULL is returned if a pipe could not be allocated.
42 */
get_pipe()43 struct pipe *get_pipe()
44 {
45 struct pipe *ret = NULL;
46 int pipefd[2];
47
48 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
49 if (likely(pipes_live)) {
50 ret = pipes_live;
51 pipes_live = pipes_live->next;
52 pipes_free--;
53 pipes_used++;
54 goto out;
55 }
56
57 if (pipes_used >= global.maxpipes)
58 goto out;
59
60 ret = pool_alloc(pool_head_pipe);
61 if (!ret)
62 goto out;
63
64 if (pipe(pipefd) < 0) {
65 pool_free(pool_head_pipe, ret);
66 ret = NULL;
67 goto out;
68 }
69 #ifdef F_SETPIPE_SZ
70 if (global.tune.pipesize)
71 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
72 #endif
73 ret->data = 0;
74 ret->prod = pipefd[1];
75 ret->cons = pipefd[0];
76 ret->next = NULL;
77 pipes_used++;
78 out:
79 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
80 return ret;
81 }
82
__kill_pipe(struct pipe * p)83 static void inline __kill_pipe(struct pipe *p)
84 {
85 close(p->prod);
86 close(p->cons);
87 pool_free(pool_head_pipe, p);
88 pipes_used--;
89 return;
90 }
91
92 /* destroy a pipe, possibly because an error was encountered on it. Its FDs
93 * will be closed and it will not be reinjected into the live pool.
94 */
kill_pipe(struct pipe * p)95 void kill_pipe(struct pipe *p)
96 {
97 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
98 __kill_pipe(p);
99 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
100 return;
101 }
102
103 /* put back a unused pipe into the live pool. If it still has data in it, it is
104 * closed and not reinjected into the live pool. The caller is not allowed to
105 * use it once released.
106 */
put_pipe(struct pipe * p)107 void put_pipe(struct pipe *p)
108 {
109 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
110 if (p->data) {
111 __kill_pipe(p);
112 goto out;
113 }
114 p->next = pipes_live;
115 pipes_live = p;
116 pipes_free++;
117 pipes_used--;
118 out:
119 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
120 }
121
122
123 __attribute__((constructor))
__pipe_module_init(void)124 static void __pipe_module_init(void)
125 {
126 init_pipe();
127 }
128
129 /*
130 * Local variables:
131 * c-indent-level: 8
132 * c-basic-offset: 8
133 * End:
134 */
135