libwp: Tidy header includes
[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 <afsconfig.h>
23 #include <afs/param.h>
24
25 #include <roken.h>
26
27 #if defined(AFS_PTHREAD_ENV)
28 #include <pthread.h>
29 #else /* defined(AFS_PTHREAD_ENV) */
30 #include "lwp.h"
31 #endif /* defined(AFS_PTHREAD_ENV) */
32
33 #define MAX_THREADS 128
34 #define MAXTHREADNAMELENGTH 64
35 int nThreads = 0;
36 #if defined(AFS_PTHREAD_ENV)
37 pthread_t ThreadId[MAX_THREADS];
38 #else /* defined(AFS_PTHREAD_ENV) */
39 PROCESS ThreadId[MAX_THREADS];
40 #endif /* defined(AFS_PTHREAD_ENV) */
41 char ThreadName[MAX_THREADS][MAXTHREADNAMELENGTH];
42
43 char *
44 threadname(void)
45 {
46     int i;
47     static char MainThread[] = "main";
48     char *ptr;
49     char *p;
50 #ifdef AFS_PTHREAD_ENV
51     pthread_t me;
52 #else /* AFS_PTHREAD_ENV */
53     PROCESS me;
54 #endif /* AFS_PTHREAD_ENV */
55
56 #ifdef AFS_PTHREAD_ENV
57     me = pthread_self();
58 #else /* AFS_PTHREAD_ENV */
59     me = (PROCESS) LWP_ThreadId();
60 #endif /* AFS_PTHREAD_ENV */
61     ptr = &MainThread[0];
62     for (i = 0; i < nThreads; i++) {
63         if (ThreadId[i] == me) {
64             ptr = &ThreadName[i][0];
65             break;
66         }
67     }
68     p = ptr;
69     return p;
70 }
71
72 int
73 registerthread(
74 #ifdef AFS_PTHREAD_ENV
75                   pthread_t id,
76 #else                           /* AFS_PTHREAD_ENV */
77                   PROCESS id,
78 #endif                          /* AFS_PTHREAD_ENV */
79                   char *name)
80 {
81     int i;
82
83     for (i = 0; i < nThreads; i++) {
84         if (ThreadId[i] == id) {
85             strncpy(&ThreadName[i][0], name, MAXTHREADNAMELENGTH);
86             return 0;
87         }
88     }
89     if (nThreads == MAX_THREADS)
90         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
100 swapthreadname(
101 #ifdef AFS_PTHREAD_ENV
102                   pthread_t id,
103 #else                           /* AFS_PTHREAD_ENV */
104                   PROCESS id,
105 #endif                          /* AFS_PTHREAD_ENV */
106                   char *new, 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 }