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>
26 #include "rx_globals.h"
27 #include "rx_pthread.h"
29 #include "rx_atomic.h"
31 /* Set rx_pthread_event_rescheduled if event_handler should just try
32 * again instead of sleeping.
34 * Protected by event_handler_mutex
36 static int rx_pthread_event_rescheduled = 0;
38 static void *rx_ListenerProc(void *);
41 * We supply an event handling thread for Rx's event processing.
42 * The condition variable is used to wakeup the thread whenever a new
43 * event is scheduled earlier than the previous earliest event.
44 * This thread is also responsible for keeping time.
46 static pthread_t event_handler_thread;
47 afs_kcondvar_t rx_event_handler_cond;
48 afs_kmutex_t event_handler_mutex;
49 afs_kcondvar_t rx_listener_cond;
50 afs_kmutex_t listener_mutex;
51 static int listeners_started = 0;
52 afs_kmutex_t rx_clock_mutex;
53 struct clock rxi_clockNow;
55 static rx_atomic_t threadHiNum;
58 rx_NewThreadId(void) {
59 return rx_atomic_inc_and_read(&threadHiNum);
63 * Delay the current thread the specified number of seconds.
72 * Called from rx_Init()
75 rxi_InitializeThreadSupport(void)
77 /* listeners_started must only be reset if
78 * the listener thread terminates */
79 /* listeners_started = 0; */
80 clock_GetTime(&rxi_clockNow);
84 server_entry(void *argp)
86 void (*server_proc) (void *) = (void (*)(void *))argp;
88 dpf(("rx_pthread.c: server_entry: Server proc returned unexpectedly\n"));
89 return (void *) -1; /* reused as return value, see pthread(3) */
93 * Start an Rx server process.
96 rxi_StartServerProc(void *(*proc) (void *), int stacksize)
102 if (pthread_attr_init(&tattr) != 0) {
103 osi_Panic("Unable to Create Rx server thread (pthread_attr_init)\n");
106 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
107 osi_Panic("Unable to Create Rx server thread (pthread_attr_setdetachstate)\n");
111 * NOTE: We are ignoring the stack size parameter, for now.
114 if (pthread_create(&thread, &tattr, server_entry, (void *)proc) != 0) {
115 osi_Panic("Unable to Create Rx server thread\n");
117 AFS_SIGSET_RESTORE();
121 * The event handling process.
124 event_handler(void *argp)
126 unsigned long rx_pthread_n_event_expired = 0;
127 unsigned long rx_pthread_n_event_waits = 0;
128 long rx_pthread_n_event_woken = 0;
129 unsigned long rx_pthread_n_event_error = 0;
130 struct timespec rx_pthread_next_event_time = { 0, 0 };
133 MUTEX_ENTER(&event_handler_mutex);
139 MUTEX_EXIT(&event_handler_mutex);
141 next.sec = 30; /* Time to sleep if there are no events scheduled */
144 rxevent_RaiseEvents(&next);
146 MUTEX_ENTER(&event_handler_mutex);
147 if (rx_pthread_event_rescheduled) {
148 rx_pthread_event_rescheduled = 0;
152 clock_Add(&cv, &next);
153 rx_pthread_next_event_time.tv_sec = cv.sec;
154 rx_pthread_next_event_time.tv_nsec = cv.usec * 1000;
155 rx_pthread_n_event_waits++;
156 error = CV_TIMEDWAIT(&rx_event_handler_cond, &event_handler_mutex, &rx_pthread_next_event_time);
158 rx_pthread_n_event_woken++;
161 else if (error == ETIMEDOUT) {
162 rx_pthread_n_event_expired++;
164 rx_pthread_n_event_error++;
167 else if (errno == ETIMEDOUT) {
168 rx_pthread_n_event_expired++;
170 rx_pthread_n_event_error++;
173 rx_pthread_event_rescheduled = 0;
180 * This routine will get called by the event package whenever a new,
181 * earlier than others, event is posted. */
183 rxi_ReScheduleEvents(void)
185 MUTEX_ENTER(&event_handler_mutex);
186 CV_SIGNAL(&rx_event_handler_cond);
187 rx_pthread_event_rescheduled = 1;
188 MUTEX_EXIT(&event_handler_mutex);
192 /* Loop to listen on a socket. Return setting *newcallp if this
193 * thread should become a server thread. */
195 rxi_ListenerProc(osi_socket sock, int *tnop, struct rx_call **newcallp)
199 struct rx_packet *p = (struct rx_packet *)0;
201 MUTEX_ENTER(&listener_mutex);
202 while (!listeners_started) {
203 CV_WAIT(&rx_listener_cond, &listener_mutex);
205 MUTEX_EXIT(&listener_mutex);
208 /* See if a check for additional packets was issued */
212 * Grab a new packet only if necessary (otherwise re-use the old one)
215 rxi_RestoreDataBufs(p);
217 if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE))) {
218 /* Could this happen with multiple socket listeners? */
219 osi_Panic("rxi_Listener: no packets!"); /* Shouldn't happen */
223 if (rxi_ReadPacket(sock, p, &host, &port)) {
225 p = rxi_ReceivePacket(p, sock, host, port, tnop, newcallp);
226 if (newcallp && *newcallp) {
236 /* This is the listener process request loop. The listener process loop
237 * becomes a server thread when rxi_ListenerProc returns, and stays
238 * server thread until rxi_ServerProc returns. */
240 rx_ListenerProc(void *argp)
243 osi_socket sock = (osi_socket)(intptr_t)argp;
244 struct rx_call *newcall;
249 rxi_ListenerProc(sock, &threadID, &newcall);
250 /* osi_Assert(threadID != -1); */
251 /* osi_Assert(newcall != NULL); */
252 sock = OSI_NULLSOCKET;
253 osi_Assert(pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)threadID) == 0);
254 rxi_ServerProc(threadID, newcall, &sock);
255 /* osi_Assert(sock != OSI_NULLSOCKET); */
261 /* This is the server process request loop. The server process loop
262 * becomes a listener thread when rxi_ServerProc returns, and stays
263 * listener thread until rxi_ListenerProc returns. */
265 rx_ServerProc(void * dummy)
269 struct rx_call *newcall = NULL;
271 rxi_MorePackets(rx_maxReceiveWindow + 2); /* alloc more packets */
272 MUTEX_ENTER(&rx_quota_mutex);
273 rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
274 /* threadID is used for making decisions in GetCall. Get it by bumping
275 * number of threads handling incoming calls */
276 /* Unique thread ID: used for scheduling purposes *and* as index into
277 * the host hold table (fileserver).
278 * The previously used rxi_availProcs is unsuitable as it
279 * will already go up and down as packets arrive while the server
280 * threads are still initialising! The recently introduced
281 * rxi_pthread_hinum does not necessarily lead to a server
282 * thread with id 0, which is not allowed to hop through the
283 * incoming call queue.
284 * So either introduce yet another counter or flag the FCFS
285 * thread... chose the latter.
287 MUTEX_ENTER(&rx_pthread_mutex);
288 threadID = rx_NewThreadId();
289 if (rxi_fcfs_thread_num == 0 && rxi_fcfs_thread_num != threadID)
290 rxi_fcfs_thread_num = threadID;
291 MUTEX_EXIT(&rx_pthread_mutex);
293 MUTEX_EXIT(&rx_quota_mutex);
296 sock = OSI_NULLSOCKET;
297 osi_Assert(pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)threadID) == 0);
298 rxi_ServerProc(threadID, newcall, &sock);
299 /* osi_Assert(sock != OSI_NULLSOCKET); */
301 rxi_ListenerProc(sock, &threadID, &newcall);
302 /* osi_Assert(threadID != -1); */
303 /* osi_Assert(newcall != NULL); */
310 * Historically used to start the listener process. We now have multiple
311 * listener processes (one for each socket); these are started by GetUdpSocket.
313 * The event handling process *is* started here (the old listener used
314 * to also handle events). The listener threads can't actually start
315 * listening until rxi_StartListener is called because most of R may not
316 * be initialized when rxi_Listen is called.
319 rxi_StartListener(void)
321 pthread_attr_t tattr;
324 if (listeners_started)
327 if (pthread_attr_init(&tattr) != 0) {
328 osi_Panic("Unable to create Rx event handling thread (pthread_attr_init)\n");
331 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
332 osi_Panic("Unable to create Rx event handling thread (pthread_attr_setdetachstate)\n");
336 if (pthread_create(&event_handler_thread, &tattr, event_handler, NULL) !=
338 osi_Panic("Unable to create Rx event handling thread\n");
341 AFS_SIGSET_RESTORE();
343 MUTEX_ENTER(&listener_mutex);
344 CV_BROADCAST(&rx_listener_cond);
345 listeners_started = 1;
346 MUTEX_EXIT(&listener_mutex);
351 * Listen on the specified socket.
354 rxi_Listen(osi_socket sock)
357 pthread_attr_t tattr;
360 if (pthread_attr_init(&tattr) != 0) {
361 osi_Panic("Unable to create socket listener thread (pthread_attr_init)\n");
364 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
365 osi_Panic("Unable to create socket listener thread (pthread_attr_setdetachstate)\n");
369 if (pthread_create(&thread, &tattr, rx_ListenerProc, (void *)(intptr_t)sock) != 0) {
370 osi_Panic("Unable to create socket listener thread\n");
373 AFS_SIGSET_RESTORE();
383 rxi_Recvmsg(osi_socket socket, struct msghdr *msg_p, int flags)
386 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
387 while((rxi_HandleSocketError(socket)) > 0)
390 ret = recvmsg(socket, msg_p, flags);
398 rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
401 ret = sendmsg(socket, msg_p, flags);
402 #ifdef AFS_LINUX22_ENV
403 /* linux unfortunately returns ECONNREFUSED if the target port
404 * is no longer in use */
405 /* and EAGAIN if a UDP checksum is incorrect */
406 if (ret == -1 && errno != ECONNREFUSED && errno != EAGAIN) {
410 dpf(("rxi_sendmsg failed, error %d\n", errno));
416 if (WSAGetLastError() > 0)
417 return -WSAGetLastError();
424 struct rx_ts_info_t * rx_ts_info_init(void) {
425 struct rx_ts_info_t * rx_ts_info;
426 rx_ts_info = (rx_ts_info_t *) malloc(sizeof(rx_ts_info_t));
427 osi_Assert(rx_ts_info != NULL && pthread_setspecific(rx_ts_info_key, rx_ts_info) == 0);
428 memset(rx_ts_info, 0, sizeof(rx_ts_info_t));
429 #ifdef RX_ENABLE_TSFPQ
430 queue_Init(&rx_ts_info->_FPQ);
432 MUTEX_ENTER(&rx_packets_mutex);
434 RX_TS_FPQ_COMPUTE_LIMITS;
435 MUTEX_EXIT(&rx_packets_mutex);
436 #endif /* RX_ENABLE_TSFPQ */