Unix port: Give threads a name on Linux

May also work on Darwin, but I can't test it :-)
This commit is contained in:
Dirk Ziegelmeier 2018-11-02 20:22:22 +01:00
parent ba8d9b0018
commit feee9d903a

View File

@ -1,8 +1,8 @@
/* /*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
@ -11,21 +11,21 @@
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products * 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE. * OF SUCH DAMAGE.
* *
* This file is part of the lwIP TCP/IP stack. * This file is part of the lwIP TCP/IP stack.
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
*/ */
@ -44,6 +44,8 @@
* will block until there is more room instead of just * will block until there is more room instead of just
* leaking messages. * leaking messages.
*/ */
#define _GNU_SOURCE /* pull in pthread_setname_np() on Linux */
#include "lwip/debug.h" #include "lwip/debug.h"
#include <string.h> #include <string.h>
@ -135,7 +137,7 @@ static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Threads */ /* Threads */
static struct sys_thread * static struct sys_thread *
introduce_thread(pthread_t id) introduce_thread(pthread_t id)
{ {
struct sys_thread *thread; struct sys_thread *thread;
@ -186,10 +188,14 @@ sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksi
thread_data->arg = arg; thread_data->arg = arg;
thread_data->function = function; thread_data->function = function;
code = pthread_create(&tmp, code = pthread_create(&tmp,
NULL, NULL,
thread_wrapper, thread_wrapper,
thread_data); thread_data);
#ifdef LWIP_UNIX_LINUX
pthread_setname_np(tmp, name);
#endif
if (0 == code) { if (0 == code) {
st = introduce_thread(tmp); st = introduce_thread(tmp);
} }
@ -268,7 +274,7 @@ sys_mbox_free(struct sys_mbox **mb)
struct sys_mbox *mbox = *mb; struct sys_mbox *mbox = *mb;
SYS_STATS_DEC(mbox.used); SYS_STATS_DEC(mbox.used);
sys_arch_sem_wait(&mbox->mutex, 0); sys_arch_sem_wait(&mbox->mutex, 0);
sys_sem_free_internal(mbox->not_empty); sys_sem_free_internal(mbox->not_empty);
sys_sem_free_internal(mbox->not_full); sys_sem_free_internal(mbox->not_full);
sys_sem_free_internal(mbox->mutex); sys_sem_free_internal(mbox->mutex);