00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _PASSENGER_SYSTEM_H_
00021 #define _PASSENGER_SYSTEM_H_
00022
00023 #include <boost/thread.hpp>
00024 #include <boost/date_time/posix_time/posix_time_types.hpp>
00025 #include <sys/types.h>
00026 #include <sys/wait.h>
00027 #include <sys/socket.h>
00028 #include <pthread.h>
00029 #include <unistd.h>
00030 #include <signal.h>
00031 #include <cstdio>
00032 #include <ctime>
00033 #include <cassert>
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 namespace Passenger {
00053
00054 using namespace boost;
00055
00056 static const int INTERRUPTION_SIGNAL = SIGINT;
00057
00058
00059
00060
00061
00062
00063
00064 void setupSyscallInterruptionSupport();
00065
00066
00067
00068
00069 class Thread: public thread {
00070 public:
00071 template <class F>
00072 explicit Thread(F f, unsigned int stackSize = 0)
00073 : thread(f, stackSize) {}
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 void interrupt() {
00087 int ret;
00088
00089 thread::interrupt();
00090 do {
00091 ret = pthread_kill(native_handle(),
00092 INTERRUPTION_SIGNAL);
00093 } while (ret == EINTR);
00094 }
00095
00096
00097
00098
00099
00100
00101 void interruptAndJoin() {
00102 bool done = false;
00103 while (!done) {
00104 interrupt();
00105 done = timed_join(posix_time::millisec(10));
00106 }
00107 }
00108 };
00109
00110
00111
00112
00113
00114
00115
00116 namespace InterruptableCalls {
00117 ssize_t read(int fd, void *buf, size_t count);
00118 ssize_t write(int fd, const void *buf, size_t count);
00119 int close(int fd);
00120
00121 int socketpair(int d, int type, int protocol, int sv[2]);
00122 ssize_t recvmsg(int s, struct msghdr *msg, int flags);
00123 ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
00124 int shutdown(int s, int how);
00125
00126 FILE *fopen(const char *path, const char *mode);
00127 int fclose(FILE *fp);
00128
00129 time_t time(time_t *t);
00130 int usleep(useconds_t usec);
00131 int nanosleep(const struct timespec *req, struct timespec *rem);
00132
00133 pid_t fork();
00134 int kill(pid_t pid, int sig);
00135 pid_t waitpid(pid_t pid, int *status, int options);
00136 }
00137
00138 }
00139
00140 namespace boost {
00141 namespace this_thread {
00142
00143
00144
00145
00146 extern thread_specific_ptr<bool> _syscalls_interruptable;
00147
00148
00149
00150
00151
00152 bool syscalls_interruptable();
00153
00154 class restore_syscall_interruption;
00155
00156
00157
00158
00159
00160 class enable_syscall_interruption {
00161 private:
00162 bool lastValue;
00163 public:
00164 enable_syscall_interruption() {
00165 if (_syscalls_interruptable.get() == NULL) {
00166 lastValue = true;
00167 _syscalls_interruptable.reset(new bool(true));
00168 } else {
00169 lastValue = *_syscalls_interruptable;
00170 *_syscalls_interruptable = true;
00171 }
00172 }
00173
00174 ~enable_syscall_interruption() {
00175 *_syscalls_interruptable = lastValue;
00176 }
00177 };
00178
00179
00180
00181
00182
00183
00184
00185 class disable_syscall_interruption {
00186 private:
00187 friend class restore_syscall_interruption;
00188 bool lastValue;
00189 public:
00190 disable_syscall_interruption() {
00191 if (_syscalls_interruptable.get() == NULL) {
00192 lastValue = true;
00193 _syscalls_interruptable.reset(new bool(false));
00194 } else {
00195 lastValue = *_syscalls_interruptable;
00196 *_syscalls_interruptable = false;
00197 }
00198 }
00199
00200 ~disable_syscall_interruption() {
00201 *_syscalls_interruptable = lastValue;
00202 }
00203 };
00204
00205
00206
00207
00208
00209 class restore_syscall_interruption {
00210 private:
00211 int lastValue;
00212 public:
00213 restore_syscall_interruption(const disable_syscall_interruption &intr) {
00214 assert(_syscalls_interruptable.get() != NULL);
00215 lastValue = *_syscalls_interruptable;
00216 *_syscalls_interruptable = intr.lastValue;
00217 }
00218
00219 ~restore_syscall_interruption() {
00220 *_syscalls_interruptable = lastValue;
00221 }
00222 };
00223
00224 }
00225 }
00226
00227 #endif
00228