2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * An implementation of the rx socket listener for pthreads (not using select).
12 * This assumes that multiple read system calls may be extant at any given
13 * time. Also implements the pthread-specific event handling for rx.
15 * rx_pthread.c is used for the thread safe RX package.
18 #include <afsconfig.h>
19 #include <afs/param.h>
24 #include <sys/types.h>
29 # include <sys/socket.h>
30 # include <sys/file.h>
32 # include <netinet/in.h>
34 # include <sys/ioctl.h>
35 # include <sys/time.h>
39 #include <rx/rx_globals.h>
41 #include <rx/rx_pthread.h>
42 #include <rx/rx_clock.h>
45 * Number of times the event handling thread was signalled because a new
46 * event was scheduled earlier than the lastest event.
48 * Protected by event_handler_mutex
50 static long rx_pthread_n_event_wakeups;
52 /* Set rx_pthread_event_rescheduled if event_handler should just try
53 * again instead of sleeping.
55 * Protected by event_handler_mutex
57 static int rx_pthread_event_rescheduled = 0;
59 static void *rx_ListenerProc(void *);
62 * We supply an event handling thread for Rx's event processing.
63 * The condition variable is used to wakeup the thread whenever a new
64 * event is scheduled earlier than the previous earliest event.
65 * This thread is also responsible for keeping time.
67 static pthread_t event_handler_thread;
68 pthread_cond_t rx_event_handler_cond;
69 pthread_mutex_t event_handler_mutex;
70 pthread_cond_t rx_listener_cond;
71 pthread_mutex_t listener_mutex;
72 static int listeners_started = 0;
73 pthread_mutex_t rx_clock_mutex;
74 struct clock rxi_clockNow;
77 * Delay the current thread the specified number of seconds.
86 * Called from rx_Init()
89 rxi_InitializeThreadSupport(void)
91 /* listeners_started must only be reset if
92 * the listener thread terminates */
93 /* listeners_started = 0; */
94 clock_GetTime(&rxi_clockNow);
98 server_entry(void *argp)
100 void (*server_proc) () = (void (*)())argp;
102 dpf(("rx_pthread.c: server_entry: Server proc returned unexpectedly\n"));
108 * Start an Rx server process.
111 rxi_StartServerProc(void (*proc) (void), int stacksize)
114 pthread_attr_t tattr;
117 if (pthread_attr_init(&tattr) != 0) {
118 dpf(("Unable to Create Rx server thread (pthread_attr_init)\n"));
122 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
124 (("Unable to Create Rx server thread (pthread_attr_setdetachstate)\n"));
129 * NOTE: We are ignoring the stack size parameter, for now.
132 if (pthread_create(&thread, &tattr, server_entry, (void *)proc) != 0) {
133 dpf(("Unable to Create Rx server thread\n"));
136 AFS_SIGSET_RESTORE();
140 * The event handling process.
143 event_handler(void *argp)
145 struct clock rx_pthread_last_event_wait_time = { 0, 0 };
146 unsigned long rx_pthread_n_event_expired = 0;
147 unsigned long rx_pthread_n_event_waits = 0;
148 long rx_pthread_n_event_woken = 0;
149 struct timespec rx_pthread_next_event_time = { 0, 0 };
151 assert(pthread_mutex_lock(&event_handler_mutex) == 0);
157 assert(pthread_mutex_unlock(&event_handler_mutex) == 0);
159 next.sec = 30; /* Time to sleep if there are no events scheduled */
162 rxevent_RaiseEvents(&next);
164 assert(pthread_mutex_lock(&event_handler_mutex) == 0);
165 if (rx_pthread_event_rescheduled) {
166 rx_pthread_event_rescheduled = 0;
170 clock_Add(&cv, &next);
171 rx_pthread_next_event_time.tv_sec = cv.sec;
172 rx_pthread_next_event_time.tv_nsec = cv.usec * 1000;
173 rx_pthread_n_event_waits++;
174 if (pthread_cond_timedwait
175 (&rx_event_handler_cond, &event_handler_mutex,
176 &rx_pthread_next_event_time) == -1) {
178 assert(errno == EAGAIN);
180 rx_pthread_n_event_expired++;
182 rx_pthread_n_event_woken++;
184 rx_pthread_event_rescheduled = 0;
190 * This routine will get called by the event package whenever a new,
191 * earlier than others, event is posted. */
193 rxi_ReScheduleEvents(void)
195 assert(pthread_mutex_lock(&event_handler_mutex) == 0);
196 pthread_cond_signal(&rx_event_handler_cond);
197 rx_pthread_event_rescheduled = 1;
198 assert(pthread_mutex_unlock(&event_handler_mutex) == 0);
202 /* Loop to listen on a socket. Return setting *newcallp if this
203 * thread should become a server thread. */
205 rxi_ListenerProc(int sock, int *tnop, struct rx_call **newcallp)
209 register struct rx_packet *p = (struct rx_packet *)0;
211 assert(pthread_mutex_lock(&listener_mutex) == 0);
212 while (!listeners_started) {
213 assert(pthread_cond_wait(&rx_listener_cond, &listener_mutex) == 0);
215 assert(pthread_mutex_unlock(&listener_mutex) == 0);
219 * Grab a new packet only if necessary (otherwise re-use the old one)
222 rxi_RestoreDataBufs(p);
224 if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE))) {
225 /* Could this happen with multiple socket listeners? */
226 dpf(("rxi_Listener: no packets!")); /* Shouldn't happen */
231 if (rxi_ReadPacket(sock, p, &host, &port)) {
233 p = rxi_ReceivePacket(p, sock, host, port, tnop, newcallp);
234 if (newcallp && *newcallp) {
244 /* This is the listener process request loop. The listener process loop
245 * becomes a server thread when rxi_ListenerProc returns, and stays
246 * server thread until rxi_ServerProc returns. */
248 rx_ListenerProc(void *argp)
251 int sock = (int)argp;
252 struct rx_call *newcall;
257 rxi_ListenerProc(sock, &threadID, &newcall);
258 /* assert(threadID != -1); */
259 /* assert(newcall != NULL); */
260 sock = OSI_NULLSOCKET;
261 assert(pthread_setspecific(rx_thread_id_key, (void *)threadID) == 0);
262 rxi_ServerProc(threadID, newcall, &sock);
263 /* assert(sock != OSI_NULLSOCKET); */
268 /* This is the server process request loop. The server process loop
269 * becomes a listener thread when rxi_ServerProc returns, and stays
270 * listener thread until rxi_ListenerProc returns. */
276 struct rx_call *newcall = NULL;
278 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
279 MUTEX_ENTER(&rx_stats_mutex);
280 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
281 /* threadID is used for making decisions in GetCall. Get it by bumping
282 * number of threads handling incoming calls */
283 /* Unique thread ID: used for scheduling purposes *and* as index into
284 * the host hold table (fileserver).
285 * The previously used rxi_availProcs is unsuitable as it
286 * will already go up and down as packets arrive while the server
287 * threads are still initialising! The recently introduced
288 * rxi_pthread_hinum does not necessarily lead to a server
289 * thread with id 0, which is not allowed to hop through the
290 * incoming call queue.
291 * So either introduce yet another counter or flag the FCFS
292 * thread... chose the latter.
294 threadID = ++rxi_pthread_hinum;
295 if (rxi_fcfs_thread_num == 0 && rxi_fcfs_thread_num != threadID)
296 rxi_fcfs_thread_num = threadID;
298 MUTEX_EXIT(&rx_stats_mutex);
301 sock = OSI_NULLSOCKET;
302 assert(pthread_setspecific(rx_thread_id_key, (void *)threadID) == 0);
303 rxi_ServerProc(threadID, newcall, &sock);
304 /* assert(sock != OSI_NULLSOCKET); */
306 rxi_ListenerProc(sock, &threadID, &newcall);
307 /* assert(threadID != -1); */
308 /* assert(newcall != NULL); */
314 * Historically used to start the listener process. We now have multiple
315 * listener processes (one for each socket); these are started by GetUdpSocket.
317 * The event handling process *is* started here (the old listener used
318 * to also handle events). The listener threads can't actually start
319 * listening until rxi_StartListener is called because most of R may not
320 * be initialized when rxi_Listen is called.
323 rxi_StartListener(void)
325 pthread_attr_t tattr;
328 if (listeners_started)
331 if (pthread_attr_init(&tattr) != 0) {
333 (("Unable to create Rx event handling thread (pthread_attr_init)\n"));
337 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
339 (("Unable to create Rx event handling thread (pthread_attr_setdetachstate)\n"));
344 if (pthread_create(&event_handler_thread, &tattr, event_handler, NULL) !=
346 dpf(("Unable to create Rx event handling thread\n"));
349 MUTEX_ENTER(&rx_stats_mutex);
351 MUTEX_EXIT(&rx_stats_mutex);
352 AFS_SIGSET_RESTORE();
354 assert(pthread_mutex_lock(&listener_mutex) == 0);
355 assert(pthread_cond_broadcast(&rx_listener_cond) == 0);
356 listeners_started = 1;
357 assert(pthread_mutex_unlock(&listener_mutex) == 0);
362 * Listen on the specified socket.
365 rxi_Listen(osi_socket sock)
368 pthread_attr_t tattr;
371 if (pthread_attr_init(&tattr) != 0) {
373 (("Unable to create socket listener thread (pthread_attr_init)\n"));
377 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
379 (("Unable to create socket listener thread (pthread_attr_setdetachstate)\n"));
384 if (pthread_create(&thread, &tattr, rx_ListenerProc, (void *)sock) != 0) {
385 dpf(("Unable to create socket listener thread\n"));
388 MUTEX_ENTER(&rx_stats_mutex);
390 MUTEX_EXIT(&rx_stats_mutex);
391 AFS_SIGSET_RESTORE();
401 rxi_Recvmsg(osi_socket socket, struct msghdr *msg_p, int flags)
404 ret = recvmsg(socket, msg_p, flags);
412 rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
415 ret = sendmsg(socket, msg_p, flags);
416 #ifdef AFS_LINUX22_ENV
417 /* linux unfortunately returns ECONNREFUSED if the target port
418 * is no longer in use */
419 /* and EAGAIN if a UDP checksum is incorrect */
420 if (ret == -1 && errno != ECONNREFUSED && errno != EAGAIN) {
424 dpf(("rxi_sendmsg failed, error %d\n", errno));
431 struct rx_ts_info_t * rx_ts_info_init() {
432 register struct rx_ts_info_t * rx_ts_info;
433 rx_ts_info = (rx_ts_info_t *) malloc(sizeof(rx_ts_info_t));
434 assert(rx_ts_info != NULL && pthread_setspecific(rx_ts_info_key, rx_ts_info) == 0);
435 memset(rx_ts_info, 0, sizeof(rx_ts_info_t));
436 #ifdef RX_ENABLE_TSFPQ
437 queue_Init(&rx_ts_info->_FPQ);
439 MUTEX_ENTER(&rx_stats_mutex);
441 RX_TS_FPQ_COMPUTE_LIMITS;
442 MUTEX_EXIT(&rx_stats_mutex);
443 #endif /* RX_ENABLE_TSFPQ */