~ubuntu-branches/ubuntu/saucy/nspr/saucy-updates

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/pthreads/ptthread.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-11-27 17:39:22 UTC
  • mfrom: (1.1.15) (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121127173922-1zfbtwmy1vczqwxq
Tags: 2:4.9.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - rules: Enable Thumb2 build on armel, armhf.
  - control: Change Vcs-* to XS-Debian-Vcs-*.
  - control: Add conflicts to evolution-documentation-*,
    language-support-translation-*.
  - control: Add Breaks: evolution-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
4
 
 *
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/
9
 
 *
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
13
 
 * License.
14
 
 *
15
 
 * The Original Code is the Netscape Portable Runtime (NSPR).
16
 
 *
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.
21
 
 *
22
 
 * Contributor(s):
23
 
 *
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.
35
 
 *
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/. */
37
5
 
38
6
/*
39
7
** File:            ptthread.c
51
19
#include <unistd.h>
52
20
#include <string.h>
53
21
#include <signal.h>
 
22
#include <dlfcn.h>
54
23
 
55
24
#ifdef SYMBIAN
56
25
/* In Open C sched_get_priority_min/max do not work properly, so we undefine
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 */
826
795
    PR_Free(thred->privateData);
827
796
    if (NULL != thred->errorString)
828
797
        PR_Free(thred->errorString);
 
798
    if (NULL != thred->name)
 
799
        PR_Free(thred->name);
829
800
    PR_Free(thred->stack);
830
801
    if (NULL != thred->syspoll_list)
831
802
        PR_Free(thred->syspoll_list);
1004
975
        rv = pthread_setspecific(pt_book.key, NULL);
1005
976
        PR_ASSERT(0 == rv);
1006
977
    }
 
978
    rv = pthread_key_delete(pt_book.key);
 
979
    PR_ASSERT(0 == rv);
1007
980
    /* TODO: free other resources used by NSPR */
1008
981
    /* _pr_initialized = PR_FALSE; */
1009
982
}  /* _PR_Fini */
1644
1617
 
1645
1618
#endif /* !defined(_PR_DCETHREADS) */
1646
1619
 
 
1620
PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name)
 
1621
{
 
1622
    PRThread *thread;
 
1623
    size_t nameLen;
 
1624
    int result;
 
1625
 
 
1626
    if (!name) {
 
1627
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
 
1628
        return PR_FAILURE;
 
1629
    }
 
1630
 
 
1631
    thread = PR_GetCurrentThread();
 
1632
    if (!thread)
 
1633
        return PR_FAILURE;
 
1634
 
 
1635
    PR_Free(thread->name);
 
1636
    nameLen = strlen(name);
 
1637
    thread->name = (char *)PR_Malloc(nameLen + 1);
 
1638
    if (!thread->name)
 
1639
        return PR_FAILURE;
 
1640
    memcpy(thread->name, name, nameLen + 1);
 
1641
 
 
1642
#if defined(OPENBSD) || defined(FREEBSD)
 
1643
    result = pthread_set_name_np(thread->id, name);
 
1644
#else /* not BSD */
 
1645
    /*
 
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.
 
1648
     */
 
1649
#if defined(DARWIN)
 
1650
    int (*dynamic_pthread_setname_np)(const char*);
 
1651
#else
 
1652
    int (*dynamic_pthread_setname_np)(pthread_t, const char*);
 
1653
#endif
 
1654
 
 
1655
    *(void**)(&dynamic_pthread_setname_np) =
 
1656
        dlsym(RTLD_DEFAULT, "pthread_setname_np");
 
1657
    if (!dynamic_pthread_setname_np)
 
1658
        return PR_SUCCESS;
 
1659
 
 
1660
    /*
 
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.
 
1665
     */
 
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);
 
1678
        name = name_dup;
 
1679
    }
 
1680
 
 
1681
#if defined(DARWIN)
 
1682
    result = dynamic_pthread_setname_np(name);
 
1683
#else
 
1684
    result = dynamic_pthread_setname_np(thread->id, name);
 
1685
#endif
 
1686
#endif /* not BSD */
 
1687
 
 
1688
    if (result) {
 
1689
        PR_SetError(PR_UNKNOWN_ERROR, result);
 
1690
        return PR_FAILURE;
 
1691
    }
 
1692
    return PR_SUCCESS;
 
1693
}
 
1694
 
 
1695
PR_IMPLEMENT(const char *) PR_GetThreadName(const PRThread *thread)
 
1696
{
 
1697
    if (!thread)
 
1698
        return NULL;
 
1699
    return thread->name;
 
1700
}
 
1701
 
1647
1702
#endif  /* defined(_PR_PTHREADS) || defined(_PR_DCETHREADS) */
1648
1703
 
1649
1704
/* ptthread.c */