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