1
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
74
43
PRCondVar *cv; /* used to signal global things */
75
44
PRInt32 system, user; /* a count of the two different types */
76
45
PRUintn this_many; /* number of threads allowed for exit */
77
pthread_key_t key; /* private private data key */
46
pthread_key_t key; /* thread private data key */
78
47
PRThread *first, *last; /* list of threads we know about */
79
48
#if defined(_PR_DCETHREADS) || defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
80
49
PRInt32 minPrio, maxPrio; /* range of scheduling priorities */
1645
1618
#endif /* !defined(_PR_DCETHREADS) */
1620
PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name)
1627
PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
1631
thread = PR_GetCurrentThread();
1635
PR_Free(thread->name);
1636
nameLen = strlen(name);
1637
thread->name = (char *)PR_Malloc(nameLen + 1);
1640
memcpy(thread->name, name, nameLen + 1);
1642
#if defined(OPENBSD) || defined(FREEBSD)
1643
result = pthread_set_name_np(thread->id, name);
1646
* On OSX, pthread_setname_np is only available in 10.6 or later, so test
1647
* for it at runtime. It also may not be available on all linux distros.
1650
int (*dynamic_pthread_setname_np)(const char*);
1652
int (*dynamic_pthread_setname_np)(pthread_t, const char*);
1655
*(void**)(&dynamic_pthread_setname_np) =
1656
dlsym(RTLD_DEFAULT, "pthread_setname_np");
1657
if (!dynamic_pthread_setname_np)
1661
* The 15-character name length limit is an experimentally determined
1662
* length of a null-terminated string that most linux distros and OS X
1663
* accept as an argument to pthread_setname_np. Otherwise the E2BIG
1664
* error is returned by the function.
1666
#define SETNAME_LENGTH_CONSTRAINT 15
1667
#define SETNAME_FRAGMENT1_LENGTH (SETNAME_LENGTH_CONSTRAINT >> 1)
1668
#define SETNAME_FRAGMENT2_LENGTH \
1669
(SETNAME_LENGTH_CONSTRAINT - SETNAME_FRAGMENT1_LENGTH - 1)
1670
char name_dup[SETNAME_LENGTH_CONSTRAINT + 1];
1671
if (nameLen > SETNAME_LENGTH_CONSTRAINT) {
1672
memcpy(name_dup, name, SETNAME_FRAGMENT1_LENGTH);
1673
name_dup[SETNAME_FRAGMENT1_LENGTH] = '~';
1674
/* Note that this also copies the null terminator. */
1675
memcpy(name_dup + SETNAME_FRAGMENT1_LENGTH + 1,
1676
name + nameLen - SETNAME_FRAGMENT2_LENGTH,
1677
SETNAME_FRAGMENT2_LENGTH + 1);
1682
result = dynamic_pthread_setname_np(name);
1684
result = dynamic_pthread_setname_np(thread->id, name);
1686
#endif /* not BSD */
1689
PR_SetError(PR_UNKNOWN_ERROR, result);
1695
PR_IMPLEMENT(const char *) PR_GetThreadName(const PRThread *thread)
1699
return thread->name;
1647
1702
#endif /* defined(_PR_PTHREADS) || defined(_PR_DCETHREADS) */
1649
1704
/* ptthread.c */