lwp-warning-cleanup-and-afsconfig-20010612
[openafs.git] / src / lwp / threadname.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */ 
9 /* ********************************************************************** */
10 /*                                                                        */
11 /*  trheadname.c                                                          */
12 /*                                                                        */
13 /*  Author: Hartmut Reuter                                                */
14 /*  reuter@rzg.mpg.de                                                     */
15 /*  Date: 01/12/00                                                        */
16 /*                                                                        */
17 /*  Function    - These routiens implement thread names for the           */
18 /*                logging from the servers                                */
19 /*                                                                        */
20 /* ********************************************************************** */
21
22 #include <afs/param.h>
23 #include <afsconfig.h>
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27 #ifdef HAVE_STRINGS_H
28 #include <strings.h>
29 #endif
30 #if defined(AFS_PTHREAD_ENV)
31 #include <pthread.h>
32 #else /* defined(AFS_PTHREAD_ENV) */
33 #include "lwp.h"
34 #endif /* defined(AFS_PTHREAD_ENV) */
35
36 #define MAX_THREADS 128
37 #define MAXTHREADNAMELENGTH 64
38 int nThreads = 0;
39 #if defined(AFS_PTHREAD_ENV)
40 pthread_t ThreadId[MAX_THREADS];
41 #else /* defined(AFS_PTHREAD_ENV) */
42 PROCESS   ThreadId[MAX_THREADS];
43 #endif /* defined(AFS_PTHREAD_ENV) */
44 char      ThreadName[MAX_THREADS][MAXTHREADNAMELENGTH];
45
46 char * threadname ()
47 {
48     int i;
49     static char MainThread[] = "main";
50     char *ptr;
51     char *p;
52 #ifdef AFS_PTHREAD_ENV
53     pthread_t me;
54 #else /* AFS_PTHREAD_ENV */
55     PROCESS me;
56 #endif /* AFS_PTHREAD_ENV */
57
58 #ifdef AFS_PTHREAD_ENV
59     me =  pthread_self();
60 #else /* AFS_PTHREAD_ENV */
61     me =  (PROCESS) LWP_ThreadId();
62 #endif /* AFS_PTHREAD_ENV */
63     ptr = (char *) &MainThread; 
64     for (i = 0; i < nThreads; i++) {
65        if (ThreadId[i] == me) {
66            ptr = (char *)&ThreadName[i];
67            break;
68        }
69  }
70     p = ptr;
71     return p;
72 }
73
74 int registerthread(id, name)
75 #ifdef AFS_PTHREAD_ENV
76     pthread_t id;
77 #else /* AFS_PTHREAD_ENV */
78     PROCESS id;
79 #endif /* AFS_PTHREAD_ENV */
80     char *name;
81 {
82     int i;
83
84     for (i = 0; i < nThreads; i++) {
85        if (ThreadId[i] == id) {
86            strncpy(&ThreadName[i][0], name, MAXTHREADNAMELENGTH);
87            return 0;
88        }
89     }
90     if (nThreads == MAX_THREADS) return 0;
91     ThreadId[nThreads] = id;
92     strncpy(&ThreadName[nThreads][0], name, MAXTHREADNAMELENGTH);
93     ThreadName[nThreads][MAXTHREADNAMELENGTH -1] =0;
94     nThreads++;
95
96     return 0;
97 }
98
99 int swapthreadname(id, new, old)
100 #ifdef AFS_PTHREAD_ENV
101     pthread_t id;
102 #else /* AFS_PTHREAD_ENV */
103     PROCESS id;
104 #endif /* AFS_PTHREAD_ENV */
105     char *new;
106     char *old;
107 {
108     int i;
109
110     for (i = 0; i < nThreads; i++) {
111         if (ThreadId[i] == id) {
112            if (old)
113                 strncpy(old, &ThreadName[i][0], MAXTHREADNAMELENGTH);
114             strncpy(&ThreadName[i][0], new, MAXTHREADNAMELENGTH);
115             return 0;
116         }
117     }
118     return 1;
119 }