Whamcloud - gitweb
Severity : enhancement
[fs/lustre-release.git] / lnet / ulnds / socklnd / proclib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cray Inc.
5  *  Copyright (c) 2003 Cluster File Systems, Inc.
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /* lib.c:
24  *  This file provides the 'library' side for the process-based nals.
25  *  it is responsible for communication with the 'api' side and
26  *  providing service to the generic portals 'library'
27  *  implementation. 'library' might be better termed 'communication'
28  *  or 'kernel'.
29  */
30  
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <procbridge.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netdb.h>
39 #include <errno.h>
40 #include <timer.h>
41 #include <dispatch.h>
42
43 /* the following functions are stubs to satisfy the nal definition
44    without doing anything particularily useful*/
45 extern int tcpnal_init(bridge);
46 extern void tcpnal_shutdown(bridge);
47
48 static void check_stopping(void *z)
49 {
50     bridge b = z;
51     procbridge p = b->local;
52
53     if ((p->nal_flags & NAL_FLAG_STOPPING) == 0)
54             return;
55     
56     tcpnal_shutdown(b);
57
58     pthread_mutex_lock(&p->mutex);
59     p->nal_flags |= NAL_FLAG_STOPPED;
60     pthread_cond_broadcast(&p->cond);
61     pthread_mutex_unlock(&p->mutex);
62
63     pthread_exit(0);
64 }
65
66
67 /* Function:  nal_thread
68  * Arguments: z: an opaque reference to a nal control structure
69  *               allocated and partially populated by the api level code
70  * Returns: nothing, and only on error or explicit shutdown
71  *
72  *  This function is the entry point of the pthread initiated on 
73  *  the api side of the interface. This thread is used to handle
74  *  asynchronous delivery to the application.
75  * 
76  *  We define a limit macro to place a ceiling on limits
77  *   for syntactic convenience
78  */
79
80 void *nal_thread(void *z)
81 {
82     bridge b = (bridge) z;
83     procbridge p=b->local;
84     int rc;
85     
86     rc = tcpnal_init(b);
87
88     /*
89      * Whatever the initialization returned is passed back to the
90      * user level code for further interpretation.  We just exit if
91      * it is non-zero since something went wrong.
92      */
93
94     pthread_mutex_lock(&p->mutex);
95     p->nal_flags |= (rc != 0) ? NAL_FLAG_STOPPED : NAL_FLAG_RUNNING;
96     pthread_cond_broadcast(&p->cond);
97     pthread_mutex_unlock(&p->mutex);
98
99     if (rc == 0) {
100         /* the thunk function is called each time the timer loop
101            performs an operation and returns to blocking mode. we
102            overload this function to inform the api side that
103            it may be interested in looking at the event queue */
104         register_thunk(check_stopping,b);
105         timer_loop();
106     }
107     return(0);
108 }